package pl.czak.retronix;

import pl.czak.retronix.engine.Backend;
import pl.czak.retronix.engine.Event;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Queue;

public class Game {
    private Backend backend;
    private Deque<State> states = new ArrayDeque<>();
    private Queue<Event> events = new ArrayDeque<>();

    public enum Sound {
        LEVEL_COMPLETE, DEATH, GAME_OVER
    }

    public Game(Backend backend) {
        this.backend = backend;
    }

    /**
     * Enter the game's main loop
     */
    public void run() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    long start = System.currentTimeMillis();

                    handleEvent();
                    update();
                    draw();

                    long duration = System.currentTimeMillis() - start;

                    try {
                        Thread.sleep(Math.max(0, 50 - duration));
                    } catch (InterruptedException ignored) { }
                }
            }
        }).start();
    }

    private void handleEvent() {
        if (events.peek() != null) {
            getCurrentState().handleEvent(events.poll());
        }
    }

    private void update() {
        getCurrentState().update();
    }

    private void draw() {
        backend.draw(getCurrentState());
    }

    // region State management
    // -----------------------

    public void pushState(State state) {
        states.push(state);
    }

    public State popState() {
        return states.pop();
    }

    public State getCurrentState() {
        return states.peek();
    }

    // ---------
    // endregion

    public void playSound(Sound sound) {
        backend.playSound(sound);
    }

    public void addEvent(Event event) {
        events.offer(event);
    }
}

@@ -1201,31 +1201,27 @@
         
-getCurrentState
+states.peek
 ().handl
@@ -1296,31 +1296,27 @@
         
-getCurrentState
+states.peek
 ().updat
@@ -1374,31 +1374,27 @@
 nd.draw(
-getCurrentState
+states.peek
 ());%0A   
@@ -1606,82 +1606,8 @@
  %7D%0A%0A
-    public State getCurrentState() %7B%0A        return states.peek();%0A    %7D%0A%0A
     

remove getCurrentState (use the stack directly)