Home
Project period: 2018, for a short period of time.

Preface


On this page I'll explain how the following Systems work:
  • Hex connection
  • The system I made that assigns a tiles neighbours using raycasts in the Start function.
  • Astar system
  • The System I made using the A* Algorithm which determines the hexes a unit can move towards and what the cheapest cost is to move there. This uses the ProcesAStarPathfinding function and the AStarStep function.
  • Movement
  • This system checks if a tile can be accessed by a unit and if so goes through the needed steps to move a unit there. This uses the ProcesUnit function.

Hex connection

Your Image


I didn't want to make a system that forces a map to be a certain form, because of something like a grid system. Instead I decided to make a system that would allow any combination of hex placements both randomly generated or premade to work. Because I mainly made this so I could take a shot at making a movement system, there is no random generation. Its all premade. The class HexInfo has everything for the system. It has a list made out of gameobjects. I decided to make the list out of gameobjects, because it is easier in case I needed more out of a tile than just the HexInfo Class. The list gets filled with the tiles neighbours. The way it does this is by using a loop in the Start function to cast a raycast from all the hexes sides. This adds the first thing they hit (Neighbouring tiles) to the list.

Astar system

Gif missing


After selecting a unit the Astar system goes into effect. It will first ask the units tile to ask the surrounding tiles what the cost is. If this cost is lower than the amount of movement points on the unit, it will ask the same thing to its surrounding tiles. This continues until there are no tiles with a cost under the movementpoints left.

The system works using 2 functions: The ProcesAStarPathfinding function which Starts the chain. It also uses the AStarStep function, which requires a refrence to the units movement points and the current cost of moving to decide the pointCost. this will also decide if a unit can move to it. These combined with the gameobject list filled with the neighbouring tiles are all in the HexInfo class.

The system starts with the ProcesAStarPathfinding function which triggers a loop, taking a tile from the list of tiles and triggering the AStarStep function in the HexInfo class on the tile. It gives these functions the movement points that are left on the unit. This also gives a cost of 0, as the cost will always be 0 in the beginning of the chain. First the function will decide its pointCost, which is the total cost the unit needs to use to move to the tile. After that the function checks if the pointCost has already been touched. It does this by checking if the value is 0. This works because all tiles that have been touched will be above 0. If it has been touched, the function will check if the newPointCost is less than pointCost. In this case newPointCost will replace pointCost. If not, the if statement ends. From here on the function checks specifically if the newPointCost is the same as the movement points. It checks the newPointCost and not the pointCost variable to avoid a infinite loop. If it is the same value, it will set the canMoveTo boolean on true. This allows a unit to move towards that tile and changes its tiles color. If this is not true, it will check if its less than the movement point left. If the outcome is false, the if statement ends. When it is true, it will just act like the if statement before, setting the canMoveTo boolean on true and changing its tiles color. This will also trigger the same code as the loop ProcesAStarPathfinding triggers. After that it will continue by calculating the price to move and check if this price is equel or lower than the movement points. If any of these two if statements are true, it will also add the ResetTyle function to a event in a class called MovementManager. This is used later to reset the pointcost and color changes made to the tiles.

Movement

Gif missing


The movement system is located in the HexInfo class. It uses 2 functions: The OnMouseOver function to interact with the tile and The ProcesUnit function, which requires a reference to a gameobject (in this case a unit). This function is used to apply a newly instantiated unit to a tile or move a unit towards another tile. Besides this it also uses information and an event in the MovementManager Class.

The movement system works by hovering your mouse over a hexagon, giving the tile a new color. Depending on if the Astar system is working, it will show a different color. If you click the right mouse button on a tile with an active unit selected, it will trigger the ProcesUnit function. This gives the movingUnit(currently selected unit) whit it in the functions overload. This function first checks if the movingUnit variable in the movement manager is not empty. When it is, it will add the reference gameobject (a new unit) to the tile and the tile itself to the gameobject. If it is not empty, it will first check if the boolean canPass is true. This boolean is linked to the type of terrain. If it is not, it will end the if statement. Otherwhise it will move on and ask if canMoveTo is true. CanMoveTo was decided to be true or false by the Astar system and if it is false the function ends.

    The steps it preforms When boolean is true:
  1. Retract the pointcost from the units movementpoints.
  2. Change the units tile refrence from the tile it was on to the current tile.
  3. Adds the unit to the current tile.
  4. Moves the unit to the current tile.
  5. Uses the event in the MovementManager reseting all color and cost values from the subscribed tiles. This removes the tiles from the event afterwards.


Click Here to go to the Github