Create the FaceDown card state lazily.

Profiling showed this was responsible for 3% of execution time when using Simulated AI and is also wasting memory.
This commit is contained in:
Myrd
2016-12-25 21:13:03 +00:00
parent 6831c990a6
commit f97ccdef5d

View File

@@ -282,7 +282,6 @@ public class Card extends GameEntity implements Comparable<Card> {
view = new CardView(id0, game == null ? null : game.getTracker());
currentState = new CardState(view.getCurrentState(), this);
states.put(CardStateName.Original, currentState);
states.put(CardStateName.FaceDown, CardUtil.getFaceDownCharacteristic(this));
view.updateChangedColorWords(this);
view.updateChangedTypes(this);
view.updateSickness(this);
@@ -357,8 +356,13 @@ public class Card extends GameEntity implements Comparable<Card> {
}
public boolean setState(final CardStateName state, boolean updateView) {
if (!states.containsKey(state)) {
System.out.println(getName() + " tried to switch to non-existant state \"" + state + "\"!");
return false; // Nonexistant state.
if (state == CardStateName.FaceDown) {
// The face-down state is created lazily only when needed.
states.put(CardStateName.FaceDown, CardUtil.getFaceDownCharacteristic(this));
} else {
System.out.println(getName() + " tried to switch to non-existant state \"" + state + "\"!");
return false; // Nonexistant state.
}
}
if (state.equals(currentStateName)) {
@@ -6281,6 +6285,8 @@ public class Card extends GameEntity implements Comparable<Card> {
public final void setManifested(final boolean manifested) {
this.manifested = manifested;
final String image = manifested ? ImageKeys.MANIFEST_IMAGE : ImageKeys.MORPH_IMAGE;
// Note: This should only be called after state has been set to CardStateName.FaceDown,
// so the below call should be valid since the state should have been created already.
getState(CardStateName.FaceDown).setImageKey(ImageKeys.getTokenKey(image));
}