Here is the code from my CubeEditor script which is attached
to the cubes (known as Waypoints in this game) in the world.
The most important part of this script is the
[ExecuteInEditMode] on line 2. It lets this script run before
you even press the play button. I thought that was pretty cool
because now it can run while in Edit Mode. So my code that
snaps the cubes to the grid will always run without me having
to anything.
My SnapToGrid() method does the grid snapping in just 2 lines.
On line 19, I am getting the value of the gridSize int
variable, which is set to 10 in my Waypoint script. This means
that 10f in the game's world is equal to 1 on the grid in the
game. So if a cube is positioned at these
coordinates(10,0,20), then we get (1,0,2) on the grid. Also,
since we are in a 3D world, we will use the z coordinate as
the y coordinate in a coordinate pair. This is because the y
in the 3D world moves the cube up/down so that cubes would be
on top/below each other instead of next to each other like on
a 2D axis of just x and y. So, (1,0,2) is just (1,2), where x
= 1 and y = 2.
The UpdateLabel() method just takes the cube's text mesh and
labels it according to it's grid position. This is so you can
see the coordinate of each cube in the world instead of having
to click on each one to see it's coordinates. It also names
the actual cube object by it's grid position. Of course, by
the time you are reading this, I have already hidden the
labels when preparing the game to be deployed, but you can the
labels in the gif above.
On line 62, we add the starting waypoint (assigned in Unity's
Inspector) to the queue.
We go into a while loop which keeps executing until we find
the end waypoint. On line 65, we remove the waypoint at the
beginning of the queue (which would be the starting waypoint
on the first execution).
Next we use the HaltIfEndFound() method to check if the
current waypoint is the end waypoint. If it is we set
isRunning to false to stop the while loop after it finsihes
the current execution.
On line 69 we use the ExploreNeighbors() method to iterate 4
times (1 for each direction: up, down, left, right) find any
waypoints and then add them to the queue. The code for this is
below.
After that we mark the current waypoint as explored on line 71
and keep executing the while loop until we find the end
waypoint.
Here is the code from my TowerFactory script that is
responsible for the ring buffer of towers. Note on line 49,
before putting the old tower back to the top of the queue it
is set to the new location (on line 46), which makes it the
most recently placed tower.
The snippet below is from my Tower script. My Shoot() method
on line 82 accesses the Particle System's Emission Module
(line 84), and then set it active or inactive depending what
bool is passed in.
My FireAtEnemy() method looks at the distance between the
enemy and tower (line 72) and uses the if statement on line 74
to determine if the tower should shoot that enemy or not. If
the enemy is within a specified range (determined by the
attackRange float in this script but not shown here) then we
pass in true into the Shoot() method to fire at the enemy,
otherwise we pass in false and the tower will not fire at the
enemy.