using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input.Touch; using Penguloon.Controls; using Penguloon.Levels; using System; namespace Penguloon.Scenes { public class GameScene : SceneBase { public LevelBase Level { get; set; } public float Speed { get; set; } = 1f; public ButtonIngame StartRoundBtn { get; set; } public SpeedButton ChangeSpeedBtn { get; set; } public ButtonIngame OptionsBtn { get; set; } public IngameOptions OptionsMenu { get; set; } public IngameEndStats IngameEndStats { get; set; } public ObjectSelector ObjectSeletor { get; set; } public GameScene(Main main, LevelBase level) : base(main) { this.Level = level; this.Level.Initialize(this); StartRoundBtn = new ButtonIngame(this, new Vector2(Level.Map.MapWidth + StaticUIValues.BorderWidth, StaticUIValues.ScreenViewport.Y - StaticUIValues.IngameUIPlayButtonHeight), new Vector2(StaticUIValues.ScreenViewport.X - (Level.Map.MapWidth + StaticUIValues.BorderWidth), StaticUIValues.IngameUIPlayButtonHeight), Main.Resources.GetString(Resource.String.IngameStart)); OptionsBtn = new ButtonIngame(this, new Vector2(Level.Map.MapWidth + StaticUIValues.BorderWidth, StaticUIValues.ScreenViewport.Y - StaticUIValues.IngameUIPlayButtonHeight - StaticUIValues.IngameUIPlayButtonHeight - StaticUIValues.BorderWidth), new Vector2(StaticUIValues.ScreenViewport.X - (Level.Map.MapWidth + StaticUIValues.BorderWidth), StaticUIValues.IngameUIPlayButtonHeight), Main.Resources.GetString(Resource.String.IngameOptions)); ChangeSpeedBtn = new SpeedButton(this, new Vector2(Level.Map.MapWidth - StaticUIValues.ChangeSpeedButtonWidth, StaticUIValues.ScreenViewport.Y - StaticUIValues.IngameUIPlayButtonHeight), new Vector2(StaticUIValues.ChangeSpeedButtonWidth, StaticUIValues.IngameUIPlayButtonHeight), "X1"); OptionsMenu = new IngameOptions(this, new Vector2((StaticUIValues.ScreenViewport.X / 2) - (StaticUIValues.IngameOptionsSize.X / 2), (StaticUIValues.ScreenViewport.Y / 2) - (StaticUIValues.IngameOptionsSize.Y / 2)), StaticUIValues.IngameOptionsSize); IngameEndStats = new IngameEndStats(this, new Vector2((StaticUIValues.ScreenViewport.X / 2) - (StaticUIValues.IngameOptionsSize.X / 2), (StaticUIValues.ScreenViewport.Y / 2) - (StaticUIValues.IngameOptionsSize.Y / 2)), StaticUIValues.IngameOptionsSize); ChangeSpeedBtn.OnClick += ChangeSpeedBtn_OnClick; StartRoundBtn.OnClick += StartRoundBtn_OnClick; OptionsBtn.OnClick += OptionsBtn_OnClick; ObjectSeletor = new ObjectSelector(level.Map, this, new Vector2(Level.Map.MapWidth + StaticUIValues.BorderWidth, StaticUIValues.IngameUITextAreaHeight + StaticUIValues.BorderWidth), new Vector2((int)StaticUIValues.ScreenViewport.X - (Level.Map.MapWidth + StaticUIValues.BorderWidth), StaticUIValues.ScreenViewport.Y - StaticUIValues.IngameUITextAreaHeight - StaticUIValues.BorderWidth - StaticUIValues.BorderWidth - StaticUIValues.IngameUIPlayButtonHeight - StaticUIValues.BorderWidth - StaticUIValues.IngameUIPlayButtonHeight)); } private void OptionsBtn_OnClick(object sender, ClickArgs e) { if (OptionsMenu.State == IngameOptionsState.Hide) { OptionsMenu.ShowTime = DateTime.Now; OptionsMenu.State = IngameOptionsState.Show; } } private void ChangeSpeedBtn_OnClick(object sender, ClickArgs e) { switch (Speed) { case 1f: Speed = 2f; break; case 2f: Speed = 3f; break; case 3f: Speed = 1f; break; default: Speed = 1f; break; } ChangeSpeedBtn.Text = "X" + Math.Round(Speed, 0).ToString(); } private void StartRoundBtn_OnClick(object sender, ClickArgs e) { Level.Map.WaveManager.StartSpawningEnemies(); ButtonIngame btn = (ButtonIngame)sender; btn.ControlState = ControlState.Disabled; } public override void CreateControls() { } public override void Draw(float deltaTime) { // background Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/background"), destinationRectangle: new Rectangle(0, 0, (int)StaticUIValues.ScreenViewport.X, (int)StaticUIValues.ScreenViewport.Y)); base.Draw(deltaTime); this.Level.Draw(deltaTime); if (Level.SelectedObject == null) { DrawUI(deltaTime); StartRoundBtn.Draw(deltaTime); ObjectSeletor.Draw(deltaTime); ChangeSpeedBtn.Draw(deltaTime); OptionsBtn.Draw(deltaTime); OptionsMenu.Draw(deltaTime); } // show upgrade menu of object is selected else { DrawUpgradeUI(); } // draw "game over" controls here IngameEndStats.Draw(deltaTime); } private void DrawUpgradeUI() { //background Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/objectSelectionBackground"), destinationRectangle: new Rectangle(Level.Map.MapWidth + StaticUIValues.BorderWidth, 0, (int)StaticUIValues.ScreenViewport.X - (Level.Map.MapWidth + StaticUIValues.BorderWidth), (int)StaticUIValues.ScreenViewport.Y)); if (Level.SelectedObject != null) { for (int i = 0; i < Level.SelectedObject.UpgradeList.Count; i++) { Level.SelectedObject.UpgradeList[i].Draw(Main.SpriteBatch, i); } } //border Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/border"), destinationRectangle: new Rectangle(Level.Map.MapWidth, 0, StaticUIValues.BorderWidth, (int)StaticUIValues.ScreenViewport.Y)); //border under text Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/border-horizontal"), destinationRectangle: new Rectangle(Level.Map.MapWidth + StaticUIValues.BorderWidth - 4, StaticUIValues.IngameUITextAreaHeight, (int)StaticUIValues.ScreenViewport.X - (Level.Map.MapWidth + StaticUIValues.BorderWidth - 5), StaticUIValues.BorderWidth)); DrawText(ContentManager.GetFont(StaticUIValues.IngameFont), Main.Resources.GetString(Resource.String.IngameGold) + ": " + Level.Money, new Vector2(Level.Map.MapWidth + StaticUIValues.BorderWidth + 5, 10), new Vector2(StaticUIValues.ScreenViewport.X - Level.Map.MapWidth - StaticUIValues.BorderWidth, StaticUIValues.IngameUITextAreaHeight), TextAllignment.LeftTop, Color.White, Color.Black, 2); DrawText(ContentManager.GetFont(StaticUIValues.IngameFont), Main.Resources.GetString(Resource.String.IngameWave) + ": " + Level.Map.WaveManager.CurrentWave.ToString(), new Vector2(Level.Map.MapWidth + StaticUIValues.BorderWidth + 5, 0), new Vector2(StaticUIValues.ScreenViewport.X - Level.Map.MapWidth - StaticUIValues.BorderWidth, StaticUIValues.IngameUITextAreaHeight), TextAllignment.LeftMiddle, Color.White, Color.Black, 2); DrawText(ContentManager.GetFont(StaticUIValues.IngameFont), Main.Resources.GetString(Resource.String.IngameHealth) + ": " + Level.Health, new Vector2(Level.Map.MapWidth + StaticUIValues.BorderWidth + 5, 0), new Vector2(StaticUIValues.ScreenViewport.X - Level.Map.MapWidth - StaticUIValues.BorderWidth, StaticUIValues.IngameUITextAreaHeight), TextAllignment.LeftBottom, Color.White, Color.Black, 2); } private void DrawUI(float deltaTime) { //background Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/objectSelectionBackground"), destinationRectangle: new Rectangle(Level.Map.MapWidth + StaticUIValues.BorderWidth, 0, (int)StaticUIValues.ScreenViewport.X - (Level.Map.MapWidth + StaticUIValues.BorderWidth), (int)StaticUIValues.ScreenViewport.Y)); //border Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/border"), destinationRectangle: new Rectangle(Level.Map.MapWidth, 0, StaticUIValues.BorderWidth, (int)StaticUIValues.ScreenViewport.Y)); //border under text Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/border-horizontal"), destinationRectangle: new Rectangle(Level.Map.MapWidth + StaticUIValues.BorderWidth - 4, StaticUIValues.IngameUITextAreaHeight, (int)StaticUIValues.ScreenViewport.X - (Level.Map.MapWidth + StaticUIValues.BorderWidth - 5), StaticUIValues.BorderWidth)); //border above options Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/border-horizontal"), destinationRectangle: new Rectangle(Level.Map.MapWidth + StaticUIValues.BorderWidth - 4, (int)StaticUIValues.ScreenViewport.Y - StaticUIValues.IngameUIPlayButtonHeight - StaticUIValues.BorderWidth - StaticUIValues.IngameUIPlayButtonHeight - StaticUIValues.BorderWidth, (int)StaticUIValues.ScreenViewport.X - (Level.Map.MapWidth + StaticUIValues.BorderWidth - 5), StaticUIValues.BorderWidth)); //border above button Main.SpriteBatch.Draw(ContentManager.GetTexture("UI/border-horizontal"), destinationRectangle: new Rectangle(Level.Map.MapWidth + StaticUIValues.BorderWidth - 4, (int)StaticUIValues.ScreenViewport.Y - StaticUIValues.IngameUIPlayButtonHeight - StaticUIValues.BorderWidth, (int)StaticUIValues.ScreenViewport.X - (Level.Map.MapWidth + StaticUIValues.BorderWidth - 5), StaticUIValues.BorderWidth)); DrawText(ContentManager.GetFont(StaticUIValues.IngameFont), Main.Resources.GetString(Resource.String.IngameGold) + ": " + Level.Money, new Vector2(Level.Map.MapWidth + StaticUIValues.BorderWidth + 5, 10), new Vector2(StaticUIValues.ScreenViewport.X - Level.Map.MapWidth - StaticUIValues.BorderWidth, StaticUIValues.IngameUITextAreaHeight), TextAllignment.LeftTop, Color.White, Color.Black, 2); DrawText(ContentManager.GetFont(StaticUIValues.IngameFont), Main.Resources.GetString(Resource.String.IngameWave) + ": " + Level.Map.WaveManager.CurrentWave.ToString(), new Vector2(Level.Map.MapWidth + StaticUIValues.BorderWidth + 5, 0), new Vector2(StaticUIValues.ScreenViewport.X - Level.Map.MapWidth - StaticUIValues.BorderWidth, StaticUIValues.IngameUITextAreaHeight), TextAllignment.LeftMiddle, Color.White, Color.Black, 2); DrawText(ContentManager.GetFont(StaticUIValues.IngameFont), Main.Resources.GetString(Resource.String.IngameHealth) + ": " + Level.Health, new Vector2(Level.Map.MapWidth + StaticUIValues.BorderWidth + 5, 0), new Vector2(StaticUIValues.ScreenViewport.X - Level.Map.MapWidth - StaticUIValues.BorderWidth, StaticUIValues.IngameUITextAreaHeight), TextAllignment.LeftBottom, Color.White, Color.Black, 2); } public override void Update(float deltaTime, TouchLocation[] touchLocations) { // We shouldn't update controls when the game is finished to prevent the user from placing any more objects if (!Level.Finished && Level.SelectedObject == null) { base.Update(deltaTime, touchLocations); StartRoundBtn.Update(deltaTime, touchLocations); ObjectSeletor.Update(deltaTime, touchLocations); ChangeSpeedBtn.Update(deltaTime, touchLocations); OptionsBtn.Update(deltaTime, touchLocations); OptionsMenu.Update(deltaTime, touchLocations); } // update finished controls here else if (Level.Finished) { IngameEndStats.Update(deltaTime, touchLocations); } // update upgrade menu here else { UpdateUpgradeMenu(touchLocations); } if (StartRoundBtn.ControlState == ControlState.Disabled && !Level.Map.WaveManager.RoundActive && !Level.Finished) { StartRoundBtn.ControlState = ControlState.Idle; Level.Map.WaveManager.FinishRound(); } this.Level.Update(deltaTime * Speed, touchLocations); } private void UpdateUpgradeMenu(TouchLocation[] touchLocations) { int startY = StaticUIValues.IngameUITextAreaHeight + StaticUIValues.BorderWidth; if (Level.SelectedObject != null) { // for each available upgrade in selected object for (int i = 0; i < Level.SelectedObject.UpgradeList.Count; i++) { Level.SelectedObject.UpgradeList[i].State = Objects.UpgradeState.Idle; Rectangle upgradeBox = new Rectangle( (int)Level.Map.MapWidth, startY + (int)StaticUIValues.UpgradePanelSize.Y * i, (int)StaticUIValues.UpgradePanelSize.X, (int)StaticUIValues.UpgradePanelSize.Y); for (int x = 0; x < touchLocations.Length; x++) { // if finger is released on upgrade box if (touchLocations[x].State == TouchLocationState.Released) { if (upgradeBox.Contains(touchLocations[x].Position.ToPoint())) { // continue of we cant afford this upgrade if (Level.SelectedObject.UpgradeList[i].Cost > Level.Money) { SoundManager.PlayUnavailableSound(); continue; } Level.SelectedObject.UpgradeList[i].Click(); // pay upgrade cost Level.Money -= Level.SelectedObject.UpgradeList[i].Cost; // play buy sound here SoundManager.PlayUpgradeSound(); // insert next upgrade if present if (Level.SelectedObject.UpgradeList[i].NextUgrade != null) { Level.SelectedObject.UpgradeList.Insert(i, Level.SelectedObject.UpgradeList[i].NextUgrade); // remove clicked upgrade if (Level.SelectedObject.UpgradeList.Count > i + 1) Level.SelectedObject.UpgradeList.RemoveAt(i + 1); } else // remove clicked upgrade if (Level.SelectedObject.UpgradeList.Count > i) Level.SelectedObject.UpgradeList.RemoveAt(i); } } else { if (upgradeBox.Contains(touchLocations[x].Position.ToPoint())) { Level.SelectedObject.UpgradeList[i].State = Objects.UpgradeState.Pressed; } } } } } } } }