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);
        }
Esempio n. 2
0
 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));
        }
Esempio n. 5
0
 protected override void OnParentSet(IHexGridCell oldParent)
 {
     if (oldParent != null)
     {
         oldParent.RemoveComponent <UnselectableComponent>();
     }
     Parent.AddComponent(new UnselectableComponent());
 }
Esempio n. 6
0
 private bool SafeUpdateLocation(IHexGridCell location)
 {
     Monitor.Enter(nextLocationLock);
     try
     {
         return(UpdateLocation(location));
     }
     finally
     {
         Monitor.Exit(nextLocationLock);
     }
 }
Esempio n. 7
0
        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));
 }
Esempio n. 9
0
        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);
     }
 }
Esempio n. 11
0
        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));
        }
Esempio n. 12
0
        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);
        }
Esempio n. 13
0
        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();
        }
Esempio n. 14
0
 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);
 }
Esempio n. 15
0
        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;
            }
        }
Esempio n. 16
0
        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);
        }
Esempio n. 17
0
        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);
 }
Esempio n. 19
0
 protected virtual bool OccupiableLocation(IHexGridCell location) => !location.HasComponent <CreatureComponent>();
Esempio n. 20
0
        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>());
        }
Esempio n. 21
0
        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));
        }
Esempio n. 22
0
 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;
 }
Esempio n. 25
0
 /// <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);