Package net.anzix.fsz

Source Code of net.anzix.fsz.MainUpdater

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package net.anzix.fsz;

import com.ardor3d.annotation.MainThread;
import com.ardor3d.example.ExampleBase;
import com.ardor3d.framework.Canvas;
import com.ardor3d.framework.NativeCanvas;
import com.ardor3d.framework.Updater;
import com.ardor3d.image.util.AWTImageLoader;
import com.ardor3d.input.GrabbedState;
import com.ardor3d.input.Key;
import com.ardor3d.input.MouseButton;
import com.ardor3d.input.MouseManager;
import com.ardor3d.input.control.FirstPersonControl;
import com.ardor3d.input.logical.AnyKeyCondition;
import com.ardor3d.input.logical.InputTrigger;
import com.ardor3d.input.logical.KeyPressedCondition;
import com.ardor3d.input.logical.LogicalLayer;
import com.ardor3d.input.logical.MouseButtonClickedCondition;
import com.ardor3d.input.logical.MouseButtonPressedCondition;
import com.ardor3d.input.logical.MouseButtonReleasedCondition;
import com.ardor3d.input.logical.TriggerAction;
import com.ardor3d.input.logical.TwoInputStates;
import com.ardor3d.light.DirectionalLight;
import com.ardor3d.math.ColorRGBA;
import com.ardor3d.math.Vector3;
import com.ardor3d.renderer.ContextCapabilities;
import com.ardor3d.renderer.ContextManager;
import com.ardor3d.renderer.pass.BasicPassManager;
import com.ardor3d.renderer.queue.RenderBucketType;
import com.ardor3d.renderer.state.CullState;
import com.ardor3d.renderer.state.LightState;
import com.ardor3d.renderer.state.WireframeState;
import com.ardor3d.renderer.state.ZBufferState;
import com.ardor3d.scenegraph.event.DirtyType;
import com.ardor3d.util.Constants;
import com.ardor3d.util.GameTaskQueue;
import com.ardor3d.util.GameTaskQueueManager;
import com.ardor3d.util.ReadOnlyTimer;
import com.ardor3d.util.Timer;
import com.ardor3d.util.resource.ResourceLocatorTool;
import com.ardor3d.util.resource.SimpleResourceLocator;
import com.ardor3d.util.stat.StatCollector;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.inject.Inject;
import java.net.URISyntaxException;
import net.anzix.fsz.eventbus.EventBus;
import net.anzix.fsz.eventbus.event.Exit;
import net.anzix.fsz.eventbus.event.Init;
import net.anzix.fsz.voxelworld.VoxelWorld;
import net.anzix.fsz.world.Physics;

/**
*
* @author elek
*/
public class MainUpdater implements Updater {

    private MouseManager _mouseManager;

    private FirstPersonControl _controlHandle;

   

    private LightState _lightState;

    private WireframeState _wireframeState;

    private NativeCanvas _canvas;

    private  LogicalLayer _logicalLayer;
   
    @Inject
    private EventBus eventbus;
   
    @Inject
    private Physics physics;
   
    @Inject
    private NodeRepository nodes;

    //private ProceduralTerrain proceduralTerrain;
    private VoxelWorld voxelworld;
    public VoxelWorld getVoxelWorld() {return voxelworld; }
   
    private BasicPassManager _passManager;

    public MainUpdater(MouseManager _mouseManager, LightState _lightState, WireframeState _wireframeState, NativeCanvas _canvas, LogicalLayer _logicalLayer, Timer _timer, BasicPassManager passManager) {
        this._mouseManager = _mouseManager;
        this._lightState = _lightState;
        this._wireframeState = _wireframeState;
        this._canvas = _canvas;
        this._logicalLayer = _logicalLayer;

        //proceduralTerrain = new ProceduralTerrain();
        voxelworld = new VoxelWorld(_timer);
        this._passManager = passManager;
    }
   
   

