-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
257 lines (239 loc) · 6.77 KB
/
main.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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
var items = {
wooden_sword: {
name: "Wooden Sword",
attack: 1,
attack_speed: 0.6
},
stone_sword: {
name: "Stone Sword",
attack: 2,
attack_speed: 0.8
},
op_sword: {
name: "OP SWORD",
attack: 999,
attack_speed: 0.0001
}
};
var player = {
health: 10,
maxHealth: 10,
gold: 0,
level: 1,
xp: 0,
xpNeeded: 10,
multiplier: 1.20,
weapon: null,
};
var enemy = {
name: "Goblin",
health: 5,
maxHealth: 5,
xpdrop: 5,
golddrop: 5,
Eattack: 1,
healthmin: 1,
healthmax: 7
};
var inventory = {
materials: {
wood: {
name: "Wood",
amount: 0
}
},
food: {
apple: {
name: "Apple",
health: 2,
amount: 0
}
}
};
var defaultinv = inventory;
var canAttack = true;
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.font = "14px Arial";
ctx.fillStyle = "red";
var damagetextx = 75;
var damagetexty = 100;
var damagetext;
//Make a inventory with selling system
//Add a THE OLDE SHOPPE
var gameLoop = setInterval("update()", 100);
var gui = setInterval("updateGUI()", 100);
var autoSave = setInterval("save()", 30000);
var log = document.getElementById("log");
function levelUp() {
player.xp = 0;
player.xpNeeded = Math.round(player.xpNeeded * player.multiplier);
player.level += 1;
player.maxHealth += 2;
player.health = player.maxHealth;
console.log("Level up! Level: " + player.level + " Xpneeded: " + player.xpNeeded);
}
function update() {
if(player.xp >= player.xpNeeded) {
levelUp();
}
if(enemy.health <= 0) {
player.gold += enemy.golddrop;
player.xp += enemy.xpdrop;
generateEnemy();
}
if(player.health <= 0) {
logText("You Died!");
player.health = player.maxHealth;
player.gold -= 5;
if(player.gold < 0 ) {
player.gold = 0;
}
}
}
function reset() {
confirm("Are you sure you want to reset?");
player = {
health: 10,
maxHealth: 10,
gold: 0,
level: 1,
xp: 0,
xpNeeded: 10,
multiplier: 1.20,
weapon: items.wooden_sword,
};
enemy = {
name: "Goblin",
health: 5,
maxHealth: 5,
xpdrop: 5,
golddrop: 5,
Eattack: 1,
healthmin: 1,
healthmax: 7
}
inventory = defaultinv;
generateEnemy();
save();
}
function save() {
localStorage.setItem("saveGame", JSON.stringify(player));
localStorage.setItem("enemy", JSON.stringify(enemy));
localStorage.setItem("inventory", JSON.stringify(inventory))
}
function load() {
if(localStorage == null) {
save();
console.log("No data found or outdated data found");
return;
}
console.log(JSON.parse(localStorage.getItem("saveGame")));
player = JSON.parse(localStorage.getItem("saveGame"));
enemy = JSON.parse(localStorage.getItem("enemy"));
inventory = JSON.parse(localStorage.getItem("inventory"))
}
function eat(itemtoeat) {
if(itemtoeat.health == undefined) return;
if(itemtoeat.amount <= 0) return;
if(player.health + itemtoeat.health > player.maxHealth) {
return;
}
player.health += itemtoeat.health;
itemtoeat.amount -= 1;
if(player.health > player.maxHealth) player.health = player.maxHealth;
}
function updateGUI() {
document.getElementById("Ehealth").innerHTML = "Enemy Health: " + enemy.health + "/" + enemy.maxHealth;
document.getElementById("health").innerHTML = "Health: " + player.health + "/" + player.maxHealth;
document.getElementById("gold").innerHTML = "Gold: " + player.gold;
document.getElementById("level").innerHTML = "Level: " + player.level;
document.getElementById("attack").innerHTML = "Damage: " + player.weapon.attack;
document.getElementById("attackspeed").innerHTML = "Attack Speed: " + player.weapon.attack_speed;
document.getElementById("apples").innerHTML = "Apples: " + inventory.food.apple.amount;
document.getElementById("wood").innerHTML = "Wood: " + inventory.materials.wood.amount;
}
function generateEnemy() {
enemy.name = "Goblin";
enemy.maxHealth = Math.floor((Math.random() * Math.round(enemy.healthmax)) + Math.round(enemy.healthmin));
enemy.health = enemy.maxHealth;
enemy.xpdrop = Math.floor((Math.random() * 9) + 1);
enemy.golddrop = Math.floor((Math.random() * 9) + 1);
enemy.Eattack = Math.floor((Math.random() * 2) + 1);
enemy.healthmin += 0.20;
enemy.healthmax += 0.10;
logText("A new enemy has spawned");
}
function damageAnimation(damageamount, critical) {
if(critical == true) {ctx.fillStyle = "yellow";}
ctx.globalAlpha = 1;
var interval = setInterval(() => {
if(damagetexty <= 70) {
ctx.fillStyle = "red";
clearInterval(interval);
}
damagetextx -= Math.random() * 7 - 2;
damagetexty -= 3;
ctx.globalAlpha -= 0.20;
ctx.clearRect(0,0, canvas.width,canvas.height);
ctx.fillText("-" + damageamount,damagetextx,damagetexty);
}, 100);
damagetextx = 75;
damagetexty = 100;
}
function attack() {
var isCritical = false;
if(!canAttack) return;
var randnum = Math.floor(Math.random() * 11)
if(randnum == 6 || randnum == 7) isCritical = true;
canAttack = false;
setTimeout(() => {
canAttack = true;
}, player.weapon.attack_speed * 1000);
if(isCritical == true) {
enemy.health -= player.weapon.attack * 2;
} else {
enemy.health -= player.weapon.attack;
}
player.health -= enemy.Eattack;
if(isCritical == true) {
damageAnimation(player.weapon.attack * 2, true);
} else {
damageAnimation(player.weapon.attack, false);
}
}
function logText(message) {
if(log.childNodes.length >= 9) {
log.children[0].innerHTML = message;
return;
}
element = document.createElement("li");
element.innerHTML = message;
log.appendChild(element);
}
function changeWeapon(weapontochange) {
player.weapon = weapontochange;
player.weapon = weapontochange;
}
function gatherForest() {
var found_apples;
var found_wood;
found_apples = Math.floor(Math.random() * 4);
found_wood = Math.floor(Math.random() * 5);
logText("You gathered in the forest and found " + found_wood + " wood and " + found_apples + " apples");
inventory.food.apple.amount += found_apples;
inventory.materials.wood.amount += found_wood;
};
function ObjectLength( object ) {
var length = 0;
for( var key in object ) {
if( object.hasOwnProperty(key) ) {
++length;
}
}
return length;
};
window.onbeforeunload = function() {
save();
};
load();