Lab 5 - 2D Roguelike: Game
GitHub Classroom Assignment Page
This assignment is due on Friday 5/18.
Continue off of Lab 4 for this assignment.
This lab assignment is a bit larger than previous labs because there’s not really a good stopping point near the middle. It will count for 1.5x the typical lab points. Make sure to give enough time to complete it.
In this lab we will be completing the second two-thirds or so of the 2D Roguelike tutorial.
For Lab 5 we will complete these parts:
- Moving Object Script
- Creating Destructible Walls
- Player Animator Controller
- Writing the Player Script
- Writing the Enemy Script
- Enemy Animator Controller
- Adding UI & Level Transitions
There is a lot going on in these videos so it will most likely be necessary to compelte the lab by playing the videos and following along.
I will outline as much as I can, but mostly just to give an overview and maybe so that more experienced Unity developers can skip some parts of the videos.
Moving Object Script
- Write an abstract class for moving objects
Creating Destructible Walls
- Write the destructible wall script
- Select all 8 Wall prefabs and add the newly created Wall script to all of them
- Add the
DmgSprite
for each prefab using the damaged tiles in the tileset.- There are only 7 wall destruction tiles yet we have 8 prefabs
- Use the 5th tile for both Wall5 and Wall6
Player Animator Controller
- Add the
Player
prefab back to the hierarchy - Open the
Player
AnimationController - Open the Parameters tab and add a parameter of type Trigger called
playerChop
- Add “to” and “from” transitions between PlayerIdle and PlayerHit and between PlayerIdle and PlayerChop
- On the transition to PlayerChop, disable Has Exit Time and set Transition Offset to
0
- Also add the trigger
playerChop
- Also add the trigger
- On the transition from PlayerChop back to PlayerIdle, set Exit Duration to
1
and Transition Offset to0
- Repeat the above two steps for the PlayerHit transitions
- Test that the above worked by:
- Docking the Animation tab on the bottom of the editor
- Playing the game
- Clicking on each trigger in the Parameters list to manually trigger it
- The respective animations should play
Writing the Player Script
- Open the
GameManger
script- Add a field
public int playerFoodPoints
initialized to100
- Add a field
[HideInInspector] public bool playersTurn
initialized totrue
- Add a method
public void GameOver()
that simply setsenabled
tofalse
- Add a field
- Create and implement a Player script component
- Note that instead of
Application.LoadLevel(Application.LoadedLevel);
, you should callSceneManager.LoadScene (0);
- You’ll need to add
using UnityEngine.SceneManagement;
at the top of the file - Add to the Player prefab
- Set the script Blocking Layer to
BlockingLayer
- Note that instead of
Writing the Enemy Script
- Create and implement an Enemy script component
Enemy Animator Controller
- Open the
Enemy
AnimationController - Open the Parameters tab and add a parameter of type Trigger called
enemyAttack
- Add “to” and “from” transitions between Enemy1Idle and Enemy1Attack
- On the transition to Enemy1Attack, disable Has Exit Time and set Transition Offset to
0
- Also add the trigger
enemyAttack
- Also add the trigger
- On the transition from Enemy1Attack back to Enemy1Idle, set Exit Duration to
1
and Transition Offset to0
- You can test the
Enemy
AnimationController in the same way we tested thePlayer
AnimationController
- Open the
GameManager
script component- Add an enemy list and implement enemy movement
- Open the
Enemy
script- in the
Start()
method, call theGameManager
methodAddEnemyToList()
- In the
OnCantMove()
method, callanimator.SetTrigger("enemyAttack");
- in the
- Add the
Enemy
script to both enemy prefabs- Set the script Blocking Layer to
BlockingLayer
- Set the Player Damage of
Enemy1
to 10 - Set the Player Damage of
Enemy2
to 20
- Set the script Blocking Layer to
Adding UI & Level Transitions
- Create a UI → Canvas
- Create a UI → Image
- Set the Anchor (including position, holding Alt or Option) to stretch-stretch
- Set the Color to black
- Create a UI → Text and rename it to
LevelText
- Set the Anchor (including position, holding Alt or Option) to center-center
- Set Font to
PressStart2P-Regular
- Set Font Size to
32
- Set both Alignment modes to middle
- Set Horizontal and Vertical Overflow to
Overflow
- Set Color to white
- Set Text to
Day 1
- Copy the
LevelText
object and name the copyFoodText
- Set the Anchor (including position, holding Alt or Option) to bottom-center
- Set the Anchor Min Y to
0.05
- Set the Anchor Max Y to
0.05
- Set the Pos Y to
0
- Set Font Size to
24
- Set the Text to
Food 100
- Move the
FoodText
aboveLevelImage
in the list, and makeLevelText
a child ofLevelImage
- Implement level loading and UI in the
GameManager
script- Note that with the current version of Unity, the
OnLevelWasLoaded
method will no longer work Instead, follow these steps: - Add
using UnityEngine.UI;
to the top of the file - Remove the
InitGame();
call fromAwake()
- Set the initial value of
level
to0
- Add the following three methods:
- Note that with the current version of Unity, the
// This is called each time a scene is loaded.
void OnLevelFinishedLoading(Scene scene, LoadSceneMode mode)
{
// Add one to our level number.
level++;
// Call InitGame to initialize our level.
InitGame();
}
void OnEnable()
{
// Tell our 'OnLevelFinishedLoading' function to start listening for a scene change event as soon as this script is enabled.
SceneManager.sceneLoaded += OnLevelFinishedLoading;
}
void OnDisable()
{
// Tell our 'OnLevelFinishedLoading' function to stop listening for a scene change event as soon as this script is disabled.
// Remember to always have an unsubscription for every delegate you subscribe to!
SceneManager.sceneLoaded -= OnLevelFinishedLoading;
}
- Implement UI elements in the
Player
script -
Back in the editor, initialize the
Food Text
field of thePlayer
prefab - The game should now be playable, but without sound
- The tutorial series continues on at this point to add sound effects and music, as well as mobile controls, but we’re going to call it quits.