├── index.html
├── styles.css
└── game.js
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | Text Adventure
10 |
11 |
12 |
13 |
Text
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/styles.css:
--------------------------------------------------------------------------------
1 | *, *::before, *::after {
2 | box-sizing: border-box;
3 | font-family: Gotham Rounded;
4 | }
5 |
6 | body {
7 | padding: 0;
8 | margin: 0;
9 | display: flex;
10 | justify-content: center;
11 | align-items: center;
12 | width: 100vw;
13 | height: 100vh;
14 | background-color: #333;
15 | }
16 |
17 | .container {
18 | width: 800px;
19 | max-width: 80%;
20 | background-color: white;
21 | padding: 10px;
22 | border-radius: 5px;
23 | box-shadow: 0 0 10px 2px;
24 | }
25 |
26 | .btn-grid {
27 | display: grid;
28 | grid-template-columns: repeat(2, auto);
29 | gap: 10px;
30 | margin-top: 20px;
31 | }
32 |
33 | .btn {
34 | background-color: hsl(200, 100%, 50%);
35 | border: 1px solid hsl(200, 100%, 30%);
36 | border-radius: 5px;
37 | padding: 5px 10px;
38 | color: white;
39 | outline: none;
40 | }
41 |
42 | .btn:hover {
43 | border-color: black;
44 | }
--------------------------------------------------------------------------------
/game.js:
--------------------------------------------------------------------------------
1 | const textElement = document.getElementById('text')
2 | const optionButtonsElement = document.getElementById('option-buttons')
3 |
4 | let state = {}
5 |
6 | function startGame() {
7 | state = {}
8 | showTextNode(1)
9 | }
10 |
11 | function showTextNode(textNodeIndex) {
12 | const textNode = textNodes.find(textNode => textNode.id === textNodeIndex)
13 | textElement.innerText = textNode.text
14 | while (optionButtonsElement.firstChild) {
15 | optionButtonsElement.removeChild(optionButtonsElement.firstChild)
16 | }
17 |
18 | textNode.options.forEach(option => {
19 | if (showOption(option)) {
20 | const button = document.createElement('button')
21 | button.innerText = option.text
22 | button.classList.add('btn')
23 | button.addEventListener('click', () => selectOption(option))
24 | optionButtonsElement.appendChild(button)
25 | }
26 | })
27 | }
28 |
29 | function showOption(option) {
30 | return option.requiredState == null || option.requiredState(state)
31 | }
32 |
33 | function selectOption(option) {
34 | const nextTextNodeId = option.nextText
35 | if (nextTextNodeId <= 0) {
36 | return startGame()
37 | }
38 | state = Object.assign(state, option.setState)
39 | showTextNode(nextTextNodeId)
40 | }
41 |
42 | const textNodes = [
43 | {
44 | id: 1,
45 | text: 'You wake up in a strange place and you see a jar of blue goo near you.',
46 | options: [
47 | {
48 | text: 'Take the goo',
49 | setState: { blueGoo: true },
50 | nextText: 2
51 | },
52 | {
53 | text: 'Leave the goo',
54 | nextText: 2
55 | }
56 | ]
57 | },
58 | {
59 | id: 2,
60 | text: 'You venture forth in search of answers to where you are when you come across a merchant.',
61 | options: [
62 | {
63 | text: 'Trade the goo for a sword',
64 | requiredState: (currentState) => currentState.blueGoo,
65 | setState: { blueGoo: false, sword: true },
66 | nextText: 3
67 | },
68 | {
69 | text: 'Trade the goo for a shield',
70 | requiredState: (currentState) => currentState.blueGoo,
71 | setState: { blueGoo: false, shield: true },
72 | nextText: 3
73 | },
74 | {
75 | text: 'Ignore the merchant',
76 | nextText: 3
77 | }
78 | ]
79 | },
80 | {
81 | id: 3,
82 | text: 'After leaving the merchant you start to feel tired and stumble upon a small town next to a dangerous looking castle.',
83 | options: [
84 | {
85 | text: 'Explore the castle',
86 | nextText: 4
87 | },
88 | {
89 | text: 'Find a room to sleep at in the town',
90 | nextText: 5
91 | },
92 | {
93 | text: 'Find some hay in a stable to sleep in',
94 | nextText: 6
95 | }
96 | ]
97 | },
98 | {
99 | id: 4,
100 | text: 'You are so tired that you fall asleep while exploring the castle and are killed by some terrible monster in your sleep.',
101 | options: [
102 | {
103 | text: 'Restart',
104 | nextText: -1
105 | }
106 | ]
107 | },
108 | {
109 | id: 5,
110 | text: 'Without any money to buy a room you break into the nearest inn and fall asleep. After a few hours of sleep the owner of the inn finds you and has the town guard lock you in a cell.',
111 | options: [
112 | {
113 | text: 'Restart',
114 | nextText: -1
115 | }
116 | ]
117 | },
118 | {
119 | id: 6,
120 | text: 'You wake up well rested and full of energy ready to explore the nearby castle.',
121 | options: [
122 | {
123 | text: 'Explore the castle',
124 | nextText: 7
125 | }
126 | ]
127 | },
128 | {
129 | id: 7,
130 | text: 'While exploring the castle you come across a horrible monster in your path.',
131 | options: [
132 | {
133 | text: 'Try to run',
134 | nextText: 8
135 | },
136 | {
137 | text: 'Attack it with your sword',
138 | requiredState: (currentState) => currentState.sword,
139 | nextText: 9
140 | },
141 | {
142 | text: 'Hide behind your shield',
143 | requiredState: (currentState) => currentState.shield,
144 | nextText: 10
145 | },
146 | {
147 | text: 'Throw the blue goo at it',
148 | requiredState: (currentState) => currentState.blueGoo,
149 | nextText: 11
150 | }
151 | ]
152 | },
153 | {
154 | id: 8,
155 | text: 'Your attempts to run are in vain and the monster easily catches.',
156 | options: [
157 | {
158 | text: 'Restart',
159 | nextText: -1
160 | }
161 | ]
162 | },
163 | {
164 | id: 9,
165 | text: 'You foolishly thought this monster could be slain with a single sword.',
166 | options: [
167 | {
168 | text: 'Restart',
169 | nextText: -1
170 | }
171 | ]
172 | },
173 | {
174 | id: 10,
175 | text: 'The monster laughed as you hid behind your shield and ate you.',
176 | options: [
177 | {
178 | text: 'Restart',
179 | nextText: -1
180 | }
181 | ]
182 | },
183 | {
184 | id: 11,
185 | text: 'You threw your jar of goo at the monster and it exploded. After the dust settled you saw the monster was destroyed. Seeing your victory you decide to claim this castle as your and live out the rest of your days there.',
186 | options: [
187 | {
188 | text: 'Congratulations. Play Again.',
189 | nextText: -1
190 | }
191 | ]
192 | }
193 | ]
194 |
195 | startGame()
--------------------------------------------------------------------------------