Enemies¶
Doelstelling¶
Ik moest ervoor zorgen dat er vijanden zijn die de player volgen.
Toegepaste oplossing¶
Mijn oplossing was om eerst op basis van Entity een Enemy aan te maken.
namespace Client.GameObjects
{
public class Enemy : Entity
{
private GameState gameState { get; set; }
public int Speed { get; set; }
public Enemy(GameState state, int health, Vector2 position, Vector2 velocity, Texture2D texture, int speed)
: base(health, position, velocity, texture, size: new Vector2(50f, 50f))
{
gameState = state;
Speed = speed;
}
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.BetterDraw(Texture, Position, Size);
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
Hierna zorgen we ervoor dat hij de player volgt met behulp van Pythagoras.
var player = gameState.Player;
if (player == null)
{
Velocity = Vector2.Zero;
return; // Early return if there is no player
}
Vector2 direction = Vector2.Normalize(player.Position - Position);
var distance = Vector2.Distance(Position, player.Position);
if (MathF.Abs(distance) >= 35)
{
Velocity = direction * Speed;
}
else
{
Velocity = Vector2.Zero;
}
}
}
Later worden ze gespawnd in Waves.
Klassendiagram¶
classDiagram
class GameObject {
+Vector2 Position
+void Update(GameTime gameTime)
}
class Entity {
-int Health
-Vector2 Velocity
-Texture2D Texture
-Vector2 Size
+Rectangle Bounds
+Entity(int health, Vector2 position, Vector2 velocity, Texture2D texture, Vector2 size)
+void Update(GameTime gameTime)
}
class Enemy {
-GameState gameState
-int Speed
+Enemy(GameState state, int health, Vector2 position, Vector2 velocity, Texture2D texture, int speed)
+void Draw(GameTime gameTime, SpriteBatch spriteBatch)
+void Update(GameTime gameTime)
}
GameObject <|-- Entity
Entity <|-- Enemy
Bronnen¶
- C# documentation. (z.d.) learn.microsoft.com.
Laatst geraadpleegd op 16 april 2024, van https://learn.microsoft.com/en-uk/dotnet/csharp/ - Monogame documentation. (z.d.) monogame.net.
Laatst geraadpleegd op 16 april 2024, van https://monogame.net/articles
Laatst geüpdatet:
May 21, 2024
Gecreëerd: April 23, 2024
Gecreëerd: April 23, 2024