private ITerrainComponent GetTileTerrain(IHexGridCell tile) { int height = GetTileHeight(tile); float waterLevel = GetTileWaterLevel(tile); ITerrainComponent terrain; if (height > Constants.mountainHeightThreshold) { bool isSnowy = waterLevel > Constants.snowMountainWaterLvlThreshold && height > Constants.snowMountainHeightThreshold; terrain = (isSnowy) ? (new SnowTerrainComponent() as ITerrainComponent) : new MountainTerrainComponent(); } else if (height < Constants.riverHeightThreshold && waterLevel > Constants.riverWaterLevelThreshold) { terrain = new RiverTerrainComponent(); } else { if (waterLevel > Constants.forestWaterLevelThreshold) { terrain = new ForestTerrainComponent(); } else if (waterLevel > Constants.plainsWaterLevelThreshold) { terrain = new GrasslandTerrainComponent(); } else { terrain = new DesertTerrainComponent(); } } terrain.Height = height; terrain.WaterLevel = waterLevel; return(terrain); }
public Node(IHexGridCell cell, Node parent, float g = 0, float h = 0) { Cell = cell; Parent = parent; G = g; H = h; }
public virtual List <IHexGridCell> GetPath(IHexGridCell currentLocation, int speed) { if (currentLocation != fixedLocation) { return(AStarModule.FindPath(currentLocation, fixedLocation, false)); } return(new List <IHexGridCell>()); }
private float GetTileWaterLevel(IHexGridCell tile) { Point <int> gridPoint = tile.Position.GridPoint; return((Mathf.Pow(((-gridPoint.X + (Constants.mapWidth / 2f)) / Constants.mapWidth) - 0.5f, 3) + 0.5f) * (Mathf.Pow(((-gridPoint.Y + (Constants.mapHeight / 2f)) / Constants.mapHeight) + 0.5f, 1 / 3f) + 0.75f) + ((float)fixedRandom.NextDouble() * 0.05f)); }
protected override void OnParentSet(IHexGridCell oldParent) { if (oldParent != null) { oldParent.RemoveComponent <UnselectableComponent>(); } Parent.AddComponent(new UnselectableComponent()); }
private bool SafeUpdateLocation(IHexGridCell location) { Monitor.Enter(nextLocationLock); try { return(UpdateLocation(location)); } finally { Monitor.Exit(nextLocationLock); } }
private static bool CellIsOccupiable(IHexGridCell cell, bool isSquad) { if (cell is null) { return(false); } CreatureComponent creatureComponent = cell.GetComponent <CreatureComponent>(); System.Type creatureType = isSquad ? typeof(EnemyMovementController) : typeof(Squads.SquadController); return(!cell.HasComponent <UnselectableComponent>() && (creatureComponent is null || creatureComponent.Creature.GetType() == creatureType)); }
public override List <IHexGridCell> GetPath(IHexGridCell currentLocation, int speed) { foreach (IHexGridCell cell in guardedCells) { CreatureComponent creatureComponent = cell.GetComponent <CreatureComponent>(); if (creatureComponent?.Creature?.GetType() == typeof(CreatureMovementController)) { chasing = creatureComponent; return(AStarModule.FindPath(currentLocation, creatureComponent.Creature.Location, false)); } } chasing = null; return(base.GetPath(currentLocation, speed)); }
private void OnDrawGizmosSelected() { if (creaturePath == null) { creaturePath = creature.GetType().GetField("path", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(creature); } IHexGridCell destination = (IHexGridCell)(creaturePath.GetType().GetProperty("Last", BindingFlags.Public | BindingFlags.Instance).GetValue(creaturePath)); if (destination == null) { destination = creature.Location; } Gizmos.color = Color.red; Gizmos.DrawCube(destination.Position.CenterAsVector3(boxSize.y / 2), boxSize); }
private void AddGuardedCells(IHexGridCell baseCell, int radius) { if (radius == 0) { if (!guardedCells.Contains(baseCell)) { guardedCells.Add(baseCell); } return; } foreach (IHexGridCell cell in baseCell.Neighbors) { AddGuardedCells(cell, radius - 1); } }
public List <IHexGridCell> GetPath(IHexGridCell currentLocation, int speed) { IHexGridCell newLocation; int iterations = 0; do { Point <int> start = currentLocation.Position.GridPoint; Point <int> randomOffset = new Point <int>(start.X - speed + Odds.DiceRoll(2 * speed), start.Y - speed + Odds.DiceRoll(2 * speed)); newLocation = currentLocation.ParentGrid[randomOffset]; iterations++; } while ((newLocation == null || newLocation.HasComponent <UnselectableComponent>()) && iterations < MAX_ITERATIONS); if (iterations == MAX_ITERATIONS) { return(new List <IHexGridCell>()); } return(AStarModule.FindPath(currentLocation, newLocation, false)); }
protected virtual void TraversePath() { if (distanceCanTravel == 0 || path.Count == 0) { targetPosition = null; return; } IHexGridCell nextCell = path.Dequeue(); if (!SafeUpdateLocation(nextCell)) { Debug.Log(debugName + ": Movement blocked going to " + nextCell.Position.GridPoint); return; } distanceCanTravel--; targetPosition = Location.Position.CenterAsVector3(height); }
public void GoTo(IHexGridCell location) { //Make A* call here IHexGridCell start = Location; if (wipePath) { path.Clear(); wipePath = false; } if (path.Count > 0) { start = path.Last; } foreach (IHexGridCell cell in AStarModule.FindPath(start, location)) { path.Enqueue(cell); } TraversePath(); }
protected virtual bool UpdateLocation(IHexGridCell location) { if (!OccupiableLocation(location)) { return(false); } if (Location != null) { AssertHelper.Assert(creatureComponent != null, "Missing creature component", this); bool success = Location.RemoveComponent(creatureComponent); AssertHelper.Assert(success, "Failed to remove creature component", this); } else { creatureComponent = new CreatureComponent(this); } Location = location; Location.AddComponent(creatureComponent); return(true); }
private void CreateBase(int x, int y, bool isPlayer, IDictionary <IHexGridCell, bool> baseTerrains) { IHexGridCell centerBaseTile = HexTiling[x, y]; centerBaseTile.RemoveComponent <CreepComponent>(); centerBaseTile.AddComponent((isPlayer) ? (new PlayerBaseComponent() as ICellComponent) : new EnemyBaseComponent()); baseTerrains.Add(centerBaseTile, isPlayer); foreach (IHexGridCell neighbor in HexTiling.CellNeighbors(centerBaseTile)) { baseTerrains.Add(neighbor, isPlayer); neighbor.RemoveComponent <CreepComponent>(); } if (isPlayer) { PlayerBase = centerBaseTile; } else { EnemyBase = centerBaseTile; } }
protected override bool UpdateLocation(IHexGridCell location) { CreatureComponent otherCreature = location.GetComponent <CreatureComponent>(); if (!(otherCreature is null) && !(otherCreature.Creature is EnemyMovementController && IsUserControlledFunction())) { return(false); } if (Location != null) { AssertHelper.Assert(creatureComponent != null, "Missing creature component", this); bool success = Location.RemoveComponent(creatureComponent); AssertHelper.Assert(success, "Failed to remove creature component", this); } else { creatureComponent = new CreatureComponent(this); } Location = location; Location.AddComponent(creatureComponent); return(true); }
protected override void OnClick(RaycastHit hit) { if (timeManager.IsTimeStepAdvancing) { return; } GameObject selectedObject = hit.collider.gameObject.transform.parent.gameObject; AssertHelper.Assert(selectedObject.name.Contains("hextile"), "Clicked on unexpected gameobject: " + selectedObject.name, this); IHexGridCell clickedTile = selectedObject.GetComponent <MonoHexGridCell>().HexGridCell; if (clickedTile.HasComponent <UnselectableComponent>()) { return; } OnHoverOutline onHoverOutline = hit.collider.gameObject.GetComponent <OnHoverOutline>(); if (onHoverOutline) { lastClicked = hit.collider.gameObject; onHoverOutline.OutlineColor = Color.cyan; } townCanvasController.DisplayedTown = null; ISelectionComponent selectionComponent = clickedTile.GetComponent <ISelectionComponent>(); if (!(selectionComponent is null)) { selectionComponent.Select(); } foreach (ITileSelectSubscriber subscriber in subscribers) { subscriber.OnTileSelect(clickedTile); } }
public GuardMovementStrategy(IHexGridCell locationToGuard, int guardRadius) : base(locationToGuard) { guardedCells = new List <IHexGridCell>(); AddGuardedCells(locationToGuard, guardRadius); }
protected virtual bool OccupiableLocation(IHexGridCell location) => !location.HasComponent <CreatureComponent>();
public static List <IHexGridCell> FindPath(IHexGridCell startCell, IHexGridCell destinationCell, bool isSquad = true) { List <Node> openList = new List <Node>(); List <Node> closedList = new List <Node>(); Node startNode = new Node(startCell, null); Node destinationNode = new Node(destinationCell, null); if (!CellIsOccupiable(destinationCell, isSquad)) { return(new List <IHexGridCell>()); } openList.Add(startNode); while (openList.Count > 0) { Node currentNode = openList[0]; foreach (Node thisNode in openList) { if (thisNode.F < currentNode.F) { currentNode = thisNode; } } openList.Remove(currentNode); Queue <Node> successors = GenerateChildren(currentNode, destinationNode, isSquad); while (successors.Count > 0) { Node successor = successors.Dequeue(); if (successor.IsSameCell(destinationNode)) { return(GeneratePath(successor)); } bool dontAdd = false; foreach (Node openNode in openList) { if (successor.IsSameCell(openNode) && openNode.F < successor.F) { dontAdd = true; break; } } if (!dontAdd) { foreach (Node closedNode in closedList) { if (successor.IsSameCell(closedNode) && closedNode.F < successor.F) { dontAdd = true; break; } } } if (!dontAdd) { openList.Add(successor); } } closedList.Add(currentNode); } return(new List <IHexGridCell>()); }
private int GetTileHeight(IHexGridCell tile) { Point <int> gridPoint = tile.Position.GridPoint; return(Mathf.FloorToInt(Mathf.PerlinNoise((gridPoint.X * Constants.hexRadius) / 25f + 1000, (gridPoint.Y * Constants.hexRadius) / 25f + 1000) * 30)); }
private static string TileName(IHexGridCell tile) { return(string.Format("hextile_{0}_{1}", tile.Position.GridPoint.X, tile.Position.GridPoint.Y)); }
public List <IHexGridCell> GetPath(IHexGridCell currentLocation, int speed) { return(AStarModule.FindPath(currentLocation, chasing.Location, false)); }
public FixedLocationMovementStrategy(IHexGridCell location) { fixedLocation = location; }
/// <summary> /// Returns the 6 cells surrounding a cell /// </summary> /// <param name="cell">The position of the cell</param> /// <returns>The 6 cells surrounding a cell</returns> public IHexGridCell[] CellNeighbors(IHexGridCell cell) => CellNeighbors(cell.Position.GridPoint);