using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input.Touch; using Penguloon.Scenes; using Penguloon.Controls; using System.Collections.Generic; using Penguloon.Objects; using Microsoft.Xna.Framework.Input; namespace Penguloon.Levels { public abstract class LevelBase { public LevelType LevelType { get; set; } public GameScene ParentScene { get; set; } public Texture2D SplashArt { get; set; } public Map Map { get; set; } public int Health { get; set; } public int Money { get; set; } = 0; public int MoneySpent { get; set; } = 0; public int ID { get; set; } public int Kills { get; set; } = 0; public bool Finished { get; set; } = false; public int MinimumLevel { get; set; } public TouchLocation[] PrevTouchLocations { get; set; } public DateTime StartDate { get; set; } public LevelBase() { } public abstract void CreateMap(); public virtual void Initialize(GameScene sceneBase) { StartDate = DateTime.Now; this.ParentScene = sceneBase; CreateMap(); lastLevelCheck = UserdataManager.GetLevel(); } public void Draw(float deltaTime) { Map.Draw(deltaTime); DrawUnique(deltaTime); DrawSelectedObject(); string time = (DateTime.Now - StartDate).ToString("hh':'mm':'ss"); ParentScene.DrawText(ContentManager.GetFont("Fonts/GWENT/36"), time, new Vector2(10, StaticUIValues.ScreenViewport.Y - ContentManager.GetFont("Fonts/GWENT/36").MeasureString("XD").Y - 10), new Vector2(), TextAllignment.LeftTop, Color.White, Color.Black, 2); } private void DrawSelectedObject() { if (ParentScene.ObjectSeletor.State == Controls.State.Idle || ParentScene.ObjectSeletor.SelectedObjectIndex == -1) return; for (int i = 0; i < PrevTouchLocations.Length; i++) { if (PrevTouchLocations[i].State != TouchLocationState.Moved && PrevTouchLocations[i].State != TouchLocationState.Pressed) continue; ObjectBase obj = ParentScene.ObjectSeletor.Objects[ParentScene.ObjectSeletor.SelectedObjectIndex].Item1; Point tilePos = PrevTouchLocations[i].Position.ToPoint(); tilePos.X = (tilePos.X / Map.TileWidth) * Map.TileWidth; tilePos.Y = (tilePos.Y / Map.TileHeight) * Map.TileHeight; int objWidth = Map.TileWidth * obj.TileSpanX; int objHeight = Map.TileHeight * obj.TileSpanY; int rangeWidth = (int)(obj.Range * 2); int rangeHeight = (int)(obj.Range * 2); Rectangle rangeCircleRec = new Rectangle(tilePos.X - ((rangeWidth - objWidth) / 2), tilePos.Y - ((rangeHeight - objHeight) / 2), rangeWidth, rangeHeight); if (obj.RangeCircle == null) obj.RangeCircle = obj.CreateCircle((int)obj.Range * 2); if (obj.RangeCircle != null) Map.ParentScene.Main.SpriteBatch.Draw(obj.RangeCircle, destinationRectangle: rangeCircleRec); if (obj.TextureBase != null) ParentScene.Main.SpriteBatch.Draw(obj.TextureBase, destinationRectangle: new Rectangle(tilePos, new Point(objWidth, objHeight))); ParentScene.Main.SpriteBatch.Draw(obj.Texture, destinationRectangle: new Rectangle(tilePos, new Point(objWidth, objHeight))); } } public void Update(float deltaTime, TouchLocation[] touchLocations) { Map.Update(deltaTime); if(Health <= 0) { Finished = true; } UpdateUnique(deltaTime, touchLocations); CheckForObjectPlacement(touchLocations); CheckForLevelUp(); PrevTouchLocations = touchLocations; } int lastLevelCheck; private void CheckForLevelUp() { if (Finished) return; int lvl = UserdataManager.GetLevel(UserdataManager.TotalKills + Kills); if (lvl != lastLevelCheck) { lastLevelCheck = lvl; Alert.Show("Leveled up to " + lvl + "!", 3000, ParentScene); } } private void CheckForObjectPlacement(TouchLocation[] touchLocations) { if (ParentScene.ObjectSeletor.State == Controls.State.Idle || ParentScene.ObjectSeletor.SelectedObjectIndex == -1) return; for(int i = 0; i < touchLocations.Length; i++) { if (touchLocations[i].State != TouchLocationState.Released) continue; if (touchLocations[i].Position.X > Map.MapWidth) return; if (touchLocations[i].Position.Y > Map.MapHeight) return; int tileX = (int)touchLocations[i].Position.X / Map.TileWidth; int tileY = (int)touchLocations[i].Position.Y / Map.TileHeight; if (Map.TileMap[tileY, tileX].Direction != Direction.None) { ParentScene.ObjectSeletor.State = Controls.State.Idle; ParentScene.ObjectSeletor.SelectedObjectIndex = -1; return; } int posToSpawnX = tileX * Map.TileWidth; int posToSpawnY = tileY * Map.TileHeight; List spawnPosToCheck = new List(); for(int x = 0; x < ParentScene.ObjectSeletor.Objects[ParentScene.ObjectSeletor.SelectedObjectIndex].Item1.TileSpanX; x++) { for (int y = 0; y < ParentScene.ObjectSeletor.Objects[ParentScene.ObjectSeletor.SelectedObjectIndex].Item1.TileSpanY; y++) { spawnPosToCheck.Add(new Vector2(posToSpawnX + (Map.TileWidth * x), posToSpawnY + (Map.TileHeight * y))); } } // check if there isnt an object already for (int x = 0; x < Map.Objects.Count; x++) { for (int px = 0; px < Map.Objects[x].TileSpanX; px++) { for (int py = 0; py < Map.Objects[x].TileSpanY; py++) { Vector2 posToCheck = Map.Objects[x].Position + new Vector2(px * Map.TileWidth, py * Map.TileHeight); for (int t = 0; t < spawnPosToCheck.Count; t++) if (posToCheck == spawnPosToCheck[t]) { ParentScene.ObjectSeletor.State = Controls.State.Idle; ParentScene.ObjectSeletor.SelectedObjectIndex = -1; return; } } } } // check if it isnt touching any walkable tiles for (int px = 0; px < ParentScene.ObjectSeletor.Objects[ParentScene.ObjectSeletor.SelectedObjectIndex].Item1.TileSpanX; px++) { for (int py = 0; py < ParentScene.ObjectSeletor.Objects[ParentScene.ObjectSeletor.SelectedObjectIndex].Item1.TileSpanY; py++) { Vector2 posToCheck = new Vector2(posToSpawnX, posToSpawnY) + new Vector2(px * Map.TileWidth, py * Map.TileHeight); int tx = (int)posToCheck.X / Map.TileWidth; int ty = (int)posToCheck.Y / Map.TileHeight; try { if (Map.TileMap[ty, tx].Direction != Direction.None) { ParentScene.ObjectSeletor.State = Controls.State.Idle; ParentScene.ObjectSeletor.SelectedObjectIndex = -1; return; } } catch { return; } } } Money -= ParentScene.ObjectSeletor.Objects[ParentScene.ObjectSeletor.SelectedObjectIndex].Item2; MoneySpent += ParentScene.ObjectSeletor.Objects[ParentScene.ObjectSeletor.SelectedObjectIndex].Item2; SoundManager.PlayPlaceObjectSound(); Map.SpawnObject(ParentScene.ObjectSeletor.Objects[ParentScene.ObjectSeletor.SelectedObjectIndex].Item1.GetType(), new Vector2(posToSpawnX, posToSpawnY)); ParentScene.ObjectSeletor.State = Controls.State.Idle; ParentScene.ObjectSeletor.SelectedObjectIndex = -1; } } public virtual void DrawUnique(float deltaTime) { } public virtual void UpdateUnique(float deltaTime, TouchLocation[] touchLocations) { } public void FinishGame() { Finished = true; // upload score here or something UserdataManager.GamesPlayed++; UserdataManager.TotalKills += Kills; UserdataManager.TotalMoneySpent += MoneySpent; if (Kills > UserdataManager.HighestKills) UserdataManager.HighestKills = Kills; if (Map.WaveManager.CurrentWave - 1 > UserdataManager.HighestRound) UserdataManager.HighestRound = Map.WaveManager.CurrentWave - 1; UserdataManager.WriteData(ParentScene.Main.Context); UserdataManager.Level = UserdataManager.GetLevel(); } } }