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

Moved path finding function to GameFunction; removed MapFunction class. Edited...

Moved path finding function to GameFunction; removed MapFunction class. Edited the path finding function logic. Added pause in mouse for animations
parent 2858ee46
No related branches found
No related tags found
No related merge requests found
......@@ -89,7 +89,6 @@
<Compile Include="MainMenu.Designer.cs">
<DependentUpon>MainMenu.cs</DependentUpon>
</Compile>
<Compile Include="MapFunction.cs" />
<Compile Include="MouseHandler.cs" />
<Compile Include="Node.cs" />
<Compile Include="Player.cs" />
......
......@@ -16,8 +16,6 @@ namespace Controller
private Unit selectedUnit;
private Player currentPlayer;
private readonly MapFunction mapFunction = new MapFunction();
// might not need; check if enemyUnitsInRange returns an empty list
public bool isAnEnemyUnitInRange(Unit unit)
{
......@@ -91,6 +89,11 @@ namespace Controller
int attackX = attackNode.getPositionX();
int attackY = attackNode.getPositionY();
if (attackNode.getIsObstacle())
{
return false;
}
// return warrior attackable nodes (one node away)
if (unitType == UnitType.Warrior &&
Math.Abs(unitX - attackX) + Math.Abs(unitY - attackY) == 1)
......@@ -125,7 +128,7 @@ namespace Controller
for (int y=0; y<graph.getHeight(); y++)
{
// if a path exists to that node, add it to list of moveable nodes
if (mapFunction.pathFinder(graph, unit, currentNode, graph.getNode(x, y)) != null) {
if (pathFinder(graph, unit, currentNode, graph.getNode(x, y)) != null) {
moveableNodes.AddLast(graph.getNode(x, y));
}
}
......@@ -158,5 +161,82 @@ namespace Controller
}
return attackableNodes;
}
// 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, Unit unit, Node start, Node end)
{
// TODO: (OPTIONAL, IGNORE FOR NOW) incorporate movability of nodes in the calculation
if (end.getIsObstacle())
{
return null;
}
int numOfMovementsLeft = unit.getMovability();
int endNodeX = end.getPositionX();
int endNodeY = end.getPositionY();
bool nodeAdded = false;
LinkedList<Node> path = new LinkedList<Node>();
path.AddFirst(start);
while (numOfMovementsLeft > 0)
{
int currentNodeX = path.Last().getPositionX();
int currentNodeY = path.Last().getPositionY();
// if it is already at the end node, break
if (currentNodeX == endNodeX && currentNodeY == endNodeY)
{
break;
}
// check if left gets closer to end node
if (!nodeAdded && Math.Abs(currentNodeX - 1 - endNodeX) < Math.Abs(currentNodeX - endNodeX))
{
path.AddLast(graph.getNode(currentNodeX - 1, currentNodeY));
nodeAdded = true;
}
// check if right gets closer to end node
if (!nodeAdded && Math.Abs(currentNodeX + 1 - endNodeX) < Math.Abs(currentNodeX - endNodeX))
{
path.AddLast(graph.getNode(currentNodeX + 1, currentNodeY));
nodeAdded = true;
}
// check if up gets closer to end node
if (!nodeAdded && Math.Abs(currentNodeY + 1 - endNodeY) < Math.Abs(currentNodeY - endNodeY))
{
path.AddLast(graph.getNode(currentNodeX, currentNodeY + 1));
nodeAdded = true;
}
// check if down gets closer to end node
if (!nodeAdded && Math.Abs(currentNodeY - 1 - endNodeY) < Math.Abs(currentNodeY - endNodeY))
{
path.AddLast(graph.getNode(currentNodeX, currentNodeY - 1));
nodeAdded = true;
}
// if no movement gets closer to the end node, there is no valid movement
if (!nodeAdded)
{
break;
}
numOfMovementsLeft--;
nodeAdded = false;
}
// if the node the algorithm ends on isnt the same as the end node, there is no valid path
if (!(path.Last() == end))
{
return null;
}
return path;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model
{
class MapFunction
{
// 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, Unit unit, Node start, Node end)
{
if (end.getIsObstacle())
{
return null;
}
int numOfMovementsLeft = unit.getMovability();
int endNodeX = end.getPositionX();
int endNodeY = end.getPositionY();
LinkedList<Node> path = new LinkedList<Node>();
path.AddFirst(start);
while (numOfMovementsLeft > 0)
{
int currentNodeX = path.Last().getPositionX();
int currentNodeY = path.Last().getPositionY();
int numberOfNodesInPath = path.Count();
// if it is already at the end node, break
if (currentNodeX == endNodeX && currentNodeY == endNodeY)
{
break;
}
// check if left gets closer to end node
if (Math.Abs(currentNodeX - 1 - endNodeX) < Math.Abs(currentNodeX - endNodeX))
{
path.AddLast(graph.getNode(currentNodeX-1, currentNodeY));
}
// check if right gets closer to end node
if (Math.Abs(currentNodeX + 1 - endNodeX) < Math.Abs(currentNodeX - endNodeX))
{
path.AddLast(graph.getNode(currentNodeX + 1, currentNodeY));
}
// check if up gets closer to end node
if (Math.Abs(currentNodeY + 1 - endNodeY) < Math.Abs(currentNodeY - endNodeY))
{
path.AddLast(graph.getNode(currentNodeX, currentNodeY+1));
}
// check if down gets closer to end node
if (Math.Abs(currentNodeY - 1 - endNodeY) < Math.Abs(currentNodeY - endNodeY))
{
path.AddLast(graph.getNode(currentNodeX, currentNodeY - 1));
}
// if no movement gets closer to the end node, there is no valid movement
if (path.Count() == numberOfNodesInPath)
{
break;
}
numOfMovementsLeft--;
}
// if the node the algorithm ends on isnt the same as the end node, there is no valid path
if (!(path.Last() == end))
{
return null;
}
return path;
}
}
}
......@@ -11,7 +11,7 @@ namespace Controller
{
class MouseHandler
{
private readonly MapFunction mapFunction = new MapFunction();
private bool isAnimating = false; // indicates whether an animation sequence is on screen
MouseState lastMouseState;
MouseState currentMouseState;
......@@ -29,6 +29,12 @@ namespace Controller
&& currentMouseState.LeftButton == ButtonState.Pressed
&& validX && validY)
{
// do not react to user input if animation is on screen
if (isAnimating)
{
return;
}
Vector2 position = new Vector2(currentMouseState.X, currentMouseState.Y);
// TODO: determine if player has clicked on a menu button
......@@ -41,7 +47,7 @@ namespace Controller
Node startNode = graph.getNode(gameFunction.getSelectedUnit().getPosition());
Node endNode = graph.getNode(position);
if (mapFunction.pathFinder(graph, gameFunction.getSelectedUnit(), startNode, endNode) != null)
if (gameFunction.pathFinder(graph, gameFunction.getSelectedUnit(), startNode, endNode) != null)
{
updateUnitPosition(position, gameFunction);
}
......@@ -92,5 +98,10 @@ namespace Controller
gameFunction.getSelectedUnit().setPosition(positionX, positionY);
}
}
public void setAnimating(bool animating)
{
isAnimating = animating;
}
}
}
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