    @MainThread
    public void init() {
        final ContextCapabilities caps = ContextManager.getCurrentContext().getCapabilities();

        registerInputTriggers();

        AWTImageLoader.registerLoader();

        try {
            SimpleResourceLocator srl = new SimpleResourceLocator(ResourceLocatorTool.getClassPathResource(
                    App.class, "com/ardor3d/example/media/"));
            ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_TEXTURE, srl);
            srl = new SimpleResourceLocator(ResourceLocatorTool.getClassPathResource(ExampleBase.class,
                    "com/ardor3d/example/media/models/"));
            ResourceLocatorTool.addResourceLocator(ResourceLocatorTool.TYPE_MODEL, srl);
        } catch (final URISyntaxException ex) {
            ex.printStackTrace();
        }

        /**
         * Create a ZBuffer to display pixels closest to the camera above farther ones.
         */
        final ZBufferState buf = new ZBufferState();
        buf.setEnabled(true);
        buf.setFunction(ZBufferState.TestFunction.LessThanOrEqualTo);
        nodes.getRootNode().setRenderState(buf);

        // ---- LIGHTS
        /** Set up a basic, default light. */
        final DirectionalLight light = new DirectionalLight();
        light.setDirection(-0.37352666, -0.50444174, -0.7784704);
        light.setDiffuse(ColorRGBA.WHITE);
        //light.setAmbient(new ColorRGBA(0.5f, 0.5f, 0.5f, 1.0f));
        light.setEnabled(true);

        /** Attach the light to a lightState and the lightState to rootNode. */
        _lightState = new LightState();
        _lightState.setEnabled(true);
        _lightState.attach(light);
        nodes.getRootNode().setRenderState(_lightState);

        final CullState cs = new CullState();
        cs.setEnabled(true);
        cs.setCullFace(CullState.Face.Back);
        nodes.getRootNode().setRenderState(cs);

        //proceduralTerrain.init(_canvas.getCanvasRenderer().getCamera(), nodes.getRootNode());
        voxelworld.init(_canvas.getCanvasRenderer().getCamera(), _passManager, nodes.getRootNode());

        nodes.getRootNode().getSceneHints().setRenderBucketType(RenderBucketType.Opaque);
       
       
       
        eventbus.fireEvent(new Init());
//        // Create a new box centered at (0,0,0) with width/height/depth of size 10.
//        Box box = new Box("Box", new Vector3(0, 0, 0), 5, 5, 5);
//        // Set a bounding box for frustum culling.
//        box.setModelBound(new BoundingBox());
//        // Move the box out from the camera 15 units.
//        box.setTranslation(new Vector3(0, 0, -15));
//        // Give the box some nice colors.
//        box.setRandomColors();
//
//        // Attach the box to the scenegraph root.
//        _root.attachChild(box);

