Ejemplo n.º 1
0
        public override void OnAddToEntity(GameEntity entity)
        {
            base.OnAddToEntity(entity);

            m_layoutC = Owner.FindComponent <LayoutComponent>();
            Debug.Assert(m_layoutC != null, "MinimapComponent depends on LayoutCompoment");

            m_modelC = Owner.FindComponent <ModelComponent>();
            Debug.Assert(m_modelC != null, "MinimapComponent depends on ModelCompoment");

            var minimapModel = m_modelC.ModelContext.DrawModel;

            m_targetMtl = minimapModel.NodeList[0].Material as MinimapMaterial;

            var  mapSys = MapSystem.GetInstance();
            Size size   = mapSys.GetBlockSize();

            m_progressInfos = new bool[size.Height, size.Width];
            for (int h = 0; h < size.Height; ++h)
            {
                for (int w = 0; w < size.Width; ++w)
                {
                    m_progressInfos[h, w] = false;
                }
            }
        }
Ejemplo n.º 2
0
        public override void OnAddToEntity(GameEntity entity)
        {
            base.OnAddToEntity(entity);

            m_behaviorC = Owner.FindComponent <ChrBehaviorComponent>();
            Debug.Assert(m_behaviorC != null, "PlayerInputCompoment depends on ChrBehaviorCompoment");

            // set first pose
            var pose = MapSystem.GetInstance().GetPose(m_currentLocation);

            m_behaviorC.Warp(pose);
        }
Ejemplo n.º 3
0
        public PlayerEntity()
            : base("player")
        {
            var entitySys = EntitySystem.GetInstance();
            var mapSys    = MapSystem.GetInstance();

            //var path = "Chr/c9000/test.blend";
            //var searchPath = "Chr/c9000";
            var path       = "Chr/c9100/c9100.blend";
            var searchPath = "Chr/c9100";
            var scene      = BlenderScene.FromFile(path);

            if (scene != null)
            {
                var drawModel = DrawModel.FromScene(path + "/draw", scene, searchPath);
                var animRes   = AnimResource.FromBlenderScene(path + "/anim", scene);
                //var debugModel = DrawModel.CreateTangentFrame(path + "/debug", scene);

                if (drawModel.BoneArray.Length != 0)
                {
                    var skeletonC = new SkeletonComponent(drawModel.BoneArray);
                    AddComponent(skeletonC);
                }

                var layoutC = new LayoutComponent();
                layoutC.Transform = Matrix.Identity;
                AddComponent(layoutC);

                //var markerC = new ModelMarkerComponent(scene);
                //AddComponent(markerC);

                var modelC = new ModelComponent(GameEntityComponent.UpdateLines.Draw);
                modelC.ModelContext.EnableCastShadow = true;
                modelC.ModelContext.DrawModel        = drawModel;
                //modelC.ModelContext.DebugModel = debugModel;
                AddComponent(modelC);

                var animC = new AnimComponent(animRes);
                AddComponent(animC);

                var behaviorC = new ChrBehaviorComponent();
                AddComponent(behaviorC);

                var minimapC = new MinimapComponent();
                AddComponent(minimapC);

                MapLocation startLocation = mapSys.GetStartInfo();
                //var inputC = new GodViewInputComponent();
                var inputC = new FpsInputComponent(startLocation);
                AddComponent(inputC);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Release all com resources.
        /// </summary>
        public void Dispose()
        {
            var mapSys = MapSystem.GetInstance();

            m_player.Dispose();
            mapSys.UnloadResource();
            Task.WaitAll(m_taskList.ToArray());
            m_numberEntity.Dispose();
            foreach (var model in m_drawModelList)
            {
                model.Dispose();
            }
        }
Ejemplo n.º 5
0
        static public void Dispose()
        {
            if (s_singleton == null)
            {
                return;
            }

            foreach (var entity in s_singleton.m_mapEntities)
            {
                entity.Dispose();
            }

            s_singleton.UnloadResource();


            s_singleton = null;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Update component
        /// </summary>
        /// <param name="dT">spend time [sec]</param>
        public override void Update(double dT)
        {
            var mapSys = MapSystem.GetInstance();
            var chrSys = ChrSystem.GetInstance();

            var currentLocation = mapSys.GetMapLocation(chrSys.Player);

            m_progressInfos[currentLocation.BlockY, currentLocation.BlockX] = true;            // check walked flag

            // set minimap material param
            Size blockSize = mapSys.GetBlockSize();
            var  mapTable  = new int[blockSize.Width * blockSize.Height];

            for (int h = 0; h < blockSize.Height; ++h)
            {
                for (int w = 0; w < blockSize.Width; ++w)
                {
                    if (!m_progressInfos[h, w])
                    {
                        // not-walked block is not displayed
                        mapTable[h * blockSize.Width + w] = 0;
                    }
                    else
                    {
                        var blockInfo = mapSys.GetBlockInfo(w, h);
                        int data      = blockInfo.CanWalkHalf()
                                                        ? 2             // walk half
                                                        : blockInfo.CanWalkThrough()
                                                                ? 1     // can walk
                                                                : 0;    // can not walk
                        mapTable[h * blockSize.Width + w] = data;
                    }
                }
            }

            // current position
            mapTable[currentLocation.BlockY * blockSize.Width + currentLocation.BlockX] = 3;

            m_targetMtl.SetMap(blockSize.Width, blockSize.Height, mapTable);
        }
Ejemplo n.º 7
0
 static public void Initialize()
 {
     s_singleton = new MapSystem();
 }
Ejemplo n.º 8
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();
        }
Ejemplo n.º 9
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;
            }
        }
