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; 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 LevelBase() { } public abstract void CreateMap(); public virtual void Initialize(GameScene sceneBase) { this.ParentScene = sceneBase; CreateMap(); lastLevelCheck = UserdataManager.GetLevel(); } public void Draw(float deltaTime) { Map.Draw(deltaTime); DrawUnique(deltaTime); } public void Update(float deltaTime, TouchLocation[] touchLocations) { Map.Update(deltaTime); if(Health <= 0) { Finished = true; } UpdateUnique(deltaTime, touchLocations); CheckForObjectPlacement(touchLocations); CheckForLevelUp(); } 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) 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]) 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) return; } catch { return; } } } Money -= ParentScene.ObjectSeletor.Objects[ParentScene.ObjectSeletor.SelectedObjectIndex].Item2; MoneySpent += ParentScene.ObjectSeletor.Objects[ParentScene.ObjectSeletor.SelectedObjectIndex].Item2; 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(); } } }