Skip to content
Snippets Groups Projects
Commit 1159d024 authored by Susan Yuen's avatar Susan Yuen
Browse files

Added limitations to movement: linear movement only

parent e5cd7432
No related branches found
No related tags found
No related merge requests found
......@@ -82,5 +82,33 @@ namespace Controller
{
selectedUnit = unit;
}
public bool isMovementValid(Unit unit, Node startNode, Node endNode)
{
int movability = unit.getMovability(); // TODO: incorporate movability of nodes in the calculation
// moving diagonally
if (startNode.getPositionX() != endNode.getPositionX() &&
startNode.getPositionY() != endNode.getPositionY())
{
return false;
}
// moving up or down
if (startNode.getPositionX() == endNode.getPositionX() &&
Math.Abs(startNode.getPositionY() - endNode.getPositionY()) <= unit.getMovability())
{
return true;
}
// moving left or right
if (startNode.getPositionY() == endNode.getPositionY() &&
Math.Abs(startNode.getPositionX() - endNode.getPositionX()) <= unit.getMovability())
{
return true;
}
return false;
}
}
}
......@@ -25,7 +25,7 @@ namespace Model
{
for (int j = 0; j < y; j++)
{
nodes[i, j] = new Node();
nodes[i, j] = new Node(i, j);
}
}
}
......@@ -47,6 +47,11 @@ namespace Model
return nodes[x, y];
}
public Node getNode(Tuple<int, int> position)
{
return nodes[position.Item1, position.Item2];
}
public void setNode(Node node, int x, int y)
{
nodes[x, y] = node;
......
......@@ -35,8 +35,10 @@ namespace Controller
{
// TODO: PATH FINDING
Node startNode = graph.getNode(gameFunction.getSelectedUnit().getPosition());
Node endNode = graph.getNode(position);
if (!endNode.getIsObstacle())
if (!endNode.getIsObstacle() &&
gameFunction.isMovementValid(gameFunction.getSelectedUnit(), startNode, endNode))
{
// update the unit's position to the clicked position
int positionX = (int)Math.Floor(position.X / 32);
......@@ -45,10 +47,10 @@ namespace Controller
{
// if new position is different from old position, set unit to new position
gameFunction.getSelectedUnit().setPosition(positionX, positionY);
gameFunction.setPlayableUnitSelected(false);
gameFunction.setSelectedUnit(null);
}
}
gameFunction.setPlayableUnitSelected(false);
gameFunction.setSelectedUnit(null);
}
else
{
......@@ -62,17 +64,17 @@ namespace Controller
}
}
}
// if playable unit exists where user clicked, return it; else, return null
private static Unit playableUnitOnNodeClicked(Node clickedNode, Vector2 positionClicked, Player currentPlayer)
{
for (int i=0; i<currentPlayer.getNumOfUnits(); i++)
for (int i = 0; i < currentPlayer.getNumOfUnits(); i++)
{
Unit unit = currentPlayer.getUnits().ElementAt(i);
int unitX = unit.getPosition().Item1;
int unitY = unit.getPosition().Item2;
int clickedX = (int) Math.Floor(positionClicked.X / 32);
int clickedY = (int) Math.Floor(positionClicked.Y / 32);
int clickedX = (int)Math.Floor(positionClicked.X / 32);
int clickedY = (int)Math.Floor(positionClicked.Y / 32);
if (unitX == clickedX && unitY == clickedY)
{
return unit;
......
......@@ -7,18 +7,22 @@ namespace Model
{
class Node
{
private int movability; // index of how far a unit can move within the tile; 0 = no hindrence
private bool isObstacle; // indicates whether a unit can stand inside the tile
private int movabilityObstruction; // index for hindrance of the movability of a unit; 0 = no hindrance
private bool isObstacle; // indicates whether a unit can stand inside the tile
private int positionX; // position x on the grid
private int positionY; // position y on the grid
public Node()
public Node(int x, int y)
{
movability = 0; // defaults to no hindrence in unit movement
isObstacle = false; // default to non-obstacle tile
positionX = x;
positionY = y;
movabilityObstruction = 0; // defaults to no hindrance in unit movement
isObstacle = false; // default to non-obstacle tile
}
public void setMovability(int m)
public void setMovabilityObstruction(int m)
{
movability = m;
movabilityObstruction = m;
}
public void setIsObstacle(bool b)
......@@ -26,14 +30,24 @@ namespace Model
isObstacle = b;
}
public int getMovability()
public int getMovabilityObstruction()
{
return movability;
return movabilityObstruction;
}
public bool getIsObstacle()
{
return isObstacle;
}
public int getPositionX()
{
return positionX;
}
public int getPositionY()
{
return positionY;
}
}
}
......@@ -18,6 +18,7 @@ namespace Model
int getDefense(); // returns unit's defense
int getResistance(); // returns unit's resistance
int getCriticalRate(); // returns unit's critical rate
int getMovability(); // returns the unit's movability (number of spaces the unit can move in one turn)
Weapon[] getEquipableWeapons(); // returns array of equipable weapons
Weapon getEquippedWeapon(); // returns weapon the unit is currently equipping
int getLevel(); // returns unit's level
......
......@@ -19,6 +19,7 @@ namespace Model
private int resistance;
private int criticalRate;
private int level;
private readonly int movability = 4; // all warriors will have 5 movability
private Weapon[] equipableWeapons;
private Weapon equippedWeapon;
private Texture2D spriteImage;
......@@ -33,7 +34,6 @@ namespace Model
int positionX = (int)Math.Round(coordinates.X / 32);
int positionY = (int)Math.Round(coordinates.Y / 32);
position = new Tuple<int, int>(positionX, positionY);
}
public bool isAlive()
......@@ -81,6 +81,11 @@ namespace Model
return criticalRate;
}
public int getMovability()
{
return movability;
}
public int getLevel()
{
return level;
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment