Search in sources :

Example 1 with GameState

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();
    }
}
Also used : Vehicle(de.adesso.anki.battle.world.bodies.Vehicle) VehicleStateProvider(de.adesso.anki.battle.providers.VehicleStateProvider) DynamicBody(de.adesso.anki.battle.world.DynamicBody) GameState(com.states.GameState) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 2 with GameState

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 "";
}
Also used : ObjectInFront(com.states.ObjectInFront) JSONObject(org.json.JSONObject) ObjectBehind(com.states.ObjectBehind) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) GameState(com.states.GameState) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

GameState (com.states.GameState)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectBehind (com.states.ObjectBehind)1 ObjectInFront (com.states.ObjectInFront)1 VehicleStateProvider (de.adesso.anki.battle.providers.VehicleStateProvider)1 DynamicBody (de.adesso.anki.battle.world.DynamicBody)1 Vehicle (de.adesso.anki.battle.world.bodies.Vehicle)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1