fix: make character selection use characterIDs and not indices

This commit is contained in:
punchready 2021-06-06 23:00:03 +02:00
parent 598a1e00c1
commit 2b11633ced
4 changed files with 34 additions and 13 deletions

View File

@ -22,6 +22,7 @@ public class CharacterConfig {
@JsonIgnore private Map<String, CharacterProperties> propertyMap; @JsonIgnore private Map<String, CharacterProperties> propertyMap;
@JsonIgnore private Map<String, CharacterProperties> unmodifiablePropertyMap; @JsonIgnore private Map<String, CharacterProperties> unmodifiablePropertyMap;
@JsonIgnore private Map<Integer, CharacterProperties> idToCharacter;
/** /**
* @return a unmodifiable {@link Map}<{@link String}, {@link CharacterProperties}> containing all properties. * @return a unmodifiable {@link Map}<{@link String}, {@link CharacterProperties}> containing all properties.
@ -41,6 +42,23 @@ public class CharacterConfig {
return unmodifiablePropertyMap; return unmodifiablePropertyMap;
} }
/**
* @return a {@link Map}<{@link Integer}, {@link CharacterProperties}> from characterID to character.
* If not yet existent, initialize the Map
*/
@JsonIgnore
public Map<Integer, CharacterProperties> getIDMap() {
// lazy initialization
if (idToCharacter == null) {
idToCharacter = new HashMap<>();
for (CharacterProperties property: characters) {
idToCharacter.put(property.characterID, property);
}
}
return idToCharacter;
}
@JsonIgnore @JsonIgnore
public Tuple<CharacterProperties[], CharacterProperties[]> getDisjointSetsOfPropertiesOfSize(int size) { public Tuple<CharacterProperties[], CharacterProperties[]> getDisjointSetsOfPropertiesOfSize(int size) {
if ((size * 2) > characters.length) { if ((size * 2) > characters.length) {

View File

@ -71,7 +71,7 @@ public class GameInstance {
} }
/** /**
* See {@link GameInstance#applyEvents(ArrayList)}. * See {@link GameInstance#applyEvents(List)}.
*/ */
public void applyEvents(Event... events) { public void applyEvents(Event... events) {
manager.applyEvents(events); manager.applyEvents(events);

View File

@ -3,6 +3,7 @@ package uulm.teamname.marvelous.gamelibrary.gamelogic;
import org.tinylog.Logger; import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.ArrayTools; import uulm.teamname.marvelous.gamelibrary.ArrayTools;
import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.config.CharacterProperties;
import uulm.teamname.marvelous.gamelibrary.config.FieldType; import uulm.teamname.marvelous.gamelibrary.config.FieldType;
import uulm.teamname.marvelous.gamelibrary.entities.Character; import uulm.teamname.marvelous.gamelibrary.entities.Character;
import uulm.teamname.marvelous.gamelibrary.entities.*; import uulm.teamname.marvelous.gamelibrary.entities.*;
@ -402,6 +403,7 @@ public class GameLogic {
} }
case MindStone -> { case MindStone -> {
EntityType target = data.originEntity.type == EntityType.P1 ? EntityType.P2 : EntityType.P1; EntityType target = data.originEntity.type == EntityType.P1 ? EntityType.P2 : EntityType.P1;
//todo: extend to infinity
for(IntVector2 pos: rasterize(data.originField, data.targetField, false, true)) { for(IntVector2 pos: rasterize(data.originField, data.targetField, false, true)) {
for(Entity entity: state.entities.findByPosition(pos)) { for(Entity entity: state.entities.findByPosition(pos)) {
if(entity.id.isSameType(target)) { if(entity.id.isSameType(target)) {
@ -735,7 +737,7 @@ public class GameLogic {
} }
int p1 = selectedCharacters1.size(); int p1 = selectedCharacters1.size();
int all = selectedCharacters1.size() + selectedCharacters2.size() - 1; // this is the highest index + 1 int all = selectedCharacters1.size() + selectedCharacters2.size();
ArrayList<Integer> characters = new ArrayList<>(selectedCharacters1); ArrayList<Integer> characters = new ArrayList<>(selectedCharacters1);
characters.addAll(selectedCharacters2); characters.addAll(selectedCharacters2);
@ -745,18 +747,18 @@ public class GameLogic {
IntVector2 position = free.get(choice); IntVector2 position = free.get(choice);
free.remove(choice); free.remove(choice);
int selected = characters.get(i); CharacterProperties selected = state.characterConfig.getIDMap().get(characters.get(i));
EntityID id = new EntityID(i < p1 ? EntityType.P1 : EntityType.P2, i % p1); EntityID id = new EntityID(i < p1 ? EntityType.P1 : EntityType.P2, i % p1);
state.entities.addEntity(new Character( state.entities.addEntity(new Character(
id, position, id, position,
state.characterConfig.characters[selected].name, selected.name,
state.characterConfig.characters[selected].HP, selected.HP,
state.characterConfig.characters[selected].MP, selected.MP,
state.characterConfig.characters[selected].AP, selected.AP,
state.characterConfig.characters[selected].attackRange, selected.attackRange,
state.characterConfig.characters[selected].rangedDamage, selected.rangedDamage,
state.characterConfig.characters[selected].meleeDamage selected.meleeDamage
)); ));
state.turnOrder.add(id); state.turnOrder.add(id);

View File

@ -27,8 +27,8 @@ public class BaseGameLogicTest {
partyConfig.mindStoneDMG = 3; partyConfig.mindStoneDMG = 3;
characterConfig.characters = new CharacterProperties[] { characterConfig.characters = new CharacterProperties[] {
generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter(0), generateCharacter(1), generateCharacter(2), generateCharacter(3), generateCharacter(4), generateCharacter(5),
generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter(), generateCharacter() generateCharacter(6), generateCharacter(7), generateCharacter(8), generateCharacter(9), generateCharacter(10), generateCharacter(11)
}; };
scenarioConfig.name = generateName(20); scenarioConfig.name = generateName(20);
@ -63,9 +63,10 @@ public class BaseGameLogicTest {
return name.toString(); return name.toString();
} }
private static CharacterProperties generateCharacter() { private static CharacterProperties generateCharacter(int id) {
CharacterProperties props = new CharacterProperties(); CharacterProperties props = new CharacterProperties();
props.characterID = id;
props.name = generateName(10); props.name = generateName(10);
props.HP = Math.abs(randomIntegers.next() % 15) + 5; props.HP = Math.abs(randomIntegers.next() % 15) + 5;