fix: more code is apparently better

This commit is contained in:
punchready 2021-06-01 00:32:07 +02:00
parent 3238f45836
commit bccffad8f0
8 changed files with 91 additions and 27 deletions

View File

@ -104,6 +104,7 @@ public class Character extends Entity {
", rangedDamage=" + rangedDamage + ", rangedDamage=" + rangedDamage +
", meleeDamage=" + meleeDamage + ", meleeDamage=" + meleeDamage +
", inventory=" + inventory + ", inventory=" + inventory +
", position=" + position +
'}'; '}';
} }
} }

View File

@ -46,6 +46,7 @@ public class InfinityStone extends Entity {
public String toString() { public String toString() {
return "InfinityStone{" + return "InfinityStone{" +
"type=" + type + "type=" + type +
", position=" + position +
'}'; '}';
} }
} }

View File

@ -23,10 +23,10 @@ public class Inventory implements Iterable<StoneType> {
} }
for(StoneType stone: content) { for(StoneType stone: content) {
if(content.contains(stone)) { if(this.content.contains(stone)) {
throw new IllegalArgumentException("Attempted to construct an inventory with duplicate entries."); throw new IllegalArgumentException("Attempted to construct an inventory with duplicate entries.");
} }
content.add(stone); this.content.add(stone);
} }
} }

View File

@ -2,21 +2,66 @@ package uulm.teamname.marvelous.gamelibrary.entities;
import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.IntVector2;
import java.util.ArrayList;
/** Represents an NPC inside the game. */ /** Represents an NPC inside the game. */
public class NPC extends Entity { public class NPC extends Entity {
/** The {@link StatType#MP} of the NPC */
public final Stat mp;
/** The {@link Inventory} of the NPC */
public final Inventory inventory;
/**
* Constructs a new {@link NPC}.
* @param id The {@link EntityID} of the NPC
* @param position The position of the NPC
* @param inventory The starting inventory the NPC should have
*/
public NPC(EntityID id, IntVector2 position, int maxMP, ArrayList<StoneType> inventory) {
super(id, position);
solid = false;
opaque = true;
this.inventory = new Inventory(inventory);
this.mp = new Stat(StatType.MP, maxMP);
}
/**
* Constructs a new {@link NPC} with an empty inventory.
* @param id The {@link EntityID} of the NPC
* @param position The position of the NPC
* @param maxMP The maximum MP of the NPC
*/
public NPC(EntityID id, IntVector2 position, int maxMP) {
this(id, position, maxMP, new ArrayList<>());
}
/**
* Constructs a new {@link NPC} with an empty inventory.
* @param id The {@link EntityID} of the NPC
* @param position The position of the NPC
* @param inventory The starting inventory of the NPC
*/
public NPC(EntityID id, IntVector2 position, ArrayList<StoneType> inventory) {
this(id, position, 0, inventory);
}
/** /**
* Constructs a new {@link NPC} with an empty inventory. * Constructs a new {@link NPC} with an empty inventory.
* @param id The {@link EntityID} of the NPC * @param id The {@link EntityID} of the NPC
* @param position The position of the NPC * @param position The position of the NPC
*/ */
public NPC(EntityID id, IntVector2 position) { public NPC(EntityID id, IntVector2 position) {
super(id, position); this(id, position, 0);
solid = false;
opaque = true;
} }
@Override @Override
public NPC clone() { public NPC clone() {
return new NPC(id, position); NPC clone = new NPC(id, position, mp.getMax());
clone.mp.setValue(mp.getValue());
for(StoneType stone: inventory) {
clone.inventory.addStone(stone);
}
return clone;
} }
} }

View File

@ -63,6 +63,7 @@ public class Rock extends Entity {
return "Rock{" + return "Rock{" +
"maxHP=" + maxHP + "maxHP=" + maxHP +
", hp=" + hp + ", hp=" + hp +
", position=" + position +
'}'; '}';
} }
} }

View File

