首页 > 代码库 > [Clojure] A Room-Escape game, playing with telnet and pure-text commands - Part 2
[Clojure] A Room-Escape game, playing with telnet and pure-text commands - Part 2
Code Path:
https://github.com/bluesilence/Lisp/blob/master/clojure/projects/room-escape/.
Data Model
Based on the draft, the data models can be built of 4 major units:
1. Story
It‘s the base unit of a room-escape story. The storyhas following members:
1) name: the name of this room, shown in the room-selection menu
2) description: the description of this room, shown in the room-selection menu
3) starting-message: the starting message when player begins to play this room
4) objects: the collection of all the objects within the story, including rooms, spots and items. They all belong to the same logical type: object. Rooms, spots and items are differentiated by the [category] field: room - 0, spot - 1, item - 2.
2. Room
There might be more than 1 room within a story. For the demo one, there is only 1 room. It holds a collection of spots within the room.
3. Spot
A spot is a special part of the room which is worth examining. It holds a collection of items related to the spot. The description field contains message about this spot when checked from far away or nearby.
4. Item
Items are the most interesting ones. They are the minimal unit of an object, and have some special properties:
1) pickable: whether the item can be picked by the player;
2) description: message about this item when checked from far away or nearby;
3) on-use: the function called when the player uses the item
4) action: custom actions that only belong to the item. Player can get the list of custom actions by typing help in the game.
5. Visibility
Note that not all objects are visible from the beginning. The player has to discover within the room to find more objects.
To implement this visibility feature, a "visible" collection is added into the player‘s context.
6. Player‘s Context
Because this is a multi-threading game which allows multiple players to play at the same time, we need an isolated context to hold the status of each players. Here‘s the structure of a player‘s context:
{:player-objects (:objects starting-room) :starting-room starting-room-index :current-status (atom {:room -1 :spot -1 :items #{}}) :visible (atom #{}) :win (atom false) :continue (atom true) :last-action (atom [])}
1) Player-objects: All the objects related to the game of the player;
2) Starting-room: The ID of the room the player starts with;
3) Current-status: Location and possessed items of the player;
4) Visible: The collection of visible objects in this game based on how the player played the game;
5) Win: Indicate if the player has won the game;
6) Continue: Indicate if the player wants to continue the game;
7) Last-Action: Stores the last action did by the player. It‘s used for helping the player with suggesting new actions based on the history.
Utilities
How is the game context generated? To bind the story with the player, and throw magic on the target object the player is interacting with, I wrote a utility class to tackle with the logic:
https://github.com/bluesilence/Lisp/blob/master/clojure/projects/room-escape/src/room_escape/util.clj
Common
The common class basically deals with the UI part, and some utility functions that cannot be extracted to the util.clj due to dependent "use" issue.
https://github.com/bluesilence/Lisp/blob/master/clojure/projects/room-escape/src/room_escape/common.clj
Next chapter will narrate on how to generalize the story‘s script into a txt, and load it at runtime.
[Clojure] A Room-Escape game, playing with telnet and pure-text commands - Part 2