Example #1
0
        /// <summary>Open or close this behavior's parent, via the specified actor.</summary>
        /// <param name="actor">The actor doing the opening or closing.</param>
        /// <param name="verb">Whether this is an "open" or "close" action.</param>
        /// <param name="newOpenedState">The new IsOpen state to be set, if the request is not cancelled.</param>
        private void OpenOrClose(Thing actor, string verb, bool newOpenedState)
        {
            // If we're already in the desired opened/closed state, we're already done with state changes.
            if (newOpenedState == IsOpen)
            {
                // TODO: Message to the actor that it is already open/closed.
                return;
            }

            var thisThing = Parent;

            if (thisThing == null)
            {
                return; // Abort if the behavior is unattached (e.g. being destroyed).
            }

            // Prepare the Close/Open 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 OpenCloseEvent(thisThing, newOpenedState, actor, message);

            // Broadcast the Open or Close Request and carry on if nothing cancelled it.
            // Broadcast from the parents of the openable/closable thing (IE the rooms an openable exit is attached to).
            thisThing.Eventing.OnMiscellaneousRequest(e, EventScope.ParentsDown);
            if (!e.IsCancelled)
            {
                // Open or Close the thing.
                IsOpen = newOpenedState;

                // Broadcast the Open or Close event.
                thisThing.Eventing.OnMiscellaneousEvent(e, EventScope.ParentsDown);
            }
        }
        /// <summary>Open or close this behavior's parent, via the specified actor.</summary>
        /// <param name="actor">The actor doing the opening or closing.</param>
        /// <param name="verb">Whether this is an "open" or "close" action.</param>
        /// <param name="newOpenedState">The new IsOpen state to be set, if the request is not cancelled.</param>
        private void OpenOrClose(Thing actor, string verb, bool newOpenedState)
        {
            // If we're already in the desired opened/closed state, we're already done with state changes.
            if (newOpenedState == this.IsOpen)
            {
                // @@@ TODO: Message to the actor that it is already open/closed.
                return;
            }

            // Prepare the Close/Open 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 OpenCloseEvent(this.Parent, newOpenedState, actor, message);

            // Broadcast the Open or Close Request and carry on if nothing cancelled it.
            // Use a temporary ref to our own parent to avoid race conditions like sudden parent removal.
            var thisThing = this.Parent;

            if (thisThing != null)
            {
                // Broadcast from the parents of the openable/closable thing (IE the rooms an openable exit is attached to).
                thisThing.Eventing.OnMiscellaneousRequest(e, EventScope.ParentsDown);
                if (!e.IsCancelled)
                {
                    // Open or Close the thing.
                    this.IsOpen = newOpenedState;

                    // Broadcast the Open or Close event.
                    thisThing.Eventing.OnMiscellaneousEvent(e, EventScope.ParentsDown);
                }
            }
        }