113 lines
3.1 KiB
C#
113 lines
3.1 KiB
C#
using AlienAttack.MonoGame.Things.Bullets;
|
|
using AlienAttack.MonoGame.Things.Explosions;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
using System;
|
|
|
|
namespace AlienAttack.MonoGame.Things.Enemies.Mines;
|
|
|
|
public abstract class Mine : MoveableSprite
|
|
{
|
|
public const int Width = 65;
|
|
public const int Height = 64;
|
|
|
|
protected abstract int MaxHealth { get; }
|
|
protected abstract string CoverColor { get; }
|
|
protected abstract float ExplodeRadius { get; }
|
|
|
|
protected int Health { get; set; }
|
|
protected float RotationSpeed { get; set; } = 1.5f;
|
|
protected double DistanceToPlayer { get; set; } = double.MaxValue;
|
|
|
|
public Mine(int x, int y) : base(x, y)
|
|
{
|
|
Origin = new(30, 33);
|
|
BoundBox = new(10, 14, 40, 38);
|
|
YVelocity = 1;
|
|
XVelocity = 0;
|
|
Health = MaxHealth;
|
|
}
|
|
|
|
public override void Draw(SpriteDrawArgs args)
|
|
{
|
|
DrawRotor(args);
|
|
DrawCover(args);
|
|
|
|
base.Draw(args);
|
|
}
|
|
|
|
private void DrawRotor(SpriteDrawArgs args)
|
|
{
|
|
Texture2D texture = args.Content.Load<Texture2D>("Sprites/Rotor");
|
|
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, Origin, Scale, SpriteEffects.None, 1);
|
|
}
|
|
|
|
private void DrawCover(SpriteDrawArgs args)
|
|
{
|
|
Texture2D texture = args.Content.Load<Texture2D>($"Sprites/Cover_{CoverColor}");
|
|
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, Origin, Scale, SpriteEffects.None, 1);
|
|
}
|
|
|
|
public override sealed void Update(SpriteUpdateContext context)
|
|
{
|
|
base.Update(context);
|
|
|
|
if (Health <= 0)
|
|
{
|
|
IsDead = true;
|
|
SpawnExplosion(context);
|
|
//OnKilled(context);
|
|
return;
|
|
}
|
|
|
|
UpdateDistanceToPlayer(context);
|
|
OnUpdate(context);
|
|
|
|
if (DistanceToPlayer <= ExplodeRadius)
|
|
{
|
|
IsDead = true;
|
|
SpawnExplosion(context);
|
|
return;
|
|
}
|
|
|
|
if (YPosition - Origin.Y > context.ViewTransform.ScreenHeight)
|
|
{
|
|
IsDead = true;
|
|
return;
|
|
}
|
|
|
|
Rotation = MathHelper.WrapAngle(Rotation + RotationSpeed * context.DeltaTime);
|
|
}
|
|
|
|
private void UpdateDistanceToPlayer(SpriteUpdateContext context)
|
|
{
|
|
float xDiff = (XPosition + Origin.X) - (context.Player.XPosition + context.Player.Origin.X);
|
|
float yDiff = (YPosition + Origin.Y) - (context.Player.YPosition + context.Player.Origin.Y);
|
|
|
|
DistanceToPlayer = Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
|
|
}
|
|
|
|
protected virtual void OnUpdate(SpriteUpdateContext context)
|
|
{
|
|
|
|
}
|
|
|
|
public override void OnCollision(SpriteCollisionContext context)
|
|
{
|
|
if (context.Sprite is Bullet bullet && bullet.Owner is Player)
|
|
{
|
|
Health -= bullet.Damage;
|
|
}
|
|
|
|
if (context.Sprite is Player)
|
|
{
|
|
Health = 0;
|
|
}
|
|
}
|
|
|
|
protected void SpawnExplosion(SpriteUpdateContext context)
|
|
{
|
|
context.SpawnSprite(new MineExplosion((int)XPosition, (int)YPosition, XVelocity, YVelocity));
|
|
context.AudioManager.PlayExplosion();
|
|
}
|
|
} |