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

Re-do movement to accomodate obstacles (incomplete, need path finder to...

Re-do movement to accomodate obstacles (incomplete, need path finder to finish). Added instructions on how to do the path finder in MapFunction.
parent e014ba1d
No related branches found
No related tags found
No related merge requests found
......@@ -16,6 +16,8 @@ namespace Controller
private Unit selectedUnit;
private Player currentPlayer;
private readonly MapFunction mapFunction;
// might not need; check if enemyUnitsInRange returns an empty list
public bool isAnEnemyUnitInRange(Unit unit)
{
......@@ -83,63 +85,26 @@ 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
// if end node is a obstacle, movement is invalid
if (endNode.getIsObstacle())
{
return false;
}
// moving diagonally, within unit's movability for number of spaces
if (startNode.getPositionX() != endNode.getPositionX() &&
startNode.getPositionY() != endNode.getPositionY() &&
Math.Abs(startNode.getPositionX() - endNode.getPositionX()) +
Math.Abs(startNode.getPositionY() - endNode.getPositionY()) < unit.getMovability())
{
return true;
}
// 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, within unit's movability for number of spaces
if (startNode.getPositionY() == endNode.getPositionY() &&
Math.Abs(startNode.getPositionX() - endNode.getPositionX()) <= unit.getMovability())
{
return true;
}
return false;
}
public bool isNodeAttackable(Unit unit, Node attackNode)
// checks if the node is attackable based on the unit's class and position
private bool isNodeAttackable(UnitType unitType, int unitX, int unitY, Node attackNode)
{
int unitX = unit.getPosition().Item1;
int unitY = unit.getPosition().Item2;
int attackX = attackNode.getPositionX();
int attackY = attackNode.getPositionY();
// return warrior attackable nodes (one node away)
if (unit.getClass() == UnitType.Warrior &&
if (unitType == UnitType.Warrior &&
Math.Abs(unitX - attackX) + Math.Abs(unitY - attackY) == 1)
{
return true;
}
// return archer attackable nodes (two nodes away)
else if (unit.getClass() == UnitType.Archer &&
else if (unitType == UnitType.Archer &&
Math.Abs(unitX - attackX) + Math.Abs(unitY - attackY) == 2)
{
}
// return mage attackable nodes (one node and two nodes away)
else if (unit.getClass() == UnitType.Mage &&
else if (unitType == UnitType.Mage &&
(Math.Abs(unitX - attackX) + Math.Abs(unitY - attackY) == 1 ||
Math.Abs(unitX - attackX) + Math.Abs(unitY - attackY) == 2))
{
......@@ -154,11 +119,13 @@ namespace Controller
LinkedList<Node> moveableNodes = new LinkedList<Node>();
Node currentNode = graph.getNode(unit.getPosition());
// iterate through all nodes in the graph
for (int x=0; x<graph.getWidth(); x++)
{
for (int y=0; y<graph.getHeight(); y++)
{
if (isMovementValid(unit, currentNode, graph.getNode(x, y))) {
// if a path exists to that node, add it to list of moveable nodes
if (mapFunction.pathFinder(graph, currentNode, graph.getNode(x, y)) != null) {
moveableNodes.AddLast(graph.getNode(x, y));
}
}
......@@ -170,14 +137,22 @@ namespace Controller
public LinkedList<Node> getAttackableNodes(Graph graph, Unit unit)
{
LinkedList<Node> attackableNodes = new LinkedList<Node>();
LinkedList<Node> moveableNodes = getMovableNodes(graph, unit);
for (int x = 0; x < graph.getWidth(); x++)
// determine attackable nodes for each moveable node
foreach(Node moveableNode in moveableNodes)
{
for (int y = 0; y < graph.getHeight(); y++)
// iterate through entire grid to determine attackable nodes for moveable node
for (int x = 0; x < graph.getWidth(); x++)
{
if (isNodeAttackable(unit, graph.getNode(x,y)))
for (int y = 0; y < graph.getHeight(); y++)
{
attackableNodes.AddLast(graph.getNode(x, y));
if (isNodeAttackable(unit.getClass(), moveableNode.getPositionX(),
moveableNode.getPositionY(), graph.getNode(x, y)))
{
// if node is attackable, add it to list of attackable nodes
attackableNodes.AddLast(graph.getNode(x, y));
}
}
}
}
......
......@@ -7,9 +7,31 @@ namespace Model
{
class MapFunction
{
// returns list of nodes representing the path from start node to end node
// ===================================================================================================
// HOW TO DO PATH FINDER:
// > You are given a start node (current unit position) and an end node.
// > Check ALL paths that go from start to end node <= optimize this if you can.
// - If path is linear (x or y doesn't change), then (unit.movability) space movements are allowed.
// - If path isn't linear, then (unit.movability-1) space movements are allowed.
// *A way to optimize, if the path exceeds unit.movability (linear) or unit.movability-1 (non-linear) spaces, then throw it out.
// > If there is a valid path, movement is allowed to that node.
// > Return the shortest path.
// Warrior example: https://gyazo.com/a350ccc14ca455d832d26ace65a1d0a2
// ===================================================================================================
// TODO: (OPTIONAL, IGNORE FOR NOW) incorporate movability of nodes in the calculation
// returns list of nodes representing the path from start node to end node; if no path is valid, return null
public LinkedList<Node> pathFinder(Graph graph, Node start, Node end)
{
if (end.getIsObstacle())
{
return null;
}
LinkedList<Node> path = new LinkedList<Node>();
path.AddFirst(start);
// TODO: finds path to highlight for unit movement
......
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