65 lines
1.8 KiB
C#
65 lines
1.8 KiB
C#
using AlienAttack.MonoGame.Things.Bullets;
|
|
using AlienAttack.MonoGame.Things.Explosions;
|
|
using Microsoft.Xna.Framework;
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
namespace AlienAttack.MonoGame.Things.Asteroids;
|
|
|
|
public abstract class Asteroid : MoveableSprite
|
|
{
|
|
protected abstract int MaxHealth { get; }
|
|
protected abstract string TextureName { get; }
|
|
|
|
protected int Health { get; set; }
|
|
protected float RotationSpeed { get; set; } = 1f;
|
|
|
|
public Asteroid(int x, int y) : base(x, y)
|
|
{
|
|
Health = MaxHealth;
|
|
RotationSpeed = 1f;
|
|
}
|
|
|
|
public override void Draw(SpriteDrawArgs args)
|
|
{
|
|
Texture2D texture = args.Textures.Get(TextureName);
|
|
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, Origin, Scale, SpriteEffects.None, 1);
|
|
|
|
base.Draw(args);
|
|
}
|
|
|
|
public override sealed void Update(SpriteUpdateContext context)
|
|
{
|
|
base.Update(context);
|
|
|
|
CollisionBox = new Rectangle((int)XPosition + BoundBox.X - (int)Origin.X, (int)YPosition + BoundBox.Y - (int)Origin.Y, BoundBox.Width, BoundBox.Height);
|
|
|
|
if (Health <= 0)
|
|
{
|
|
IsDead = true;
|
|
context.SpawnSprite(new Explosion((int)XPosition, (int)YPosition, XVelocity, YVelocity));
|
|
context.AudioManager.PlayExplosion();
|
|
return;
|
|
}
|
|
|
|
if (YPosition - Origin.Y > context.ViewTransform.ScreenHeight)
|
|
{
|
|
IsDead = true;
|
|
return;
|
|
}
|
|
|
|
Rotation = MathHelper.WrapAngle(Rotation + RotationSpeed * context.DeltaTime);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |