feat: implement GameState.snapshot and make entities cloneable

This commit is contained in:
2021-04-30 21:48:28 +02:00
parent 7f225b850c
commit eccac70656
8 changed files with 82 additions and 8 deletions

View File

@ -58,4 +58,16 @@ public class Character extends Entity {
public boolean isAlive() {
return hp.getValue() > 0;
}
@Override
public Character clone() {
Character clone = new Character(id, position, name, hp.max, mp.max, ap.max, attackRange, rangedDamage, meleeDamage);
for(StoneType stone: inventory) {
clone.inventory.addStone(stone);
}
clone.hp.setValue(hp.getValue());
clone.mp.setValue(mp.getValue());
clone.ap.setValue(ap.getValue());
return clone;
}
}

View File

@ -5,10 +5,10 @@ import uulm.teamname.marvelous.gamelibrary.IntVector2;
/** Represents an abstract entity. */
public abstract class Entity {
/** Whether or not the entity is currently active in the game */
private boolean active = true;
protected boolean active = true;
/** The position of the entity */
private IntVector2 position;
protected IntVector2 position;
/** The {@link EntityID} of the entity */
public final EntityID id;
@ -23,6 +23,12 @@ public abstract class Entity {
this.id = id;
}
/**
* Clones the {@link Entity}.
* @return A new entity cloned from this one
*/
public abstract Entity clone();
public boolean isActive() {
return active;
}

View File

@ -27,6 +27,14 @@ public class EntityID {
return type == other;
}
/**
* Clones this entity id.
* @return The cloned {@link EntityID}
*/
public EntityID clone() {
return new EntityID(this.id, this.type);
}
/**
* Serializes the id for debugging.
* @return A debug string containing all the necessary information about the id

View File

@ -17,4 +17,9 @@ public class InfinityStone extends Entity {
super(id, position);
this.type = type;
}
@Override
public InfinityStone clone() {
return new InfinityStone(id, position, type);
}
}

View File

@ -21,7 +21,7 @@ public class Inventory implements Iterable<StoneType> {
throw new IllegalArgumentException("Attempted to construct an inventory with more than "+size+" initial stones.");
}
for(StoneType stone : content) {
for(StoneType stone: content) {
if(content.contains(stone)) {
throw new IllegalArgumentException("Attempted to construct an inventory with duplicate entries.");
}

View File

@ -29,4 +29,13 @@ public class NPC extends Entity {
super(id, position);
this.inventory = new Inventory();
}
@Override
public NPC clone() {
NPC clone = new NPC(id, position);
for(StoneType stone: inventory) {
clone.inventory.addStone(stone);
}
return clone;
}
}

View File

@ -29,4 +29,11 @@ public class Rock extends Entity {
public void decreaseHp(int damage) {
this.hp -= damage;
}
@Override
public Rock clone() {
Rock clone = new Rock(id, position, maxHP);
clone.hp = hp;
return clone;
}
}