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

Drop down menu works

parent bee1c501
No related branches found
No related tags found
No related merge requests found
......@@ -217,14 +217,9 @@ namespace Controller
#region Drop Down menu
if (unit.getMenuOpen()) // if dropDowMenu should be opened, draw dropDownMenu
{
Vector2 menuIncrement = new Vector2(0, 32); //increment downwards for successive button
Vector2 initialOffset = new Vector2(32, 0);
Vector2 currentButtonPosition = Vector2.Add(unit.getPixelCoordinates(), initialOffset); //starting location for button
foreach (MenuButtonType buttonType in Enum.GetValues(typeof(MenuButtonType)))
foreach (MenuButton button in unit.getMenuButtons())
{
spriteBatch.Draw(unit.getButtonImage(buttonType), currentButtonPosition, null, Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0f);
currentButtonPosition = Vector2.Add(currentButtonPosition, menuIncrement); //increment location
spriteBatch.Draw(button.getImage(), button.getPixelCoordinates(), null, Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0f);
}
}
#endregion
......
......@@ -70,4 +70,12 @@ namespace Model
CharMenu, // character drop down info menu
AttackMenu // attack menu
}
enum TurnState
{
Wait,
Attack,
Move,
Items
}
}
......@@ -6,6 +6,7 @@ using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using Model;
using View;
using System.Threading;
using System.Diagnostics;
......@@ -15,7 +16,7 @@ namespace Controller
{
private static MouseState lastMouseState; // this state allows for detection of a single click (not click and hold)
private static MouseState currentMouseState;
private static int turnState = 0; // 0 = no button selected, 1 = attack selected, 2 = move selected
private static TurnState turnState = TurnState.Wait;
public static void updateMouse(Graph graph)
{
......@@ -26,6 +27,14 @@ namespace Controller
bool validX = currentMouseState.X < GameState.SCREEN_WIDTH && currentMouseState.X > 0;
bool validY = currentMouseState.Y < GameState.SCREEN_HEIGHT && currentMouseState.Y > 0;
// if unit is selected when right-clicked, deselect it
if (lastMouseState.RightButton == ButtonState.Released
&& currentMouseState.RightButton == ButtonState.Pressed
&& validX && validY && GameState.isPlayableUnitSelected())
{
setSelectedUnit(null, false);
}
// checks for single mouse left click inside the game window
if (lastMouseState.LeftButton == ButtonState.Released
&& currentMouseState.LeftButton == ButtonState.Pressed
......@@ -44,23 +53,31 @@ namespace Controller
// if player clicks after unit is already selected ...
if (GameState.isPlayableUnitSelected())
{
// TODO: determine if player has clicked on a menu button
//if (mouseClickCoordinates == GameState.getSelectedUnit())
//{
//}
// determine if player has clicked on a menu button; if so, change turnState
if (getMenuButtonClicked(mouseClickCoordinates) != null)
{
MenuButton menuButton = getMenuButtonClicked(mouseClickCoordinates);
buttonAction(menuButton.getButtonType());
return;
}
// if user clicks on a valid end node, move to it
Node startNode = graph.getNode(GameState.getSelectedUnit().getPosition());
Node endNode = graph.getNode(mouseClickCoordinates);
LinkedList<Node> path = GameFunction.pathFinder(graph, GameState.getSelectedUnit(), startNode, endNode);
// if path finder returns a non-null path, then end node is valid
if (path != null)
if (turnState == TurnState.Move)
{
updateUnitPosition(mouseClickCoordinates, path);
GameState.getSelectedUnit().setMenuOpen(false); // remove drop down menu from screen
Node startNode = graph.getNode(GameState.getSelectedUnit().getPosition());
Node endNode = graph.getNode(mouseClickCoordinates);
LinkedList<Node> path = GameFunction.pathFinder(graph, GameState.getSelectedUnit(), startNode, endNode);
// if path finder returns a non-null path, then end node is valid
if (path != null)
{
updateUnitPosition(mouseClickCoordinates, path);
}
GameState.getSelectedUnit().setMenuOpen(true); // opens drop down menu on screen again
setSelectedUnit(null, false); // unselect the unit
turnState = TurnState.Wait;
}
setSelectedUnit(null, false); // unselect the unit
}
// if player clicks when a unit is not selected ...
else
......@@ -83,7 +100,6 @@ namespace Controller
if (unit != null)
{
unit.setMenuOpen(selected);
unit.setClickedOn(selected);
}
GameState.setPlayableUnitSelected(selected);
GameState.setSelectedUnit(unit);
......@@ -115,27 +131,46 @@ namespace Controller
// moves the unit to each node in the path (one node at a time)
foreach(Node node in path)
{
Thread.Sleep(200);
unit.setPosition(node.getPositionX(), node.getPositionY());
}
}
// returns a menu button that was clicked; if no menu button was clicked, returns null
private static MenuButton getMenuButtonClicked(Vector2 mouseCoordinates)
{
Unit unit = GameState.getSelectedUnit();
MenuButton[] menuButtons = unit.getMenuButtons();
int clickX = (int) mouseCoordinates.X;
int clickY = (int) mouseCoordinates.Y;
for (int i=0; i<menuButtons.Length; i++)
{
int buttonX = (int) menuButtons[i].getPixelCoordinates().X;
int buttonY = (int) menuButtons[i].getPixelCoordinates().Y;
if (buttonX <= clickX && clickX < buttonX+128 && buttonY <= clickY && clickY < buttonY+32)
{
return menuButtons[i];
}
}
return null;
}
// sets which menu option is selected
private static void buttonAction(int i)
private static void buttonAction(MenuButtonType buttonType)
{
// take action corresponding to which button was clicked
switch (i)
switch (buttonType)
{
case 0: // if attack clicked
turnState = 1;
case MenuButtonType.Attack: // if attack clicked
turnState = TurnState.Attack;
break;
case 1: // if moved is clicked
turnState = 2;
case MenuButtonType.Move: // if moved is clicked
turnState = TurnState.Move;
break;
case 2: // if item is clicked
turnState = 3;
case MenuButtonType.Items: // if item is clicked
turnState = TurnState.Items;
break;
case 3: // if wait is clicked
case MenuButtonType.Wait: // if wait is clicked
break;
default:
......
......@@ -38,6 +38,7 @@ namespace Model
void setMoved(bool a); // on start of players turn, set all units to unmoved (F)
void setMenuOpen(bool a); // Sets if the dropdown menu should be opened
bool getMenuOpen(); // returns if the dropdown menu should be open
MenuButton[] getMenuButtons(); // returns the dropdown menu buttons of the unit
}
enum UnitType { Warrior, Archer, Mage }; //defines the possible classes of a unit
......
......@@ -47,6 +47,8 @@ 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);
setMenuButtonCoordinates(pixelCoordinates);
}
public bool isAlive()
......@@ -213,13 +215,18 @@ namespace Model
{
return menuOpen;
}
public MenuButton[] getMenuButtons()
{
return menuButtons;
}
// updates menu button positions
private void setMenuButtonCoordinates(Vector2 pixelCoordinates)
{
for (int i=0; i<menuButtons.Length; i++)
{
menuButtons[i].setPixelCoordinates((int) pixelCoordinates.X, (int) pixelCoordinates.Y + (i*32));
menuButtons[i].setPixelCoordinates((int) (pixelCoordinates.X+32), (int) (pixelCoordinates.Y + (i*32)));
}
}
}
......
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