Browse Source

modified entity hierarchy and added custom size

master
Mathieu Sérandour 2 years ago
parent
commit
ca69ce7635
  1. 4
      entities/orc.json
  2. 1
      src/client/net/UDPClient.java
  3. 12
      src/game/NetProtocol.java
  4. 2
      src/game/entities/DrawableEntity.java
  5. 86
      src/game/entities/Entity.java
  6. 7
      src/game/entities/Projectile.java
  7. 54
      src/game/entities/characters/Enemy.java
  8. 2
      src/game/entities/characters/EnemyOrc.java
  9. 35
      src/game/entities/characters/EnemyShooter.java

4
entities/orc.json

@ -1,11 +1,13 @@
{
"hp": 15,
"damage": 5,
"sprite": 2,
"projectileSprite": 12,
"speed": 0.0035,
"target range": 10.0,
"minTargetDist": 5.0,
"maxTargetDist": 15.0,
"shootingRate": 500,
"reactionRate": 200
"reactionRate": 200,
"directionHold": 1000
}

1
src/client/net/UDPClient.java

@ -184,6 +184,7 @@ public class UDPClient extends Thread {
final Id entityId = new Id(wrapper);
EntityFactory.setDrawableFromBinaryAndUpdate(wrapper, entityId, time);
}
}

12
src/game/NetProtocol.java

