Android Open Source - Sneik Game World






From Project

Back to project page Sneik.

License

The source code is released under:

Apache License

If you think the Android project Sneik listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.plastboks.gameworld;
//from w  w  w. jav  a2 s .c  o  m
import net.plastboks.gameobjects.*;
import net.plastboks.sneikhelpers.AssetLoader;

import java.util.LinkedList;

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

public class GameWorld {

    private Snake snake;
    public enum GameState { READY, RUNNING, GAMEOVER, HIGHSCORE }
    private GameState currentState;
    private GamePlay gp;

    private int score = 0;
    private int midPointY;

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

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

        gp = new GamePlay(snake);
    }

    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) {
        snake.update(delta);
        for(Autonomous a : gp.getFood()) {
            a.update(delta);
            if (snake.collides(a)) {
                gp.increment(a);
                a.respawn();
                addScore(a.getScore());
                a.playSound();
            }
            int i = snake.getBody().size();
            for (Node n : snake.getBody()) {
                /* prey collides with snake body */
                if (a.collides(n)) { a.changeDir(); }
                /* snake eats itself - game over - */
                if (i > snake.getWidth() && snake.collides(n)) {
                    gameOver();
                    return;
                }
                i--;
            }
        }
    }

    private void gameOver() {
        snake.die();
        AssetLoader.dead.play();
        if (getScore() > AssetLoader.getHighScore()) {
            AssetLoader.setHighScore(score);
            currentState = GameState.HIGHSCORE;
        } else {
            currentState = GameState.GAMEOVER;
        }
    }

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

    public void start() { currentState = GameState.RUNNING; }

    public void addScore(int inc) { score += inc; }
    public int getScore() { return score; }

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

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




Java Source Code List

net.plastboks.gameobjects.Artificial.java
net.plastboks.gameobjects.Autonomous.java
net.plastboks.gameobjects.Bird.java
net.plastboks.gameobjects.Creature.java
net.plastboks.gameobjects.Mouse.java
net.plastboks.gameobjects.Node.java
net.plastboks.gameobjects.Snake.java
net.plastboks.gameworld.GamePlay.java
net.plastboks.gameworld.GameRenderer.java
net.plastboks.gameworld.GameWorld.java
net.plastboks.screens.GameScreen.java
net.plastboks.shared.Directions.java
net.plastboks.sneik.IOSLauncher.java
net.plastboks.sneik.SneikGame.java
net.plastboks.sneik.android.AndroidLauncher.java
net.plastboks.sneik.client.HtmlLauncher.java
net.plastboks.sneik.desktop.DesktopLauncher.java
net.plastboks.sneikhelpers.AssetLoader.java
net.plastboks.sneikhelpers.InputHandler.java