A look at the data and behaviour of the scripts in the 'Classic Project'.
There is no data stored in the Enemy class.
Behaviours:
- Destroy the gameObject if there is no Player in the scene.
- Set Health component to -1 if the gameObject's Position is not within the bounds defined in Settings.
- If possible, fire a shot by instantiating a 'Shot' at the current position, headed towards the Player's current position.
Data:
- Cooldown - a float to show how long an enemy needs to wait before they can shoot again.
There is no behaviour in this file.
Data:
- SpawnedEnemyCount - An int counting the number of Spawned Enemies.
- Cooldown - a float to show how long before another Enemy can be spawned.
- RandomState - A Random.State value to hold the current state of the Random Number Generator.
Behaviours:
Start
- Set initial values for the Data elements
- Copy the current RNG state. Call initState for the value '0xaf77' and store the resultant state in 'RandomState', copy the initial value back into Random.state.
Update
- RNG Copy as in Start.
- Decrease Cooldown by Time.deltaTime.
- If Cooldown <= 0
- Spawn a new enemy
- Set enemy positition using ComputeSpawnLocation
- Increase SpawnedEnemyCount.
- Reset Cooldown using ComputeCooldown.
- Reset RNG state.
ComputeCooldown
- Return a value to be used in resetting 'Cooldown'
ComputeSpawnLocation
- Set the transform position for an enemy to be a random point on the line y = settings.playfield.yMax, between x = settings.playfield.xMin and settings.playfield.xMax.
Data:
- Type - An enum with values:
- Enemy = 0
- Player = 1
- Value - A 'Type' variable.
There is no behaviour in this class/file.
Data:
- m_Value - a private float.
- Value - a float property.
- get - returns m_Value;
- set - Assign a value to m_Value, if this is <= 0 then destroy the GameObject.
Data:
- Speed - A float.
Behaviours:
- Move Transform2D.Position in the direction of Transform2D.Heading by an amount based on the Speed value and Time.delaTime.
Data:
- Move - A float2 to hold the Move input values.
- Shoot - A float2 to hold the Shoot input values.
- FireCooldown - A float for how long before we can shoot again.
- Fire - An auto-property bool determined by FireCooldown <=0 and math.length(Shoot).
Behaviours:
- Assign values to Move, Shoot and FireCooldown from Inputs and time passed.
- Update the player's Transform2D component.
- If Fire is true - fire a shot by instantiating a 'Shot' at the current position, in the direction indicated by the Shoot axes.
Data:
- TimeToLive - A float to determine the time a Shot has before it will be destroyed.
- Energy - How much damage the Shot inflicts upon hitting a target.
Behaviours:
Update
-
Destroy the gameObject if there are no Health objects in the scene.
-
Get the faction of the gameObject.
-
For all Health Objects in the scene.
-
Get the Health Object's faction, collisionRadius, Transform2D
-
If the faction is not equal to the gameObject's faction
- Use the square of the collisionRadius and the gameObjects Position to determine if a collision has occurred.
- If so, decrease the value of the Health Object by Energy, and destroy the gameObject.
-
-
Decrease TimeToLive by Time.deltaTime, if the result is <=0 then destroy the gameObject.
GetCollisionRadius(TwoStickExampleSettings settings, Faction.Type faction)
- If faction is Player return settings.playerCollisionRadius, else return settings.enemyCollisionRadius.
Data:
- ShotSpawnData - A Class to hold the following variables.
- Position - A float2 to indicate the 2D Position where the shot will be spawned.
- Heading - A float2 vector - the heading that a spawned shot will have.
- Faction - The faction a spawned shot will have.
Behaviours:
static class ShotSpawnSystem - static void SpawnShot(ShotSpawnData data)
- Use the faction from data to determine if we are spawning a PlayerShot prefab or a EnemyShot prefab.
- Instantiate the prefab.
- Assign the Position and Heading values from data to the prefab's Transform2D component. 4 Assign the Faction value from data to the prefab's Faction component.
Data:
- Position - A float2 vector to represent a 2D position.
- Heading - A float2 vector to represent a 2D heading.
Behaviours:
LateUpdate
- Update the gameObject's transform.position with the values in Position.
- Update the gameObject's rotation, using the values in Heading.
Data:
- Settings - A static reference to the TwoStickExampleSettings class.
Behaviours:
NewGame
- Instantiate a Player Object and give it a position and a heading.
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
- [Call this method after the scene has loaded](https://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html)
public static void InitializeWithScene()
- Find an object in the scene called "Settings" and assign this to the static variable Settings.
Data:
- playerMoveSpeed - float - The movement speed of the Player.
- playerFireCoolDown - float - The minimum time between successive shots by the Player.
- enemySpeed - float - The movement speed of an Enemy.
- enemyShootRate - float - The minimum time between successive shots by an Enemy.
- playerCollisionRadius - float - The hit box for the Player.
- enemyCollisionRadius - The hit box for an Enemy.
- playfield - Rect - The bounds for the game.
- PlayerShotPrefab - A prefab for the Player's shots.
- EnemyShotPrefab - A prefab for the Enemies' shots.
- PlayerPrefab - The Player prefab.
- EnemyPrefab - The Enemy prefab.
- EnemyFaction - The Enemy Faction prefab.
Data:
- m_CachedHealth - private float - The last known value for the Player's Health.
- NewGameButton - Button - A button to be shown at the start of the game.
- HealthText - Text - Shows the Player's Health value.
Behaviours:
Start
- Add a listener to NewGameButton to call TwoStickBootstrap.NewGame when clicked.
Update
- If there is a Player then call UpdateAlive, if not call UpdateDead
UpdateDead
- If it hasn't already been deactivated, deactivate HealthText.
- If it isn't active, activate NewGameButton.
UpdateAlive
- Activate HealthText and deactivate NewGameButton.
- If there is a player, get the health value and store in the var displayedHealth, if not, set displayedHealth to 0.
- If there is a difference between displayedHealth and m_CachedHealth.
- if displayedHealth > 0
- update HealthText to show the new value.
- if not set HealthText to "GameOver"
- update m_CachedHealth with displayHealth.
- if displayedHealth > 0