Skip to content

Commit

Permalink
implement proper serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
adamtomi committed Nov 25, 2022
1 parent f003cd0 commit a43e080
Show file tree
Hide file tree
Showing 19 changed files with 152 additions and 93 deletions.
15 changes: 14 additions & 1 deletion src/main/java/com/vamk/tbg/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.io.IOException;
import java.util.logging.Logger;

import static com.vamk.tbg.game.GameState.FILEPATH;

public class Main {

public static void main(String[] args) {
Expand All @@ -28,6 +30,17 @@ private Bootstrap() {
}

private void launch() {
if (IOUtil.fileExists(FILEPATH)) {
try {
GameState state = IOUtil.readObject(GameState.class, FILEPATH);
this.game.importState(state);
IOUtil.remove(FILEPATH);
} catch (ClassNotFoundException | IOException ex) {
LOGGER.severe("Failed to read game state from file");
ex.printStackTrace();
}
}

this.game.launch();
this.window.dispose();
}
Expand All @@ -36,7 +49,7 @@ private void handleForceShutdown() {
LOGGER.info("Force shutdown initiated, writing game state to file");
GameState state = this.game.exportState();
try {
IOUtil.writeObject(state, GameState.FILEPATH);
IOUtil.writeObject(state, FILEPATH);
} catch (IOException ex) {
LOGGER.severe("Failed to write game state to file");
ex.printStackTrace();
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/com/vamk/tbg/combat/AbstractMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import com.vamk.tbg.game.Entity;

import java.io.Serial;

public abstract class AbstractMove implements Move {
@Serial
private static final long serialVersionUID = 1008816914619703220L;
private final String id;
private final boolean attack;

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/vamk/tbg/combat/BuffMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.vamk.tbg.game.MoveContext;
import com.vamk.tbg.util.LogUtil;

import java.io.Serial;
import java.util.logging.Logger;

import static com.vamk.tbg.util.RandomUtil.chance;
Expand All @@ -15,8 +14,6 @@ public class BuffMove extends AbstractMove {
private static final int REGEN_CHANCE = 40;
private static final int LIFESTEAL_CHANCE = 10;
private static final int CAFFEINATED_CHANCE = 5;
@Serial
private static final long serialVersionUID = 9096256805043526477L;

public BuffMove() {
super("BUFF", false);
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/vamk/tbg/combat/CombatRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.vamk.tbg.combat;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class CombatRegistry {
private final Map<String, Move> moves;

public CombatRegistry() {
this.moves = Stream.of(new BuffMove(), new CureMove(), new DebuffMove(), new GenericAttackMove(), new HealAllMove(), new HealMove(), new SplashDamageMove())
.collect(Collectors.toMap(Move::getId, Function.identity()));
}

public Move findMove(String id) {
return this.moves.get(id);
}
}
3 changes: 0 additions & 3 deletions src/main/java/com/vamk/tbg/combat/CureMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
import com.vamk.tbg.game.MoveContext;
import com.vamk.tbg.util.LogUtil;

import java.io.Serial;
import java.util.logging.Logger;

public class CureMove extends AbstractMove {
private static final Logger LOGGER = LogUtil.getLogger(CureMove.class);
@Serial
private static final long serialVersionUID = 4985779855365158332L;

public CureMove() {
super("MOVE", false);
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/vamk/tbg/combat/DebuffMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.vamk.tbg.game.MoveContext;
import com.vamk.tbg.util.LogUtil;

import java.io.Serial;
import java.util.logging.Logger;

import static com.vamk.tbg.util.RandomUtil.chance;
Expand All @@ -15,8 +14,6 @@ public class DebuffMove extends AbstractMove {
private static final int BLEEDING_CHANCE = 50;
private static final int FROZEN_CHANCE = 10;
private static final int CONFUSED_CHANCE = 5;
@Serial
private static final long serialVersionUID = 3531606415607174312L;

public DebuffMove() {
super("DEBUFF", true);
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/vamk/tbg/combat/GenericAttackMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@
import com.vamk.tbg.game.MoveContext;
import com.vamk.tbg.util.LogUtil;

import java.io.Serial;
import java.util.Random;
import java.util.logging.Logger;

public class GenericAttackMove extends AbstractMove {
private static final Logger LOGGER = LogUtil.getLogger(GenericAttackMove.class);
@Serial
private static final long serialVersionUID = 7741676913360830574L;
private final transient Random random;

public GenericAttackMove() {
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/vamk/tbg/combat/HealAllMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
import com.vamk.tbg.game.MoveContext;
import com.vamk.tbg.util.LogUtil;

import java.io.Serial;
import java.util.logging.Logger;

public class HealAllMove extends AbstractMove {
private static final Logger LOGGER = LogUtil.getLogger(HealAllMove.class);
@Serial
private static final long serialVersionUID = -1552305301295494669L;

public HealAllMove() {
super("HEAL_ALL", false);
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/vamk/tbg/combat/HealMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.vamk.tbg.game.MoveContext;
import com.vamk.tbg.util.LogUtil;

import java.io.Serial;
import java.util.logging.Logger;

import static com.vamk.tbg.util.RandomUtil.chance;
Expand All @@ -14,8 +13,6 @@ public class HealMove extends AbstractMove {
private static final Logger LOGGER = LogUtil.getLogger(HealMove.class);
private static final int REGENERATION_CHANCE = 15;
private static final int LIFESTEAL_CHANCE = 10;
@Serial
private static final long serialVersionUID = -8821620767840800124L;

public HealMove() {
super("HEAL", false);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/vamk/tbg/combat/Move.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.vamk.tbg.game.Entity;
import com.vamk.tbg.game.MoveContext;

import java.io.Serializable;

public interface Move extends Serializable {
public interface Move {

String getId();

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/vamk/tbg/combat/SplashDamageMove.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@
import com.vamk.tbg.game.MoveContext;
import com.vamk.tbg.util.LogUtil;

import java.io.Serial;
import java.util.logging.Logger;

import static com.vamk.tbg.util.RandomUtil.chance;

public class SplashDamageMove extends AbstractMove {
private static final Logger LOGGER = LogUtil.getLogger(SplashDamageMove.class);
private static final int BLEEDING_CHANCE = 50;
@Serial
private static final long serialVersionUID = 1791438698674539499L;

public SplashDamageMove() {
super("SPLASH_DAMAGE", true);
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/com/vamk/tbg/effect/BleedingEffectHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@
public class BleedingEffectHandler implements StatusEffectHandler {
private static final Logger LOGGER = LogUtil.getLogger(BleedingEffectHandler.class);
private final Random random;
private final Entity entity;

public BleedingEffectHandler(Entity entity) {
public BleedingEffectHandler() {
this.random = new Random();
this.entity = entity;
}

@Override
public void tick() {
if (!this.entity.hasEffect(StatusEffect.BLEEDING)) return;
public void applyTo(Entity entity) {
if (!entity.hasEffect(StatusEffect.BLEEDING)) return;

// TODO make this configurable
int damage = this.random.nextInt(Math.max((int) (this.entity.getMaxHealth() * 0.05), 1));
this.entity.damage(damage);
LOGGER.info("Entity %d loses %d health to bleeding".formatted(this.entity.getId(), damage));
int damage = this.random.nextInt(Math.max((int) (entity.getMaxHealth() * 0.05), 1));
entity.damage(damage);
LOGGER.info("Entity %d loses %d health to bleeding".formatted(entity.getId(), damage));
}
}
14 changes: 6 additions & 8 deletions src/main/java/com/vamk/tbg/effect/RegenEffectHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@
public class RegenEffectHandler implements StatusEffectHandler {
private static final Logger LOGGER = LogUtil.getLogger(RegenEffectHandler.class);
private final Random random;
private final Entity entity;

public RegenEffectHandler(Entity entity) {
public RegenEffectHandler() {
this.random = new Random();
this.entity = entity;
}

@Override
public void tick() {
if (!this.entity.hasEffect(StatusEffect.REGENERATION)) return;
public void applyTo(Entity entity) {
if (!entity.hasEffect(StatusEffect.REGENERATION)) return;

// TODO make this configurable
int hp = this.random.nextInt(Math.max((int) (this.entity.getMaxHealth() * 0.01), 1));
this.entity.heal(hp);
LOGGER.info("Entity %d gains %d health thanks to regeneration".formatted(this.entity.getId(), hp));
int hp = this.random.nextInt(Math.max((int) (entity.getMaxHealth() * 0.01), 1));
entity.heal(hp);
LOGGER.info("Entity %d gains %d health thanks to regeneration".formatted(entity.getId(), hp));
}
}
7 changes: 5 additions & 2 deletions src/main/java/com/vamk/tbg/effect/StatusEffectHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.vamk.tbg.effect;

import com.vamk.tbg.util.Tickable;
import com.vamk.tbg.game.Entity;

public interface StatusEffectHandler extends Tickable {}
public interface StatusEffectHandler {

void applyTo(Entity entity);
}
43 changes: 25 additions & 18 deletions src/main/java/com/vamk/tbg/game/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

import com.vamk.tbg.Constants;
import com.vamk.tbg.combat.Move;
import com.vamk.tbg.effect.BleedingEffectHandler;
import com.vamk.tbg.effect.RegenEffectHandler;
import com.vamk.tbg.combat.CombatRegistry;
import com.vamk.tbg.effect.StatusEffect;
import com.vamk.tbg.effect.StatusEffectHandler;
import com.vamk.tbg.signal.SignalDispatcher;
import com.vamk.tbg.signal.impl.EffectsUpdatedSignal;
import com.vamk.tbg.signal.impl.EntityDeathSignal;
import com.vamk.tbg.signal.impl.EntityHealthChangedSignal;
import com.vamk.tbg.util.LogUtil;
import com.vamk.tbg.util.Tickable;

import java.io.Serial;
import java.io.Serializable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -24,31 +20,27 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class Entity implements Serializable, Tickable {
@Serial
private static final long serialVersionUID = 6101117327941388691L;
public class Entity implements Tickable {
private final Map<StatusEffect, Integer> activeEffects;
private final transient Set<StatusEffectHandler> effectHandlers;
private final int id;
private final boolean hostile;
private final int maxHealth;
private int health;
private final List<Move> moves;
private final transient SignalDispatcher dispatcher;
private final SignalDispatcher dispatcher;

public Entity(int id, boolean hostile, int maxHealth, List<Move> moves, SignalDispatcher dispatcher) {
public Entity(int id, boolean hostile, int health, int maxHealth, List<Move> moves, SignalDispatcher dispatcher) {
this.id = id;
this.hostile = hostile;
this.health = health;
this.maxHealth = maxHealth;
this.health = maxHealth;
this.moves = moves;
this.dispatcher = dispatcher;

this.activeEffects = new HashMap<>();
this.effectHandlers = Set.of(
new BleedingEffectHandler(this),
new RegenEffectHandler(this)
);
}

public Entity(int id, boolean hostile, int maxHealth, List<Move> moves, SignalDispatcher dispatcher) {
this(id, hostile, maxHealth, maxHealth, moves, dispatcher);
}

public int getId() {
Expand Down Expand Up @@ -143,9 +135,13 @@ public void cure() {
this.dispatcher.dispatch(new EffectsUpdatedSignal(this));
}

public EntitySnapshot createSnapshot() {
List<String> moves = this.moves.stream().map(Move::getId).toList();
return new EntitySnapshot(this.id, this.hostile, this.health, this.maxHealth, moves);
}

@Override
public void tick() {
this.effectHandlers.forEach(Tickable::tick);
Set<StatusEffect> expired = new HashSet<>();
for (Map.Entry<StatusEffect, Integer> entry : this.activeEffects.entrySet()) {
int rounds = entry.getValue() - 1;
Expand Down Expand Up @@ -183,10 +179,12 @@ public int hashCode() {
static final class Factory {
private static final Logger LOGGER = LogUtil.getLogger(Factory.class);
private final SignalDispatcher dispatcher;
private final CombatRegistry combatRegistry;
private final AtomicInteger nextId;

Factory(SignalDispatcher dispatcher) {
this.dispatcher = dispatcher;
this.combatRegistry = new CombatRegistry();
this.nextId = new AtomicInteger(0);
}

Expand All @@ -195,5 +193,14 @@ Entity create(boolean hostile, int maxHeath, List<Move> moves) {
LOGGER.info("Created entity %s".formatted(entity));
return entity;
}

Entity create(EntitySnapshot snapshot) {
List<Move> moves = snapshot.moves().stream()
.map(this.combatRegistry::findMove)
.toList();
Entity entity = new Entity(snapshot.id(), snapshot.hostile(), snapshot.health(), snapshot.maxHealth(), moves, this.dispatcher);
LOGGER.info("Restored entity %s".formatted(entity));
return entity;
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/vamk/tbg/game/EntitySnapshot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.vamk.tbg.game;

import java.io.Serial;
import java.io.Serializable;
import java.util.List;

public record EntitySnapshot (int id, boolean hostile, int health, int maxHealth, List<String> moves) implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
}
Loading

0 comments on commit a43e080

Please sign in to comment.