/// <summary>Lock or unlock this behavior's parent, via the specified actor.</summary> /// <param name="actor">The actor doing the locking or unlocking.</param> /// <param name="verb">Whether this is an "lock" or "unlock" action.</param> /// <param name="newLockedState">The new IsLocked state to be set, if the request is not cancelled.</param> private void LockOrUnlock(Thing actor, string verb, bool newLockedState) { // If we're already in the desired locked/unlocked state, we're already done with state changes. if (newLockedState == IsLocked) { // TODO: Message to the actor that it is already locked/unlocked. return; } // Use a temporary ref to our own parent to avoid race conditions like sudden parent removal. var thisThing = Parent; if (thisThing == null) { return; // Abort if the behavior is unattached (e.g. being destroyed). } if (newLockedState && thisThing != null) { // If we are attempting to lock an opened thing, cancel the lock attempt. var opensClosesBehavior = thisThing.Behaviors.FindFirst <OpensClosesBehavior>(); if (opensClosesBehavior != null && opensClosesBehavior.IsOpen) { // TODO: Message to the actor that they can't lock an open thing. return; } } // Prepare the Lock/Unlock game event for sending as a request, and if not cancelled, again as an event. var contextMessage = new ContextualString(actor, thisThing) { ToOriginator = $"You {verb} {thisThing.Name}.", ToReceiver = $"{actor.Name} {verb}s you.", ToOthers = $"{actor.Name} {verb}s {thisThing.Name}.", }; var message = new SensoryMessage(SensoryType.Sight, 100, contextMessage); var e = new LockUnlockEvent(thisThing, false, actor, message); // Broadcast the Lock or Unlock Request and carry on if nothing cancelled it. // Broadcast from the parents of the lockable/unlockable thing (IE a room or inventory where the lockable resides). thisThing.Eventing.OnMiscellaneousRequest(e, EventScope.ParentsDown); if (!e.IsCancelled) { // Lock or Unlock the thing. IsLocked = newLockedState; // Broadcast the Lock or Unlock event. thisThing.Eventing.OnMiscellaneousEvent(e, EventScope.ParentsDown); } }
/// <summary>Lock or unlock this behavior's parent, via the specified actor.</summary> /// <param name="actor">The actor doing the locking or unlocking.</param> /// <param name="verb">Whether this is an "lock" or "unlock" action.</param> /// <param name="newLockedState">The new IsLocked state to be set, if the request is not cancelled.</param> private void LockOrUnlock(Thing actor, string verb, bool newLockedState) { // If we're already in the desired locked/unlocked state, we're already done with state changes. if (newLockedState == this.IsLocked) { // @@@ TODO: Message to the actor that it is already locked/unlocked. return; } // Use a temporary ref to our own parent to avoid race conditions like sudden parent removal. var thisThing = this.Parent; if (newLockedState && thisThing != null) { // If we are attempting to lock an opened thing, cancel the lock attempt. var opensClosesBehavior = thisThing.Behaviors.FindFirst <OpensClosesBehavior>(); if (opensClosesBehavior != null && opensClosesBehavior.IsOpen) { // @@@ TODO: Message to the actor that they can't lock an open thing. return; } } // Prepare the Lock/Unlock game event for sending as a request, and if not cancelled, again as an event. var csb = new ContextualStringBuilder(actor, this.Parent); csb.Append(@"You " + verb + " $TargetThing.Name.", ContextualStringUsage.OnlyWhenBeingPassedToOriginator); csb.Append(@"$ActiveThing.Name " + verb + "s you.", ContextualStringUsage.OnlyWhenBeingPassedToReceiver); csb.Append(@"$ActiveThing.Name " + verb + "s $TargetThing.Name.", ContextualStringUsage.WhenNotBeingPassedToReceiverOrOriginator); var message = new SensoryMessage(SensoryType.Sight, 100, csb); var e = new LockUnlockEvent(this.Parent, false, actor, message); // Broadcast the Lock or Unlock Request and carry on if nothing cancelled it. if (thisThing != null) { // Broadcast from the parents of the lockable/unlockable thing (IE a room or inventory where the lockable resides). thisThing.Eventing.OnMiscellaneousRequest(e, EventScope.ParentsDown); if (!e.IsCancelled) { // Lock or Unlock the thing. this.IsLocked = newLockedState; // Broadcast the Lock or Unlock event. thisThing.Eventing.OnMiscellaneousEvent(e, EventScope.ParentsDown); } } }