Updated asteriods, mines, and explosions.
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using AlienAttack.MonoGame.Things.Bullets;
|
||||
using AlienAttack.MonoGame.Things.Explosions;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System;
|
||||
|
||||
@@ -9,7 +11,13 @@ 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)
|
||||
{
|
||||
@@ -17,38 +25,45 @@ public abstract class Mine : MoveableSprite
|
||||
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, 1f, SpriteEffects.None, 1);
|
||||
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, 1f, SpriteEffects.None, 1);
|
||||
args.SpriteBatch.Draw(texture, Position, null, DrawColor, Rotation, Origin, Scale, SpriteEffects.None, 1);
|
||||
}
|
||||
|
||||
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;
|
||||
SpawnExplosion(context);
|
||||
//OnKilled(context);
|
||||
return;
|
||||
}
|
||||
|
||||
float xDiff = (XPosition + Origin.X) - (context.Player.XPosition + context.Player.Origin.X);
|
||||
float yDiff = (YPosition + Origin.Y) - (context.Player.YPosition + context.Player.Origin.Y);
|
||||
UpdateDistanceToPlayer(context);
|
||||
OnUpdate(context);
|
||||
|
||||
double distance = Math.Sqrt(xDiff * xDiff + yDiff * yDiff);
|
||||
|
||||
if (distance < 100)
|
||||
if (DistanceToPlayer <= ExplodeRadius)
|
||||
{
|
||||
IsDead = true;
|
||||
SpawnExplosion(context);
|
||||
@@ -61,17 +76,38 @@ public abstract class Mine : MoveableSprite
|
||||
return;
|
||||
}
|
||||
|
||||
Rotation += 0.01f;
|
||||
Rotation = MathHelper.WrapAngle(Rotation + RotationSpeed * context.DeltaTime);
|
||||
}
|
||||
|
||||
if (Rotation > 360f)
|
||||
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)
|
||||
{
|
||||
Rotation = 0;
|
||||
Health -= bullet.Damage;
|
||||
}
|
||||
|
||||
if (context.Sprite is Player)
|
||||
{
|
||||
Health = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private void SpawnExplosion(SpriteUpdateContext context)
|
||||
protected void SpawnExplosion(SpriteUpdateContext context)
|
||||
{
|
||||
context.SpawnSprite(new Explosion((int)XPosition, (int)YPosition, XVelocity, YVelocity));
|
||||
context.SpawnSprite(new MineExplosion((int)XPosition, (int)YPosition, XVelocity, YVelocity));
|
||||
context.AudioManager.PlayExplosion();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user