/// <summary>Handle the events of our parent moving; need to adjust our exit context commands and such.</summary> /// <param name="root">The event sender.</param> /// <param name="e">The event args.</param> private void ParentMovementEventHandler(Thing root, Events.GameEvent e) { // If our parent (the thing with exit behavior) was removed from something (like a room)... var removeChildEvent = e as RemoveChildEvent; if (removeChildEvent != null && removeChildEvent.ActiveThing == Parent && removeChildEvent.OldParent != null) { // Remove the old exit command, if one was rigged up to the old location. string oldExitCommand = GetExitCommandFrom(removeChildEvent.OldParent); removeChildEvent.OldParent.Commands.Remove(oldExitCommand); } // If our parent (the thing with exit behavior) was placed in something (like a room)... var addChildEvent = e as AddChildEvent; if (addChildEvent != null && addChildEvent.ActiveThing == Parent && addChildEvent.NewParent != null) { // Add the appropriate exit command for the new location. AddExitContextCommands(addChildEvent.NewParent); } }
public void Init() { // Create the basic actor instances and behavior for test. this.witness = new Thing() { Name = "Witness", ID = TestThingID.Generate("testthing") }; this.stalker1 = new Thing() { Name = "Stalker1", ID = TestThingID.Generate("testthing") }; this.stalker2 = new Thing() { Name = "Stalker2", ID = TestThingID.Generate("testthing") }; this.victim1 = new Thing() { Name = "Victim1", ID = TestThingID.Generate("testthing") }; this.victim2 = new Thing() { Name = "Victim2", ID = TestThingID.Generate("testthing") }; // Set up the rooms. this.room1 = new Thing() { Name = "Room", ID = TestThingID.Generate("room") }; this.room2 = new Thing() { Name = "Room 2", ID = TestThingID.Generate("room") }; // Set up an exit connecting the two rooms. this.exit = new Thing() { Name = "East Exit", ID = TestThingID.Generate("exit") }; var exitBehavior = new ExitBehavior(); ////exitBehavior.AddDestination("west", room1.ID); ////exitBehavior.AddDestination("east", room1.ID); ////this.exit.BehaviorManager.Add(exitBehavior); this.room1.Add(this.exit); this.room2.Add(this.exit); // Populate the first room. this.room1.Add(this.witness); this.room1.Add(this.stalker1); this.room1.Add(this.stalker2); this.room1.Add(this.victim1); this.room1.Add(this.victim2); // Prepare to verify correct eventing occurs. this.witness.Eventing.MovementRequest += (root, e) => { this.lastWitnessRequest = e; }; this.witness.Eventing.MovementEvent += (root, e) => { this.lastWitnessEvent = e; }; this.stalker1.Eventing.MovementRequest += (root, e) => { this.lastStalkerRequest = e; }; this.stalker1.Eventing.MovementEvent += (root, e) => { this.lastStalkerEvent = e; }; this.stalker2.Eventing.MovementRequest += (root, e) => { this.lastStalkerRequest = e; }; this.stalker2.Eventing.MovementEvent += (root, e) => { this.lastStalkerEvent = e; }; this.victim1.Eventing.MovementRequest += (root, e) => { this.lastVictimRequest = e; }; this.victim1.Eventing.MovementEvent += (root, e) => { this.lastVictimEvent = e; }; this.victim2.Eventing.MovementRequest += (root, e) => { this.lastVictimRequest = e; }; this.victim2.Eventing.MovementEvent += (root, e) => { this.lastVictimEvent = e; }; }
public void Init() { // Create the basic actor instances and behavior for test. this.witnessThing = new Thing() { Name = "WitnessThing", ID = TestThingID.Generate("testthing") }; this.actingThing = new Thing() { Name = "ActingThing", ID = TestThingID.Generate("testthing") }; this.lockableThing = new Thing() { Name = "LockableThing", ID = TestThingID.Generate("testthing") }; this.locksUnlocksBehavior = new LocksUnlocksBehavior(); // Set up the actors inside another (which we'll call a "room" although it needn't actually be a room). this.room = new Thing() { Name = "Room", ID = TestThingID.Generate("room") }; this.room.Add(witnessThing); this.room.Add(actingThing); this.room.Add(lockableThing); // Prepare to verify correct eventing occurs. this.witnessThing.Eventing.MiscellaneousRequest += (root, e) => { this.lastWitnessRequest = e; }; this.witnessThing.Eventing.MiscellaneousEvent += (root, e) => { this.lastWitnessEvent = e; }; this.actingThing.Eventing.MiscellaneousRequest += (root, e) => { this.lastActorRequest = e; }; this.actingThing.Eventing.MiscellaneousEvent += (root, e) => { this.lastActorEvent = e; }; }
private void OnEvent(Func<ThingEventing, GameEventHandler> handlerSelector, GameEvent e) { // Build an event 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> eventTargetQueue = new Queue<Thing>(); eventTargetQueue.Enqueue(this.owner); while (eventTargetQueue.Count > 0) { // If anything (like one of the thing's Behaviors) is subscribed to this event, send it there. Thing currentEventTarget = eventTargetQueue.Dequeue(); var handler = handlerSelector(currentEventTarget.Eventing); if (handler != null) { handler(currentEventTarget, e); } // Enqueue all the current target's children for processing. foreach (Thing child in currentEventTarget.Children) { eventTargetQueue.Enqueue(child); } } }
private void OnEvent(Func<ThingEventing, GameEventHandler> handlerSelector, GameEvent 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 event to each parent. List<Thing> allParents = this.owner.Parents; foreach (var parent in allParents) { parent.Eventing.OnEvent(handlerSelector, e); } break; case EventScope.SelfDown: this.OnEvent(handlerSelector, e); break; default: throw new NotImplementedException(); } }
/// <summary> /// Raises the <see cref="MovementEvent"/> event. /// </summary> /// <param name="e">The <see cref="WheelMUD.Core.Events.GameEvent"/> instance containing the event data.</param> /// <param name="eventScope">The base target(s) to broadcast to, including their children.</param> public void OnMovementEvent(GameEvent e, EventScope eventScope) { Func<ThingEventing, GameEventHandler> handlerSelector = (t) => { return t.MovementEvent; }; this.OnEvent(handlerSelector, e, eventScope); }
/// <summary>Called when a player logs in, to raise the player log in events.</summary> /// <param name="player">The player.</param> /// <param name="e">The <see cref="WheelMUD.Core.Events.GameEvent"/> instance containing the event data.</param> public static void OnPlayerLogIn(Thing player, GameEvent e) { var eventHandler = GlobalPlayerLogInEvent; if (eventHandler != null) { eventHandler(player.Parent, e); } }
/// <summary> /// Receives an event. /// </summary> /// <param name="root">The root.</param> /// <param name="theEvent">The event to be received.</param> public void Receive(Thing root, GameEvent theEvent) { ////this.brain.ProcessStimulus(theEvent); }
/// <summary>Raises the <see cref="CombatEvent"/> event.</summary> /// <param name="e">The <see cref="WheelMUD.Core.Events.GameEvent"/> instance containing the event data.</param> /// <param name="eventScope">The base target(s) to broadcast to, including their children.</param> public void OnCombatEvent(GameEvent e, EventScope eventScope) { this.OnEvent(t => t.CombatEvent, e, eventScope); }
/// <summary>Raises the <see cref="MiscellaneousEvent"/> event.</summary> /// <param name="e">The <see cref="WheelMUD.Core.Events.GameEvent"/> instance containing the event data.</param> /// <param name="eventScope">The base target(s) to broadcast to, including their children.</param> public void OnMiscellaneousEvent(GameEvent e, EventScope eventScope) { this.OnEvent(t => t.MiscellaneousEvent, e, eventScope); }
/// <summary>Raises the <see cref="CommunicationEvent"/> event.</summary> /// <param name="e">The <see cref="WheelMUD.Core.Events.GameEvent"/> instance containing the event data.</param> /// <param name="eventScope">The base target(s) to broadcast to, including their children.</param> public void OnCommunicationEvent(GameEvent e, EventScope eventScope) { this.OnEvent(t => t.CommunicationEvent, e, eventScope); }
/// <summary>Raises the <see cref="CombatEvent"/> event.</summary> /// <param name="e">The <see cref="WheelMUD.Core.Events.GameEvent"/> instance containing the event data.</param> /// <param name="eventScope">The base target(s) to broadcast to, including their children.</param> public void OnCombatEvent(GameEvent e, EventScope eventScope) { Func <ThingEventing, GameEventHandler> handlerSelector = (t) => { return(t.CombatEvent); }; this.OnEvent(handlerSelector, e, eventScope); }
/// <summary>Raises the <see cref="MiscellaneousEvent"/> event.</summary> /// <param name="e">The <see cref="WheelMUD.Core.Events.GameEvent"/> instance containing the event data.</param> /// <param name="eventScope">The base target(s) to broadcast to, including their children.</param> public void OnMiscellaneousEvent(GameEvent e, EventScope eventScope) { Func <ThingEventing, GameEventHandler> handlerSelector = (t) => { return(t.MiscellaneousEvent); }; this.OnEvent(handlerSelector, e, eventScope); }
/// <summary>Executes the command.</summary> /// <param name="actionInput">The full input specified for executing the command.</param> public override void Execute(ActionInput actionInput) { // Contextual message text to be supplied based on the action below var response = new ContextualString(this.sender.Thing, this.room.Parent); if (this.command == "add") { // Add or update the description this.room.Visuals[this.visualName] = this.visualDescription; response.ToOriginator = string.Format("Visual '{0}' added/updated on room {1} [{2}].", this.visualName, this.roomName, this.roomId); //// TODO: Save change this.room.Save(); } else if (this.command == "remove") { if (this.room.Visuals.ContainsKey(this.visualName)) { this.room.Visuals.Remove(this.visualName); response.ToOriginator = string.Format("Visual '{0}' removed from room {1} [{2}]", this.visualName, this.roomName, this.roomId); } //// TODO: Save change this.room.Save(); } else if (this.command == "show") { var output = new StringBuilder(); if (this.room.Visuals.Count > 0) { output.AppendLine(string.Format("Visuals for {0} [{1}]:", this.roomName, this.roomId)).AppendLine(); foreach (var name in this.room.Visuals.Keys) { output.AppendLine(string.Format(" {0}: {1}", name, this.room.Visuals[name])); } } else { output.Append(string.Format("No visuals found for {0} [{1}].", this.roomName, this.roomId)); } //// HACK: Using sender.Write() for now to avoid the ViewEngine stripping newlines. this.sender.Write(output.ToString()); // No need to raise event. return; } var message = new SensoryMessage(SensoryType.Sight, 100, response); var evt = new GameEvent(this.sender.Thing, message); this.sender.Thing.Eventing.OnMiscellaneousEvent(evt, EventScope.SelfDown); }
/// <summary> /// Processes the player log out events from the player manager; disconnects logged out characters. /// </summary> /// <param name="root">The root location where the log out event originated.</param> /// <param name="e">The event arguments.</param> private void PlayerManager_GlobalPlayerLogOutEvent(Thing root, GameEvent e) { // If the player was user-controlled during log out, disconnect that user. var userControlledBehavior = e.ActiveThing.Behaviors.FindFirst<UserControlledBehavior>(); if (userControlledBehavior != null && userControlledBehavior.Controller != null) { var session = userControlledBehavior.Controller as Session; if (session != null && session.Connection != null) { session.Connection.Disconnect(); } } }
/// <summary> /// Raises the <see cref="CommunicationEvent"/> event. /// </summary> /// <param name="e">The <see cref="WheelMUD.Core.Events.GameEvent"/> instance containing the event data.</param> /// <param name="eventScope">The base target(s) to broadcast to, including their children.</param> public void OnCommunicationEvent(GameEvent e, EventScope eventScope) { Func<ThingEventing, GameEventHandler> handlerSelector = (t) => { return t.CommunicationEvent; }; this.OnEvent(handlerSelector, e, eventScope); }
/// <summary> /// Processes the movement event. /// </summary> /// <param name="root">The root.</param> /// <param name="e">The e.</param> private void ProcessMovementEvent(Thing root, GameEvent e) { var self = this.Parent; var currentRoom = self.Parent; var arriveEvent = e as ArriveEvent; if (arriveEvent != null && arriveEvent.ActiveThing == this.Target && arriveEvent.GoingFrom == currentRoom) { var leaveMessage = this.CreateLeaveMessage(self); var arriveMessage = this.CreateArriveMessage(self); var movableBehavior = self.Behaviors.FindFirst<MovableBehavior>(); movableBehavior.Move(arriveEvent.GoingTo, arriveEvent.GoingVia, leaveMessage, arriveMessage); } }
/// <summary>Raises the <see cref="MovementEvent"/> event.</summary> /// <param name="e">The <see cref="WheelMUD.Core.Events.GameEvent"/> instance containing the event data.</param> /// <param name="eventScope">The base target(s) to broadcast to, including their children.</param> public void OnMovementEvent(GameEvent e, EventScope eventScope) { this.OnEvent(t => t.MovementEvent, e, eventScope); }
/// <summary> /// Clear all potentially tracked events so we can verify new ones. /// </summary> private void ClearTrackedEvents() { this.lastActorEvent = null; this.lastActorRequest = null; this.lastWitnessEvent = null; this.lastWitnessRequest = null; }
/// <summary> /// Process a specified event. /// </summary> /// <param name="root">The root.</param> /// <param name="e">The event to be processed.</param> public void ProcessEvent(Thing root, GameEvent e) { // Events with no sensory component have no chance to be percieved/relayed to player's terminal... if (e.SensoryMessage != null) { string output = this.ProcessMessage(e.SensoryMessage); if (output != string.Empty) { this.userControlledBehavior.Controller.Write(output); } } }
/// <summary> /// Clear all potentially tracked events so we can verify new ones. /// </summary> private void ClearTrackedEvents() { this.lastStalkerEvent = null; this.lastStalkerRequest = null; this.lastVictimEvent = null; this.lastVictimRequest = null; this.lastWitnessEvent = null; this.lastWitnessRequest = null; }
private void ProcessPlayerLogOutEvent(Thing root, GameEvent e) { // If this is a friend, ensure we get a 'your friend logged out' message regardless of location. if (this.IsFriend(e.ActiveThing.Name) && e is PlayerLogOutEvent) { var userControlledBehavior = this.Parent.Behaviors.FindFirst<UserControlledBehavior>(); string message = string.Format("Your friend {0} has logged out.", e.ActiveThing.Name); userControlledBehavior.Controller.Write(message); } }