diff options
Diffstat (limited to 'Penguloon/Enemies')
| -rw-r--r-- | Penguloon/Enemies/EnemyBase.cs | 88 | ||||
| -rw-r--r-- | Penguloon/Enemies/RedBalloon.cs | 15 |
2 files changed, 103 insertions, 0 deletions
diff --git a/Penguloon/Enemies/EnemyBase.cs b/Penguloon/Enemies/EnemyBase.cs new file mode 100644 index 0000000..2179e58 --- /dev/null +++ b/Penguloon/Enemies/EnemyBase.cs @@ -0,0 +1,88 @@ +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 EnemyBase(Map map) + { + this.Map = map; + + this.Position = Map.SpawnPoint; + this.TargetPosition = Map.SpawnPointTargetPos; + } + + public void Update(float deltaTime) + { + 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() + { + Map.Enemies.Remove(this); + } + + public void GetHit() + { + Health--; + + if (Health <= 0) + Map.Enemies.Remove(this); + + SpawnChild(); + + // play pop sound + } + + private void SpawnChild() + { + if (this.ChildObject == null) return; + + Map.SpawnEnemy(ChildObject, Position, TargetPosition); + } + } +}
\ No newline at end of file diff --git a/Penguloon/Enemies/RedBalloon.cs b/Penguloon/Enemies/RedBalloon.cs new file mode 100644 index 0000000..cfe9caf --- /dev/null +++ b/Penguloon/Enemies/RedBalloon.cs @@ -0,0 +1,15 @@ +using Penguloon.Levels; + +namespace Penguloon.Enemies +{ + class RedBalloon : EnemyBase + { + public RedBalloon(Map map) : base(map) + { + this.Texture = ContentManager.GetTexture("Enemies/red"); + this.Speed = 135f; + this.Health = 1; + this.ChildObject = null; + } + } +}
\ No newline at end of file |
