package net.plastboks.gameworld;

import net.plastboks.gameobjects.Autonomous;
import net.plastboks.gameobjects.Bird;
import net.plastboks.gameobjects.Mouse;
import net.plastboks.gameobjects.Snake;

import java.util.LinkedList;

/**
 * Created by alex on 12/12/14.
 */

public class GameWorld {

    private Snake snake;
    private LinkedList<Autonomous> food;
    private int score = 0;
    private int midPointY;

    public enum GameState { READY, RUNNING, GAMEOVER, HIGHSCORE }

    private GameState currentState;

    public GameWorld(int midPointY) {
        this.midPointY = midPointY;
        currentState = GameState.READY;

        snake = new Snake(33, midPointY - 5, 15, 15, midPointY * 2);

        food = new LinkedList<Autonomous>();
        initFood();
    }

    private void initFood() {
        food.add(new Bird(15, 15));
        food.add(new Mouse(15, 15));
    }

    public void update(float delta) {

        switch (currentState) {
            case READY:
                updateReady(delta);
                break;
            case RUNNING:
                updateRunning(delta);
                break;
            default:
                break;
        }

    }

    public void updateReady(float delta) { }

    public void updateRunning(float delta) {

        if (delta > .15f) { delta = .15f; }

        snake.update(delta);
        for(Autonomous a : food) {
            a.update(delta);
            if (snake.collides(a)) { a.respawn(); }
        }

        //if (snake.isAlive()) {
        //    snake.die();
        //    AssetLoader.dead.play();
        //}

    }

    public void restart() {
        currentState = GameState.READY;
        score = 0;
        snake.onRestart(midPointY - 5);
        currentState = GameState.READY;
    }

    public void addScore(int inc) { score += inc; }
    public void start() { currentState = GameState.RUNNING; }

    public Snake getSnake() { return snake; }
    public LinkedList<Autonomous> getFood() { return food; }

    public int getScore() { return score; }
    public boolean isReady() { return currentState == GameState.READY; }
    public boolean isGameOver() { return currentState == GameState.GAMEOVER; }
    public boolean isHighScore() { return currentState == GameState.HIGHSCORE; }
}

@@ -64,137 +64,9 @@
 cts.
-Autonomous;%0Aimport net.plastboks.gameobjects.Bird;%0Aimport net.plastboks.gameobjects.Mouse;%0Aimport net.plastboks.gameobjects.Snake
+*
 ;%0A%0Ai
@@ -1334,24 +1334,138 @@
 espawn(); %7D%0A
+            for (Node n : snake.getBody()) %7B%0A                if (a.collides(n)) %7B a.changeDir(); %7D%0A            %7D%0A%0A
         %7D%0A%0A 

check autonomous creature collition with snake body