Recap: Door States
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 41 of 64.
This chapter gave each state a class of its own, ran them with one StateMachine, and let every state decide its own way out. The door puts the three together.
Challenge
MediumFill in the castle door's five states in DoorStates.cs; Door.cs has the helpers.
- Locked:
door.PressedNear()logsLocked.Door.Unlock()changes a locked door toclosed. - Closed:
PressedNear()changes toopening. Opening: oncedoor.SlideTo(door.openY)is true,open. - Open: after
door.stayOpenseconds,closing. - Closing:
PressedNear()opens again; else onceSlideTo(door.closedY),closed.
Press Play, fetch the key and open the door. Then Check.
Try it yourself
using UnityEngine;
public class LockedState : IState
{
private Door door;
public LockedState(Door door)
{
this.door = door;
}
public void Enter()
{
}
public void Tick()
{
}
public void Exit()
{
}
}
public class ClosedState : IState
{
private Door door;
public ClosedState(Door door)
{
this.door = door;
}
public void Enter()
{
}
public void Tick()
{
}
public void Exit()
{
}
}
public class OpeningState : IState
{
private Door door;
public OpeningState(Door door)
{
this.door = door;
}
public void Enter()
{
}
public void Tick()
{
}
public void Exit()
{
}
}
public class OpenState : IState
{
private Door door;
public OpenState(Door door)
{
this.door = door;
}
public void Enter()
{
}
public void Tick()
{
}
public void Exit()
{
}
}
public class ClosingState : IState
{
private Door door;
public ClosingState(Door door)
{
this.door = door;
}
public void Enter()
{
}
public void Tick()
{
}
public void Exit()
{
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Game Architecture in Unity
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular TurretRecap: Twin Cannon4Serialized Data
Serializable ClassesLists in the InspectorEnums in the InspectorRecap: Loot TableRecap: Roll the Loot7State Machines
States as ClassesThe Alert StateA State MachineTransitionsEnemy BrainsThe Knight AttacksRecap: Door States2Interfaces in Unity
IDamageableInterfaces and TryGetComponentGetComponentsRecap: Interact KeyRecap: Nearest in ReachPractice on your own: Unity playground