@@ -47,79 +47,67 @@ class Dog extends Enemy {
|
||||
}
|
||||
|
||||
@override
|
||||
void update({
|
||||
({Coordinate2D movement, double newAngle}) update({
|
||||
required int elapsedMs,
|
||||
required Coordinate2D playerPosition,
|
||||
required bool Function(int x, int y) isWalkable,
|
||||
required void Function(int damage) onDamagePlayer,
|
||||
}) {
|
||||
Coordinate2D movement = const Coordinate2D(0, 0);
|
||||
double newAngle = angle;
|
||||
|
||||
if (state == EntityState.idle &&
|
||||
hasLineOfSight(playerPosition, isWalkable)) {
|
||||
state = EntityState.patrolling;
|
||||
lastActionTime = elapsedMs;
|
||||
}
|
||||
|
||||
double distance = position.distanceTo(playerPosition);
|
||||
double angleToPlayer = position.angleTo(playerPosition);
|
||||
|
||||
if (state == EntityState.patrolling || state == EntityState.shooting) {
|
||||
newAngle = angleToPlayer;
|
||||
}
|
||||
|
||||
double diff = newAngle - angleToPlayer;
|
||||
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;
|
||||
|
||||
if (state == EntityState.idle) {
|
||||
if (hasLineOfSight(playerPosition, isWalkable)) {
|
||||
spriteIndex = 99 + octant;
|
||||
} else if (state == EntityState.patrolling) {
|
||||
if (distance > 0.8) {
|
||||
movement = Coordinate2D(math.cos(newAngle), math.sin(newAngle)) * speed;
|
||||
}
|
||||
|
||||
int walkFrame = (elapsedMs ~/ 100) % 4;
|
||||
spriteIndex = 107 + (walkFrame * 8) + octant;
|
||||
|
||||
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;
|
||||
}
|
||||
} else {
|
||||
state = EntityState.patrolling;
|
||||
lastActionTime = elapsedMs;
|
||||
}
|
||||
}
|
||||
|
||||
if (state == EntityState.idle ||
|
||||
state == EntityState.patrolling ||
|
||||
state == EntityState.shooting) {
|
||||
// "Shooting" here means biting
|
||||
|
||||
double dx = playerPosition.x - x;
|
||||
double dy = playerPosition.y - y;
|
||||
double distance = math.sqrt(dx * dx + dy * dy);
|
||||
double angleToPlayer = math.atan2(dy, dx);
|
||||
|
||||
if (state == EntityState.patrolling || state == EntityState.shooting) {
|
||||
angle = angleToPlayer;
|
||||
}
|
||||
|
||||
double diff = angle - angleToPlayer;
|
||||
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;
|
||||
|
||||
if (state == EntityState.idle) {
|
||||
spriteIndex = 99 + octant; // Base dog standing sprite
|
||||
} else if (state == EntityState.patrolling) {
|
||||
if (distance > 0.8) {
|
||||
double moveX = x + math.cos(angle) * speed;
|
||||
double moveY = y + math.sin(angle) * speed;
|
||||
|
||||
if (isWalkable(moveX.toInt(), y.toInt())) x = moveX;
|
||||
if (isWalkable(x.toInt(), moveY.toInt())) y = moveY;
|
||||
}
|
||||
|
||||
// Dogs only have 4 walking angles, alternating legs
|
||||
int walkFrame = (elapsedMs ~/ 100) % 4;
|
||||
spriteIndex = 107 + (walkFrame * 8) + octant;
|
||||
|
||||
// Dog Bite Attack (Must be practically touching the player)
|
||||
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; // Jumping/Biting frame
|
||||
if (!_hasBittenThisCycle) {
|
||||
onDamagePlayer(5); // Dogs do less damage than guards
|
||||
_hasBittenThisCycle = true;
|
||||
}
|
||||
} else {
|
||||
state = EntityState.patrolling;
|
||||
lastActionTime = elapsedMs;
|
||||
}
|
||||
}
|
||||
}
|
||||
return (movement: movement, newAngle: newAngle);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user