using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Penguloon.Levels; namespace Penguloon.Enemies { public abstract class EnemyBase { public float Speed { get; set; } public int Health { get; set; } public Vector2 Position { get; set; } public Vector2 TargetPosition { get; set; } public Texture2D Texture { get; set; } public Map Map { get; set; } public bool MovingTowardsFinish { get; set; } = false; public Type ChildObject { get; set; } public Rectangle Box { get; set; } public int HealthToTake { get; set; } public bool Dead { get; set; } = false; public DateTime PopDate { get; set; } public EnemyBase(Map map) { this.Map = map; this.Position = Map.SpawnPoint; this.TargetPosition = Map.SpawnPointTargetPos; } public void Update(float deltaTime) { if (Dead) { if((DateTime.Now - PopDate).TotalMilliseconds > (100 / Map.Level.ParentScene.Speed)) Map.Enemies.Remove(this); return; } int margin = 5; Box = new Rectangle(new Point((int)Position.X + margin, (int)Position.Y + margin), new Point(Map.TileWidth - (margin * 2), Map.TileHeight - (margin * 2))); int tileX = (int)(TargetPosition.X + 2) / Map.TileWidth; int tileY = (int)(TargetPosition.Y + 2) / Map.TileHeight; if (Vector2.Distance(Position, TargetPosition) <= 3) { if(MovingTowardsFinish) { ReachEnd(); return; } Tile currentTile = Map.TileMap[tileY, tileX]; switch (currentTile.Direction) { case Direction.Up: TargetPosition = new Vector2(tileX * Map.TileWidth, (tileY - 1) * Map.TileHeight); break; case Direction.Down: TargetPosition = new Vector2(tileX * Map.TileWidth, (tileY + 1) * Map.TileHeight); break; case Direction.Left: TargetPosition = new Vector2((tileX - 1) * Map.TileWidth, tileY * Map.TileHeight); break; case Direction.Right: TargetPosition = new Vector2((tileX + 1) * Map.TileWidth, tileY * Map.TileHeight); break; case Direction.Finish: TargetPosition = Map.FinishPoint; MovingTowardsFinish = true; break; } } Vector2 movement = TargetPosition - Position; movement.Normalize(); this.Position += (movement * Speed) * deltaTime; } private void ReachEnd() { if (!Map.Level.Finished) { Map.Level.Health -= HealthToTake; if (Map.Level.Health < 0) Map.Level.Health = 0; } Map.Enemies.Remove(this); } public void GetHit() { Health--; Map.Level.Money++; Map.Level.Kills++; SpawnChild(); if (Health <= 0) { Dead = true; PopDate = DateTime.Now; Texture = ContentManager.GetTexture("Enemies/pop"); } SoundManager.PlayBalloonPopSound(); } private void SpawnChild() { if (this.ChildObject == null) return; Map.SpawnEnemy(ChildObject, Position, TargetPosition, Map.Enemies.IndexOf(this)); } } }