Can now open secret walls and pick up machine gun
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -51,21 +51,28 @@ class Dog extends Enemy {
|
||||
required int elapsedMs,
|
||||
required Coordinate2D playerPosition,
|
||||
required bool Function(int x, int y) isWalkable,
|
||||
required void Function(int x, int y) tryOpenDoor, // NEW
|
||||
required void Function(int damage) onDamagePlayer,
|
||||
}) {
|
||||
Coordinate2D movement = const Coordinate2D(0, 0);
|
||||
double newAngle = angle;
|
||||
|
||||
// 1. Wake up logic
|
||||
if (state == EntityState.idle &&
|
||||
hasLineOfSight(playerPosition, isWalkable)) {
|
||||
state = EntityState.patrolling;
|
||||
lastActionTime = elapsedMs;
|
||||
if (reactionTimeMs == 0) {
|
||||
reactionTimeMs = elapsedMs + 100 + math.Random().nextInt(200);
|
||||
} else if (elapsedMs >= reactionTimeMs) {
|
||||
state = EntityState.patrolling;
|
||||
lastActionTime = elapsedMs;
|
||||
reactionTimeMs = 0;
|
||||
}
|
||||
}
|
||||
|
||||
double distance = position.distanceTo(playerPosition);
|
||||
double angleToPlayer = position.angleTo(playerPosition);
|
||||
|
||||
if (state == EntityState.patrolling || state == EntityState.shooting) {
|
||||
if (state != EntityState.idle && state != EntityState.dead) {
|
||||
newAngle = angleToPlayer;
|
||||
}
|
||||
|
||||
@@ -76,36 +83,60 @@ class Dog extends Enemy {
|
||||
while (diff > math.pi) {
|
||||
diff -= 2 * math.pi;
|
||||
}
|
||||
|
||||
int octant = ((diff + (math.pi / 8)) / (math.pi / 4)).floor() % 8;
|
||||
if (octant < 0) octant += 8;
|
||||
|
||||
if (state == EntityState.idle) {
|
||||
spriteIndex = 99 + octant;
|
||||
} else if (state == EntityState.patrolling) {
|
||||
if (distance > 0.8) {
|
||||
movement = Coordinate2D(math.cos(newAngle), math.sin(newAngle)) * speed;
|
||||
}
|
||||
// 3. Clean State Machine
|
||||
switch (state) {
|
||||
case EntityState.idle:
|
||||
spriteIndex = 99 + octant;
|
||||
break;
|
||||
|
||||
int walkFrame = (elapsedMs ~/ 100) % 4;
|
||||
spriteIndex = 107 + (walkFrame * 8) + octant;
|
||||
case EntityState.patrolling:
|
||||
if (distance > 0.8) {
|
||||
double deltaX = playerPosition.x - position.x;
|
||||
double deltaY = playerPosition.y - position.y;
|
||||
|
||||
if (distance < 1.0 && elapsedMs - lastActionTime > 1000) {
|
||||
state = EntityState.shooting;
|
||||
lastActionTime = elapsedMs;
|
||||
_hasBittenThisCycle = false;
|
||||
}
|
||||
} else if (state == EntityState.shooting) {
|
||||
int timeAttacking = elapsedMs - lastActionTime;
|
||||
if (timeAttacking < 200) {
|
||||
spriteIndex = 139;
|
||||
if (!_hasBittenThisCycle) {
|
||||
onDamagePlayer(5); // DOG BITE
|
||||
_hasBittenThisCycle = true;
|
||||
double moveX = deltaX > 0 ? speed : (deltaX < 0 ? -speed : 0);
|
||||
double moveY = deltaY > 0 ? speed : (deltaY < 0 ? -speed : 0);
|
||||
|
||||
Coordinate2D intendedMovement = Coordinate2D(moveX, moveY);
|
||||
|
||||
// Pass tryOpenDoor down!
|
||||
movement = getValidMovement(
|
||||
intendedMovement,
|
||||
isWalkable,
|
||||
tryOpenDoor,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
state = EntityState.patrolling;
|
||||
lastActionTime = elapsedMs;
|
||||
}
|
||||
|
||||
int walkFrame = (elapsedMs ~/ 100) % 4;
|
||||
spriteIndex = 107 + (walkFrame * 8) + octant;
|
||||
|
||||
if (distance < 1.0 && elapsedMs - lastActionTime > 1000) {
|
||||
state = EntityState.shooting;
|
||||
lastActionTime = elapsedMs;
|
||||
_hasBittenThisCycle = false;
|
||||
}
|
||||
break;
|
||||
|
||||
case EntityState.shooting:
|
||||
int timeAttacking = elapsedMs - lastActionTime;
|
||||
if (timeAttacking < 200) {
|
||||
spriteIndex = 139;
|
||||
if (!_hasBittenThisCycle) {
|
||||
onDamagePlayer(5);
|
||||
_hasBittenThisCycle = true;
|
||||
}
|
||||
} else {
|
||||
state = EntityState.patrolling;
|
||||
lastActionTime = elapsedMs;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return (movement: movement, newAngle: newAngle);
|
||||
|
||||
Reference in New Issue
Block a user