        nodes.getRootNode().updateGeometricState(0);
    }

    @MainThread
    public void update(final ReadOnlyTimer timer) {
        if (_canvas.isClosing()) {
            eventbus.fireEvent(new Exit());
        }

        /** update stats, if enabled. */
        if (Constants.stats) {
            StatCollector.update();
        }

        updateLogicalLayer(timer);

        // Execute updateQueue item
        GameTaskQueueManager.getManager(_canvas.getCanvasRenderer().getRenderContext()).getQueue(GameTaskQueue.UPDATE).execute();


        voxelworld.update(timer,_canvas.getCanvasRenderer().getCamera());
       
        _passManager.updatePasses(timer.getTimePerFrame());

        /** Update controllers/render states/transforms/bounds for rootNode. */
        nodes.getRootNode().updateGeometricState(timer.getTimePerFrame(), true);
    }

    protected void updateLogicalLayer(final ReadOnlyTimer timer) {
        // check and execute any input triggers, if we are concerned with input
        if (_logicalLayer != null) {
            _logicalLayer.checkTriggers(timer.getTimePerFrame());
        }
    }


    protected void registerInputTriggers() {

        // check if this example worries about input at all
        if (_logicalLayer == null) {
            return;
        }

        _controlHandle = FirstPersonControl.setupTriggers(_logicalLayer, new Vector3(0, 1, 0), true);


        _logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.ESCAPE), new TriggerAction()                       {

            public void perform(final Canvas source, final TwoInputStates inputState, final double tpf) {
                //exit();
            }
        }));

        _logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.L), new TriggerAction()                       {

            public void perform(final Canvas source, final TwoInputStates inputState, final double tpf) {
                _lightState.setEnabled(!_lightState.isEnabled());
                // Either an update or a markDirty is needed here since we did not touch the affected spatial directly.
                nodes.getRootNode().markDirty(DirtyType.RenderState);
            }
        }));

        /*_logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.NUMPAD0), new TriggerAction() {
            public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
                voxelworld.changeDetailLevel(0);
            }
        }));
         _logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.NUMPAD1), new TriggerAction() {
            public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
                voxelworld.changeDetailLevel(1);
            }
        }));
        _logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.NUMPAD2), new TriggerAction() {
            public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
                voxelworld.changeDetailLevel(2);
            }
        }));*/

        _logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.TAB), new TriggerAction() {
            public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
                voxelworld.nextActivePool();
            }
        }));

        _logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.T), new TriggerAction()                       {

            public void perform(final Canvas source, final TwoInputStates inputState, final double tpf) {
                voxelworld.changeWireframeState();
            }
        }));



        _logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.C), new TriggerAction()                       {

            public void perform(final Canvas source, final TwoInputStates inputState, final double tpf) {
                System.out.println("Camera: " + _canvas.getCanvasRenderer().getCamera());
            }
        }));

        _logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.NUMPADSUBTRACT), new TriggerAction()                       {

            public void perform(final Canvas source, final TwoInputStates inputState, final double tpf) {
                _controlHandle.setMoveSpeed(_controlHandle.getMoveSpeed() / 2);
            }
        }));
        _logicalLayer.registerTrigger(new InputTrigger(new KeyPressedCondition(Key.NUMPADADD), new TriggerAction()                       {

            public void perform(final Canvas source, final TwoInputStates inputState, final double tpf) {
                _controlHandle.setMoveSpeed(_controlHandle.getMoveSpeed() * 2);
            }
        }));




        final Predicate<TwoInputStates> clickLeftOrRight = Predicates.or(new MouseButtonClickedCondition(
                MouseButton.LEFT), new MouseButtonClickedCondition(MouseButton.RIGHT));

        _logicalLayer.registerTrigger(new InputTrigger(clickLeftOrRight, new TriggerAction()                       {

            public void perform(final Canvas source, final TwoInputStates inputStates, final double tpf) {
                System.err.println("clicked: " + inputStates.getCurrent().getMouseState().getClickCounts());
            }
        }));

        _logicalLayer.registerTrigger(new InputTrigger(new MouseButtonPressedCondition(MouseButton.LEFT),
                new TriggerAction()                       {

                    public void perform(final Canvas source, final TwoInputStates inputState, final double tpf) {
                        if (_mouseManager.isSetGrabbedSupported()) {
                            _mouseManager.setGrabbed(GrabbedState.GRABBED);
                        }
                    }
                }));
        _logicalLayer.registerTrigger(new InputTrigger(new MouseButtonReleasedCondition(MouseButton.LEFT),
                new TriggerAction()                       {

                    public void perform(final Canvas source, final TwoInputStates inputState, final double tpf) {
                        if (_mouseManager.isSetGrabbedSupported()) {
                            _mouseManager.setGrabbed(GrabbedState.NOT_GRABBED);
                        }
                    }
                }));

        _logicalLayer.registerTrigger(new InputTrigger(new AnyKeyCondition(), new TriggerAction()                       {

            public void perform(final Canvas source, final TwoInputStates inputState, final double tpf) {
                System.out.println("Key character pressed: "
                        + inputState.getCurrent().getKeyboardState().getKeyEvent().getKeyChar());
            }
        }));



    }
}
TOP

Related Classes of net.anzix.fsz.MainUpdater

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.