Exemplo n.º 1
0
        public void Activate(ICamera oldCamera)
        {
            m_player = ChrSystem.GetInstance().Player;
            Debug.Assert(m_player != null, "player must not be null");

            m_isEnableHeadRotation = !GameSystem.GetInstance().Config.IsUseHmd;
        }
Exemplo n.º 2
0
        public void Update(double dt)
        {
            if (m_player == null)
            {
                return;
            }

            var layoutC = m_player.FindComponent <LayoutComponent>();

            Debug.Assert(layoutC != null, "");

            Matrix headRotTrans = Matrix.Identity;

            if (m_isEnableHeadRotation)
            {
                var gameSys = GameSystem.GetInstance();
                switch (gameSys.Config.InputDevice)
                {
                case GameConfig.UserInputDevices.MouseKeyboard:
                    break;

                case GameConfig.UserInputDevices.Pad:
                {
                    IPadInputSource src       = InputSystem.GetInstance().Pad;
                    var             thumbDir  = src.RightThumbInput.Direction;
                    float           magnitude = src.RightThumbInput.NormalizedMagnitude;
                    if (magnitude >= MinimumPadMagnitude)
                    {
                        float maxAngle = 0.9f;
                        m_headRotAngle.X = Math.Min(Math.Max(m_headRotAngle.X + thumbDir.Y * (float)dt, -maxAngle), maxAngle);
                        m_headRotAngle.Y = Math.Min(Math.Max(m_headRotAngle.Y + thumbDir.X * (float)dt, -maxAngle), maxAngle);
                        m_headRotAngle.Z = 0;
                    }
                    else
                    {
                        m_headRotAngle *= 0.9f;
                    }
                }
                break;
                }

                headRotTrans = Matrix.RotationYawPitchRoll(m_headRotAngle.Y, m_headRotAngle.X, m_headRotAngle.Z);
            }



            //var markerC = m_player.FindComponent<ModelMarkerComponent>();
            //Debug.Assert(markerC != null, "");
            //var mtx = markerC.FindMarkerMatrix(10) * layoutC.Transform;

            var mtx = headRotTrans * layoutC.Transform * Matrix.Translation(0, 1, 0);

            Vector3 eye, lookAt, up;

            eye      = mtx.TranslationVector + Vector3.UnitY;
            lookAt   = eye + mtx.Forward * Zoom;
            up       = Vector3.UnitY;
            m_camera = new DrawSystem.CameraData(eye, lookAt, up);
        }
Exemplo n.º 3
0
        private void _OnLoad(object sender, EventArgs e)
        {
#if DEBUG
            m_debugDialog = new DebugDialog();
            m_debugDialog.Show();
            var debugMenu = m_debugDialog.GetSystemMenuItem();
            DrawSystem.GetInstance().CreateDebugMenu(debugMenu);
            GameSystem.GetInstance().CreateDebugMenu(debugMenu);
            CameraSystem.GetInstance().CreateDebugMenu(debugMenu);
#endif // DEBUG
        }
Exemplo n.º 4
0
        /// <summary>
        /// Update component
        /// </summary>
        /// <param name="dT">spend time [sec]</param>
        public override void Update(double dT)
        {
            var cameraSys = CameraSystem.GetInstance();
            var drawSys   = DrawSystem.GetInstance();

            // Calc camera
            var viewHeadMat = cameraSys.GetCameraData().GetViewMatrix() * drawSys.GetDrawContext().GetHeadMatrix();

            viewHeadMat.Invert();
            var moveDirection = new Vector3(viewHeadMat.Backward.X, 0, viewHeadMat.Backward.Z);

            moveDirection.Normalize();

            var gameSys = GameSystem.GetInstance();

            switch (gameSys.Config.InputDevice)
            {
            case GameConfig.UserInputDevices.MouseKeyboard:
            {
                IMouseKeyboardInputSource src = InputSystem.GetInstance().MouseKeyboard;

                var v = Vector3.Zero;
                if (src.TestKeyState(Keys.W))
                {
                    v.Z += PositionFactor;
                }
                if (src.TestKeyState(Keys.A))
                {
                    v.X -= PositionFactor;
                }
                if (src.TestKeyState(Keys.D))
                {
                    v.X += PositionFactor;
                }
                if (src.TestKeyState(Keys.S))
                {
                    v.Z -= PositionFactor;
                }

                if (v.X != 0 || v.Z != 0)
                {
                    float angleY = (float)Math.Atan2(moveDirection.X, moveDirection.Z);
                    v = Vector3.Transform(v, Matrix3x3.RotationY(angleY));
                    m_behaviorC.RequestMove(v);
                    m_behaviorC.RequestTurn(v);
                }
            }
            break;

            case GameConfig.UserInputDevices.Pad:
            {
                IPadInputSource src = InputSystem.GetInstance().Pad;

                float angleY    = (float)Math.Atan2(moveDirection.X, moveDirection.Z);
                var   thumbDir  = src.LeftThumbInput.Direction;
                float magnitude = src.LeftThumbInput.NormalizedMagnitude;
                if (magnitude >= MinimumPadMagnitude)
                {
                    var v = new Vector3(thumbDir.X, 0, thumbDir.Y);
                    v = Vector3.Transform(v, Matrix3x3.RotationY(angleY));
                    v = v * magnitude;
                    m_behaviorC.RequestMove(v);
                    m_behaviorC.RequestTurn(v);
                }
            }
            break;

            default:
                Debug.Fail("unsupported input device type : " + gameSys.Config.InputDevice);
                break;
            }
        }
Exemplo n.º 5
0
        static void Main()
        {
            bool bStereoRendering = false; // change to 'false' due to non-stereo rendering for debug
            int  multiThreadCount = 4;     // 1 is single thread

            HmdDevice hmd = null;

            try
            {
                // init oculus rift hmd system
                HmdSystem.Initialize();
                var hmdSys = HmdSystem.GetInstance();
                hmd = hmdSys.DetectHmd();
            }
            catch (Exception)
            {
                // failed to detect hmd
                hmd = null;
            }

#if !DEBUG
            var configForm = new ConfigForm(hmd);
            Application.Run(configForm);
            if (configForm.HasResult())
            {
                bStereoRendering = configForm.GetResult().UseHmd;
            }
            else
            {
                // cancel
                return;
            }
#endif
            Size resolution = new Size();
            if (!bStereoRendering)
            {
                //resolution.Width = 1920;// Full HD
                //resolution.Height = 1080;
                resolution.Width  = 1280;
                resolution.Height = 720;
            }
            else
            {
                hmd.ResetPose();
                resolution = hmd.Resolution;
            }

            var form = new MainForm();
            form.ClientSize = resolution;

            // Create Device & SwapChain
            var desc = new SwapChainDescription()
            {
                BufferCount     = 2,
                ModeDescription =
                    new ModeDescription(resolution.Width, resolution.Height, new Rational(0, 1), DrawSystem.GetRenderTargetFormat()),
                IsWindowed        = true,
                OutputHandle      = form.GetRenderTarget().Handle,
                SampleDescription = new SampleDescription(1, 0),
                SwapEffect        = SwapEffect.Sequential,
                Usage             = Usage.RenderTargetOutput,
                Flags             = SwapChainFlags.AllowModeSwitch,
            };

            FeatureLevel[] levels =
            {
                FeatureLevel.Level_11_0
            };

            Device    device;
            SwapChain swapChain;
#if DEBUG
            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.Debug, levels, desc, out device, out swapChain);
#else
            Device.CreateWithSwapChain(DriverType.Hardware, DeviceCreationFlags.None, levels, desc, out device, out swapChain);
#endif

            // Ignore all windows events
            var factory = swapChain.GetParent <Factory>();
            factory.MakeWindowAssociation(form.Handle, WindowAssociationFlags.IgnoreAll);

            DrawSystem.Initialize(form.GetRenderTarget().Handle, device, swapChain, hmd, bStereoRendering, multiThreadCount);
            InputSystem.Initialize(form.GetRenderTarget());
            EntitySystem.Initialize();
            MapSystem.Initialize();
            ChrSystem.Initialize();
            CameraSystem.Initialize();
            CullingSystem.Initialize();
            GameSystem.Initialize();

            GameSystem.GetInstance().Config.IsUseHmd = bStereoRendering;
            var scene = new Scene(device, swapChain, form.GetRenderTarget(), hmd, bStereoRendering, multiThreadCount);
            RenderLoop.Run(form, () => { scene.RenderFrame(); });
            scene.Dispose();

            // Release
            GameSystem.Dispose();
            CullingSystem.Dispose();
            CameraSystem.Dispose();
            ChrSystem.Dispose();
            MapSystem.Dispose();
            EntitySystem.Dispose();
            InputSystem.Dispose();
            DrawSystem.Dispose();
            device.Dispose();
            swapChain.Dispose();
            HmdSystem.Dispose();
        }
Exemplo n.º 6
0
        /// <summary>
        /// Update component
        /// </summary>
        /// <param name="dT">spend time [sec]</param>
        public override void Update(double dT)
        {
            var mapSys = MapSystem.GetInstance();

            m_coroutine.Update(dT);

            if (!m_coroutine.HasCompleted())
            {
                // wait for end of character operation
                return;
            }

            var cameraSys = CameraSystem.GetInstance();
            var drawSys   = DrawSystem.GetInstance();

            var gameSys = GameSystem.GetInstance();

            switch (gameSys.Config.InputDevice)
            {
            case GameConfig.UserInputDevices.MouseKeyboard:
            {
                IMouseKeyboardInputSource src = InputSystem.GetInstance().MouseKeyboard;

                var v = Vector3.Zero;
                if (src.TestKeyState(Keys.W))
                {
                    v.Z += PositionFactor;
                }
                if (src.TestKeyState(Keys.A))
                {
                    v.X -= PositionFactor;
                }
                if (src.TestKeyState(Keys.D))
                {
                    v.X += PositionFactor;
                }
                if (src.TestKeyState(Keys.S))
                {
                    v.Z -= PositionFactor;
                }

                if (v.X != 0 || v.Z != 0)
                {
                    // @todo
                }
            }
            break;

            case GameConfig.UserInputDevices.Pad:
            {
                IPadInputSource src = InputSystem.GetInstance().Pad;

                // update move
                {
                    _InputType inputType = _InputType.None;

                    var   thumbDir  = src.LeftThumbInput.Direction;
                    float magnitude = src.LeftThumbInput.NormalizedMagnitude;
                    if (magnitude >= MinimumPadMagnitude)
                    {
                        // use pad stick
                        if (thumbDir.Y <= -0.851)
                        {
                            inputType = _InputType.Backward;
                        }
                        else if (thumbDir.Y >= 0.851)
                        {
                            inputType = _InputType.Forward;
                        }
                        else
                        {
                            if (thumbDir.X < 0)
                            {
                                inputType = _InputType.Left;
                            }
                            else
                            {
                                inputType = _InputType.Right;
                            }
                        }
                    }
                    else
                    {
                        // use pad cross key
                        if (src.TestButtonState(InputSystem.PadButtons.Down))
                        {
                            inputType = _InputType.Backward;
                        }
                        else if (src.TestButtonState(InputSystem.PadButtons.Up))
                        {
                            inputType = _InputType.Forward;
                        }
                        else if (src.TestButtonState(InputSystem.PadButtons.Left))
                        {
                            inputType = _InputType.Left;
                        }
                        else if (src.TestButtonState(InputSystem.PadButtons.Right))
                        {
                            inputType = _InputType.Right;
                        }
                    }

#if true
                    switch (inputType)
                    {
                    case _InputType.Forward:
                    {
                        var         nextDir      = MapLocation.GetForwardDirection(m_currentLocation.Direction);
                        MapLocation?nextLocation = mapSys.Walk(m_currentLocation, nextDir);
                        if (nextLocation.HasValue)
                        {
                            m_currentLocation = nextLocation.Value;
                            var pose = mapSys.GetPose(m_currentLocation);
                            m_coroutine.Start(new _MoveToTask(this, pose.TranslationVector));                                                            // no turn
                        }
                    }
                    break;

                    case _InputType.Backward:
                    {
                        var nextDir = MapLocation.GetBackwardDirection(m_currentLocation.Direction);
                        m_currentLocation.Direction = nextDir;
                        var pose = mapSys.GetPose(m_currentLocation);
                        m_coroutine.Start(new _TurnToTask(this, pose.Forward));
                    }
                    break;

                    case _InputType.Left:
                    {
                        var nextDir = MapLocation.GetLeftDirection(m_currentLocation.Direction);
                        m_currentLocation.Direction = nextDir;
                        var pose = mapSys.GetPose(m_currentLocation);
                        m_coroutine.Start(new _TurnToTask(this, pose.Forward));
                    }
                    break;

                    case _InputType.Right:
                    {
                        var nextDir = MapLocation.GetRightDirection(m_currentLocation.Direction);
                        m_currentLocation.Direction = nextDir;
                        var pose = mapSys.GetPose(m_currentLocation);
                        m_coroutine.Start(new _TurnToTask(this, pose.Forward));
                    }
                    break;
                    }
#else
                    switch (inputType)
                    {
                    case _InputType.Forward:
                    {
                        var         nextDir      = MapLocation.GetForwardDirection(m_currentLocation.Direction);
                        MapLocation?nextLocation = mapSys.Walk(m_currentLocation, nextDir);
                        if (nextLocation.HasValue)
                        {
                            m_currentLocation = nextLocation.Value;
                            var pose = mapSys.GetPose(m_currentLocation);
                            m_coroutine.Start(new _MoveToTask(this, pose.TranslationVector));                                                            // no turn
                        }
                    }
                    break;

                    case _InputType.Backward:
                    {
                        var         nextDir      = MapLocation.GetBackwardDirection(m_currentLocation.Direction);
                        MapLocation?nextLocation = mapSys.Walk(m_currentLocation, nextDir);
                        if (nextLocation.HasValue)
                        {
                            m_currentLocation = nextLocation.Value;
                            var pose = mapSys.GetPose(m_currentLocation);
                            m_coroutine.Start(Coroutine.Join(new _TurnToTask(this, pose.Forward), new _MoveToTask(this, pose.TranslationVector)));
                        }
                    }
                    break;

                    case _InputType.Left:
                    {
                        var         nextDir      = MapLocation.GetLeftDirection(m_currentLocation.Direction);
                        MapLocation?nextLocation = mapSys.Walk(m_currentLocation, nextDir);
                        if (nextLocation.HasValue)
                        {
                            m_currentLocation = nextLocation.Value;
                            var pose = mapSys.GetPose(m_currentLocation);
                            m_coroutine.Start(Coroutine.Join(new _TurnToTask(this, pose.Forward), new _MoveToTask(this, pose.TranslationVector)));
                        }
                    }
                    break;

                    case _InputType.Right:
                    {
                        var         nextDir      = MapLocation.GetRightDirection(m_currentLocation.Direction);
                        MapLocation?nextLocation = mapSys.Walk(m_currentLocation, nextDir);
                        if (nextLocation.HasValue)
                        {
                            m_currentLocation = nextLocation.Value;
                            var pose = mapSys.GetPose(m_currentLocation);
                            m_coroutine.Start(Coroutine.Join(new _TurnToTask(this, pose.Forward), new _MoveToTask(this, pose.TranslationVector)));
                        }
                    }
                    break;
                    }
#endif
                }
            }
            break;

            default:
                Debug.Fail("unsupported input device type : " + gameSys.Config.InputDevice);
                break;
            }
        }