-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrules.js
92 lines (81 loc) · 3.82 KB
/
rules.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
let unlocked = {};
let inventory = {};
class Start extends Scene {
create() {
this.engine.setTitle(this.engine.storyData["Title"]); // TODO: replace this text using this.engine.storyData to find the story title
this.engine.addChoice("Begin the story");
}
handleChoice() {
this.engine.gotoScene(Location, this.engine.storyData["InitialLocation"]); // TODO: replace this text by the initial location of the story
}
}
class Location extends Scene {
create(key) {
let locationData = key; // TODO: use `key` to get the data object for the current story location
if (unlocked[locationData]) {
console.log("In unlocked part");
this.engine.show(this.engine.storyData["Locations"][locationData]["UnlockBody"]);
if(this.engine.storyData["Locations"][locationData]["UnlockChoices"]) { // TODO: check if the location has any Choices
for(let choice of this.engine.storyData["Locations"][locationData]["UnlockChoices"]) { // TODO: loop over the location's Choices
this.engine.addChoice(choice["Text"], choice); // TODO: use the Text of the choice
// TODO: add a useful second argument to addChoice so that the current code of handleChoice below works
}
} else {
this.engine.addChoice("The end.")
}
if (this.engine.storyData["Locations"][locationData]["UnlockItems"]) {
for (let choice of this.engine.storyData["Locations"][locationData]["UnlockItems"]) {
this.engine.addItem(choice["Text"], choice);
}
}
} else {
this.engine.show(this.engine.storyData["Locations"][locationData]["Body"]); // TODO: replace this text by the Body of the location data
if(this.engine.storyData["Locations"][locationData]["Choices"]) { // TODO: check if the location has any Choices
for(let choice of this.engine.storyData["Locations"][locationData]["Choices"]) { // TODO: loop over the location's Choices
this.engine.addChoice(choice["Text"], choice); // TODO: use the Text of the choice
// TODO: add a useful second argument to addChoice so that the current code of handleChoice below works
}
} else {
this.engine.addChoice("The end.")
}
if (this.engine.storyData["Locations"][locationData]["Items"]) {
for (let choice of this.engine.storyData["Locations"][locationData]["Items"]) {
this.engine.addItem(choice["Text"], choice);
}
}
}
}
handleChoice(choice) {
if(choice) {
this.engine.show("> "+choice.Text);
if (choice["ItemRequired"] && inventory[choice["ItemRequired"]]) {
this.engine.gotoScene(Location, choice.ItemTarget);
} else {
this.engine.gotoScene(Location, choice.Target);
}
} else {
this.engine.gotoScene(End);
}
}
handleItem(choice) {
if(choice["Type"] == "Unlock") {
this.engine.show("> "+choice.Text);
this.engine.show(choice["UseText"]);
unlocked[choice["Result"]] = true;
this.engine.gotoScene(Location, choice.Target);
} else if (choice["Type"] == "Key") {
console.log("here");
this.engine.show("> "+choice.Text);
this.engine.show(choice["UseText"]);
inventory[choice["Name"]] = true;
this.engine.gotoScene(Location, choice.Target);
}
}
}
class End extends Scene {
create() {
this.engine.show("<hr>");
this.engine.show(this.engine.storyData.Credits);
}
}
Engine.load(Start, 'myStory.json');