đž The Enemies
The GameManager
node
The GameManager
node is very important as it's responsible for the spawning of the enemies thanks to the WaveFactory
component. It keeps also track of the score thanks to the scoreKeeper
components that import the scoreManager
instance.
GameManager
ââ WaveFactory
â ââ Wave Prefab
â â ââ EnemyFactory
â â ââ ...
â â ââ Enemy
â â ââ Enemy
â ââ ...
â ââ Wave Prefab
â ââ Wave Prefab
ââ ScoreKeeper
The WaveFactory
component
The WaveFactory.ts
is responsible for spawning waves of enemies. It can loop through an array of Wave
prefabs. When the scene starts the script creates a generator that yields all the waves. The wave instantiates the enemies and then after a given amount of time it instantiates the next one.
The Wave
prefab
The Wave
prefab is a container for enemies. It contains just one component, EnemyFactory
, the script that instantiates the enemies and lets them follow a given path in the scene.
The EnemyFactory
component
The EnemyFactory.ts
script handles the instantiation of the enemies (like WaveFactory
does for waves, but this time the prefab is an Enemy
node). It accepts an array of Enemy
prefabs so that you can have different enemies in the same wave. It also accepts a Path
prefab, so that every wave can have its path. It's time now to see how the Enemy
prefab works.
The Enemy
prefab
The Enemy prefab has 8 components, 3 of them are built-in components, and the others are custom scripts. It is very similar to the Player
node and we already discussed most of its components. It doesn't have PlayerController
script but it has Enemy
and Score
components.
Enemy
ââ cc.UITransform
ââ cc.CircleCollider2D
ââ cc.RigidBody2D
ââ Enemy.ts
ââ Health.ts
ââ DamageDealer.ts
ââ Shooter.ts
ââ Score.ts
The Enemy
component
The Enemy.ts
script is responsible for the enemy movement. It has the followPath
method that iterates through some waypoints (waypoints are in the Path
prefab but we'll get to that later) and it uses the cc.tween
API to move the enemy from one waypoint to the next. When the enemy reaches the last waypoint which is outside of the camera boundaries it is destroyed.
The Score
component
The Score.ts
script is responsible for adding points to the score when the enemy is destroyed. It has a score
property that is set by the inspector. When the enemy is destroyed the ScoreKeeper
component is notified and the score is updated.
The Path
prefab
The Path
prefab is a container for the waypoints. Waypoints are just regular nodes positioned in the scene. The Enemy
component uses the Path
component (it gets it from its parent, the Wave
prefab) to follow the waypoints, giving the illusion of a path.
Path
ââ Waypoint-001
ââ Waypoint-002
ââ Waypoint-003
ââ Waypoint-004
ââ Waypoint-005
ââ Waypoint-006
ââ Waypoint-007
ââ Waypoint-008
Paths can be very complex like the one in the image below that is used for the boss.
Wrapping up
So this is it, we have now all the pieces to build a shoot 'em up game. I hope you enjoyed this tutorial and that you learned something new. Stay tuned for the next article!