Example #1
0
        PlayerUnit(Gamestate oldState, GamestateBuilder newState)
        {
            Assert.Ref(oldState, newState);

            PlayerUnit oldUnit = oldState.PlayerUnit;

            texture = oldUnit.texture;

            InputDevice device = oldState.Input.Devices[oldUnit.inputDevice];
            RigidBody   body   = (RigidBody)(oldState.Physics.PhysicsBodies[oldUnit.rigidBody]);
            Sprite      spr    = (Sprite)(oldState.Video.Drawables[oldUnit.sprite]);

            float   x    = device.GetAxisFrac("Horizontal");
            float   y    = device.GetAxisFrac("Vertical");
            Vector2 move = new Vector2(x, y).ClampMagnitude(0, 1) * accel;

            body = body.AddImpulse(move);

            inputDevice = newState.Input.AddDevice(device);
            rigidBody   = newState.Physics.AddPhysicsObject(body);
            sprite      = newState.Video.AddDrawable(spr.Reposition(body.Position));
            newState.Audio.SetListenerPosition(body.Position);

            Window     window       = oldState.Video.Windows[0];
            Vector2    centerOffset = new Vector2(window.Rect.W / 2, window.Rect.H / 2);
            WorldPoint cameraPos    = body.Position.PixelTranslate(-centerOffset + new Vector2(16, 16));

            newState.Video.SetCamera(new Camera(cameraPos));
        }