Lock
A class in Root.Township, Alta.Inventory.
A lock that opens when its matching key prefab is pushed in and turned far enough. Used on chests (Chest) and subclassed by ConsumableLock; the server decides when it unlocks and syncs the state.
- Listed in Items, inventory and logic
- Inherits or implements
NetworkEntityBehaviour - Derived types
ConsumableLock,LockEffectUnityEvent,LockEvent,LockItemEvent,LockUnityEvent - Documented in Items, interaction and damage
Constructors
| Member | What it is |
|---|---|
Lock() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
| Member | What it is |
|---|---|
protected KeyItem currentKey | The key currently inserted, or null. |
protected bool hasKey | Whether a key is held in the lock by its joint. |
protected ConfigurableJoint keyJoint | The joint holding the inserted key; breaks when the player pulls their hand too far away. |
protected NetworkRigidbody rigidBody | The lock's NetworkRigidbody, cached on Awake. |
20 private and internal fields
| Member | What it is |
|---|---|
private Collider[] collidersToIgnore | Private: the lock's colliders, also exposed as Colliders. |
private Coroutine coolDownCoroutine | Private: the running grab cooldown. |
private uint correctKeyHash | Private: prefab hash a key must have to fit this lock. |
private NetworkPrefab correctKeyPrefab | The key prefab that fits this lock. |
private float delayBetweenUnlockingAndOpening | Seconds between the key turning and the lock opening. Defaults to 1. |
private float distanceToBreakJoint | How far the hand can be from the held key before the key is pulled out. Defaults to 0.2. |
private float dotToUnlock | The key must turn past this angle (stored as a dot product) to unlock. |
private bool isDetachedLock | Backing field for IsDetachedLock. A detached lock switches layer so players can't touch it while locked. |
private bool isLocked | Backing field for IsLocked. |
private LockEvent isLockedEvent | Backing field for IsLockedEvent. |
private bool isOutOfCooldown | Private: false for a second after the key is grabbed, which blocks unlocking. |
private KeyItemEvent keyTurnEvent | Backing field for KeyTurnEvent. |
private Transform lockPosition | Where the key tip sits and which way it faces when inserted. |
private int nonInteractingLayer | Private: the NotInteractingWithPlayerBody layer used while a detached lock is locked. |
private int originalLayer | Private: the lock's own layer, restored when unlocked. |
private KeyItemEvent receivedNewKeyEvent | Backing field for ReceivedNewKeyEvent. |
private float sqrBreakDistance | Private: distanceToBreakJoint squared. |
private MethodSync syncBreakKeyGrip | Private: message for SyncBreakKeyGrip. |
private MethodSync syncBreakKeyJoint | Private: message for SyncBreakKeyJoint. |
private MethodSync syncLocked | Private: message for SyncLocked, also sent to players as they connect. |
Properties
| Member | What it is |
|---|---|
Collider[] Colliders { get; } | All colliders under the lock, ignored against the key while the key is inserted. |
bool IsDetachedLock { get; } | Whether this lock is a separate object. Detached locks switch to a layer that ignores the player body while locked. |
bool IsLocked { get; } | Whether the lock is currently locked. |
LockEvent IsLockedEvent { get; } | Raised whenever the lock becomes locked or unlocked. |
KeyItemEvent KeyTurnEvent { get; } | Raised when the key has been turned far enough and is released from the player's hand. |
KeyItemEvent ReceivedNewKeyEvent { get; } | Raised when the matching key enters the lock and is jointed in place. |
NetworkRigidbody Rigidbody { get; } | The lock's NetworkRigidbody. |
Methods
| Member | What it is |
|---|---|
override void Initialize() | Sets up the synced lock state, key joint break and key grip break messages. |
void JingleJangle(Vector3 jingleJangleTorque) | Adds a torque to the lock's rigidbody to rattle it, unless a key is inserted. Called by Chest when shaken. |
override void OnConnected(Player player) | Server: sends the current locked state to a newly connected player. |
void SetLock(bool isLocked) | Sets the locked state, raises the event and, on the server, syncs it to clients. Does nothing if unchanged. |
protected virtual void Locked() | Called when the lock becomes locked; sets the flag and raises IsLockedEvent. Override to add behaviour. |
protected virtual void Unlocked() | Called when the lock becomes unlocked; clears the flag and raises IsLockedEvent. Override to add behaviour. |
13 private and internal methods
| Member | What it is |
|---|---|
private void Awake() | Caches the colliders, rigidbody, layers and squared break distance, and the hash of the correct key prefab. |
private void BreakKeyGrip() | Private: forces every hand off the inserted key, freezes it and raises KeyTurnEvent. The server syncs this to clients. |
private IEnumerator CollidersIgnoreDelay(IEnumerable<Collider> colliders) (iterator) | Private: turns collisions between the lock and a removed key back on after half a second. |
private void LateUpdate() | Breaks the key joint when the local hand pulls too far, and on the server starts unlocking once the key is turned far enough. |
private void OnJointBreak(float breakForce) | Private: releases the key when its joint breaks, giving physics back to both the lock and key. |
private void OnTriggerEnter(Collider other) | Private: when a key of the correct prefab enters the trigger, freezes both and seats the key with SetupKey. |
private void RecentGrab(Interactable key, Interactor interactor) | Private, server: starts a one second cooldown whenever the inserted key is grabbed. |
private IEnumerator RunGrabCooldown() (iterator) | Private: waits one second, then allows unlocking again. |
private void SetupKey(KeyItem newKey) | Private: snaps a key into the lock and joins it with a joint that can only twist, up to 90 degrees. |
private void SyncBreakKeyGrip(IPlayer player, Stream stream) | Private: client side of BreakKeyGrip. |
private void SyncBreakKeyJoint(IPlayer player, Stream stream) | Private: breaks the key joint when a client reports pulling out the key; the server relays it to other players. |
private void SyncLocked(IPlayer player, Stream stream) | Private: syncs the locked state, applied on clients with SetLock. |
private IEnumerator UnlockDelay() (iterator) | Private: breaks the key grip, waits delayBetweenUnlockingAndOpening, then unlocks. |
Used in
Members of other types that take, return or hold a Lock: