1
This commit is contained in:
150
backend/src/main/java/com/zombie/game/model/Bullet.java
Normal file
150
backend/src/main/java/com/zombie/game/model/Bullet.java
Normal file
@@ -0,0 +1,150 @@
|
||||
package com.zombie.game.model;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class Bullet {
|
||||
private int id;
|
||||
private float x, y;
|
||||
private float z;
|
||||
private float vx, vy, vz;
|
||||
private float speed;
|
||||
private int damage;
|
||||
private String ownerId;
|
||||
private String weapon;
|
||||
private float range;
|
||||
private float distanceTraveled;
|
||||
private boolean explosive;
|
||||
private float explosionRadius;
|
||||
private float flightTime;
|
||||
private float maxFlightTime;
|
||||
private float targetX, targetY;
|
||||
private boolean isGrenade;
|
||||
|
||||
public Bullet(int id, float x, float y, float angle, float speed, int damage,
|
||||
String ownerId, String weapon, float range) {
|
||||
this.id = id;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = 0.5f;
|
||||
this.speed = speed;
|
||||
this.vx = (float) Math.sin(angle) * speed;
|
||||
this.vy = (float) Math.cos(angle) * speed;
|
||||
this.vz = 0;
|
||||
this.damage = damage;
|
||||
this.ownerId = ownerId;
|
||||
this.weapon = weapon;
|
||||
this.range = range;
|
||||
this.distanceTraveled = 0;
|
||||
this.explosive = weapon.equals(Constants.WEAPON_GRENADE);
|
||||
this.explosionRadius = explosive ? 3.0f : 0;
|
||||
this.flightTime = 0;
|
||||
this.maxFlightTime = 0;
|
||||
this.isGrenade = weapon.equals(Constants.WEAPON_GRENADE);
|
||||
this.targetX = x + (float) Math.sin(angle) * range;
|
||||
this.targetY = y + (float) Math.cos(angle) * range;
|
||||
}
|
||||
|
||||
public Bullet(int id, float startX, float startY, float targetX, float targetY,
|
||||
float flightDuration, int damage, String ownerId, float explosionRadius) {
|
||||
this.id = id;
|
||||
this.x = startX;
|
||||
this.y = startY;
|
||||
this.z = 0.5f;
|
||||
this.targetX = targetX;
|
||||
this.targetY = targetY;
|
||||
this.damage = damage;
|
||||
this.ownerId = ownerId;
|
||||
this.weapon = Constants.WEAPON_GRENADE;
|
||||
this.range = 0;
|
||||
this.distanceTraveled = 0;
|
||||
this.explosive = true;
|
||||
this.explosionRadius = explosionRadius;
|
||||
this.flightTime = 0;
|
||||
this.maxFlightTime = flightDuration;
|
||||
this.isGrenade = true;
|
||||
this.speed = 0;
|
||||
|
||||
float dx = targetX - startX;
|
||||
float dy = targetY - startY;
|
||||
this.vx = dx / flightDuration;
|
||||
this.vy = dy / flightDuration;
|
||||
this.vz = 3.0f;
|
||||
}
|
||||
|
||||
public int getId() { return id; }
|
||||
public float getX() { return x; }
|
||||
public float getY() { return y; }
|
||||
public float getZ() { return z; }
|
||||
public float getVx() { return vx; }
|
||||
public float getVy() { return vy; }
|
||||
public int getDamage() { return damage; }
|
||||
public String getOwnerId() { return ownerId; }
|
||||
public String getWeapon() { return weapon; }
|
||||
public boolean isExplosive() { return explosive; }
|
||||
public float getExplosionRadius() { return explosionRadius; }
|
||||
public float getTargetX() { return targetX; }
|
||||
public float getTargetY() { return targetY; }
|
||||
public boolean isGrenade() { return isGrenade; }
|
||||
|
||||
public boolean update(float dt, GameMap map) {
|
||||
if (isGrenade) {
|
||||
flightTime += dt;
|
||||
|
||||
x += vx * dt;
|
||||
y += vy * dt;
|
||||
|
||||
float progress = flightTime / maxFlightTime;
|
||||
z = 0.5f + 4.0f * (float) Math.sin(progress * Math.PI);
|
||||
|
||||
if (flightTime >= maxFlightTime || z <= 0.5f && progress > 0.5f) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (x < 0 || x >= Constants.GRID_SIZE || y < 0 || y >= Constants.GRID_SIZE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
float moveX = vx * dt;
|
||||
float moveY = vy * dt;
|
||||
x += moveX;
|
||||
y += moveY;
|
||||
distanceTraveled += (float) Math.sqrt(moveX * moveX + moveY * moveY);
|
||||
|
||||
if (distanceTraveled >= range) return false;
|
||||
if (x < 0 || x >= Constants.GRID_SIZE || y < 0 || y >= Constants.GRID_SIZE) return false;
|
||||
|
||||
int gx = (int) Math.floor(x);
|
||||
int gy = (int) Math.floor(y);
|
||||
if (map.isWall(gx, gy)) return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hitsEntity(float ex, float ey, float size) {
|
||||
float dx = x - ex;
|
||||
float dy = y - ey;
|
||||
float dist = (float) Math.sqrt(dx * dx + dy * dy);
|
||||
return dist < size / 2 + 0.1f;
|
||||
}
|
||||
|
||||
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("z", z);
|
||||
if (isGrenade) {
|
||||
map.put("angle", (float) Math.atan2(vx, vy));
|
||||
map.put("targetX", targetX);
|
||||
map.put("targetY", targetY);
|
||||
} else {
|
||||
map.put("angle", (float) Math.atan2(vx, vy));
|
||||
}
|
||||
map.put("weapon", weapon);
|
||||
map.put("ownerId", ownerId);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user