package net.plastboks.gameobjects;

import com.badlogic.gdx.math.Vector2;
import net.plastboks.shared.Directions;

import java.util.LinkedList;

/**
 * Created by alex on 12/12/14.
 */
public class Snake extends Creature {

    private final int startBodySize = 4;

    private LinkedList<Node> body;
    private int bodySize = startBodySize;
    private int collisions;
    private float lvl = 0.8f;
    private float lvlInc = 0.2f;
    private float maxLvl = 1.6f;

    public Snake(float x, float y, int width, int height, int gameHeight) {
        super(x, y, width, height);
        setGameHeight(gameHeight);
        setAlive(true);

        this.body = new LinkedList<Node>();
    }

    public void incrementBodySizeBy(int n) { bodySize += n; }

    private void pushToBody(float delta, int max) {
        if (body.size() >= max) { body.removeFirst(); }
        body.add(new Node(new Vector2(getX(), getY()), getDir()));
    }

    public void changeDir(Directions d) {
        switch (d) {
            case NORTH:
                if (getDir() != Directions.SOUTH) {
                    setDir(d);
                    setRotation(Directions.NORTH);
                }
                break;
            case SOUTH:
                if (getDir() != Directions.NORTH) {
                    setDir(d);
                    setRotation(Directions.SOUTH);
                }
                break;
            case WEST:
                if (getDir() != Directions.EAST) {
                    setDir(d);
                    setRotation(Directions.WEST);
                }
                break;
            case EAST:
                if (getDir() != Directions.WEST) {
                    setDir(d);
                    setRotation(Directions.EAST);
                }
                break;
        }
    }

    public void incrementSpeed() {
        if (lvl + lvlInc <= maxLvl) { lvl += lvlInc; }
    }

    public void update(float delta) {
        pushToBody(delta, bodySize);
        move(delta + lvl);
    }

    public void onClick(float x, float y) {
        if (isAlive()) {
            switch (getDir()) {
                case EAST:
                case WEST:
                    if (y > getY()) {
                        changeDir(Directions.SOUTH);
                    } else {
                        changeDir(Directions.NORTH);
                    }
                    break;
                case NORTH:
                case SOUTH:
                    if (x > getX()) {
                        changeDir(Directions.EAST);
                    } else {
                        changeDir(Directions.WEST);
                    }
                    break;
            }
        }
    }

    public void onRestart(int y) {
        setRotation(Directions.WEST);
        setY(y);
        setAlive(true);
        body = new LinkedList<Node>();
        bodySize = startBodySize;
    }

    public LinkedList<Node> getBody() { return body; }

    public void die() { setAlive(false); }
}

@@ -257,16 +257,57 @@
 ize = 4;
+%0A    private final float startLvl = 0.8f;
 %0A%0A    pr
@@ -429,20 +429,24 @@
 t lvl = 
-0.8f
+startLvl
 ;%0A    pr
@@ -2917,24 +2917,48 @@
 rtBodySize;%0A
+        lvl = startLvl;%0A
     %7D%0A%0A    p

reset snake lvl on restart