-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwine-ff.js
More file actions
220 lines (185 loc) · 5.11 KB
/
Copy pathtwine-ff.js
File metadata and controls
220 lines (185 loc) · 5.11 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/*
https://twinery.org/cookbook/
window
store
name - story name
author - author name
history - array of passage ids visited
passages - object with passage objects indexed by id
startPassage - starting passage id
state - story context (exposed as window.s by twine-ff.js itself for convenience)
userScripts - array of JS code
userStyles - array of CSS code
start() - to run story
show(passageName) - to move to given passage
render(passageName) - to get given passage markup
save() - save context to hash
restore() - restore context from hash
Snowman basics:
[[label->passageName]]
<% escape JS code %>
<%= return value from JS %>
*/
window.setup = window.setup || {};
setup.roll1d6 = () => ~~(Math.random() * 6) + 1;
setup.roll2d6 = () => setup.roll1d6() + setup.roll1d6();
setup.prepare = (s) => {
if (s.prepared) {
return;
}
s.prepared = true;
s.inventory = [];
s._defeated = [];
s.defeated = [];
s.skill = setup.roll1d6() + 6;
s.stamina = setup.roll2d6() + 12;
s.luck = setup.roll1d6() + 6;
s.provisions = 10;
s.skillMax = s.skill;
s.staminaMax = s.stamina;
s.luckMax = s.luck;
const sidebarEl = document.createElement('div');
sidebarEl.id = 'sidebar';
const passageEl = document.querySelector('#passage');
passageEl.parentElement.insertBefore(sidebarEl, passageEl);
const DEBUG = true;
if (DEBUG) {
const currentPassageEl = document.createElement('input');
currentPassageEl.id = 'currentPassage';
document.body.appendChild(currentPassageEl);
currentPassageEl.addEventListener('change', () => {
window.story.show(currentPassageEl.value);
});
}
$(window).on('showpassage:after', () => {
console.log('passage: ', window.setup.now());
if (DEBUG) {
document.querySelector('#currentPassage').value = window.setup.now();
}
});
setup.updateStats();
};
setup.updateStats = () => {
document.querySelector('#sidebar').innerHTML = window.story.render('Header');
};
setup.testSkill = (s) => {
const v = setup.roll1d6() + setup.roll1d6();
s.wasSkilled = v <= s.skill;
setup.refresh();
return s.wasSkilled;
};
setup.testLuck = (s) => {
const v = setup.roll1d6();
s.wasLucky = v <= s.luck;
setup.refresh();
return s.wasLucky;
};
setup.take = (s, item) => {
if (setup.has(s, item)) {
return;
}
s.inventory.push(item);
setup.updateStats();
setup.refresh();
};
setup.has = (s, item) => {
return s.inventory.indexOf(item) !== -1;
};
setup.drop = (s, item) => {
if (!setup.has(s, item)) {
return;
}
s.inventory = s.inventory.filter((it) => it !== item);
setup.updateStats();
setup.refresh();
};
setup.decrease = (s, attr, n = 1) => {
if (s[attr] < n) {
return;
}
s[attr] -= n;
if (attr === 'provisions') {
s.stamina = Math.min(s.staminaMax, s.stamina + n * 4);
}
setup.updateStats();
setup.refresh();
};
setup.defeated = (s, enemyName, i = 0) => {
const enemy = s.defeated[s.defeated.length - 1 - i];
return enemy === enemyName;
};
setup.now = () => {
const st = window.story;
return st.passages[st.history[st.history.length - 1]].name;
};
setup.refresh = () => {
window.story.show(setup.now());
};
setup.prepareFight = (s, enemies, config = {}) => {
if (s.fight) {
return;
}
if (setup.defeated(s, enemies[0].name)) {
return;
}
s.fight = { enemies, ...config };
};
setup.renderFightRound = (s) => {
if (s.fight.started) {
return (
s.fight.output.join('<br/>') +
s.fight.actions
.map((act) => `<a onclick="setup._fightRound(s, '${act}')">${act}</a>`)
.join(' ')
);
} else {
return '<a onclick="setup._fightRound(s)">fight</a>';
}
};
setup._fightRound = (s, action) => {
const f = s.fight;
function log(msg) {
msg = msg.replace(/>/g, '>');
f.output.push(msg);
}
function round(enemy) {
const enemyAttackStrength = setup.roll2d6() + enemy.skill;
const ourAttackStrength = setup.roll2d6() + s.skill;
const attacks =
'enemy:' + enemyAttackStrength + ', we:' + ourAttackStrength;
if (ourAttackStrength > enemyAttackStrength) {
log(` ${attacks} -> you wounded the enemy`);
enemy.stamina -= 2;
} else if (enemyAttackStrength > ourAttackStrength) {
log(` ${attacks} -> the enemy wounded you`);
s.stamina -= 2;
} else {
log(` ${attacks} -> draw`);
}
setup.updateStats();
if (s.stamina <= 0) {
log('game over');
}
if (enemy.stamina > 0) {
round(enemy);
} else {
log(enemy.name + ' died');
s._defeated.push(enemy.name);
f.actions.push('continue');
}
}
f.actions = [];
if (!f.started) {
f.output = [];
f.started = true;
f.enemy = f.enemies.shift();
log(`fighting ${f.enemy.name}`);
round(f.enemy);
} else if (action === 'continue') {
s.defeated = s._defeated;
s.fight = undefined;
} else {
log('?!');
}
setup.refresh();
};