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> unmodifiablePropertyMap;
@JsonIgnore private Map<Integer, CharacterProperties> idToCharacter;
/**
* @return a unmodifiable {@link Map}<{@link String}, {@link CharacterProperties}> containing all properties.
@ -41,6 +42,23 @@ public class CharacterConfig {
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
public Tuple<CharacterProperties[], CharacterProperties[]> getDisjointSetsOfPropertiesOfSize(int size) {
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) {
manager.applyEvents(events);

View File

@ -3,6 +3,7 @@ package uulm.teamname.marvelous.gamelibrary.gamelogic;
import org.tinylog.Logger;
import uulm.teamname.marvelous.gamelibrary.ArrayTools;
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.entities.Character;
import uulm.teamname.marvelous.gamelibrary.entities.*;
@ -402,6 +403,7 @@ public class GameLogic {
}
case MindStone -> {
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(Entity entity: state.entities.findByPosition(pos)) {
if(entity.id.isSameType(target)) {
@ -735,7 +737,7 @@ public class GameLogic {
}
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);
characters.addAll(selectedCharacters2);
@ -745,18 +747,18 @@ public class GameLogic {
IntVector2 position = free.get(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);
state.entities.addEntity(new Character(
id, position,
state.characterConfig.characters[selected].name,
state.characterConfig.characters[selected].HP,
state.characterConfig.characters[selected].MP,
state.characterConfig.characters[selected].AP,
state.characterConfig.characters[selected].attackRange,
state.characterConfig.characters[selected].rangedDamage,
state.characterConfig.characters[selected].meleeDamage
selected.name,
selected.HP,
selected.MP,
selected.AP,
selected.attackRange,
selected.rangedDamage,
selected.meleeDamage
));
state.turnOrder.add(id);

View File

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