public ActionOutcome Attempt()
        {
            ActionOutcome outcome = IsValid();

            if (!outcome)
            {
                Announce(outcome);
                return(outcome);
            }

            foreach (var(type, stakeholder) in Stakeholders())
            {
                outcome = stakeholder.Allows(new ActionAnnouncement(this, null, type));
                if (!outcome)
                {
                    Announce(outcome);
                    return(outcome);
                }
            }
            //Actor.SuspendMessages();
            outcome = Execute();
            Announce(outcome);
            //Actor.ResumeMessages();
            return(outcome);
        }
        private void Announce(ActionOutcome outcome)
        {
            foreach (var(type, stakeholder) in Stakeholders())
            {
                var announcement = new ActionAnnouncement(this, outcome, type);
                stakeholder.HandleAnnouncement(announcement);
            }
            var locationAnnouncement = new ActionAnnouncement(this, outcome, TargetType.Scene);

            Actor.Location.HandleAnnouncement(locationAnnouncement);
        }
Beispiel #3
0
        protected override ActionOutcome Run(bool execute)
        {
            ActionOutcome prereqs =
                Target.Is <InventoryItem>($"You can only take reasonably sized inanimate objects.") &&
                CanReach(Target) &&
                DoesNotHave(Target);

            if (!prereqs)
            {
                return(prereqs);
            }

            if (execute)
            {
                Target.Move(Actor);
            }
            return(Succeed());
        }
Beispiel #4
0
        protected override ActionOutcome Run(bool execute)
        {
            ActionOutcome prereqs = Has(Tool) && CanReach(Target);

            if (!prereqs)
            {
                return(prereqs);
            }
            if (execute)
            {
                DamageType type = DamageType.Blunt;
                if (Tool.Is(out Weapon? weapon))
                {
                    type = weapon.DamageType;
                }
                Target.TakeDamage(Actor.MeleeDamage(Tool), type, BodyPartKind.Any);
            }
            return(Succeed());
        }
Beispiel #5
0
        public ActionOutcome Act(T self)
        {
            if (this.lastCompletedAction != null)
            {
                (var previousAction, var previousOutcome) = this.lastCompletedAction;

                if (previousOutcome.Status == InProgress)
                {
                    if (!string.IsNullOrEmpty(previousOutcome.Message))
                    {
                        io.WriteLine($"({previousOutcome.Message})...");
                    }
                    // Repeat (i.e. continue) the action if it is still in progress
                    ActionOutcome nextOutcome = previousAction.Execute();
                    this.lastCompletedAction = new CompletedAction(previousAction, nextOutcome);
                    return(nextOutcome);
                }

                else
                {
                    if (!string.IsNullOrEmpty(previousOutcome.Message))
                    {
                        io.WriteLine($"{previousOutcome.Message}");
                    }
                }
            }
            while (true)
            {
                io.Write(">");
                string        input         = io.ReadLine();
                ActionOutcome?backupFailure = null;
                IGameAction?  validAction   = null;
                foreach (IGameAction action in verbSuite.GetMatches(self, input))
                {
                    ActionOutcome testOutcome = action.Execute(dryRun: true);
                    if (testOutcome.Status == Failed)
                    {
                        if (backupFailure == null)
                        {
                            backupFailure = testOutcome;
                        }
                    }
                    else
                    {
                        validAction = action;
                    }
                }

                if (validAction is null)
                {
                    if (string.IsNullOrEmpty(backupFailure?.Message))
                    {
                        Console.WriteLine("Invalid: I couldn't recognize the verb of that sentence.");
                    }
                    else
                    {
                        Console.WriteLine(backupFailure.Message);
                    }
                    continue;
                }

                ActionOutcome outcome = validAction.Execute();
                this.lastCompletedAction = new CompletedAction(validAction, outcome);
                return(outcome);
            }
        }