using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Penguloon.Enemies; using Penguloon.Scenes; using System.Collections.Generic; using System; using Penguloon.Objects; using Penguloon.Controls; namespace Penguloon.Levels { public class Map { // 18 x 13 public Tile[,] TileMap { get; set; } public int MapWidth { get; private set; } public int MapHeight { get; private set; } public int TileWidth { get; private set; } public int TileHeight { get; private set; } public GameScene ParentScene { get; set; } public List Enemies { get; set; } = new List(); public List Objects { get; set; } = new List(); public Vector2 SpawnPoint { get; set; } public Vector2 SpawnPointTargetPos { get; set; } public Vector2 FinishPoint { get; set; } public WaveManager WaveManager { get; set; } public LevelBase Level { get; set; } public int SpareTilesY { get; set; } public Map(GameScene parentScene, LevelBase level) { this.ParentScene = parentScene; this.Level = level; MapWidth = (int)(StaticUIValues.ScreenViewport.X - StaticUIValues.IngameUIWidth); MapHeight = (int)(StaticUIValues.ScreenViewport.Y); TileWidth = MapWidth / 18; if (TileWidth * 13 > MapHeight) { TileHeight = MapHeight / 13; TileWidth = TileHeight; } else { TileHeight = TileWidth; } MapWidth = TileWidth * 18; MapHeight = TileHeight * 13; SpareTilesY = ((int)StaticUIValues.ScreenViewport.Y - MapHeight) / TileHeight; SpareTilesY += 1; WaveManager = new WaveManager(this); } public void Draw(float deltaTime) { DrawMap(); DrawSpareTiles(); for (int i = 0; i < Enemies.Count; i++) { if (Enemies[i].Texture != null) ParentScene.Main.SpriteBatch.Draw(Enemies[i].Texture, destinationRectangle: Enemies[i].Box); } for (int i = 0; i < Objects.Count; i++) { Objects[i].Draw(deltaTime); } } private void DrawMap() { for (int y = 0; y < TileMap.GetLength(0); y++) { for (int x = 0; x < TileMap.GetLength(1); x++) { Texture2D tileTexture = null; switch (Level.LevelType) { case LevelType.Ice: tileTexture = ContentManager.GetTileTextureByType(TileMap[y, x].Type); break; case LevelType.Sand: tileTexture = ContentManager.GetSandTileTextureByType(TileMap[y, x].Type); break; } if (tileTexture != null) ParentScene.Main.SpriteBatch.Draw(tileTexture, destinationRectangle: new Rectangle(x * TileWidth, y * TileHeight, TileWidth, TileHeight)); //if (ParentScene.ObjectSeletor.State == Controls.State.Selected) // ParentScene.Main.SpriteBatch.Draw(tileTexture, // destinationRectangle: new Rectangle(x * TileWidth, y * TileHeight, TileWidth, TileHeight)); } } } private void DrawSpareTiles() { Texture2D tileTextureSpare = null; Texture2D unplaceableTileTexture = null; unplaceableTileTexture = ContentManager.GetTexture("UI/unselectableTile"); switch (Level.LevelType) { case LevelType.Ice: tileTextureSpare = ContentManager.GetTileTextureByType(0); break; case LevelType.Sand: tileTextureSpare = ContentManager.GetSandTileTextureByType(0); break; } // draw spare tiles for (int y = 0; y < SpareTilesY; y++) { for (int x = 0; x < TileMap.GetLength(1); x++) { if (tileTextureSpare != null) ParentScene.Main.SpriteBatch.Draw(tileTextureSpare, destinationRectangle: new Rectangle(x * TileWidth, MapHeight + (y * TileHeight), TileWidth, TileHeight)); if (ParentScene.ObjectSeletor.State == Controls.State.Selected) ParentScene.Main.SpriteBatch.Draw(unplaceableTileTexture, destinationRectangle: new Rectangle(x * TileWidth, MapHeight + (y * TileHeight), TileWidth, TileHeight)); } } for (int i = 0; i < Objects.Count; i++) { if (ParentScene.ObjectSeletor.State != Controls.State.Selected) continue; for (int x = 0; x < Objects[i].TileSpanX; x++) { for (int y = 0; y < Objects[i].TileSpanY; y++) { ParentScene.Main.SpriteBatch.Draw(unplaceableTileTexture, destinationRectangle: new Rectangle((int)Objects[i].Position.X + (x * TileWidth), (int)Objects[i].Position.Y + (y * TileHeight), TileWidth, TileHeight)); } } } } public void Update(float deltaTime) { for (int i = 0; i < Enemies.Count; i++) { Enemies[i].Update(deltaTime); } for (int i = 0; i < Objects.Count; i++) { Objects[i].Update(deltaTime); } CheckIfRoundCompleted(); } private void CheckIfRoundCompleted() { if(Enemies.Count == 0 && WaveManager.DoneSpawning && WaveManager.RoundActive) { WaveManager.RoundActive = false; Level.Money += (WaveManager.CurrentWave * 30); } } public void SpawnEnemy(Type childObject, Vector2 position, Vector2 targetPosition, int index) { AddEnemyToList(position, targetPosition, childObject, 0); } public void SpawnEnemy(Type childObject) { AddEnemyToList(SpawnPoint, SpawnPointTargetPos, childObject, 0); } private void AddEnemyToList(Vector2 pos, Vector2 target, Type type, int index) { if (type == typeof(RedBalloon)) { var b = new RedBalloon(this); b.Position = pos; b.TargetPosition = target; Enemies.Insert(index, b); } if (type == typeof(BlueBalloon)) { var b = new BlueBalloon(this); b.Position = pos; b.TargetPosition = target; Enemies.Insert(index, b); } if (type == typeof(YellowBalloon)) { var b = new YellowBalloon(this); b.Position = pos; b.TargetPosition = target; Enemies.Insert(index, b); } if (type == typeof(GreenBalloon)) { var b = new GreenBalloon(this); b.Position = pos; b.TargetPosition = target; Enemies.Insert(index, b); } if (type == typeof(OrangeBalloon)) { var b = new OrangeBalloon(this); b.Position = pos; b.TargetPosition = target; Enemies.Insert(index, b); } if (type == typeof(PurpleBalloon)) { var b = new PurpleBalloon(this); b.Position = pos; b.TargetPosition = target; Enemies.Insert(index, b); } if (type == typeof(RainbowBalloon)) { var b = new RainbowBalloon(this); b.Position = pos; b.TargetPosition = target; Enemies.Insert(index, b); } if (type == typeof(DarkRainbowBalloon)) { var b = new DarkRainbowBalloon(this); b.Position = pos; b.TargetPosition = target; Enemies.Insert(index, b); } } public void SpawnObject(Type type, Vector2 pos) { if (type == typeof(PenguinObject)) { var b = new PenguinObject(pos, this); Objects.Add(b); } if (type == typeof(GoldPenguinObject)) { var b = new GoldPenguinObject(pos, this); Objects.Add(b); } if (type == typeof(CannonObject)) { var b = new CannonObject(pos, this); Objects.Add(b); } if (type == typeof(HealthGeneratorObject)) { var b = new HealthGeneratorObject(pos, this); Objects.Add(b); } if (type == typeof(MortarObject)) { var b = new MortarObject(pos, this); Objects.Add(b); } if (type == typeof(KingPenguinObject)) { var b = new KingPenguinObject(pos, this); Objects.Add(b); } } } public struct Tile { public int Type { get; set; } public Direction Direction { get; set; } public Tile(int type, Direction direction) { this.Type = type; this.Direction = direction; } } }