Пример #1
0
        private void ReadPreferences()
        {
            var preferencesPath = @"preferences.json";

            using (StreamReader sr = new StreamReader(preferencesPath))
            {
                GameLogger.Instance().LogDebug("Attempting to read preferences from path {@path}",
                                               Path.Combine(Environment.CurrentDirectory, preferencesPath)
                                               );
                string json = sr.ReadToEnd();
                _preferences = JsonConvert.DeserializeObject <Dictionary <string, object> >(json);
            }
        }
Пример #2
0
        private void EvaluateConditions(Entity entity, GameStateHelper gameStateHelper)
        {
            List <AiStateMachineNode> nodes = GetAllNodesForCurrentState();

            foreach (var node in nodes)
            {
                if (node.Condition(entity, gameStateHelper))
                {
                    CurrentState = node.PostState;
                    GameLogger.Instance().LogDebug($"Switching entity ${entity.Id} from state {node.PreState} to {node.PostState}");
                    return;
                }
            }
        }
Пример #3
0
        public GameSystemManager(MapFile _gameMap)
        {
            this._gameMap = _gameMap;
            Entities      = new List <Entity>();
            Systems       = new Dictionary <Type, GameSystem>();

            _sfmlWindow       = new RenderWindow(new VideoMode(620, 620), "CSConsoleRL");
            _entitiesToRemove = new List <Entity>();

            _mainEntity = new ActorEntity();

            GameLogger.Instance();

            CreateSystems();

            CreateEntities();

            InitializeSystems();

            MainGameLoop();
        }
Пример #4
0
        public List <Vector2i> Path(Tile[,] gameTiles, Vector2i start, Vector2i end)
        {
            var openPath = new List <PathfindingNode>()
            {
                new PathfindingNode(start.X, start.Y, 0, CalculateH(start.X, start.Y, end.X, end.Y), null)
            };
            var closedPath = new List <PathfindingNode>();

            var odd = -1; // Used to alternate between horizontal and vertical

            while (openPath.Count > 0)
            {
                odd *= -1;
                var current = openPath[0];

                // Find lowest value node
                foreach (var node in openPath)
                {
                    if (node.F < current.F)
                    {
                        current = node;
                    }
                }

                openPath.Remove(current);
                closedPath.Add(current);

                // Reached goal co-ordinates, build list back to start
                if (current.X == end.X && current.Y == end.Y)
                {
                    var path = new List <Vector2i>()
                    {
                        new Vector2i(current.X, current.Y)
                    };

                    var parent = current.Parent;
                    while (parent != null)
                    {
                        path.Add(new Vector2i(parent.X, parent.Y));
                        parent = parent.Parent;
                    }
                    // Remove last tile as is starting one
                    path.RemoveAt((path.Count - 1));
                    path.Reverse();

                    GameLogger.Instance().LogDebug($"openPath: ${openPath.Count}, closedPath: ${closedPath.Count}");

                    return(path);
                }

                for (int dir = (odd == 1 ? 0 : 3); odd == 1 ? dir <= 3 : dir >= 0; dir += odd)
                {
                    int x = 0, y = 0;
                    switch (dir)
                    {
                    case 0: // Above
                        x = current.X;
                        y = current.Y - 1;
                        if (y < 0)
                        {
                            continue;
                        }
                        break;

                    case 1: // Below
                        x = current.X;
                        y = current.Y + 1;
                        if (y >= gameTiles.GetLength(1))
                        {
                            continue;
                        }
                        break;

                    case 2: // Left
                        x = current.X - 1;
                        y = current.Y;
                        if (x < 0)
                        {
                            continue;
                        }
                        break;

                    case 3: // Right
                        x = current.X + 1;
                        y = current.Y;
                        if (x >= gameTiles.GetLength(0))
                        {
                            continue;
                        }
                        break;
                    }

                    if (GetExistingPathfindingNode(closedPath, x, y) != null)
                    {
                        continue;
                    }

                    if (!_tileDict[gameTiles[x, y].TileType].BlocksMovement)
                    {
                        var g = current.G + 1;
                        var h = CalculateH(x, y, end.X, end.Y);

                        // Check if node already exists in openList
                        var existing = GetExistingPathfindingNode(openPath, x, y);

                        // If it does but this has lower cost, update node
                        // Otherwise create new PathfindingNode
                        if (existing != null)
                        {
                            if (g + h < existing.F)
                            {
                                existing.Parent = current;
                                existing.G      = g;
                            }
                        }
                        else
                        {
                            var newNode = new PathfindingNode(x, y, g, h, current);
                            openPath.Add(newNode);
                        }
                    }
                }
            }

            GameLogger.Instance().LogDebug("PathfindingHelper returned null when trying to find path");
            return(null);
        }