Search in sources :

Example 1 with VehicleStateProvider

use of de.adesso.anki.battle.providers.VehicleStateProvider 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)

Aggregations

GameState (com.states.GameState)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 Scheduled (org.springframework.scheduling.annotation.Scheduled)1