@ -104,7 +104,7 @@ public class EntityManager {
*/ */
public Entity findEntity(EntityID id) { public Entity findEntity(EntityID id) {
for(Entity entity: entities) { for(Entity entity: entities) {
if(entity.id == id) { if(entity.id.equals(id)) {
return entity; return entity;
} }
} }
@ -119,7 +119,7 @@ public class EntityManager {
public ArrayList<Entity> findByPosition(IntVector2 pos) { public ArrayList<Entity> findByPosition(IntVector2 pos) {
ArrayList<Entity> found = new ArrayList<>(); ArrayList<Entity> found = new ArrayList<>();
for(Entity entity: entities) { for(Entity entity: entities) {
if(entity.getPosition() == pos) { if(entity.getPosition().equals(pos)) {
found.add(entity); found.add(entity);
} }
} }
@ -133,7 +133,7 @@ public class EntityManager {
*/ */
public boolean blocksVision(IntVector2 pos) { public boolean blocksVision(IntVector2 pos) {
for(Entity entity: entities) { for(Entity entity: entities) {
if(entity.getPosition() == pos && entity.blocksVision()) { if(entity.getPosition().equals(pos) && entity.blocksVision()) {
return true; return true;
} }
} }
@ -147,7 +147,7 @@ public class EntityManager {
*/ */
public boolean blocksMovement(IntVector2 pos) { public boolean blocksMovement(IntVector2 pos) {
for(Entity entity: entities) { for(Entity entity: entities) {
if(entity.getPosition() == pos && entity.blocksMovement()) { if(entity.getPosition().equals(pos) && entity.blocksMovement()) {
return true; return true;
} }
} }

View File

@ -582,7 +582,7 @@ class GameLogic {
int selected = characters.get(i); int selected = characters.get(i);
EntityID id = new EntityID(i < p1 ? EntityType.P1 : EntityType.P2, selected); EntityID id = new EntityID(i < p1 ? EntityType.P1 : EntityType.P2, i % p1);
result.add(new EventBuilder(EventType.SpawnEntityEvent) result.add(new EventBuilder(EventType.SpawnEntityEvent)
.withEntity(new Character( .withEntity(new Character(
id, position, id, position,
@ -693,7 +693,7 @@ class GameLogic {
Collections.shuffle(state.turnOrder); Collections.shuffle(state.turnOrder);
for (EntityID id: state.turnOrder) { for (EntityID id: state.turnOrder) {
if(revived.contains(id) || ((Character)state.entities.findEntity(id)).hp.getValue() > 0){ if(id.type == EntityType.NPC || revived.contains(id) || ((Character)state.entities.findEntity(id)).hp.getValue() > 0){
state.activeCharacter = id; state.activeCharacter = id;
break; break;
} }
@ -717,8 +717,10 @@ class GameLogic {
public static ArrayList<Event> handleGoose(GameState state) { public static ArrayList<Event> handleGoose(GameState state) {
ArrayList<Event> result = new ArrayList<>(); ArrayList<Event> result = new ArrayList<>();
StoneType[] available = state.unvomitedStones.toArray(new StoneType[0]); ArrayList<StoneType> inventory = new ArrayList<>(state.unvomitedStones);
StoneType stone = available[rand.nextInt(available.length)]; int picked = rand.nextInt(inventory.size());
StoneType stone = inventory.get(picked);
inventory.remove(picked);
state.unvomitedStones.remove(stone); state.unvomitedStones.remove(stone);
ArrayList<IntVector2> free = new ArrayList<>(); ArrayList<IntVector2> free = new ArrayList<>();
@ -734,7 +736,7 @@ class GameLogic {
EntityID goose = new EntityID(EntityType.NPC, 0); EntityID goose = new EntityID(EntityType.NPC, 0);
result.add(new EventBuilder(EventType.SpawnEntityEvent) result.add(new EventBuilder(EventType.SpawnEntityEvent)
.withEntity(new NPC(goose, position)) .withEntity(new NPC(goose, position, inventory))
.buildEntityEvent()); .buildEntityEvent());
result.add(new EventBuilder(EventType.SpawnEntityEvent) result.add(new EventBuilder(EventType.SpawnEntityEvent)
.withEntity(new InfinityStone(new EntityID(EntityType.InfinityStones, stone.getID()), position, stone)) .withEntity(new InfinityStone(new EntityID(EntityType.InfinityStones, stone.getID()), position, stone))
@ -829,7 +831,7 @@ class GameLogic {
EntityID thanos = new EntityID(EntityType.NPC, 2); EntityID thanos = new EntityID(EntityType.NPC, 2);
result.add(new EventBuilder(EventType.SpawnEntityEvent) result.add(new EventBuilder(EventType.SpawnEntityEvent)
.withEntity(new Character(thanos, position, "Thanos", 1, maxMP, 0, 0, 0, 0)) .withEntity(new NPC(thanos, position, maxMP))
.buildEntityEvent()); .buildEntityEvent());
state.turnOrder.add(thanos); state.turnOrder.add(thanos);
@ -842,10 +844,10 @@ class GameLogic {
* @param state The game state to work on * @param state The game state to work on
* @return The list of resulting {@link Event}s * @return The list of resulting {@link Event}s
*/ */
public static ArrayList<Event> handleThanos(GameState state, Character thanos) { public static ArrayList<Event> handleThanos(GameState state, NPC thanos) {
ArrayList<Event> result = new ArrayList<>(); ArrayList<Event> result = new ArrayList<>();
//TODO: implement thanos ai
return result; return result;
} }
@ -860,13 +862,31 @@ class GameLogic {
state.turnNumber++; state.turnNumber++;
Character activeCharacter = (Character)state.entities.findEntity(state.activeCharacter); if(state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == 2) {
boolean isThanos = state.activeCharacter.type == EntityType.NPC && state.activeCharacter.id == 2; NPC thanos = (NPC)state.entities.findEntity(state.activeCharacter);
if(isThanos && state.roundNumber > state.partyConfig.maxRounds + 1) { if(state.roundNumber > state.partyConfig.maxRounds + 1) {
activeCharacter.mp.setMax(activeCharacter.mp.getMax() + 1);//TODO: use event for this... thanos.mp.setMax(thanos.mp.getMax() + 1);//TODO: use event for this...
} }
if(thanos.mp.getValue() != thanos.mp.getMax()) {
result.add(new EventBuilder(EventType.ConsumedMPEvent)
.withTargetEntity(state.activeCharacter)
.withTargetField(thanos.getPosition())
.withAmount(thanos.mp.getValue() - thanos.mp.getMax())
.buildGameEvent());
}
result.add(new EventBuilder(EventType.TurnEvent)
.withTurnCount(state.turnOrder.size())
.withNextCharacter(state.activeCharacter)
.buildGameEvent());
result.addAll(handleThanos(state, thanos));
return result;
}
Character activeCharacter = (Character)state.entities.findEntity(state.activeCharacter);
if(activeCharacter.ap.getValue() != activeCharacter.ap.getMax()) { if(activeCharacter.ap.getValue() != activeCharacter.ap.getMax()) {
result.add(new EventBuilder(EventType.ConsumedAPEvent) result.add(new EventBuilder(EventType.ConsumedAPEvent)
.withTargetEntity(state.activeCharacter) .withTargetEntity(state.activeCharacter)
@ -886,10 +906,6 @@ class GameLogic {
.withNextCharacter(state.activeCharacter) .withNextCharacter(state.activeCharacter)
.buildGameEvent()); .buildGameEvent());
if(isThanos) {
result.addAll(handleThanos(state, activeCharacter));
}
return result; return result;
} }

View File

@ -13,7 +13,7 @@ import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent;
import uulm.teamname.marvelous.gamelibrary.json.basic.BasicMessage; import uulm.teamname.marvelous.gamelibrary.json.basic.BasicMessage;
import uulm.teamname.marvelous.gamelibrary.json.basic.EventMessage; import uulm.teamname.marvelous.gamelibrary.json.basic.EventMessage;
import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig; import uulm.teamname.marvelous.gamelibrary.json.config.CharacterConfig;
import uulm.teamname.marvelous.gamelibrary.json.ingame.EntityDeserializer; import uulm.teamname.marvelous.gamelibrary.json.ingame.deserialize.EntityDeserializer;
/** /**
* Class that contains JSON encoding and decoding. It is initiated with the Character configuration. * Class that contains JSON encoding and decoding. It is initiated with the Character configuration.