/// <summary>Called when a player is trying to log out, to raise the player log out request.</summary> /// <param name="player">The player.</param> /// <param name="e">The event arguments.</param> public static void OnPlayerLogOutRequest(Thing player, CancellableGameEvent e) { var eventHandler = GlobalPlayerLogOutRequest; if (eventHandler != null) { eventHandler(player.Parent, e); } }
/// <summary>Event handler for denying the changing of the parent of a Thing which has a WorldBehavior since World should be highest.</summary> /// <param name="sender">The sender of the event.</param> /// <param name="e">The event arguments.</param> private void DenyWorldParentChanges(Thing sender, CancellableGameEvent e) { if (e.ActiveThing == this.Parent) { var addChildRequest = e as AddChildEvent; if (addChildRequest != null) { addChildRequest.Cancel("World cannot become a child of anything."); } } }
/// <summary>Handle any movement requests.</summary> /// <param name="root">The root Thing where this event broadcast started.</param> /// <param name="e">The cancellable event/request arguments.</param> private void MovementRequestHandler(Thing root, CancellableGameEvent e) { // Only cancel movement requests through our parent if it is currently closed. if (!this.IsOpen) { var parent = this.Parent; if (parent != null) { // If this is a standard movement request, find out if we need to cancel it. var movementEvent = e as MovementEvent; if (movementEvent != null && movementEvent.GoingVia == this.Parent) { // @@@ TODO: If the actor also cannot perceive our parent properly, perhaps broadcast // a sensory event like "Dude blindly ran into a door." string message = string.Format("You cannot move through {0} since it is closed!", this.Parent.Name); movementEvent.Cancel(message); } } } }
/// <summary>Called when a player is trying to log out, to raise the player log out request.</summary> /// <param name="player">The player.</param> /// <param name="e">The event arguments.</param> public static void OnPlayerLogOutRequest(Thing player, CancellableGameEvent e) { GlobalPlayerLogOutRequest?.Invoke(player.Parent, e); }
/// <summary>Called when a player is trying to log in, to raise the player log in request.</summary> /// <param name="player">The player.</param> /// <param name="e">The event arguments.</param> public void OnPlayerLogInRequest(Thing player, CancellableGameEvent e) { this.GlobalPlayerLogInRequest?.Invoke(player.Parent, e); }
/// <summary>Raises the <see cref="MiscellaneousRequest"/> event.</summary> /// <param name="e">The <see cref="WheelMUD.Core.Events.CancellableGameEvent"/> instance containing the event data.</param> /// <param name="eventScope">The base target(s) to broadcast to, including their children.</param> public void OnMiscellaneousRequest(CancellableGameEvent e, EventScope eventScope) { OnRequest(t => t.MiscellaneousRequest, e, eventScope); }
/// <summary>Raises the <see cref="CommunicationRequest"/> event.</summary> /// <param name="e">The <see cref="WheelMUD.Core.Events.CancellableGameEvent"/> instance containing the event data.</param> /// <param name="eventScope">The base target(s) to broadcast to, including their children.</param> public void OnCommunicationRequest(CancellableGameEvent e, EventScope eventScope) { OnRequest(t => t.CommunicationRequest, e, eventScope); }
/// <summary>Raises the <see cref="MovementRequest"/> event.</summary> /// <param name="e">The <see cref="WheelMUD.Core.Events.CancellableGameEvent"/> instance containing the event data.</param> /// <param name="eventScope">The base target(s) to broadcast to, including their children.</param> public void OnMovementRequest(CancellableGameEvent e, EventScope eventScope) { OnRequest(t => t.MovementRequest, e, eventScope); }
/// <summary>Shared code for request handling.</summary> /// <param name="handlerSelector">A function which returns the appropriate Handler for a given Thing.</param> /// <param name="e">The game event args to pass through.</param> /// <param name="cascadeEventToChildren">If true, the event should be cascaded to child things as well.</param> private void OnRequest(Func <ThingEventing, CancellableGameEventHandler> handlerSelector, CancellableGameEvent e, bool cascadeEventToChildren) { // Build a request target queue which starts with our owner Thing and visits all it's Children. // (This is a queue instead of recursion to help avoid stack overflows and such with very large object trees.) Queue <Thing> requestTargetQueue = new Queue <Thing>(); requestTargetQueue.Enqueue(owner); while (requestTargetQueue.Count > 0) { // If anything (like one of the thing's Behaviors) is subscribed to this request, send it there. Thing currentRequestTarget = requestTargetQueue.Dequeue(); var handler = handlerSelector(currentRequestTarget.Eventing); if (handler != null) { handler(currentRequestTarget, e); // If the event has been cancelled by the handler, we no longer need to look for further permission. if (e.IsCancelled) { break; } } if (cascadeEventToChildren) { // Enqueue all the current target's children for processing. foreach (Thing child in currentRequestTarget.Children) { requestTargetQueue.Enqueue(child); } } } }
private void OnRequest(Func <ThingEventing, CancellableGameEventHandler> handlerSelector, CancellableGameEvent e, EventScope eventScope) { // Determine what layer(s) we're broadcasting to; beyond the first layer, these should all be to Children. switch (eventScope) { case EventScope.ParentsDown: // Send the request to each parent, until cancellation is noticed or we've finished. Queue <Thing> requestQueue = new Queue <Thing>(owner.Parents); while (requestQueue.Count > 0 && !e.IsCancelled) { Thing currentParent = requestQueue.Dequeue(); currentParent.Eventing.OnRequest(handlerSelector, e, true); } break; case EventScope.SelfDown: OnRequest(handlerSelector, e, true); break; case EventScope.SelfOnly: OnRequest(handlerSelector, e, false); break; default: throw new NotImplementedException(); } }