private static void CreateOneRoomNodes(IMap map, HashSet <string> edgeHash, Room room) { for (var x = 0; x < room.Width; x++) { for (var y = 0; y < room.Height; y++) { var nodeX = x + room.PositionX * ROOM_CELL_SIZE; var nodeY = y + room.PositionY * ROOM_CELL_SIZE; var node = new HexNode(nodeX, nodeY); room.Nodes.Add(node); map.Nodes.Add(node); var neighbors = HexNodeHelper.GetNeighbors(node, room.Nodes); foreach (var neighbor in neighbors) { var isExists = IsExistsEdge(edgeHash, node, neighbor); if (!isExists) { AddEdgeToMap(map, edgeHash, node, neighbor); } } } } }
private void CreateOneRoomNodes(ISectorMap map, HashSet <string> edgeHash, Room room, Size cellSize) { var interiorObjects = _randomSource.RollInteriorObjects(room.Width, room.Height); for (var x = 0; x < room.Width; x++) { for (var y = 0; y < room.Height; y++) { var nodeX = x + room.PositionX * cellSize.Width; var nodeY = y + room.PositionY * cellSize.Height; var isObstacle = false; var interiorObjectForCoords = interiorObjects .SingleOrDefault(o => o.Coords.CompsEqual(x, y)); if (interiorObjectForCoords != null) { //TODO Сделать так, чтобы укрытия не генерировались на узлах с выходами // Как вариант - если выбираем узел, как выход, то снимаем флаг укрытия. // Вообще, нужно поискать алгоритмы, которые бы расставляли укрытия и выходы, оставляя комнату проходимой. isObstacle = true; } var node = new HexNode(nodeX, nodeY, isObstacle); room.Nodes.Add(node); map.AddNode(node); var neighbors = HexNodeHelper.GetSpatialNeighbors(node, room.Nodes); foreach (var neighbor in neighbors) { var isExists = IsExistsEdge(edgeHash, node, neighbor); if (!isExists) { AddEdgeToMap(map, edgeHash, node, neighbor); } } } } // создаём переходы, если они есть в данной комнате if (room.Transitions.Any()) { //TODO Отфильтровать узлы, которые на входах в коридор var availableNodes = room.Nodes.Where(x => !x.IsObstacle); var openRoomNodes = new List <HexNode>(availableNodes); foreach (var transition in room.Transitions) { var transitionNode = _randomSource.RollTransitionNode(openRoomNodes); map.Transitions.Add(transitionNode, transition); openRoomNodes.Remove(transitionNode); } } }
/// <summary> /// Получение точек патруля, которые можно обходить. /// </summary> /// <returns> Набор узлов карты. </returns> private HexNode[] CalcActualRoutePoints() { var hexNodes = _patrolRoute.Points.Cast <HexNode>(); var actorHexNode = (HexNode)Actor.Node; var actualRoutePoints = from node in hexNodes where !HexNodeHelper.EqualCoordinates(node, actorHexNode) select node; return(actualRoutePoints.ToArray()); }
private IMapNode CalcNearbyPatrolPoint(IEnumerable <HexNode> routePoints) { var targets = routePoints; var node = (HexNode)_actor.Node; var nearbyNode = HexNodeHelper.GetNearbyCoordinates(node, targets); if (nearbyNode == null) { throw new InvalidOperationException("Ближайший узел не найден."); } return(nearbyNode); }
private int?CalcCurrentPatrolPointIndex() { int?currentIndex = null; for (var i = 0; i < _patrolRoute.Points.Count(); i++) { var routeNode = (HexNode)_patrolRoute.Points[i]; var actorNode = (HexNode)_actor.Node; if (HexNodeHelper.EqualCoordinates(routeNode, actorNode)) { currentIndex = i; break; } } return(currentIndex); }
private static void CreateEdges(IMap map) { foreach (var node in map.Nodes) { var currentNode = (HexNode)node; var nodes = map.Nodes.Cast <HexNode>().ToArray(); var neighbors = HexNodeHelper.GetSpatialNeighbors(currentNode, nodes); foreach (var neighbor in neighbors) { var existsNeibors = map.GetNext(node); if (!existsNeibors.Contains(neighbor)) { map.AddEdge(node, neighbor); } } } }
public void GetNeighborsTest() { // ARRANGE var nodes = new List <HexNode>(); for (var i = 0; i < 10; i++) { for (var j = 0; j < 10; j++) { nodes.Add(new HexNode(i, j)); } } var testedNode = nodes.SelectBy(3, 3); var expectedNeighbors = new[] { nodes.SelectBy(2, 3), nodes.SelectBy(3, 4), nodes.SelectBy(4, 4), nodes.SelectBy(4, 3), nodes.SelectBy(4, 2), nodes.SelectBy(3, 2) }; // ACT var factNeighbors = HexNodeHelper.GetSpatialNeighbors(testedNode, nodes.ToArray()); // ASSERT for (var i = 0; i < 6; i++) { factNeighbors[i].OffsetX.Should().Be(expectedNeighbors[i].OffsetX); factNeighbors[i].OffsetY.Should().Be(expectedNeighbors[i].OffsetY); } factNeighbors.Should().BeEquivalentTo <HexNode>(expectedNeighbors); }
public static void AddAllNeighborToMap(IMap map, HashSet <string> edgeHash, Room room, HexNode node) { if (room is null) { throw new System.ArgumentNullException(nameof(room)); } var neighbors = HexNodeHelper.GetSpatialNeighbors(node, room.Nodes); foreach (var neighbor in neighbors) { var isExists = IsExistsEdge(edgeHash, node, neighbor); if (!isExists) { AddEdgeToMap(map, edgeHash, node, neighbor); } } }
/// <summary> /// Возвращает доступные соседние узлы карты с учётом обхода соседей по часовой стрелке. /// </summary> /// <param name="current"> Текущий узел. </param> /// <param name="map"> Карта, на которой проводится проверка. </param> /// <returns> Возвращает список соседних узлов, соединённых ребрами с текущим. </returns> private IMapNode[] GetAvailableNeighbors(IMapNode current, IMap map) { var hexCurrent = (HexNode)current; var hexNodes = map.Nodes.Cast <HexNode>().ToArray(); var neighbors = HexNodeHelper.GetNeighbors(hexCurrent, hexNodes); var currentEdges = from edge in map.Edges where edge.Nodes.Contains(current) select edge; var currentEdgeArray = currentEdges.ToArray(); var actualNeighbors = new List <HexNode>(); foreach (var testedNeighbor in neighbors) { var edge = currentEdgeArray.SingleOrDefault(x => x.Nodes.Contains(testedNeighbor)); if (edge == null) { continue; } if (_context.TargetNode == null) { if (!map.IsPositionAvailableFor(testedNeighbor, _context.Actor)) { continue; } } else { if (_context.TargetNode != testedNeighbor && !map.IsPositionAvailableFor(testedNeighbor, _context.Actor)) { continue; } } actualNeighbors.Add(testedNeighbor); } return(actualNeighbors.ToArray()); }
private void CreateEdges(IMap map) { foreach (var node in map.Nodes) { var currentNode = (HexNode)node; var nodes = map.Nodes.Cast <HexNode>().ToArray(); var neighbors = HexNodeHelper.GetNeighbors(currentNode, nodes); foreach (var neighbor in neighbors) { var currentEdge = GetExistsEdge(map, (HexNode)node, neighbor); if (currentEdge != null) { continue; } AddEdgeToMap(map, (HexNode)node, neighbor); } } }
public void Move1() { var sectorManager = _container.GetInstance <ISectorManager>(); var playerState = _container.GetInstance <ISectorUiState>(); var moveCommand = _container.GetInstance <ICommand>("move-command"); var schemeService = _container.GetInstance <ISchemeService>(); var humanPlayer = _container.GetInstance <HumanPlayer>(); var actorManager = _container.GetInstance <IActorManager>(); var humanActorTaskSource = _container.GetInstance <IHumanActorTaskSource>(); var commandManger = _container.GetInstance <ICommandManager>(); for (var i = 0; i < 1; i++) { var currentActorNode = (HexNode)playerState.ActiveActor.Actor.Node; var nextNodes = HexNodeHelper.GetSpatialNeighbors(currentActorNode, sectorManager.CurrentSector.Map.Nodes.Cast <HexNode>()); var moveTargetNode = nextNodes.First(); playerState.HoverViewModel = new TestNodeViewModel { Node = moveTargetNode }; commandManger.Push(moveCommand); ICommand command = null; do { command = commandManger.Pop(); try { command?.Execute(); } catch (Exception exception) { throw new InvalidOperationException($"Не удалось выполнить команду {command}.", exception); } } while (command != null); } }
public void Move1() { var sectorManager = _serviceProvider.GetService <ISectorManager>(); var playerState = _serviceProvider.GetService <ISectorUiState>(); var moveCommand = _serviceProvider.GetService <MoveCommand>(); var commandManger = _serviceProvider.GetService <ICommandManager>(); for (var i = 0; i < 1; i++) { var currentActorNode = (HexNode)playerState.ActiveActor.Actor.Node; var nextNodes = HexNodeHelper.GetSpatialNeighbors(currentActorNode, sectorManager.CurrentSector.Map.Nodes.Cast <HexNode>()); var moveTargetNode = nextNodes.First(); playerState.SelectedViewModel = new TestNodeViewModel { Node = moveTargetNode }; commandManger.Push(moveCommand); ICommand command; do { command = commandManger.Pop(); try { command?.Execute(); } catch (Exception exception) { throw new InvalidOperationException($"Не удалось выполнить команду {command}.", exception); } } while (command != null); } }
public async System.Threading.Tasks.Task MoveCommandTestAsync() { var sectorManager = _container.GetInstance <ISectorManager>(); var playerState = _container.GetInstance <ISectorUiState>(); var moveCommand = _container.GetInstance <ICommand>("move-command"); var schemeService = _container.GetInstance <ISchemeService>(); var humanPlayer = _container.GetInstance <HumanPlayer>(); var actorManager = _container.GetInstance <IActorManager>(); var humanActorTaskSource = _container.GetInstance <IHumanActorTaskSource>(); var commandManger = _container.GetInstance <ICommandManager>(); var sectorGenerator = _container.GetInstance <ISectorGenerator>(); var locationScheme = new TestLocationScheme { SectorLevels = new ISectorSubScheme[] { new TestSectorSubScheme { RegularMonsterSids = new[] { "rat" }, RegionCount = 20, RegionSize = 20, IsStart = true, ChestDropTableSids = new[] { "default" }, RegionChestCountRatio = 9 } } }; var globeNode = new GlobeRegionNode(0, 0, locationScheme); humanPlayer.GlobeNode = globeNode; await sectorManager.CreateSectorAsync(); var personScheme = schemeService.GetScheme <IPersonScheme>("human-person"); var playerActorStartNode = sectorManager.CurrentSector.Map.Regions .SingleOrDefault(x => x.IsStart).Nodes .First(); var playerActorVm = CreateHumanActorVm(humanPlayer, personScheme, actorManager, playerActorStartNode); //Лучше централизовать переключение текущего актёра только в playerState playerState.ActiveActor = playerActorVm; humanActorTaskSource.SwitchActor(playerState.ActiveActor.Actor); var currentActorNode = (HexNode)playerState.ActiveActor.Actor.Node; var nextNodes = HexNodeHelper.GetSpatialNeighbors(currentActorNode, sectorManager.CurrentSector.Map.Nodes.Cast <HexNode>()); var moveTargetNode = nextNodes.First(); var nodeViewModel = new TestNodeViewModel { Node = moveTargetNode }; playerState.HoverViewModel = nodeViewModel; playerState.SelectedViewModel = nodeViewModel; commandManger.Push(moveCommand); ICommand command = null; do { command = commandManger.Pop(); try { command?.Execute(); } catch (Exception exception) { throw new InvalidOperationException($"Не удалось выполнить команду {command}.", exception); } } while (command != null); }