Guest User

Untitled

a guest
Jul 25th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.35 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Stratego.Beans;
  6. using Microsoft.Xna.Framework.Input;
  7. using Microsoft.Xna.Framework;
  8. using Stratego.Load;
  9. using Stratego.Database;
  10.  
  11. namespace Stratego.Update
  12. {
  13.     class UpdateButtonEvent
  14.     {
  15.         private static Random rand = new Random();
  16.         private static MouseState prevMouseState;
  17.         private static KeyboardState prevKeyState;
  18.  
  19.         public static void update(Global global)
  20.         {
  21.             MouseState mouseState = Mouse.GetState();
  22.             KeyboardState keyState = Keyboard.GetState();
  23.  
  24.             if (mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton != ButtonState.Pressed)
  25.             {
  26.                 Rectangle mousePos = new Rectangle(mouseState.X, mouseState.Y, 1, 1);
  27.                 foreach (MenuItem item in global.current.menu.items)
  28.                 {
  29.                     if (item.pos.Intersects(mousePos))
  30.                     {
  31.                         if (item.name.Equals("exit")) processExitButton(global);
  32.                         else if (item.name.Equals("options")) processOptionsButton(global);
  33.                         else if (item.name.Equals("play")) processPlayButton(global);
  34.                         else if (item.name.Equals("menu")) processMenuButton(global);
  35.                         else if (item.name.Equals("undo")) processUndoButton(global);
  36.                         else if (item.name.Equals("end")) processEndButton(global);
  37.                         else if (item.name.Equals("auto")) processAutoButton(global);
  38.                         else if (item.name.Equals("start")) processStartButton(global);
  39.                         else if (item.name.Equals("login")) processLoginButton(global);
  40.                     }
  41.                 }
  42.             }
  43.  
  44.             //Keyboard Shortcuts
  45.             if (global.current.menu.name.Contains("ingame"))
  46.             {
  47.                 if (keyState.IsKeyDown(Keys.E) && !prevKeyState.IsKeyDown(Keys.E))
  48.                 {
  49.                     processEndButton(global);
  50.                 }
  51.                 else if (keyState.IsKeyDown(Keys.M) && !prevKeyState.IsKeyDown(Keys.M))
  52.                 {
  53.                     processMenuButton(global);
  54.                 }
  55.                 if (keyState.IsKeyDown(Keys.Z) && !prevKeyState.IsKeyDown(Keys.Z))
  56.                 {
  57.                     processUndoButton(global);
  58.                 }
  59.             }
  60.  
  61.             prevMouseState = mouseState;
  62.             prevKeyState = keyState;
  63.         }
  64.  
  65.         public static void processExitButton(Global global)
  66.         {
  67.             Environment.Exit(0);
  68.         }
  69.         public static void processOptionsButton(Global global)
  70.         {
  71.  
  72.         }
  73.         public static void processPlayButton(Global global)
  74.         {
  75.             LoadLevels.load(global);
  76.             global.current.menu = global.getMenuByName("pregame");
  77.         }
  78.         public static void processMenuButton(Global global)
  79.         {
  80.             global.current.menu = global.getMenuByName("main");
  81.         }
  82.         public static void processUndoButton(Global global)
  83.         {
  84.             UpdateUnitMove.undoLastMove(global);
  85.         }
  86.         public static void processEndButton(Global global)
  87.         {
  88.             int dayNum = Convert.ToInt32(global.current.menu.getItemByName("daynum").msg);
  89.             dayNum++;
  90.             global.current.menu.getItemByName("daynum").msg = "" + dayNum;
  91.  
  92.             //Change current player variable
  93.             if (global.current.owner == 1) global.current.owner = 2;
  94.  
  95.             //process AI thinking
  96.             UpdateUnitMove.movedUnit = false;
  97.             UpdateUnitMove.prevUnit = null;
  98.             UpdateUnitMove.prevPos = null;
  99.  
  100.             //Make computer player think
  101.             //WORK IN PROGRESS
  102.             //ArtificialIntelligence.processAI(global);
  103.  
  104.             //Change current player variable
  105.             if (global.current.owner == 2) global.current.owner = 1;
  106.         }
  107.         public static void processAutoButton(Global global)
  108.         {
  109.             placeUnits(global, 1, 370);
  110.             placeUnits(global, 2, 290);
  111.         }
  112.         public static void processStartButton(Global global)
  113.         {
  114.             processAutoButton(global);
  115.             //switch to ingame menu and begin coding gameplay work
  116.             global.current.menu = global.getMenuByName("ingame");
  117.         }
  118.         public static void processLoginButton(Global global)
  119.         {
  120.             //Checking database and logging in
  121.             Console.WriteLine("Logging in!");
  122.             String userName = global.current.menu.getItemByName("inputUserName").msg.ToLower();
  123.             String userPass = global.current.menu.getItemByName("inputUserPass").msg.ToLower();
  124.             ConnectionUtil.initConnection(null);
  125.             Boolean valid;
  126.             if (!userName.Equals("") && !userPass.Equals(""))
  127.             {
  128.                 valid = ConnectionUtil.validateLogin(userName, userPass);
  129.             }
  130.             else
  131.             {
  132.                 valid = true;
  133.             }
  134.             if (valid)
  135.             {
  136.                 global.current.userName = userName;
  137.                 global.current.menu = global.getMenuByName("main");
  138.                 if (userName.Equals("")) userName = "Default";
  139.                 global.current.menu.getItemByName("userName").msg = userName;
  140.             }
  141.             else
  142.             {
  143.                 Console.WriteLine("Invalid :(");
  144.             }
  145.         }
  146.         public static void placeUnits(Global global, int own, int yCap)
  147.         {
  148.             //randomly place enemy units on board
  149.             while (true)
  150.             {
  151.                 List<Unit> units = new List<Unit>();
  152.                 foreach (Unit unit in global.current.level.unitsOffBoard)
  153.                     if (unit.owner == own) units.Add(unit);
  154.                 foreach (Unit unit in units)
  155.                 {
  156.                     if (unit.owner == own)
  157.                     {
  158.                         foreach (GridBlock block in global.current.level.gridBoard)
  159.                         {
  160.                             //Check owner 1 placement
  161.                             if (own == 1)
  162.                             {
  163.                                 if (block.occupant == null && block.pos.Y > yCap)
  164.                                 {
  165.                                     if (rand.Next(0, 10) < 1)
  166.                                     {
  167.                                         block.occupant = unit;
  168.                                         unit.pos = block.pos;
  169.                                         global.current.level.unitsOffBoard.Remove(unit);
  170.                                         global.current.level.unitsOnBoard.Add(unit);
  171.                                         foreach (GridBlock b in global.current.level.gridOffBoard1)
  172.                                         {
  173.                                             if (b.occupant == unit) b.occupant = null;
  174.                                         }
  175.                                         break;
  176.                                     }
  177.                                 }
  178.                             }
  179.                             //Check owner 2 placement
  180.                             else if (own == 2)
  181.                             {
  182.                                 if (block.occupant == null && block.pos.Y < yCap)
  183.                                 {
  184.                                     if (rand.Next(0, 10) < 1)
  185.                                     {
  186.                                         block.occupant = unit;
  187.                                         unit.pos = block.pos;
  188.                                         global.current.level.unitsOffBoard.Remove(unit);
  189.                                         global.current.level.unitsOnBoard.Add(unit);
  190.                                         foreach (GridBlock b in global.current.level.gridOffBoard2)
  191.                                         {
  192.                                             if (b.occupant == unit) b.occupant = null;
  193.                                         }
  194.                                         break;
  195.                                     }
  196.                                 }
  197.                             }
  198.                         }
  199.                     }
  200.                 }
  201.                 if (units.Count == 0) break;
  202.             }  
  203.         }
  204.     }
  205. }
Add Comment
Please, Sign In to add comment