Убрать аллокации из игрового hot path
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package com.projectsacrifice.game;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/** Allocation-free critical-state comparison after a level has been configured. */
|
||||
final class AccessibilityStateCache {
|
||||
private List<String> signalIds = Collections.emptyList();
|
||||
private GameLanguage language;
|
||||
private int activeSignals = Integer.MIN_VALUE;
|
||||
private int openDoors = Integer.MIN_VALUE;
|
||||
private int totalDoors = Integer.MIN_VALUE;
|
||||
private boolean playerAlive;
|
||||
private String statusText;
|
||||
|
||||
void configureSignals(List<String> sourceIds) {
|
||||
ArrayList<String> unique = new ArrayList<>();
|
||||
for (String signalId : sourceIds) {
|
||||
if (!unique.contains(signalId)) unique.add(signalId);
|
||||
}
|
||||
signalIds = Collections.unmodifiableList(unique);
|
||||
language = null;
|
||||
activeSignals = Integer.MIN_VALUE;
|
||||
openDoors = Integer.MIN_VALUE;
|
||||
totalDoors = Integer.MIN_VALUE;
|
||||
statusText = null;
|
||||
}
|
||||
|
||||
List<String> signalIds() {
|
||||
return signalIds;
|
||||
}
|
||||
|
||||
boolean update(GameStrings strings, int updatedActiveSignals, int updatedOpenDoors,
|
||||
int updatedTotalDoors, boolean updatedPlayerAlive) {
|
||||
GameLanguage updatedLanguage = strings.language();
|
||||
if (statusText != null
|
||||
&& language == updatedLanguage
|
||||
&& activeSignals == updatedActiveSignals
|
||||
&& openDoors == updatedOpenDoors
|
||||
&& totalDoors == updatedTotalDoors
|
||||
&& playerAlive == updatedPlayerAlive) {
|
||||
return false;
|
||||
}
|
||||
language = updatedLanguage;
|
||||
activeSignals = updatedActiveSignals;
|
||||
openDoors = updatedOpenDoors;
|
||||
totalDoors = updatedTotalDoors;
|
||||
playerAlive = updatedPlayerAlive;
|
||||
statusText = AccessibilityStatus.describe(strings, activeSignals, signalIds.size(),
|
||||
openDoors, totalDoors, playerAlive);
|
||||
return true;
|
||||
}
|
||||
|
||||
String statusText() {
|
||||
return statusText;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user