Create a Decorative object class
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -18,17 +18,33 @@ class BrownGuard extends Enemy {
|
||||
state: EntityState.idle,
|
||||
);
|
||||
|
||||
// Checks if a Map ID is a valid Brown Guard for the selected difficulty
|
||||
static bool isSpawnableForDifficulty(int objId, int difficultyLevel) {
|
||||
switch (difficultyLevel) {
|
||||
case 0:
|
||||
return objId >= 108 && objId <= 115;
|
||||
case 1:
|
||||
return objId >= 144 && objId <= 151;
|
||||
case 2:
|
||||
return objId >= 180 && objId <= 187;
|
||||
case 3:
|
||||
return objId >= 216 && objId <= 223;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void update({
|
||||
required int elapsedMs,
|
||||
required LinearCoordinates player,
|
||||
required bool Function(int x, int y) isWalkable,
|
||||
required bool Function(Entity entity) hasLineOfSight,
|
||||
required void Function(int damage) onDamagePlayer,
|
||||
}) {
|
||||
// 1. Wake up if the player is spotted!
|
||||
if (state == EntityState.idle) {
|
||||
if (hasLineOfSight(this)) {
|
||||
// Look how clean this is now:
|
||||
if (hasLineOfSight(player, isWalkable)) {
|
||||
state = EntityState.patrolling;
|
||||
lastActionTime = elapsedMs;
|
||||
}
|
||||
@@ -48,12 +64,8 @@ class BrownGuard extends Enemy {
|
||||
}
|
||||
|
||||
double diff = angle - angleToPlayer;
|
||||
while (diff <= -math.pi) {
|
||||
diff += 2 * math.pi;
|
||||
}
|
||||
while (diff > math.pi) {
|
||||
diff -= 2 * math.pi;
|
||||
}
|
||||
while (diff <= -math.pi) diff += 2 * math.pi;
|
||||
while (diff > math.pi) diff -= 2 * math.pi;
|
||||
|
||||
int octant = ((diff + (math.pi / 8)) / (math.pi / 4)).floor() % 8;
|
||||
if (octant < 0) octant += 8;
|
||||
@@ -61,7 +73,6 @@ class BrownGuard extends Enemy {
|
||||
if (state == EntityState.idle) {
|
||||
spriteIndex = 50 + octant;
|
||||
} else if (state == EntityState.patrolling) {
|
||||
// A. Move towards the player
|
||||
if (distance > 0.8) {
|
||||
double moveX = x + math.cos(angle) * speed;
|
||||
double moveY = y + math.sin(angle) * speed;
|
||||
@@ -70,33 +81,30 @@ class BrownGuard extends Enemy {
|
||||
if (isWalkable(x.toInt(), moveY.toInt())) y = moveY;
|
||||
}
|
||||
|
||||
// B. Animate the walk cycle
|
||||
int walkFrame = (elapsedMs ~/ 150) % 4;
|
||||
spriteIndex = 58 + (walkFrame * 8) + octant;
|
||||
|
||||
// C. Decide to shoot!
|
||||
if (distance < 5.0 && elapsedMs - lastActionTime > 2000) {
|
||||
if (hasLineOfSight(this)) {
|
||||
// Clean call here too:
|
||||
if (hasLineOfSight(player, isWalkable)) {
|
||||
state = EntityState.shooting;
|
||||
lastActionTime = elapsedMs;
|
||||
_hasFiredThisCycle = false; // Arm the weapon
|
||||
_hasFiredThisCycle = false;
|
||||
}
|
||||
}
|
||||
} else if (state == EntityState.shooting) {
|
||||
int timeShooting = elapsedMs - lastActionTime;
|
||||
|
||||
if (timeShooting < 150) {
|
||||
spriteIndex = 96; // Aiming
|
||||
spriteIndex = 96;
|
||||
} else if (timeShooting < 300) {
|
||||
spriteIndex = 97; // BANG!
|
||||
|
||||
// DEAL DAMAGE ONCE!
|
||||
spriteIndex = 97;
|
||||
if (!_hasFiredThisCycle) {
|
||||
onDamagePlayer(10); // 10 damage per shot
|
||||
onDamagePlayer(10);
|
||||
_hasFiredThisCycle = true;
|
||||
}
|
||||
} else if (timeShooting < 450) {
|
||||
spriteIndex = 98; // Recoil
|
||||
spriteIndex = 98;
|
||||
} else {
|
||||
state = EntityState.patrolling;
|
||||
lastActionTime = elapsedMs;
|
||||
|
||||
Reference in New Issue
Block a user