Ejemplo n.º 10
0
        public void RenderFrame()
        {
            double dt = m_fps.GetDeltaTime();

            var drawSys    = DrawSystem.GetInstance();
            var cameraSys  = CameraSystem.GetInstance();
            var inputSys   = InputSystem.GetInstance();
            var entitySys  = EntitySystem.GetInstance();
            var mapSys     = MapSystem.GetInstance();
            var cullingSys = CullingSystem.GetInstance();

            // update fps
            {
                double avgDT = m_fps.GetAverageDeltaTime();
                string text  = String.Format("FPS:{0:f2}, DeltaTime:{1:f2}ms", 1.0 / avgDT, avgDT * 1000.0f);
                m_numberEntity.SetNumber(1.0f / (float)avgDT);
            }

            if (m_multiThreadCount > 1)
            {
                Task.WaitAll(m_taskList.ToArray());
                var tmpTaskResult = new List <CommandList>(m_taskResultList);

                inputSys.Update(dt);
                entitySys.UpdateComponents(GameEntityComponent.UpdateLines.Input, dt);
                entitySys.UpdateComponents(GameEntityComponent.UpdateLines.Behavior, dt);
                cameraSys.Update(dt);
                entitySys.UpdateComponents(GameEntityComponent.UpdateLines.PostBehavior, dt);

                DrawSystem.WorldData worldData;
                worldData.AmbientColor     = new Color3(0.4f, 0.45f, 0.55f);
                worldData.FogColor         = new Color3(0.3f, 0.5f, 0.8f);
                worldData.NearClip         = 0.01f;
                worldData.FarClip          = 100.0f;
                worldData.DirectionalLight = new DrawSystem.DirectionalLightData()
                {
                    Direction = new Vector3(0.3f, -0.2f, 0.4f),
                    Color     = new Color3(0.6f, 0.6f, 0.5f),
                };
                worldData.Camera = cameraSys.GetCameraData();

                drawSys.BeginScene(worldData);
                var context = drawSys.GetDrawContext();


                entitySys.UpdateComponents(GameEntityComponent.UpdateLines.Posing, dt);
                mapSys.Update(dt, context);
                cullingSys.UpdateFrustum();
                entitySys.UpdateComponents(GameEntityComponent.UpdateLines.PreDraw, dt);
                drawSys.GetDrawBuffer().Process(drawSys.GetDrawContext());
                entitySys.UpdateComponents(GameEntityComponent.UpdateLines.Draw, dt);

                // start command list generation for the next frame
                m_taskList.Clear();
                m_taskResultList.Clear();
                m_taskResultList.AddRange(Enumerable.Repeat <CommandList>(null, m_multiThreadCount));
                m_accTime += dt;
                for (int threadIndex = 0; threadIndex < m_multiThreadCount; ++threadIndex)
                {
                    int resultIndex = threadIndex;

                    var subThreadContext = drawSys.GetSubThreadContext(threadIndex);
                    m_taskList.Add(Task.Run(() =>
                    {
                        // todo : do sub-thread task
                        m_taskResultList[resultIndex] = subThreadContext.FinishCommandList();
                    }));
                }

                foreach (var result in tmpTaskResult)
                {
                    context.ExecuteCommandList(result);
                }

                m_numberEntity.SetPose(ChrSystem.GetInstance().Player.FindComponent <LayoutComponent>().Transform);
                m_numberEntity.Draw(context);
                drawSys.EndScene();
            }
            else
            {
                // not supported
            }

            m_fps.EndFrame();
            m_fps.BeginFrame();
        }
