-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
108 lines (80 loc) · 2.51 KB
/
game.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var inquirer = require('inquirer');
var connect = require('./lib/connect');
var Combat = require('./lib/Combat');
var CharacterFactory = require('./lib/CharacterFactory');
var Script = require('./lib/Script');
var STATES = require('./lib/states').STATES;
var ACTIONS = require('./lib/states').ACTIONS;
var COMMANDS = require('./lib/states').COMMANDS;
// The Game
// ===
function Game() {
this.endProcess = false; // set true to exit the game
this.skipPrompt = false; // set to true if you want to prevent command prompt
this.STATES = STATES;
this.ACTIONS = ACTIONS;
this.COMMANDS = COMMANDS;
this.currentState = STATES.default;
}
// Help
// ---
//
// Probably a menu that can be pulled up at any time. Lists all the commands
// that can be run at any given time.
Game.prototype.help = function() {
console.log('Is this the help menu?');
};
// Executor
// ---
//
// The executor is the running process that ensures the game continues to run.
// It's a looped game state that continues to run until the game state
// determines that something happens, at which point "that something" happens!
// Then the loop will continue until the next "something happens".
Game.prototype.executor = function() {
var p = [{
type: "list",
name: "input",
message: 'What will you do?',
choices: this.getCommands()
}];
// Skip prompt
if (this.skipPrompt) {
return;
}
inquirer.prompt(p, function(response) {
this.processCommand(response);
if (this.endProcess) {
return false;
process.exit(0);
}
this.executor(); // continue the loop!
}.bind(this));
};
// Get Commands
// ---
//
// Based on the current game state, get the current list of commands
Game.prototype.getCommands = function() {
var currentActions = this.ACTIONS[this.currentState];
return Object.keys(currentActions).map(function(key) {
return currentActions[key];
});
};
// Process Command
// ---
Game.prototype.processCommand = function(response) {
console.log('\n');
this.COMMANDS[this.currentState](response, this);
console.log('\n');
};
// Run the Game!
// ---
var game = new Game();
setTimeout(function() {
game.executor();
game.abilities = connect('jobAbilities');
game.characters = new CharacterFactory().init(connect('characters'), game.abilities);
game.bestiary = new CharacterFactory().init(connect('bestiary'), game.abilities);
game.script = new Script(connect('script'));
}, 2000);