Can you have characters move on a grid (think LEGO)?

Started by darthmonkey, Thu 20/08/2009 01:54:07

Previous topic - Next topic

darthmonkey

Hi, I'm wondering if it's possible to limit characters to movement on a grid, so that the character can only stand in certain coordinates on the screen.

The application would be to use with a LEGO man moving on LEGO pieces, but it would look really bad if it wasn't in a place that he can actually attach to.  Hopefully that gives a picture of what I'm talking about.

Joe

#1
Yeah this is possible but I think this question shold be moved to tech forum.

Anyway I'll give you a shot of what you're talking about translated into script:
Code: ags

//at top of global script
#define SIZE 10.0 //this is the size of each grid square
int i;
//Global script at repeaedly executed
  i=0;
  while(i<Game.CharacterCount){
    if(!character[i].Moving)
      character[i].x=FloatToInt(IntToFloat(player.x)/SIZE);
    i++;
  }


Ok maybe this code doesn't work properly but now you have an idea of what you must do.
Copinstar © Oficial Site

Khris

How is the Lego man moved? Still by mouse clicks? Or using the cursor keys?
Is the grid a standard one or iso?

A bit more info please.

darthmonkey

I'd like to move by mouse clicks to keep the interface standard.  Although, I could see how using the keyboard would make it fairly easy.

What do you mean by standard vs iso grid?

tzachs

I think you can also try something like this:

Code: ags

#define SIZE 10.0 //this is the size of each grid square
#define MOUSE_OFFSET 3.0 //this is the offset you will have to click from your current location to move the player

function on_mouse_click(MouseButton button) // called when a mouse button is clicked. button is either LEFT or RIGHT
{
    if (button == eMouseLeft) 
   {
       if (mouse.x <  cYourCharacter.X - MOUSE_OFFSET)
       {
             if (mouse.y < cYourCharacter.Y - MOUSEOFFSET)
             {
                   cYourCharacter.Walk(cYourCharacter.X - SIZE, cYourCharacter.Y - SIZE);
             }
             else if (mouse.Y > cYourCharacter.Y + MOUSEOFFSET)
             {
                   cYourCharacter.Walk(cYourCharacter.X - SIZE, cYourCharacter.Y + SIZE);
             }
             else
             {
                  cYourCharacter.Walk(cYourCharacter.X - SIZE, cYourCharacter.Y);
             }
       }
       else  if (mouse.x > cYourCharacter.X + SIZE)
       {
                  if (mouse.y < cYourCharacter.Y - MOUSEOFFSET)
             {
                   cYourCharacter.Walk(cYourCharacter.X + SIZE, cYourCharacter.Y - SIZE);
             }
             else if (mouse.Y > cYourCharacter.Y + MOUSEOFFSET)
             {
                   cYourCharacter.Walk(cYourCharacter.X + SIZE, cYourCharacter.Y + SIZE);
             }
             else
             {
                  cYourCharacter.Walk(cYourCharacter.X + SIZE, cYourCharacter.Y);
             }
       }
       else 
       {
             if (mouse.y < cYourCharacter.Y - MOUSEOFFSET)
             {
                   cYourCharacter.Walk(cYourCharacter.X, cYourCharacter.Y - SIZE);
             }
             else if (mouse.Y > cYourCharacter.Y + MOUSEOFFSET)
             {
                   cYourCharacter.Walk(cYourCharacter.X, cYourCharacter.Y + SIZE);
             }

        }
   }
}


Note that this should move the character one grid cell at a time, if you want to click on a distant cell you'll have to enhance this code a bit...

Scarab

Quote from: darthmonkey on Thu 20/08/2009 21:57:52
I'd like to move by mouse clicks to keep the interface standard.  Although, I could see how using the keyboard would make it fairly easy.

What do you mean by standard vs iso grid?

A regular grid goes up and down the page, like in the Pokémon games, whereas Isometric an grid is skewed at an angle (not quite diagonal, approx 30 degrees from the horizontal), like what you'd find in the Age of Empires games.

darthmonkey

Regular probably, isometric feels rotated (probably because it is).  Although, I may be able to cheat with perspective with isometric more.
From the code examples I can kind of see how this should work, thanks.

GarageGothic

#7
You could use an invisible player character for pathfinding and a dummy character for the visible character. Then in repeatedly_execute_always simply move the dummy character to player.x, player.y +- the offset to place him on the nearest grid coordinate. Depending on how you want him to animate, you could either set his View/Loop/Frame to that of the invisible character every loop, or handle animation manually (if you only want him to animate when transitioning to a new coordinate in the grid).

Edit: I took your question to mean that the character shouldn't be allowed to ever stand in-between LEGO pegs. If that's not the case you could still use a dummy character for pathfinding but with a movement speed of 0. This will immediately teleport to the walkable coordinate nearest the cursor. Then make the visible character walk to the nearest grid coordinate (remember checking if it's within a walkable area, otherwise choose the nearest grid coordinate that is).

SMF spam blocked by CleanTalk