Pure isometric movement

Started by Aviva, Thu 19/10/2017 09:53:05

Previous topic - Next topic

Aviva

Hey people!

I'm trying to use pure isometric movement for my characters. So I basically want to get rid of everything that is not moving diagonally. I tried the code below while disabling eModeWalkto, but that doesn't seem to work. Any ideas on how I can do this?

Thanks in advance.

Code: ags
  if (keycode == eKeyS && keycode == eKeyS && player.Moving) player.Loop = 4;
  if (keycode == eKeyW && keycode == eKeyD && player.Moving) player.Loop = 5;
  if (keycode == eKeyS && keycode == eKeyA && player.Moving) player.Loop = 6;
  if (keycode == eKeyW && keycode == eKeyA && player.Moving) player.Loop = 7;


Working on "Drained", a distopian RPG

Khris

That code can't work, because a variable can't store two separate values at the same time.
Plus, you'll want to actually move the character while keys are pressed, not just turn them by changing the .Loop.

To check for multiple keys being pressed at the same time you need to use repeatedly_execute and IsKeyPressed:
Code: ags
  if (IsKeyPressed(eKeyS) && IsKeyPressed(eKeyD)) ...


However if you aren't planning on allowing 8 movement directions anyway, forcing the player to always hold down two buttons seems cruel.
I'd go with S moving the player down and to the right, A moving them down and to the left, etc.

Code: ags
int old_mx, old_my;
void handleInput() {
  int mx = IsKeyPressed(eKeyD) - IsKeyPressed(eKeyA);  // only A => -1, A & D => 0, only D => 1
  int my = IsKeyPressed(eKeyS) - IsKeyPressed(eKeyW);
  if (mx * my != 0) return; // only allow either A/D or W/S
  // rotate by 45°, stretch to 2:1
  int tmx = (mx + my) * 2;
  int tmy = -mx + my;
  // player is still holding down same key as last frame, check for end of walkable area / do nothing
  if (mx == old_mx && my == old_my) {
    int a = GetWalkableAreaAt(player.x + player.WalkSpeedX * tmx / 2, player.y + player.WalkSpeedX * tmy / 4);
    if (a == 0) {
      player.StopMoving();
      player.PlaceOnWalkableArea();
    }
    return;
  }
  // player picked new direction or released movement key
  player.StopMoving();
  player.Walk(player.x + tmx * 1000, player.y + tmy * 1000, eNoBlock, eAnywhere);
  old_mx = mx;
  old_my = my;
}


Paste that code into your GlobalScript, somewhere above the repeatedly_execute function. Then add handleInput() to repeatedly_execute.

And don't forget to deactivate the KeyboardMovement module by removing the line from game_start!

Note that this code assumes diagonal iso movement of 2:1, while your image uses an actual 60° angle. I'd advise against that; Iso art typically uses crisp lines of either 2 pixels over, 1 pixel down or 3 pixels over, 1 pixel down.

Aviva

Thank you so much Khris! Your code works perfectly. My C# knowledge is a bit rusty, but I'm seeing the bigger picture again after getting back into it this morning.
Working on "Drained", a distopian RPG

Laura Hunt

#3
Hi all, apologies for necroposting, but I don't really see the point of opening a brand new thread when this one is so perfectly suited to my question  :)

Basically, I want to know if it's possible to do this (restricting the player character's movement to diagonals) without having to resort to using keyboard controls, i.e., with AGS's regular click-somewhere-in-the-screen-and-the-character-walks-there mechanic. Should I simply use only the diagonal loops, or would it be more complicated than that?

Edit: Looks like this message would be a possible way to go about it?

Khris

#4
Not possible out of the box, I'm afraid.

I did play around with custom path finding a few years ago but it's hell to implement and has side effects, like not being able to use built-in functions like AddWaypoint() or .IsMoving and the like (otoh, maybe I did use AddWaypoint() when I wrote that code, so it might be fine).

Laura Hunt

Hi Khris, thanks for replying. I was afraid that would be the case :/

Would a walkable area mask like the one suggested by eri0o in the message I linked be a viable (if very simplified) alternative?

Khris

Yes, but the character (algorithm) might choose a weird path. Give it a try and see what happens :)

eri0o

Here are caveats for the walkable mask solution:

- If you change pathfinding algorithm, you will have to readjust for new algorithm;
- using a key combo (I forgot which) you can see the walkable area in game, noticed it's slightly different from what you see in the editor;
- using aseprite it's easier to draw the walkables and just import in ags;
- pathfinding uses nodes, that are fed into and consumed when the player is walking, you want the distance the player walks at each step be forced to go in diagonal, so that, and the walkable resolution you figure out before from observations of the walkable in game, to be so that the movements the character is forced to do is always in diagonals.

Laura Hunt

Thanks, Khris and eri0o. With the help of this post I've been brainstorming with the artist and we're going to give the pre-defined walkable areas a try. We'll use a 2:1 isometric perspective for the paths and see what happens! I'll report back with any findings worth sharing.

SMF spam blocked by CleanTalk