0
\$\begingroup\$

I have a board game where you have entities on a tiled map and you can only move forward, left, and right. Each tile represents a position of the entity, and based on the entity's angle and position, I need to make a method that processes the move. For example if the entity has decided to move left, my function needs to perform a left movement.

An example:

img

My ship is currently on the tile where the arrow starts and the entity selected to move left, the arrow represents how the entity should move, with a motion.

So it slowly goes up and then slowly starts to go left and changing its rotation.

My question is, how can I make such a function which will know the entity's position, current angle (rotation), and where it should move to, and then with these parameters, it will process the movement animation motion to the target tile?

I am trying to solve this for a long time now, but no luck. Thank you a lot!

\$\endgroup\$
2
  • \$\begingroup\$ im sure someone more knowledgable will be here to help soon. But id say a smple approach might be to keep a record of the last tile the piece visited and then using that and the one its on now, you can find out what way it moved last. Or perhaps just create a separate method for each movement type possible, and call each method as and when its needed \$\endgroup\$ Aug 14, 2017 at 2:42
  • \$\begingroup\$ for example if you record the previous tile it was on, called prevTile. Then if prevTile was 2,2 and newTile is 1,2 we know he is facing left. So then you need an enum perhaps to store what "currentFacingState" he is in. Hope that helps \$\endgroup\$ Aug 14, 2017 at 2:50

1 Answer 1

1
\$\begingroup\$

Something like this:

Vector2 prevGridTile = new Vector2();
Vector2 currentGridTile = new Vector2();
bool allowRightMovement = true;

enum facingStates {LEFT, RIGHT, UP, DOWN}
facingStates currentFacingState = facingStates.RIGHT; // just a randomly    picked default as right, you could set this to how it starts

void update()

if (prevGridTile.x > currentGridTile.x){
currentFacingState = facingStates.LEFT;
}
// etc etc etc for each case


// then to access that information, you can do switch:

switch (currentFacingState){
    case facingStates.LEFT:
        allowRightMovement = false;
        break;
    case facingStates.RIGHT:
        allowLeftMovement = false;
        break;
    case ETC ETC ETC:
        break;
       }
}

This was just a quick example i knocked up without seeing your code. This is c# and pretty much same in java. Of course , this wont work just pasted in youll have to adapt it to your own project. Hope it works out for u

\$\endgroup\$
7
  • \$\begingroup\$ Hey! Thanks for your answer. What do you mean by current and prev gird? Each tile size is 50x50 pixels, and when I move the entity, I know the target tile, e.g if my entity is currently at coords 1,1 (which translates to 50,50 in the map draw and I want to turn left then the target coord is 0, 2 and translated is 0, 100, am I right? \$\endgroup\$
    – Ben Beri
    Aug 14, 2017 at 3:27
  • \$\begingroup\$ Seems like it created me an account with a different name \$\endgroup\$
    – Ben Beri
    Aug 14, 2017 at 3:35
  • \$\begingroup\$ @Jony Hi there, sorry for delay I went to sleep :) . Well it depends hwo you have your movement and grid set up. its up to you. I tend to make a constant called gridSquareSize = 10 for example and then I have a set amount of squares like 10 x 10. That way, each movement 'speed' can just be a 1xGridSquareSize. My examples may have been a bit advanced for you, perhaps it is worth creating a new simpler project just moving them around a grid in any direction , and then adding further rules after you're fully to grips with that. \$\endgroup\$ Aug 14, 2017 at 13:42
  • \$\begingroup\$ So to clarify using your grid, Id have a float call GridSquareSize = 50; , the currentGrid will start a Vector2(0,0) (lets say it starts in bottom left so thats why 0,0). If you piece moves to the right, the code prevGrid = currentGrid, and then you can make currentGrid = new Vector2(1,0). you have moved right, so you can code to increment the currentGrid vector2.x value by one. Then to actually move the piece you'd do position.x += 1*GridSquareSize, hopefully that makes sense Im not the best at explaining, i'd hoped maybe someone better would help out but its still just me im afraid :) \$\endgroup\$ Aug 14, 2017 at 13:52
  • \$\begingroup\$ currentGrid means the currentTile, prevGrid meant the prevTile. I used the wrong words , that might make it clearer for your example. \$\endgroup\$ Aug 14, 2017 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .