use of com.states.GameState in project anki-battle-showcase by adessoAG.
the class GameEngine method gameLoop.
@Scheduled(fixedRate = 50)
public void gameLoop() {
VehicleStateProvider vehicleStateProvider = new VehicleStateProvider();
if (running) {
// Step 0: Calculate elapsed nanoseconds since last loop
long step = System.nanoTime();
long deltaNanos = step - lastStep;
lastStep = step;
// TODO: Synchronize with Anki vehicles
if (stepCount == 0) {
for (DynamicBody body : world.getDynamicBodies()) {
if (body instanceof Vehicle)
anki.synchronizeState((Vehicle) body);
}
}
// Step 2: Simulate movement
updateSimulation(deltaNanos);
// Step 3: Process input
// TODO: Process input from frontend
// Step 4: Evaluate behavior
stepCount++;
if (stepCount > 3) {
List<DynamicBody> dynBodies = world.getDynamicBodies();
for (DynamicBody body : dynBodies) {
log.debug(body.toString());
if (!(body instanceof Vehicle)) {
continue;
}
List<GameState> factsRoad = vehicleStateProvider.getRoadFacts((Vehicle) body);
List<GameState> factsInventory = vehicleStateProvider.getInventoryFacts((Vehicle) body);
List<GameState> factsObstacles = vehicleStateProvider.getObstacleFacts((Vehicle) body);
body.setFacts(factsRoad, factsInventory, factsObstacles);
}
evaluateBehavior();
stepCount = 0;
}
// Remove while iterating, leads to exception
// java.util.ConcurrentModificationException: in renderer?
// collisionHandling();
collectOrphanedWeapons();
// Step 5: Render world
renderWorld();
calculateLaptime();
}
}
use of com.states.GameState in project anki-battle-showcase by adessoAG.
the class Vehicle method convertFactsToMessage.
public String convertFactsToMessage() {
ObjectMapper objMapper = new ObjectMapper();
JSONObject json = new JSONObject();
JSONArray arr = new JSONArray();
try {
json.put("speed", this.speed);
for (GameState gameState : this.factsRoad) {
arr.put(gameState.getClass().getSimpleName());
}
json.put("nextRoadPiece", arr);
arr = new JSONArray();
for (GameState gameState : this.factsInventory) {
arr.put(gameState.getClass().getSimpleName());
}
json.put("inv", arr);
// 1 ebene tiefer die einzelnen attribute
JSONArray allObstacleFacts = new JSONArray();
for (GameState gameState : this.factsObstacles) {
JSONArray factArr = new JSONArray();
if (gameState instanceof ObjectBehind) {
factArr.put("Behind");
// obstacle type
factArr.put(((ObjectBehind) gameState).getType());
factArr.put(((ObjectBehind) gameState).getMetersBehind());
}
if (gameState instanceof ObjectInFront) {
factArr.put("Front");
// obstacle type
factArr.put(((ObjectInFront) gameState).getType());
factArr.put(((ObjectInFront) gameState).getMetersInFront());
}
allObstacleFacts.put(factArr);
}
json.put("obstacles", allObstacleFacts);
return json.toString();
} catch (JSONException e) {
log.error("Constructing Json error", e);
}
return "";
}
Aggregations