コード例 #1
0
        private void Move(Room room)
        {
            Room nextRoom = room;

            if (!CurrentRoom.IsLit() && !room.Light)
            {
                room = Rooms.Get <Darkness>();
            }

            Context.Story.Location = room;

            if (!room.Visited)
            {
                CurrentRoom.Look(true);

                room.Initial?.Invoke();
            }
            else
            {
                if (!CurrentRoom.IsLit() && room.Visited)
                {
                    nextRoom.DarkToDark();
                }

                CurrentRoom.Look(false);
            }


            room.Visited = true;

            Context.Story.Location = nextRoom;
        }
コード例 #2
0
        public void Run()
        {
            var story = Context.Story;

            story.Initialize();

            MovePlayer.To(story.Location);

            while (!story.IsDone)
            {
                var  room   = CurrentRoom.Location;
                bool wasLit = CurrentRoom.IsLit();

                var parser = new CommandLineParser();
                var result = parser.Parse(CommandPrompt.GetInput());

                if (result.Error.HasValue())
                {
                    Output.Print(result.Error);
                }
                else
                {
                    var handler = result.CommandHandler();
                    handler.Run();
                }

                if (!wasLit && CurrentRoom.IsLit() && CurrentRoom.Location == room)
                {
                    CurrentRoom.Look(true);
                }

                if (wasLit && !CurrentRoom.IsLit())
                {
                    Output.Print("\r\nIt is now pitch dark in here!");
                }

                story.Moves++;

                RunDaemons();

                story.AfterTurn();
            }
        }
コード例 #3
0
        private CommandLineParserResult Parse(Verb verb, TokenizedInput tokens)
        {
            var result = new CommandLineParserResult
            {
                Verb = verb
            };

            var lastToken = "";

            foreach (string token in tokens)
            {
                var obj = GetObject(result, token);

                if (obj is Skip)
                {
                    continue;
                }

                if (result.Objects.Contains(obj))
                {
                    // will happen for something like "take the brass lamp"
                    continue;
                }

                if (obj != null)
                {
                    if (obj.InScope)
                    {
                        if (result.Preposition == null || !result.Objects.Any())
                        {
                            // handles commands like "put on coat", "put down book", etc.
                            result.Ordered.Add(obj);
                            result.Objects.Add(obj);
                        }
                        else
                        {
                            result.Ordered.Add(obj);
                            result.IndirectObject = obj;
                        }
                    }
                    else if (obj is MultipleObjectsFound)
                    {
                        return(ResolveMultipleObjects(verb, (MultipleObjectsFound)obj));
                    }
                    else
                    {
                        result.Error = Messages.CantSeeObject;
                        return(result);
                    }
                }

                // distinguish between prepositions and movement - "go south", "put bottle down", "close up grate"
                else if ((result.Verb is IDirectionProxy) && token.IsDirection() && !result.Objects.Any())
                {
                    var v = token.ToVerb();
                    result.Ordered.Add(v);
                    result.Verb = v;
                }

                else if (token.IsPreposition())
                {
                    var p = Prepositions.Get(token);
                    result.Ordered.Add(p);
                    result.Preposition = p;
                }

                else if (token == "all" && !result.Objects.Any())
                {
                    result.IsAll = true;

                    if (!verb.Multi && !verb.MultiHeld)
                    {
                        result.Error = Messages.MultiNotAllowed;
                        break;
                    }

                    var multi = new List <Object>();

                    if (verb.Multi)
                    {
                        var objectsInRoom =
                            from o in CurrentRoom.ObjectsInRoom()
                            where !o.Animate
                            select o;

                        multi.AddRange(objectsInRoom);
                    }

                    if (verb.MultiHeld)
                    {
                        multi.AddRange(Inventory.Items);
                    }

                    // if object count is only 1, we don't add it so it can be handled in the verb using implict
                    // messages e.g. (the small bottle)
                    if (multi.Count > 1)
                    {
                        result.Objects.AddRange(multi);
                    }
                }

                else if (token == "except" && (verb.Multi || verb.MultiHeld) && lastToken == "all")
                {
                    var except = HandleExcept(result, tokens, token);

                    if (except != null)
                    {
                        return(except);
                    }

                    break;
                }

                else
                {
                    obj = result.Objects.FirstOrDefault();

                    if (obj != null && !result.IsAll)
                    {
                        result.Error = Messages.PartialUnderstanding(verb, obj);
                    }
                    else
                    {
                        result.Error = Messages.CantSeeObject;
                    }

                    return(result);
                }

                lastToken = token;
            }

            return(result);
        }