@ -25,11 +25,7 @@ public class NetProtocol {
Properties properties = new Properties();
try(FileInputStream str = new FileInputStream(PROPERTIES_FILENAME)) {
properties.load(str);
/*timeout=10000
serverPort=62350
mapServerPort=80
serverIpAddress=127.0.0.1
*/
Integer i = getIntProperty(properties, "timeout");
if(i != null)
timeout = i;
@ -80,9 +76,9 @@ public class NetProtocol {
public static final int MAX_ENTITIES = 2000;
public static final int ENTITY_MAXSIZE = 30;
public static final int ENTITY_MAXSIZE = 37;
public static final int MAX_ENTITY_PER_REQUEST = 12;
public static final int PACKET_MAXSIZE = 430;
public static final int PACKET_MAXSIZE = 460;
public static final int UPDATE_ENTITY_REQUEST_BODY_SIZE = PACKET_MAXSIZE - 1;
@ -93,6 +89,8 @@ public class NetProtocol {
public static final int SEND_TICK = 1000 / 60;
public static final long MAP_CACHE_DURATION = 60_000;
private static int timeout = 10_000;
private static int serverPort = 62350;

2
src/game/entities/DrawableEntity.java

@ -62,7 +62,7 @@ public class DrawableEntity extends Entity implements Drawable {
*
* @param buffer
*/
public DrawableEntity(ByteBuffer buffer) {
public DrawableEntity(final ByteBuffer buffer) {
super(buffer);
spriteSet = SpriteSet.get(spriteId);

86
src/game/entities/Entity.java

@ -28,8 +28,15 @@ public class Entity implements Updatable, Serializable {
protected Vec2 oldPos = new Vec2();
protected Vec2 velocity;
protected Vec2 size;
protected float damage;
private Map map;
protected float life;
protected float lifeMax;
protected int spriteId;
private boolean exists = true;
private transient Map map;
public void setMap(Map map) {
this.map = map;
@ -43,12 +50,6 @@ public class Entity implements Updatable, Serializable {
this.velocity = velocity;
}
protected float life;
protected float lifeMax;
protected int spriteId;
private boolean exists = true;
/**
* Entity ctor sets to defaults all of the other attributes
*
@ -60,6 +61,19 @@ public class Entity implements Updatable, Serializable {
public Entity(Vec2 pos, int spriteId) {
this(pos, new Vec2(), spriteId);
}
/**
* basic Entity ctor:
* creates empty attributes
*
* the point of this ctor is to be able to
* create an entity by overriding it
*/
private Entity() {
pos = new Vec2();
velocity = new Vec2();
size = new Vec2();
}
/**
* Entity ctor sets to defaults all of the other attributes
@ -138,9 +152,9 @@ public class Entity implements Updatable, Serializable {
* @param buffer
* the buffer that contains all of the attributes for the entity
*/
public Entity(ByteBuffer buffer) {
this(new Vec2(buffer.getFloat(), buffer.getFloat()), new Vec2(buffer.getFloat(), buffer.getFloat()),
buffer.getFloat(), buffer.getFloat(), buffer.get());
public Entity(final ByteBuffer buffer) {
this(); // create empty attributes
override(buffer);
}
public Entity(ByteBuffer buffer, Map map) {
@ -218,31 +232,13 @@ public class Entity implements Updatable, Serializable {
public boolean exists() {
return exists;
}
/**
* writes the entity state on a buffer
*
* @param buffer
* the buffer to fill with the Entity state
*/
public void toBytes(ByteBuffer buffer) {
pos.write(buffer);
velocity.write(buffer);
buffer.putFloat(life);
buffer.putFloat(lifeMax);
buffer.put((byte) spriteId);
}
/**
*
* @return the size of the binary generated by toBytes
*/
public int getBinSize() {
return 8 + 8 + 4 + 4 + 1;
return 8*3 + 4*3 + 1;
}
@ -282,19 +278,43 @@ public class Entity implements Updatable, Serializable {
* @param buffer
* the buffer containing the attributes of the entity
*/
public void override(ByteBuffer buffer) {
public void override(final ByteBuffer buffer) {
pos.vals[0] = Functions.expMoy(pos.vals[0], buffer.getFloat(), SMOOTH_COEFF);
pos.vals[1] = Functions.expMoy(pos.vals[1], buffer.getFloat(), SMOOTH_COEFF);
velocity.override(buffer.getFloat(), buffer.getFloat());
size.override(buffer.getFloat(), buffer.getFloat());
life = buffer.getFloat();
lifeMax = buffer.getFloat();
damage = buffer.getFloat();
spriteId = buffer.get();
}
/**
* writes the entity state on a buffer
*
* @param buffer
* the buffer to fill with the Entity state
*/
public void toBytes(final ByteBuffer buffer) {
pos.write(buffer);
velocity.write(buffer);
size.write(buffer);
buffer.putFloat(life);
buffer.putFloat(lifeMax);
buffer.putFloat(damage);
buffer.put((byte) spriteId);
}
@Override
public String toString() {
return "p={" + pos + "} v={" + velocity + "} hp: " + life + "/" + lifeMax + " sprite" + spriteId;
return "p={" + pos + "} v={" + velocity + "} hp: " + life + "/" + lifeMax + " sprite" + spriteId
+ " size: {" + size + "} damage: " + damage;
}
/**
@ -366,4 +386,8 @@ public class Entity implements Updatable, Serializable {
velocity.vals[1] = signY * vel;
}
}
public float getDamage() {
return damage;
}
}

7
src/game/entities/Projectile.java

@ -12,10 +12,6 @@ import math.vec.Vec2;
* arrows, bullets, etc. over the Entity class
*/
public class Projectile extends Entity implements Updatable {
/**
*
*/
private static final long serialVersionUID = 6674160725628029626L;
public static final float SPEED = 0.01f;
@ -30,6 +26,7 @@ public class Projectile extends Entity implements Updatable {
deathTimer = new Timer(LIFETIME);
this.caster = caster;
this.damage = caster.getDamage();
}
@Override
@ -52,7 +49,7 @@ public class Projectile extends Entity implements Updatable {
@Override
public void interact(final Entity e) {
die();
e.die();
e.takeDamage(damage);
}

54
src/game/entities/characters/Enemy.java

@ -43,13 +43,14 @@ public abstract class Enemy extends Entity {
private final transient UDPServer server;
private final float minDistanceToTarget;
private final float maxDistanceToTarget;
private final float targetRange;
private final float speed;
private final int directionHold;
private final transient Timer shootingTimer;
private final transient Timer reactivityTimer;
private float minDistanceToTarget;
private float maxDistanceToTarget;
private float targetRange;
private float speed;
private int directionHold;
private transient Timer shootingTimer;
private transient Timer reactivityTimer;
protected Vec2 velDirection = new Vec2();
@ -62,24 +63,27 @@ public abstract class Enemy extends Entity {
* @throws ParseException
* @throws IOException
*/
public Enemy(final Vec2 pos, final String fileName, UDPServer server) throws IOException, ParseException {
super(pos, 0);
JSONObject jsonObject = (JSONObject) new JSONParser().parse(new FileReader(fileName));
this.server = server;
this.lifeMax =
this.life = (int) (long) jsonObject.get("hp");
this.spriteId = (int) (long) jsonObject.get("sprite");
this.speed = (float)(double) jsonObject.get("speed");
this.targetRange = (float)(double) jsonObject.get("target range");
this.directionHold = (int)(long) jsonObject.get("directionHold");
this.minDistanceToTarget = (float)(double) jsonObject.get("minTargetDist");
this.maxDistanceToTarget = (float)(double) jsonObject.get("maxTargetDist");
shootingTimer = new Timer((int) (long) jsonObject.get("shootingRate"));
reactivityTimer = new Timer((int) (long) jsonObject.get("reactionRate"));
public Enemy(final Vec2 pos, final String fileName, final UDPServer server) throws IOException, ParseException {
super(pos, 0);
this.server = server;
parseAttributes((JSONObject) new JSONParser().parse(new FileReader(fileName)));
}
protected void parseAttributes(JSONObject jsonObject) {
this.lifeMax =
this.life = ((Number)jsonObject.get("hp")).intValue();
this.spriteId = ((Number)jsonObject.get("sprite")).intValue();
this.speed = ((Number)jsonObject.get("speed")).floatValue();
this.targetRange = ((Number)jsonObject.get("target range")).floatValue();
this.minDistanceToTarget = ((Number)jsonObject.get("minTargetDist")).floatValue();
this.maxDistanceToTarget = ((Number)jsonObject.get("maxTargetDist")).floatValue();
this.directionHold = ((Number)jsonObject.get("directionHold")).intValue();
this.shootingTimer = new Timer(((Number)jsonObject.get("shootingRate")).intValue());
this.reactivityTimer = new Timer(((Number)jsonObject.get("reactionRate")).intValue());
}
@Override

2
src/game/entities/characters/EnemyOrc.java

@ -16,7 +16,7 @@ import server.UDPServer;
*
* this enemy is a very simple one
*/
public class EnemyOrc extends Enemy {
public class EnemyOrc extends EnemyShooter {
private static final long serialVersionUID = 5911823535604829003L;
private static final String DESCRIPTION_FILE = "entities/orc.json";

35
src/game/entities/characters/EnemyShooter.java

@ -0,0 +1,35 @@
package game.entities.characters;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
import game.entities.Entity;
import game.entities.Projectile;
import math.vec.Vec2;
import server.UDPServer;
public abstract class EnemyShooter extends Enemy {
protected int projectileSprite;
private static final long serialVersionUID = -1431367786700836265L;
public EnemyShooter(Vec2 pos, String fileName, UDPServer server) throws IOException, ParseException {
super(pos, fileName, server);
}
@Override
protected void parseAttributes(JSONObject jsonObject) {
super.parseAttributes(jsonObject);
this.projectileSprite = ((Number)jsonObject.get("projectileSprite")).intValue();
this.damage = ((Number)jsonObject.get("damage")).floatValue();
}
@Override
public Entity shoot(final Vec2 direction) {
return new Projectile(pos.clone(), direction, this, projectileSprite);
}
}
Loading…
Cancel
Save