package mygame;

import com.jme3.app.DebugKeysAppState;
import com.jme3.app.SimpleApplication;
import com.jme3.app.StatsAppState;
import com.jme3.math.FastMath;
import com.jme3.scene.Node;

public class Main extends SimpleApplication {
    public static final int NUMBER_OF_ANIMALS = 10;
    Node groundNode;
    Node[] animalNodes;
    Cannon cannon;
    Cannonball cannonball;
    float time;
    boolean switched = false;
    JMEInit jmeInit;

    // -------------------------------------------------------------------------
    public static void main(String[] args) {
        Main app = new Main();
        JMEInit.initAppScreen(app); // avoid the monkey ...
        app.start();
    }

    // -------------------------------------------------------------------------
    // This constructor removes the flyCam
    public Main() {
        super(new StatsAppState(), new DebugKeysAppState());
    }
    
    // -------------------------------------------------------------------------
    @Override
    public void simpleInitApp() {
        // initialize graphics (cam, materials etc.)
        jmeInit = new JMEInit(this);
        //
        // add ground
        groundNode = new Ground(this);
        rootNode.attachChild(groundNode);
        //
        // add animals
        addAnimals();
        //
        // add cannon
        cannon = new Cannon(this);
        groundNode.attachChild(cannon);
        //
        // add cannonball
        cannonball = new Cannonball(this,cannon,groundNode);
    }

    // -------------------------------------------------------------------------
    private void addAnimals(){
        animalNodes = new Node[NUMBER_OF_ANIMALS];
        for (int i = 0; i < NUMBER_OF_ANIMALS; i++) {
            animalNodes[i] = new Node();
            Animal animal;
            if (i % 2 == 0) {
                animal = new Dinosaur(this);
            } else {
                animal = new Elephant(this);
            }
            animalNodes[i].attachChild(animal);
            float angle = (float) i / NUMBER_OF_ANIMALS * 2 * FastMath.PI;
            float radius = 30f;
            animal.setLocalTranslation(radius * FastMath.sin(angle), 0, radius * FastMath.cos(angle));
            animal.rotate(0, angle, 0);
            groundNode.attachChild(animalNodes[i]);
        }
    }
    
    
    // -------------------------------------------------------------------------
    @Override
    public void simpleUpdate(float tpf) {
        // move the animals
        for (int i = 0; i < animalNodes.length; i++) {
            animalNodes[i].getChild(0).rotate(0, tpf / 2, 0);
            time += tpf;
        }
        //
        // move the cannonball
        cannonball.fly(tpf);
        //
        // the cannon is directly steered by the input events, 
        // it is not part of the update loop.
    }
}
