Skip to main content

PostboxSender

A class in Root.Township, the global namespace.

The machine for mailing items to a friend. The player pays coins into costDock, docks items in sendingDocks, picks a friend and pulls the handle; the server packs the items into a gift and pushes it into the friend's postbox.

Constructors​

MemberWhat it is
PostboxSender()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

21 private and internal fields
MemberWhat it is
private TransformHandler coinInsertEffectEffect played at the coin dock when coins go in.
private PickupDock costDockThe dock that holds the coins paid for sending.
private int currentPlayerIdentifierIdentifier of the player using the machine, or 0 when free.
private FriendSelector friendSelectorPicks the friend the items go to.
private Distribution giftPrefabDistributionRandom choice of gift prefab that wraps the sent items.
private PlayerUnityEvent gotPlayerEvent fired when a player takes ownership of the machine.
private Interactable handleThe handle the player pulls to send.
private bool isOpenTrue while enough coins are paid for at least one dock.
private bool isSendingTrue while a send is in progress, so only one runs at a time.
private AltaEvent lostPlayerEvent fired when the owner releases the machine.
private TransformHandler rejectEffectEffect played where rejected items are thrown out.
private Transform rejectPositionWhere rejected coins and items are undocked to.
private Interactable[] restrictionsInteractables only the current owner may use.
private TransformHandler sendEffectEffect played when a gift is sent.
private Transform sendEffectPositionWhere the send effect plays.
private int sendingCostPerDockCoins needed for each filled sending dock.
private PickupDock[] sendingDocksThe docks for items to send. Dock N needs N times sendingCostPerDock coins paid.
private MethodSyncStruct<int> syncEffectMessage that tells clients which effect to play.
private MethodSync syncPlayerMessage that syncs the current owner.
private MethodSyncStruct<int> syncSendMessage a client sends to ask the server to send to a friend.
private BoolUnityEvent updatedOpenStateEvent fired with the new open state when it changes.

Methods​

MemberWhat it is
void CheckSend()Sends to the friend picked in friendSelector, if the local player owns the machine. Runs directly on the server, otherwise asks the server.
void CloseForPlayer(IPlayer player)Releases the machine if this player owns it: clears the docks and syncs the change.
override void Initialize()Creates the send, owner and effect sync messages.
override void InitializeAsServer(SpawnData spawnData)Server: clears the docks on the next frame and hooks coin dock changes to ownership and state updates.
13 private and internal methods
MemberWhat it is
private void Awake()Hooks the interaction filters on the restricted interactables, the handle, the sending docks and the coin dock.
private void Clear()Server: throws everything in the coin dock and sending docks out at rejectPosition.
private void FilterCoins(Interactor interactor, Pickup pickup, ref bool result)Dock filter: only accepts coins from a known player, and only from the current owner once there is one.
private void FilterHandleInteraction(Interactor interactor, Interactable interactable, ref bool result)Interaction filter: blocks the handle until at least one dock's worth of coins is paid.
private void FilterInteractions(Interactor interactor, Interactable interactable, ref bool result)Interaction filter for restrictions: only the current owner may use them.
private void PlayEffect(PostboxSender.EffectType effectType)Plays the coin, reject or send effect locally (not on headless) and, on the server, tells nearby clients to play it too.
private async void Send(int from, int to)Server: packs the docked items (limited by what the sender may still send to that friend) into a random gift prefab, takes the coins and pushes the gift into the receiver's postbox.
private void SetupForPlayer(IPlayer player)Server: makes this player the owner, fires gotPlayer and syncs the owner.
private void SyncEffect(IPlayer player, Stream stream, int effectIndex)Serializes an effect index; remote clients play that effect.
private void SyncPlayer(IPlayer player, Stream stream)Serializes the current owner's player identifier.
private void SyncSend(IPlayer player, Stream stream, int toIdentifier)Serializes a send request; the server runs Send if the requester is the owner.
private void UpdateOwnership(Interactor interactor, Pickup pickup, PickupDock dock)Server: when a coin is docked with no owner yet, makes the coin's player the owner, then plays the coin effect.
private void UpdateState()Server: opens or closes the machine as the coin count crosses one dock's cost, and rejects items from docks that aren't paid for.