/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { lastKeyboardState = keyboardState; keyboardState = Keyboard.GetState(); lastPlayerOneState = playerOneState; playerOneState = GamePad.GetState(PlayerIndex.One); // Update _elapsed_time += (float)gameTime.ElapsedGameTime.TotalMilliseconds; battleTimer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; // 1 Second has passed if (_elapsed_time >= 1000.0f) { _fps = _total_frames; _total_frames = 0; _elapsed_time = 0; } if (showMenu && !gameOver) { switch (battleStep) { case 0: if (battleTimer >= 500.0f) { if (chain[0] == Skills.Skill.Attack) { if (cadwyn.HP > cadwyn.MaxHP / 4) { cadwyn.CurrentRow = 0; } else { cadwyn.CurrentRow = 2; } _frame_timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (_frame_timer > 50) { cadwyn.CurrentFrame++; _frame_timer = 0; } if (cadwyn.CurrentFrame == 5) { cadwyn.CurrentFrame = 0; battleTimer = 0; battleStep++; } } else { if (cadwyn.HP > cadwyn.MaxHP / 4) { cadwyn.CurrentRow = 1; } else { cadwyn.CurrentRow = 3; } _frame_timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (_frame_timer > 50) { cadwyn.CurrentFrame++; _frame_timer = 0; } if (cadwyn.CurrentFrame == 3) { cadwyn.CurrentFrame = 0; battleTimer = 0; battleStep++; } } } break; case 1: if (battleTimer >= 500.0f) { if (chain[1] == Skills.Skill.Attack) { if (cadwyn.HP > cadwyn.MaxHP / 4) { cadwyn.CurrentRow = 0; } else { cadwyn.CurrentRow = 2; } _frame_timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (_frame_timer > 50) { cadwyn.CurrentFrame++; _frame_timer = 0; } if (cadwyn.CurrentFrame == 5) { cadwyn.CurrentFrame = 0; battleTimer = 0; battleStep++; } } else { if (cadwyn.HP > cadwyn.MaxHP / 4) { cadwyn.CurrentRow = 1; } else { cadwyn.CurrentRow = 3; } _frame_timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (_frame_timer > 50) { cadwyn.CurrentFrame++; _frame_timer = 0; } if (cadwyn.CurrentFrame == 3) { cadwyn.CurrentFrame = 0; battleTimer = 0; battleStep++; } } } break; case 2: if (battleTimer >= 500.0f) { if (chain[2] == Skills.Skill.Attack) { if (cadwyn.HP > cadwyn.MaxHP / 4) { cadwyn.CurrentRow = 0; } else { cadwyn.CurrentRow = 2; } _frame_timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (_frame_timer > 50) { cadwyn.CurrentFrame++; _frame_timer = 0; } if (cadwyn.CurrentFrame == 5) { cadwyn.CurrentFrame = 0; battleTimer = 0; battleStep++; } } else { if (cadwyn.HP > cadwyn.MaxHP / 4) { cadwyn.CurrentRow = 1; } else { cadwyn.CurrentRow = 3; } _frame_timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (_frame_timer > 50) { cadwyn.CurrentFrame++; _frame_timer = 0; } if (cadwyn.CurrentFrame == 3) { cadwyn.CurrentFrame = 0; battleTimer = 0; battleStep++; } } } break; case 3: if (battleTimer >= 750.0f) { cadwyn.CurrentFrame = 0; enemy.Position -= new Vector2(25, 0); showPrevHP[0] = false; battleTimer = 0; battleStep++; } break; case 4: if (battleTimer >= 750.0f) { enemy.Position += new Vector2(25, 0); battleTimer = 0; battleStep++; } break; case 5: if (battleTimer >= 1000.0f) { _frame_timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (_frame_timer > 100) { enemy.CurrentFrame++; _frame_timer = 0; enemy.Position += new Vector2(25, 0); } if (enemy.CurrentFrame == 3) { enemy.CurrentFrame = 0; enemy.Position += new Vector2(-50, 0); cadwyn.Position += new Vector2(25, 0); showPrevHP[1] = false; battleTimer = 0; battleStep++; } } break; case 6: if (battleTimer >= 500.0f) { enemy.CurrentFrame = 0; enemy.Position -= new Vector2(25, 0); cadwyn.Position -= new Vector2(25, 0); battleTimer = 0; battleStep++; } break; case 7: if (battleTimer >= 500.0f) { battleTimer = 0; battleStep = 0; showMenu = false; menu = new Menu(Menu.MenuType.Battle); skillCount = 0; chain = new List<Skills.Skill>(); menu.InfoText = string.Empty; if (cadwyn.HP == 0) { gameOver = true; showMenu = true; break; } else if (enemy.HP == 0) { gameOver = true; showMenu = true; } } if (win) if (enemy.CharType == Character.CharacterType.BossSlime) currentGameState = GameState.GameOver; break; } } // Allows the game to exit if (playerOneState.Buttons.Back == ButtonState.Pressed || KeyPressed(Keys.Escape)) this.Exit(); switch (currentGameState) { case GameState.TitleScreen: if (KeyPressed(Keys.Space) || ButtonPressed(Buttons.A) || ButtonPressed(Buttons.Start)) { currentGameState = GameState.Map; MediaPlayer.Play(battleTheme); MediaPlayer.IsRepeating = true; } break; case GameState.Map: bool is_idle = true; KeyboardState ks = Keyboard.GetState(); Keys[] keys = ks.GetPressedKeys(); foreach (Keys key in keys) { switch (key) { case Keys.Up: cad.CurrentRow = 5; is_idle = false; trans += new Vector2(0, 4); break; case Keys.Down: cad.CurrentRow = 4; is_idle = false; trans += new Vector2(0, -4); break; case Keys.Left: cad.CurrentRow = 7; is_idle = false; trans += new Vector2(4, 0); break; case Keys.Right: cad.CurrentRow = 6; is_idle = false; trans += new Vector2(-4, 0); break; } } if (ButtonDown(Buttons.DPadDown)) { cad.CurrentRow = 4; is_idle = false; trans += new Vector2(0, -4); } else if (ButtonDown(Buttons.DPadUp)) { cad.CurrentRow = 5; is_idle = false; trans += new Vector2(0, 4); } else if (ButtonDown(Buttons.DPadLeft)) { cad.CurrentRow = 7; is_idle = false; trans += new Vector2(4, 0); } else if (ButtonDown(Buttons.DPadRight)) { cad.CurrentRow = 6; is_idle = false; trans += new Vector2(-4, 0); } if (-400 < trans.X && trans.X < -350 && 1450 > trans.Y && trans.Y > 1350) { currentGameState = GameState.Battle; enemy = bossSlime; win = false; showMenu = false; gameOver = false; cadwyn = new Character(Character.CharacterType.Cadwyn, Content.Load<Texture2D>("cadwynsheet3"), new Vector2(graphics.PreferredBackBufferWidth - (385 / 2 * 1.5f + 25), graphics.PreferredBackBufferHeight - (327 / 2 * 1.5f + 25)), 384, 327); } if (is_idle) { if (cad.CurrentRow == 4) cad.CurrentRow = 0; if (cad.CurrentRow == 5) cad.CurrentRow = 1; if (cad.CurrentRow == 6) cad.CurrentRow = 2; if (cad.CurrentRow == 7) cad.CurrentRow = 3; cad.CurrentFrame = 0; idle_timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (idle_timer > 3000) //Wait for a specific time { cad.CurrentFrame++; } if (idle_timer > 3100) //Wait for a specific time { cad.CurrentFrame++; } if (idle_timer > 3200) //Wait for a specific time { cad.CurrentFrame++; } if (idle_timer > 3300) //Wait for a specific time { cad.CurrentFrame = 0; idle_timer = 0f; } } else //Character is moving { idle_timer = 0f; timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (timer > interval) { //Show the next frame cad.CurrentFrame++; //Reset the timer timer = 0f; } if (cad.CurrentFrame == 3) //Depending on the sprites per row of the spritesheet { cad.CurrentFrame = 0; } stepCounter++; } cad.SourceRect = new Rectangle(cad.Width * cad.CurrentFrame, cad.Height * cad.CurrentRow, cad.Width, cad.Height); if (stepCounter > 200) { stepCounter = 0; currentGameState = GameState.Battle; LoadBattle(); } break; case GameState.Battle: if (!showMenu) { if (KeyPressed(Keys.Down) || ButtonPressed(Buttons.DPadDown)) menu.Iterator += 1; else if (KeyPressed(Keys.Up) || ButtonPressed(Buttons.DPadUp)) menu.Iterator -= 1; else if (KeyPressed(Keys.Space) || ButtonPressed(Buttons.A)) { if (menu.CurrentMenuType == Menu.MenuType.Battle && menu.Iterator == 0) menu = new Menu(Menu.MenuType.Fight); else if (menu.CurrentMenuType == Menu.MenuType.Fight) { if (skillCount < 3) { switch (menu.Iterator) { case 0: chain.Add(Skills.Skill.Attack); skillCount++; if (skillCount == 3) { showMenu = true; menu.InfoText += "Attack"; } else menu.InfoText += "Attack-->"; break; case 1: chain.Add(Skills.Skill.Fire); skillCount++; if (skillCount == 3) { showMenu = true; menu.InfoText += "Fire"; } else menu.InfoText += "Fire-->"; break; case 2: chain.Add(Skills.Skill.Ice); skillCount++; if (skillCount == 3) { showMenu = true; menu.InfoText += "Ice"; } else menu.InfoText += "Ice-->"; break; case 3: chain.Add(Skills.Skill.Bolt); skillCount++; if (skillCount == 3) { showMenu = true; menu.InfoText += "Bolt"; } else menu.InfoText += "Bolt-->"; break; } } } } else if (KeyPressed(Keys.Back) || ButtonPressed(Buttons.B)) { if (menu.CurrentMenuType == Menu.MenuType.Fight) { menu = new Menu(Menu.MenuType.Battle); skillCount = 0; chain = new List<Skills.Skill>(); menu.InfoText = string.Empty; } } if (skillCount == 3) { List<Skills.Skill> slimeChain = new List<Skills.Skill>(); slimeChain.Add(Skills.Skill.Attack); slimeChain.Add(Skills.Skill.Fire); slimeChain.Add(Skills.Skill.Attack); Console.WriteLine(string.Format("Boss Slime HP before: {0:0}/{1:0}", enemy.HP, enemy.MaxHP)); cadwyn.attack(enemy, chain); Console.WriteLine(string.Format("Boss Slime HP after: {0:0}/{1:0}", enemy.HP, enemy.MaxHP)); Console.WriteLine(string.Format("Cadwyn HP before: {0:0}/{1:0}", cadwyn.HP, cadwyn.MaxHP)); enemy.attack(cadwyn, slimeChain); Console.WriteLine(string.Format("Cadwyn HP after: {0:0}/{1:0}", cadwyn.HP, cadwyn.MaxHP)); showMenu = true; showPrevHP[0] = true; showPrevHP[1] = true; if (enemy.HP == 0) { win = true; cadwyn.HP = cadwyn.PreviousHealth; } else if (cadwyn.HP == 0) win = false; } } if (gameOver && battleTimer >= 1000) { MediaPlayer.Stop(); currentGameState = GameState.Map; } break; //end Battle } // TODO: Add your update logic here base.Update(gameTime); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic menu = new Menu(Menu.MenuType.Battle); chain = new List<Skills.Skill>(); base.Initialize(); }