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

Added diagonal movement to units

parent 1159d024
No related branches found
No related tags found
No related merge requests found
......@@ -87,21 +87,23 @@ namespace Controller
{
int movability = unit.getMovability(); // TODO: incorporate movability of nodes in the calculation
// moving diagonally
// moving diagonally, within unit's movability for number of spaces
if (startNode.getPositionX() != endNode.getPositionX() &&
startNode.getPositionY() != endNode.getPositionY())
startNode.getPositionY() != endNode.getPositionY() &&
Math.Abs(startNode.getPositionX() - endNode.getPositionX()) +
Math.Abs(startNode.getPositionY() - endNode.getPositionY()) < unit.getMovability())
{
return false;
return true;
}
// moving up or down
// moving up or down, within unit's movability for number of spaces
if (startNode.getPositionX() == endNode.getPositionX() &&
Math.Abs(startNode.getPositionY() - endNode.getPositionY()) <= unit.getMovability())
{
return true;
}
// moving left or right
// moving left or right, within unit's movability for number of spaces
if (startNode.getPositionY() == endNode.getPositionY() &&
Math.Abs(startNode.getPositionX() - endNode.getPositionX()) <= unit.getMovability())
{
......
......@@ -31,31 +31,27 @@ namespace Controller
// TODO: determine if player has clicked on a menu button
// if player clicks after unit is already selected ...
if (gameFunction.isPlayableUnitSelected())
{
// TODO: PATH FINDING
Node startNode = graph.getNode(gameFunction.getSelectedUnit().getPosition());
Node endNode = graph.getNode(position);
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);
int positionY = (int)Math.Floor(position.Y / 32);
if (!(gameFunction.getSelectedUnit().getPosition() == new Tuple<int, int>(positionX, positionY)))
{
// if new position is different from old position, set unit to new position
gameFunction.getSelectedUnit().setPosition(positionX, positionY);
}
updateUnitPosition(position, gameFunction);
}
gameFunction.setPlayableUnitSelected(false);
gameFunction.setSelectedUnit(null);
}
// if player clicks when no unit is selected ...
else
{
// if there is a playable, player-owned unit on position clicked, set selected unit status
Unit unit = playableUnitOnNodeClicked(graph.getNode(position), position, gameFunction.playerCurrentlyMoving());
Unit unit = getPlayableUnitOnNodeClicked(graph.getNode(position), position, gameFunction.playerCurrentlyMoving());
if (unit != null)
{
gameFunction.setPlayableUnitSelected(true);
......@@ -66,7 +62,7 @@ namespace Controller
}
// if playable unit exists where user clicked, return it; else, return null
private static Unit playableUnitOnNodeClicked(Node clickedNode, Vector2 positionClicked, Player currentPlayer)
private Unit getPlayableUnitOnNodeClicked(Node clickedNode, Vector2 positionClicked, Player currentPlayer)
{
for (int i = 0; i < currentPlayer.getNumOfUnits(); i++)
{
......@@ -82,5 +78,18 @@ namespace Controller
}
return null;
}
// update the unit's position to the clicked position
private void updateUnitPosition(Vector2 position, GameFunction gameFunction)
{
int positionX = (int)Math.Floor(position.X / 32);
int positionY = (int)Math.Floor(position.Y / 32);
// if new position is different from old position, set unit to new position
if (!(gameFunction.getSelectedUnit().getPosition() == new Tuple<int, int>(positionX, positionY)))
{
gameFunction.getSelectedUnit().setPosition(positionX, positionY);
}
}
}
}
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