- Home /
Square Based Movement
I'm trying to create a game with a movement system like final fantasy tactics. I have made it so the code selects a unit and moves the cursor to the unit. Then you can move the cursor to select a place within the movement range of the unit, as long as the place is not occupied by another unit. The problem is that I don't know how to prevent the selection of a space behind a unit.
An example is this:
Green = Unit
Red = Enemy
Blue = Possible Moves
Yellow = Shouldn't be Possible moves
This is what I have done so far. I don't know how to check if a point is in the array because I can't figure out how to make Array.contains work in the Check function.
function Check ()
{
for(var tile in tiles)
{
ntiles.Add(tile+Vector3.right+Vector3.right);
ntiles.Add(tile-Vector3.right-Vector3.right);
ntiles.Add(tile+Vector3.forward+Vector3.forward);
ntiles.Add(tile-Vector3.forward-Vector3.forward);
}
for(var ntile in ntiles)
{
if(ntile == tiles)
{
print("contains");
return;
}
tiles.Add(ntile);
}
print(tiles);
movecheck = movecheck + 1;
if( movecheck == mov)
{
BroadcastMessage("DoneCheck");
return;
}
BroadcastMessage("Check");
}
function DoneCheck ()
{
for(var tile in tiles)
{
Instantiate(posmov,tile,Quaternion.identity);
}
}
This is Javascript
Any help is appreciated.
I am also interested in making a ff tactics/shining force style game. Not much of a programmer here, mostly 3ds max/zbrush/photoshop dude. If you are interested in combining our efforts let me know. (yeah i know im off topic here before everyone flames me, couldn't find femi on the forums).
I'm glad that other people are interested in a FFT or Shining Force style game. At the moment, the only help I need is with the coding, but I will probably need more help down the road. Thanks for the offer :D
Edit: I'm on the forums now too
for sure! just hit me up if you ever need anything or want to work together. just out of curiosity, are you making a fantasy or futuristic rpg strategy game?
Answer by hathol · Jun 11, 2012 at 11:41 AM
I would store the possible fields in a collection and then use a recursive function starting from your player position to get all the walkable tiles. Would look something like this (just pseudocode):
// a simple struct to hold information about your tile
// use and extend as you see fit
public struct Tile
{
bool isBlocked; // is the tile blocked (occupied by player/enemy)
Tile[] neighbours; // the tiles that are directly reachable from this one (probably the 4 or 8 neighbouring ones)
}
//the list that will hold all your walkable tiles
List<Tile> tileList = new List<Tile>();
void CheckWalkables(Tile tile, uint stepsMoved)
{
//add the tile to the list
tileList.Add(tile);
if(stepsMoved < maxMovement) // we can walk another step
{
Tile[] neighbours = tile.neighbours; // get the neighbouring tiles
foreach(Tile currentTile in neighbours)
{
//the tile is not blocked and not yet on the list
if(!currentTile.isBlocked && !tileList.Contains(currentTile))
{
//continue searching from there
CheckWalkables(currentTile, stepsMoved+1);
}
}
}
}
then you can start the search by calling
CheckWalkables(tileYourPlayerIsOn, 0);
This is a good idea but I don't know C#. I understand the basics of what this does so I'm going to try and replicate it in javascript if I can and get back to you.
Answer by jumble · Nov 11, 2013 at 11:21 PM
Preventing the selection of space behind the unit involves counting the 'path' to that space. For example, in your second picture, you can't get to the yellow spaces because the nearest one is four squares away and your green box clearly can only move three squares.
I handle it by computing how many squares of movement it would take to get there, then simply checking against my available movement. In your javascript there it'd be pretty easy to either prototype a method into "tile" or you could have a seperate method that takes a tile and the chosen unit and does the check.
If you need to get into pathing algorithms, there's the easy way or the fun way, but thats a different question.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I move objects in one executed turn? 1 Answer
My Gameobject position when i add child objects to it 0 Answers
Why can't I turn more then 45 degrees 0 Answers