示例#1
0
        protected override void OnUpdate()
        {
            switch (_state)
            {
            case eGameState.Startup:
            {
                bool done = TryGenerateViewport();
                if (done)
                {
                    var playerEntity = _creatureLibrary.SpawnPlayer(EntityManager);
                    // Re-parent the camera on graphical to follow the character.
                    if (!GlobalGraphicsSettings.ascii)
                    {
                        Entities.WithAll <Camera2D>().ForEach((ref Parent parent) =>
                            {
                                parent.Value = playerEntity;
                            });
                    }

                    _dungeon = EntityManager.World.GetExistingSystem <DungeonSystem>();
                    MoveToTitleScreen(PostUpdateCommands);
                }
            }
            break;

            case eGameState.Title:
            {
                var input = EntityManager.World.GetExistingSystem <InputSystem>();
                if (input.GetKeyDown(KeyCode.H))
                {
                    MoveToHiScores(PostUpdateCommands);
                }
                else if (input.GetKeyDown(KeyCode.Space))
                {
                    MoveToInGame(PostUpdateCommands);
                }
            } break;

            case eGameState.InGame:
            {
                var input = EntityManager.World.GetExistingSystem <InputSystem>();
                if (input.GetKeyDown(KeyCode.I))
                {
                    MoveToInventoryScreen(PostUpdateCommands);
                }
            } break;

            case eGameState.Inventory:
            {
                var input = EntityManager.World.GetExistingSystem <InputSystem>();
                if (populateInventory)
                {
                    PopulateInventory();
                }
                if (input.GetKeyDown(KeyCode.Escape))
                {
                    MoveBackToGame(PostUpdateCommands);
                }
            } break;

            case eGameState.ReadQueuedLog:
            {
                var input = EntityManager.World.GetExistingSystem <InputSystem>();
                var log   = EntityManager.World.GetExistingSystem <LogSystem>();

                if (log.HasQueuedLogs())
                {
                    if (input.GetKeyDown(KeyCode.Space))
                    {
                        log.ShowNextLog(PostUpdateCommands);
                    }
                }
                else
                {
                    _state = eGameState.InGame;
                }
            } break;

            case eGameState.GameOver:
            {
                var input = EntityManager.World.GetExistingSystem <InputSystem>();
                if (input.GetKeyDown(KeyCode.Space))
                {
                    ResetPlayerAnimations();
                    MoveToTitleScreen(PostUpdateCommands);
                }
                else if (input.GetKeyDown(KeyCode.R))
                {
                    ResetPlayerAnimations();
                    MoveToReplay(PostUpdateCommands);
                }
            } break;

            case eGameState.NextLevel:
            {
                var input = EntityManager.World.GetExistingSystem <InputSystem>();
                var log   = EntityManager.World.GetExistingSystem <LogSystem>();

                GenerateLevel();
                log.AddLog("You descend another floor.");
                log.ShowNextLog(PostUpdateCommands);
                var tms = EntityManager.World.GetExistingSystem <TurnManagementSystem>();
                tms.NeedToTickDisplay = true;
                _state = eGameState.InGame;
            } break;

            case eGameState.HiScores:
            {
                var input = EntityManager.World.GetExistingSystem <InputSystem>();
                if (input.GetKeyDown(KeyCode.Space))
                {
                    MoveToTitleScreen(PostUpdateCommands);
                }
            } break;
            }
        }
        protected override void OnUpdate()
        {
            var tms = EntityManager.World.GetOrCreateSystem <TurnManagementSystem>();
            var gss = EntityManager.World.GetExistingSystem <GameStateSystem>();
            // Find player to navigate towards
            int2 playerPos = int2.zero;
            int  viewDepth = 0;

            Entities.WithAll <Player>().ForEach((Entity player, ref WorldCoord coord, ref Animated animated, ref Sight sight) =>
            {
                playerPos.x = coord.x;
                playerPos.y = coord.y;
                viewDepth   = sight.SightRadius;
            });

            // Move all creatures towards Player
            Entities.WithNone <PatrollingState>().WithAll <MeleeAttackMovement>()
            .ForEach((Entity creature, ref WorldCoord coord, ref Speed speed, ref Animated animated, ref TurnPriorityComponent pri) =>
            {
                if (tms.TurnCount % speed.SpeedRate == 0)
                {
                    int2 creaturePos = new int2(coord.x, coord.y);
                    int2 nextPos     =
                        AStarPathfinding.getNextStep(creaturePos, playerPos, gss.View, EntityManager);
                    Action movement = getDirection(creaturePos, nextPos);
                    tms.AddActionRequest(movement, creature, coord, animated.Direction, pri.Value);
                }
            });

            Entities.ForEach((Entity creature, ref WorldCoord coord, ref PatrollingState patrol, ref Speed speed, ref Animated animated, ref TurnPriorityComponent pri) =>
            {
                if (tms.TurnCount % speed.SpeedRate == 0)
                {
                    int2 monsterPos = new int2(coord.x, coord.y);
                    if (patrol.destination.Equals(new int2(0, 0)) || patrol.destination.Equals(monsterPos))
                    {
                        DungeonSystem ds   = EntityManager.World.GetExistingSystem <DungeonSystem>();
                        patrol.destination = ds.GetRandomPositionInRandomRoom();
                    }

                    EntityManager.SetComponentData(creature, patrol);
                    // Follow defined path now that we have ensured that one exists
                    int2 nextPos =
                        AStarPathfinding.getNextStep(monsterPos, patrol.destination, gss.View, EntityManager);
                    Action movement = getDirection(monsterPos, nextPos);
                    tms.AddActionRequest(movement, creature, coord, animated.Direction, pri.Value);
                }
            });

            View view = gss.View;

            bool[] blockedPosition = new bool[view.Height * view.Width];
            Entities.WithAll <BlockMovement>().ForEach((Entity e, ref WorldCoord coord) =>
            {
                int i = View.XYToIndex(new int2(coord.x, coord.y), view.Width);
                blockedPosition[i] = true;
            });

            // Monsters stop Patrolling and start actively following the Player if they can spot the Player
            Entities.WithAll <PatrollingState>().ForEach(
                (Entity e, ref WorldCoord coord, ref Sight sight) =>
            {
                int2 pos            = new int2(coord.x, coord.y);
                float totalDistance = math.sqrt(math.pow(math.distance(playerPos.x, pos.x), 2) +
                                                math.pow(math.distance(playerPos.y, pos.y), 2));

                if (totalDistance <= viewDepth && !FogOfWarSystem.SightBlocked(playerPos, pos, view, blockedPosition))
                {
                    PostUpdateCommands.RemoveComponent(e, typeof(PatrollingState));
                }
            });
        }