Moving attacks to Domain-Driven Design and Entity-Component architecture

Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
2026-03-14 00:22:47 +01:00
parent 0fe530e58e
commit 09c28028ad
4 changed files with 112 additions and 68 deletions

View File

@@ -14,18 +14,21 @@ abstract class Enemy extends Entity {
super.lastActionTime,
});
int health = 25; // Standard guard health
// Standard guard health
int health = 25;
bool isDying = false;
void takeDamage(int amount, int currentTime) {
if (state == EntityState.dead) return;
health -= amount;
lastActionTime = currentTime; // CRITICAL: Mark the start of the death/pain
// Mark the start of the death/pain
lastActionTime = currentTime;
if (health <= 0) {
state = EntityState.dead;
isDying = true; // This triggers the animation in BrownGuard
// This triggers the dying animation
isDying = true;
} else if (math.Random().nextDouble() < 0.5) {
state = EntityState.pain;
} else {
@@ -36,7 +39,8 @@ abstract class Enemy extends Entity {
// Decodes the Map ID to figure out which way the enemy is facing
static double getInitialAngle(int objId) {
int normalizedId = (objId - 108) % 36;
int direction = normalizedId % 4; // 0=East, 1=North, 2=West, 3=South
// 0=East, 1=North, 2=West, 3=South
int direction = normalizedId % 4;
switch (direction) {
case 0:
@@ -88,7 +92,24 @@ abstract class Enemy extends Entity {
return true;
}
// Update signature is cleaner now
// The weapon asks the enemy if it is unobstructed from the shooter
bool hasLineOfSightFrom(
LinearCoordinates source,
double sourceAngle,
double distance,
bool Function(int x, int y) isWalkable,
) {
double dirX = math.cos(sourceAngle);
double dirY = math.sin(sourceAngle);
for (double i = 0.5; i < distance; i += 0.2) {
int checkX = (source.x + dirX * i).toInt();
int checkY = (source.y + dirY * i).toInt();
if (!isWalkable(checkX, checkY)) return false;
}
return true;
}
void update({
required int elapsedMs,
required LinearCoordinates player,