Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Stratego.Beans;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework;
- using Stratego.Load;
- using Stratego.Database;
- namespace Stratego.Update
- {
- class UpdateButtonEvent
- {
- private static Random rand = new Random();
- private static MouseState prevMouseState;
- private static KeyboardState prevKeyState;
- public static void update(Global global)
- {
- MouseState mouseState = Mouse.GetState();
- KeyboardState keyState = Keyboard.GetState();
- if (mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton != ButtonState.Pressed)
- {
- Rectangle mousePos = new Rectangle(mouseState.X, mouseState.Y, 1, 1);
- foreach (MenuItem item in global.current.menu.items)
- {
- if (item.pos.Intersects(mousePos))
- {
- if (item.name.Equals("exit")) processExitButton(global);
- else if (item.name.Equals("options")) processOptionsButton(global);
- else if (item.name.Equals("play")) processPlayButton(global);
- else if (item.name.Equals("menu")) processMenuButton(global);
- else if (item.name.Equals("undo")) processUndoButton(global);
- else if (item.name.Equals("end")) processEndButton(global);
- else if (item.name.Equals("auto")) processAutoButton(global);
- else if (item.name.Equals("start")) processStartButton(global);
- else if (item.name.Equals("login")) processLoginButton(global);
- }
- }
- }
- //Keyboard Shortcuts
- if (global.current.menu.name.Contains("ingame"))
- {
- if (keyState.IsKeyDown(Keys.E) && !prevKeyState.IsKeyDown(Keys.E))
- {
- processEndButton(global);
- }
- else if (keyState.IsKeyDown(Keys.M) && !prevKeyState.IsKeyDown(Keys.M))
- {
- processMenuButton(global);
- }
- if (keyState.IsKeyDown(Keys.Z) && !prevKeyState.IsKeyDown(Keys.Z))
- {
- processUndoButton(global);
- }
- }
- prevMouseState = mouseState;
- prevKeyState = keyState;
- }
- public static void processExitButton(Global global)
- {
- Environment.Exit(0);
- }
- public static void processOptionsButton(Global global)
- {
- }
- public static void processPlayButton(Global global)
- {
- LoadLevels.load(global);
- global.current.menu = global.getMenuByName("pregame");
- }
- public static void processMenuButton(Global global)
- {
- global.current.menu = global.getMenuByName("main");
- }
- public static void processUndoButton(Global global)
- {
- UpdateUnitMove.undoLastMove(global);
- }
- public static void processEndButton(Global global)
- {
- int dayNum = Convert.ToInt32(global.current.menu.getItemByName("daynum").msg);
- dayNum++;
- global.current.menu.getItemByName("daynum").msg = "" + dayNum;
- //Change current player variable
- if (global.current.owner == 1) global.current.owner = 2;
- //process AI thinking
- UpdateUnitMove.movedUnit = false;
- UpdateUnitMove.prevUnit = null;
- UpdateUnitMove.prevPos = null;
- //Make computer player think
- //WORK IN PROGRESS
- //ArtificialIntelligence.processAI(global);
- //Change current player variable
- if (global.current.owner == 2) global.current.owner = 1;
- }
- public static void processAutoButton(Global global)
- {
- placeUnits(global, 1, 370);
- placeUnits(global, 2, 290);
- }
- public static void processStartButton(Global global)
- {
- processAutoButton(global);
- //switch to ingame menu and begin coding gameplay work
- global.current.menu = global.getMenuByName("ingame");
- }
- public static void processLoginButton(Global global)
- {
- //Checking database and logging in
- Console.WriteLine("Logging in!");
- String userName = global.current.menu.getItemByName("inputUserName").msg.ToLower();
- String userPass = global.current.menu.getItemByName("inputUserPass").msg.ToLower();
- ConnectionUtil.initConnection(null);
- Boolean valid;
- if (!userName.Equals("") && !userPass.Equals(""))
- {
- valid = ConnectionUtil.validateLogin(userName, userPass);
- }
- else
- {
- valid = true;
- }
- if (valid)
- {
- global.current.userName = userName;
- global.current.menu = global.getMenuByName("main");
- if (userName.Equals("")) userName = "Default";
- global.current.menu.getItemByName("userName").msg = userName;
- }
- else
- {
- Console.WriteLine("Invalid :(");
- }
- }
- public static void placeUnits(Global global, int own, int yCap)
- {
- //randomly place enemy units on board
- while (true)
- {
- List<Unit> units = new List<Unit>();
- foreach (Unit unit in global.current.level.unitsOffBoard)
- if (unit.owner == own) units.Add(unit);
- foreach (Unit unit in units)
- {
- if (unit.owner == own)
- {
- foreach (GridBlock block in global.current.level.gridBoard)
- {
- //Check owner 1 placement
- if (own == 1)
- {
- if (block.occupant == null && block.pos.Y > yCap)
- {
- if (rand.Next(0, 10) < 1)
- {
- block.occupant = unit;
- unit.pos = block.pos;
- global.current.level.unitsOffBoard.Remove(unit);
- global.current.level.unitsOnBoard.Add(unit);
- foreach (GridBlock b in global.current.level.gridOffBoard1)
- {
- if (b.occupant == unit) b.occupant = null;
- }
- break;
- }
- }
- }
- //Check owner 2 placement
- else if (own == 2)
- {
- if (block.occupant == null && block.pos.Y < yCap)
- {
- if (rand.Next(0, 10) < 1)
- {
- block.occupant = unit;
- unit.pos = block.pos;
- global.current.level.unitsOffBoard.Remove(unit);
- global.current.level.unitsOnBoard.Add(unit);
- foreach (GridBlock b in global.current.level.gridOffBoard2)
- {
- if (b.occupant == unit) b.occupant = null;
- }
- break;
- }
- }
- }
- }
- }
- }
- if (units.Count == 0) break;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment