Esempio n. 1
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;
            }
        }
Esempio n. 2
0
 static public void Initialize()
 {
     s_singleton = new GameSystem();
 }
Esempio n. 3
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();
        }
        /// <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;
            }
        }