import gengine.*;
import gengine.components.*;
import gengine.math.*;
import gengine.graphics.*;
import gengine.components.Light;

class GameSystem extends System
{
    private var terrainEntity:Entity;
    private var cameraEntity:Entity;
    private var angle:Float = 0.0;

    public function new()
    {
        super();
    }

    override public function addToEngine(engine:Engine):Void
    {
        terrainEntity = new Entity();
        terrainEntity.add(new Terrain());

        terrainEntity.get(Terrain).setPatchSize(64);
        terrainEntity.get(Terrain).setSpacing(new Vector3(1.0, 0.5, 1.0));
        terrainEntity.get(Terrain).setSmoothing(true);
        terrainEntity.get(Terrain).setCastShadows(true);
        terrainEntity.get(Terrain).setHeightMap(Gengine.getResourceCache().getImage('HeightMap.png', true));
        terrainEntity.get(Terrain).setMaterial(Gengine.getResourceCache().getMaterial('Terrain.xml', true));
        terrainEntity.setPosition(new Vector3(-0.7, 0, -0.2));

        engine.addEntity(terrainEntity);

        cameraEntity = new Entity();
        cameraEntity.add(new Camera());
        engine.addEntity(cameraEntity);
        cameraEntity.setPosition(new Vector3(0.0, 30.0, -30.0));
        cameraEntity.lookAt(new Vector3(0, terrainEntity.get(Terrain).getHeight(new Vector3(0, 0, 0)) + 15.0, 0));

        var viewport:Viewport = new Viewport(Gengine.getContext());
        viewport.setScene(Gengine.getScene());
        viewport.setCamera(cameraEntity.get(Camera));
        Gengine.getRenderer().setViewport(0, viewport);

        var lightEntity = new Entity();
        var light = new Light();
        light.setLightType(0);
        light.setCastShadows(true);
        light.setColor(new Color(0.7, 0.7, 0.7, 1));
        light.setShadowBias(new BiasParameters(0.00025, 0.5, 0));
        light.setShadowCascade(new CascadeParameters(10.0, 50.0, 200.0, 0.0, 0.8, 1.0));
        light.setSpecularIntensity(0.5);
        lightEntity.add(light);
        lightEntity.setDirection(new Vector3(0.6, -1.0, 0.8));

        engine.addEntity(lightEntity);

        var skyEntity = new Entity();
        skyEntity.setScale(new Vector3(100,100,100));
        skyEntity.add(new Skybox());
        skyEntity.get(Skybox).setModel(Gengine.getResourceCache().getModel('Box.mdl', true));
        skyEntity.get(Skybox).setMaterial1(Gengine.getResourceCache().getMaterial('Skybox.xml', true));
        engine.addEntity(skyEntity);
    }

    override public function update(dt:Float):Void
    {
        angle += dt * 0.5;

        var position = cameraEntity.position;
        position.x = 100 * Math.cos(angle);
        position.z = 100 * Math.sin(angle);
        position.y = terrainEntity.get(Terrain).getHeight(position) + 15.0;
        cameraEntity.position = position;

        cameraEntity.lookAt(new Vector3(0, 50.0, 0));

        if(Gengine.getInput().getScancodePress(41))
        {
            Gengine.exit();
        }
    }
}

class Application
{
    public static function init()
    {
        Gengine.setWindowSize(new IntVector2(1024, 768));
        Gengine.setWindowTitle("01-terrain");
    }

    public static function start(engine:Engine)
    {
        engine.addSystem(new GameSystem(), 2);
        engine.addSystem(new ScreenshotSystem(), 0);

        Gengine.getRenderer().getDefaultZone().setAmbientColor(new Color(0.15, 0.15, 0.15, 1));
    }
}