1
This commit is contained in:
129
backend/src/main/java/com/zombie/game/model/Zombie.java
Normal file
129
backend/src/main/java/com/zombie/game/model/Zombie.java
Normal file
@@ -0,0 +1,129 @@
|
||||
package com.zombie.game.model;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Zombie {
|
||||
private int id;
|
||||
private float x, y;
|
||||
private float angle;
|
||||
private float health;
|
||||
private float maxHealth;
|
||||
private float speed;
|
||||
private long lastAttackTime;
|
||||
private List<float[]> path;
|
||||
private int pathIndex;
|
||||
private float targetX, targetY;
|
||||
private boolean isElite;
|
||||
private long lastRangedAttackTime;
|
||||
|
||||
public Zombie(int id, float x, float y, float health, float speed) {
|
||||
this(id, x, y, health, speed, false);
|
||||
}
|
||||
|
||||
public Zombie(int id, float x, float y, float health, float speed, boolean isElite) {
|
||||
this.id = id;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.angle = 0;
|
||||
this.health = health;
|
||||
this.maxHealth = health;
|
||||
this.speed = speed;
|
||||
this.lastAttackTime = 0;
|
||||
this.path = null;
|
||||
this.pathIndex = 0;
|
||||
this.isElite = isElite;
|
||||
this.lastRangedAttackTime = 0;
|
||||
}
|
||||
|
||||
public int getId() { return id; }
|
||||
public float getX() { return x; }
|
||||
public float getY() { return y; }
|
||||
public float getAngle() { return angle; }
|
||||
public float getHealth() { return health; }
|
||||
public float getMaxHealth() { return maxHealth; }
|
||||
public List<float[]> getPath() { return path; }
|
||||
public int getPathIndex() { return pathIndex; }
|
||||
public boolean isElite() { return isElite; }
|
||||
|
||||
public void takeDamage(float damage) {
|
||||
this.health -= damage;
|
||||
if (this.health < 0) this.health = 0;
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
return health > 0;
|
||||
}
|
||||
|
||||
public void updatePath(GameMap map, float targetX, float targetY) {
|
||||
this.targetX = targetX;
|
||||
this.targetY = targetY;
|
||||
this.path = map.findPath(x, y, targetX, targetY);
|
||||
this.pathIndex = 0;
|
||||
}
|
||||
|
||||
public void move(GameMap map, float dt) {
|
||||
if (path != null && pathIndex < path.size()) {
|
||||
float[] target = path.get(pathIndex);
|
||||
float dx = target[0] - x;
|
||||
float dy = target[1] - y;
|
||||
float dist = (float) Math.sqrt(dx * dx + dy * dy);
|
||||
|
||||
if (dist < 0.2f) {
|
||||
pathIndex++;
|
||||
if (pathIndex >= path.size()) {
|
||||
path = null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
float moveX = (dx / dist) * speed * dt;
|
||||
float moveY = (dy / dist) * speed * dt;
|
||||
|
||||
float newX = x + moveX;
|
||||
float newY = y + moveY;
|
||||
|
||||
if (map.isWalkable(newX, y, Constants.ZOMBIE_SIZE)) {
|
||||
x = newX;
|
||||
}
|
||||
if (map.isWalkable(x, newY, Constants.ZOMBIE_SIZE)) {
|
||||
y = newY;
|
||||
}
|
||||
|
||||
angle = (float) Math.atan2(dx, dy);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canAttack(long now) {
|
||||
return now - lastAttackTime >= Constants.ZOMBIE_ATTACK_RATE * 1000;
|
||||
}
|
||||
|
||||
public void attack(long now) {
|
||||
lastAttackTime = now;
|
||||
}
|
||||
|
||||
public boolean canRangedAttack(long now) {
|
||||
if (!isElite) return false;
|
||||
return now - lastRangedAttackTime >= Constants.ELITE_ZOMBIE_ATTACK_RATE * 1000;
|
||||
}
|
||||
|
||||
public void rangedAttack(long now) {
|
||||
lastRangedAttackTime = now;
|
||||
}
|
||||
|
||||
public float distanceTo(float px, float py) {
|
||||
float dx = px - x;
|
||||
float dy = py - y;
|
||||
return (float) Math.sqrt(dx * dx + dy * dy);
|
||||
}
|
||||
|
||||
public Map<String, Object> toStateMap() {
|
||||
Map<String, Object> map = new LinkedHashMap<>();
|
||||
map.put("id", id);
|
||||
map.put("x", x);
|
||||
map.put("y", y);
|
||||
map.put("angle", angle);
|
||||
map.put("health", health);
|
||||
map.put("isElite", isElite);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user