Lab 8 Starting screen and Game Content
This is the final lab of this topic! As it is quite lengthy, you will likely not finish it in the practice session. Use the "development-problems" channel in discord to ask questions! (and also answer, as you can learn a lot by teaching others)
The deadline for this lab is in 2 weeks (Wednesday at 14.00).
Step 1. Data objects
As a starting point, we will create all the data objects that we are going to need to finish our task. The objects will be left mostly empty for now. More fields can be added as we go along.
ScenarioData This will be the highest level object. It should connect the unity scene with the rest of the game data.
- Create a new C# class
- Delete the start and update methods
- Change the parent type from MonoBehaviour to ScriptableObject
- Add a [CreateAssetMenu] attribute with the menuName parameter set to "TowerDefence/Scenario". (Or insert your own game name here)
- Add fields:
- A string for the unity scene name
- A string for the level name presented to the player
- Integers for lives and gold at the start of the level.
It should all look something like this in the end:
[CreateAssetMenu(menuName = "TowerDefence/Scenario")] public class ScenarioData : ScriptableObject { public string PresentedName; public string SceneName; public int Lives; public int StartingGold; }
EnemyData
- Create a new C# class
- Delete the start and update methods
- Change the parent type from MonoBehaviour to ScriptableObject
- Add a [CreateAssetMenu] attribute.
- Add fields for enemy starting health, damage to players' lives when they reach base, movement speed and the sprite they are represented with.
WaveData
- Create a new C# class
- Delete the start and update methods
- Remove the parent class type. This will be a regular C# class with no parent.
- Add a [System.Serializable] attribute to the class.
- Add a field for the number of enemies in the wave
- Add a field for the type of enemy
- Add a field for cooldown between enemies spawned within a wave.
- Add a list of waves to the scenario
Tower
- Improve the Tower Data by adding projectilePrefab and spawnCooldown to it.
- Use these variables in the Tower script to shoot different projectiles with different cooldowns
- Add a list of towers to the scenario
Create instances
- In editor create a Data folder
- In the data folder create separate folders for: Scenario, Enemy and Tower.
- Open the Scenario folder
- Create a Scenario data object
- Right click on project area inside the folder
- Choose create > TowerDefence > Scenario
- Fill in the data
Step 2. Level Selection
The view
- Create a new scene
- Add a new canvas to the scene
- On this canvas
- Add the name of your game
- A layout panel for the level selection
- An exit button
The presenter
- Add a new MenuPresenter script to the canvas. This script will be responsible for creating the level selection buttons and loading up levels.
- Make sure there is only a single instance of this menu script. In the Awake method stop more than a single instance of this object being created and call the DoNotDestroyOnLoad method on the gameobject of that instance.
- Add fields for the selected Scenario and a list for all possible scenarios.
- Create an exit game method and call it from the button.
- In the start method create a button for each scenario. It is helpful to create a prefab of this button first as we did in the previous lab for towers. If this is too complicated, you can also attach the prefabs manually in the editor.
- Create a StartScenario method that loads the level from selected scenario.
- Editor approach: Set each level button to load the scene based on an int index to the scenarios list.
- Scripting approach: Attach the scenario loading method with correct parameter to the button OnClick event when creating the button prefabs.
- Add a method for the Unity OnLevelWasLoaded(int level) message. In the method
- If this isn't the menu then hide the menu panel and pass the scenario to the ScenarioController.
- If this is the menu then show the main menu panel.
- For this all to work the scenes have to be also added to the Build settings in editor:
- Open the File> Build Settings
- Drag both scenes into the Scenes In Build area.
- Make sure the Menu is the first scene at the top. This is the scene that is loaded at the start of the game.
Returning to menu
- Add a new menu button to the level scene.
- In ScenarioController create a method ExitToMenu. In this method load the menu scene.
- Test that you are now able to move into and out from the levels.
Step 3. Connecting data with the prefabs
ScenarioController
- Add a field for the current scenario.
- Create a set scenario method that sets it.
- In the Start method:
- Set the lives and gold value based on the current scenario.
- Call the StartWave method of the Spawn script with the first wave as the argument.
- Create a WaveCompleted method
- If there are waves left in the current scenario, then call StartWave on the spawn(s).
- If the waves are completed, then call the WinLevel method.
Spawn and enemies
- Disable the Spawn script in the editor. It can be done using a tickbox in the inspector panel.
- Enable the script in the StartWave method.
- Modify the variables of the Spawn based on the values in the Wave object that came as a parameter in the StartWave method.
- Call WaveCompleted method when all enemies are spawned and there are no more enemies left in the game. We added a static field to keep track of the enemy count in the WaypointFollower class back in the first lab.
- In the update method change the values of the spawned enemy based on the Enemy data that is part of the wave data.
Towers
- Create at least 4 tower data objects
- Add a subset of the towers as a list to the scenario data objects
- In scenario controller Spawn an action key prefab for each of the towers
- In action key script
- Add a field to keep track of the type of tower it creates
- Set the graphic of the key to the same as the sprite inside the tower data object.
- Pass the Tower data object to the BuildTarget script
- In BuildTarget script
- Display the correct sprite based on tower data
- Modify the build method to correctly set the configurable values of instantiated tower prefab.
Step 4. Finishing up
- Playtest and make sure that the player can both win and lose a level.
- Add panels for victory and loss. Make sure that the game can't go on to win after a loss or vice versa.
- Create another unity scene with another level layout. You can duplicate the current level to get started.
- Add at least 4 different enemies to the game
- Have at least 2 different scenarios for both levels.
- Make one of the scenarios almost impossible to fail and one that can be completed with only small number of errors. Have the difficulty of the other two somewhere in the middle.
- Create a standalone build of the game.
- Test the build of the game.
- Submit the build of the game.
The deadline for this lab is in 2 weeks (Wednesday at 14.00).
8. Lab 6 & 7 & 8