-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
163 lines (143 loc) · 4.49 KB
/
script.js
File metadata and controls
163 lines (143 loc) · 4.49 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
// Imports
import { addBreak, dBreak, randomNumber } from './utils.js';
// Functions
function focusTextareaToBottom() {
textOutput.scrollTop = textOutput.scrollHeight;
}
function clearTextarea() {
textOutput.value = '';
}
// const items = ['basil', 'leek', 'potato', 'gold'];
//Variables
const name = prompt("Profession Simulator! Please enter your character's name: ");
let level = '1';
let profession = 'Gatherer';
let basilCount = 0;
let leekCount = 0;
let potatoCount = 0;
let soupCount = 0;
let gold = 0;
let outputHistory = [];
// Title/Loading heading
const title = document.getElementById("title");
title.innerText = "Profession Simulator v1.0";
// Character Pane
const nameLabel = document.createElement("label");
if (name != null) {
nameLabel.innerText = `Name: ${name}`;
document.body.appendChild(nameLabel);
}
else {
nameLabel.innerText = `Name: Player 1`;
document.body.appendChild(nameLabel);
}
const levelLabel = document.createElement("label");
levelLabel.innerText = `Level: ${level}`;
document.body.appendChild(levelLabel);
const professionLabel = document.createElement("label");
professionLabel.innerText = `Profession: ${profession}`;
document.body.appendChild(professionLabel);
dBreak();
// Output Textarea
const textOutput = document.createElement("textarea");
document.body.appendChild(textOutput);
dBreak();
// Button Row
// Search Button
const searchButton = document.createElement("button");
searchButton.innerText = "Search";
searchButton.addEventListener('click', () => {
let rollValue = randomNumber();
if (rollValue == 0) {
while (rollValue == 0) {
rollValue = randomNumber();
}
}
const events = [
{
range: [1, 20],
action: () => {
textOutput.value += "You find a basil leaf.\n";
basilCount += 1;
textOutput.value += `Basil Count: ${basilCount}\n\n`;
focusTextareaToBottom();
}
},
{
range: [21, 40],
action: () => {
textOutput.value += "You find a leek.\n";
leekCount += 1;
textOutput.value += `Leek Count: ${leekCount}\n\n`;
focusTextareaToBottom();
}
},
{
range: [41, 60],
action: () => {
textOutput.value += "You find a potato.\n";
potatoCount += 1;
textOutput.value += `Potato Count: ${potatoCount}\n\n`;
focusTextareaToBottom();
}
},
{
range: [61, 80],
action: () => {
textOutput.value += "You find some gold.\n";
gold += 5;
textOutput.value += `Gold Count: ${gold}\n\n`;
focusTextareaToBottom();
}
},
{
range: [81, 100],
action: () => {
textOutput.value += "You find nothing.\n\n";
focusTextareaToBottom();
}
}
];
let eventTriggered = false;
for (const event of events) {
const [min, max] = event.range;
if (rollValue >= min && rollValue <= max) {
event.action(); // Execute the action function
eventTriggered = true;
break;
}
}
if (!eventTriggered) {
textOutput.value += `${rollValue}: Error during roll. Event not found!\n\n`;
focusTextareaToBottom();
}
});
document.body.appendChild(searchButton);
// Craft Button
const craftButton = document.createElement("button");
craftButton.innerText = "Craft";
craftButton.addEventListener('click', () => {
if (basilCount >= 1 && leekCount >= 1 && potatoCount >= 1) {
textOutput.value += "You craft a bowl of soup.\n";
soupCount += 1;
basilCount -= 1;
leekCount -= 1;
potatoCount -=1;
textOutput.value += `Soup Count: ${soupCount}\n\n`;
focusTextareaToBottom();
} else {
textOutput.value += "You don't have enough ingredients!\n\n";
focusTextareaToBottom();
}
});
document.body.appendChild(craftButton);
// Clear History Button
const clearHistoryButton = document.createElement("button");
clearHistoryButton.innerText = 'Clear History';
clearHistoryButton.addEventListener('click', () => {
outputHistory = [];
textOutput.value = '';
textOutput.value += "History has been succesfully cleared.\n\n";
focusTextareaToBottom();
});
document.body.appendChild(clearHistoryButton);