Ejemplo n.º 11
0
        public Scene(Device device, SwapChain swapChain, Panel renderTarget, HmdDevice hmd, bool bStereoRendering, int multiThreadCount)
        {
            var drawSys = DrawSystem.GetInstance();
            var mapSys  = MapSystem.GetInstance();

            // load textures
            var textures = new List <TextureView>(new[]
            {
                TextureView.FromFile("dot", drawSys.D3D, "Image/dot.png"),
                TextureView.FromFile("floor", drawSys.D3D, "Image/floor.jpg"),
            });
            var numTextures = new DrawSystem.TextureData[10];

            for (int i = 0; i < 10; ++i)
            {
                var name = String.Format("number_{0}", i);
                numTextures[i] = new DrawSystem.TextureData
                {
                    Resource = TextureView.FromFile(name, drawSys.D3D, String.Format("Image/{0}.png", name)),
                    UvScale  = Vector2.One,
                };
            }
            textures.AddRange(numTextures.Select(item => item.Resource));
            foreach (var tex in textures)
            {
                drawSys.ResourceRepository.AddResource(tex);
            }

            // create number entity
            m_fps          = new FpsCounter();
            m_numberEntity = new NumberEntity(new NumberEntity.InitParam()
            {
                Dot = new DrawSystem.TextureData
                {
                    Resource = drawSys.ResourceRepository.FindResource <TextureView>("dot"),
                    UvScale  = Vector2.One,
                },
                Numbers = numTextures,
                Layout  = Matrix.RotationYawPitchRoll(1.0f, -1.5f, MathUtil.PI) * Matrix.Translation(-1.5f, 2.5f, -4.5f)
            });

            // create map
            mapSys.LoadResource();
            mapSys.CreateMap("Level/l9000.tmx");

            // create player
            m_player = new PlayerEntity();
            ChrSystem.GetInstance().Player = m_player;

            m_multiThreadCount = multiThreadCount;
            m_taskList         = new List <Task>(m_multiThreadCount);
            m_taskResultList   = new List <CommandList>(m_multiThreadCount);

            // other settings
#if DEBUG
            CameraSystem.GetInstance().ActivateCamera(CameraSystem.FollowEntityCameraName);
            //CameraSystem.GetInstance().ActivateCamera(CameraSystem.FixedCameraName);
            //CameraSystem.GetInstance().ActivateCamera(CameraSystem.FreeCameraName);
#else
            CameraSystem.GetInstance().ActivateCamera(CameraSystem.FollowEntityCameraName);
#endif // DEBUG
        }