-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
180 lines (134 loc) · 4.52 KB
/
main.js
File metadata and controls
180 lines (134 loc) · 4.52 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
// check out https://www.vandelaydesign.com/parallax-scrolling-best-practices-examples-and-tutorials/
// localStorage.clear();
console.log("...may the odds be ever in our favor.")
// opening stats for game level, player, and opponents
let gameLevel = 1
let mage = {
name: 'Sage',
hp: parseInt(localStorage.getItem('hp')) || 200,
powerLevel: parseInt(localStorage.getItem('powerLevel')) || 1,
spells: {
shift: parseInt(localStorage.getItem('shift')) || 12,
attack: parseInt(localStorage.getItem('attack')) || 12
}
}
// Variables that populate the stats bar.
$('#HP').text('Health: '+ mage.hp)
$('#powerLevel').text('Power Level: ' + mage.powerLevel)
$('#attackSpells').text('Attack Spells Left: ' + mage.spells['attack'])
$('#shiftSpells').text('Shift Spells Left: ' + mage.spells['shift'])
let machine = {
name: 'Knier\'s Machine',
hp: 50*gameLevel,
powerLevel: 1,
enemies : //JSON.parse(localStorage.getItem("enemies")) ||
[$('#machine1'),$('#machine2'),
$('#machine3'),$('#machine4'),
$('#machine5'),$('#machine6')]
}
//modular functions, for spells and leveling up, etc.
let saveStats = () =>{
localStorage.setItem('hp', mage.hp);
localStorage.setItem('powerLevel', mage.powerLevel);
localStorage.setItem('shift', mage.spells['shift']);
localStorage.setItem('attack', mage.spells['attack']);
localStorage.setItem("enemies", JSON.stringify(machine.enemies));
// machine.enemies = JSON.parse(localStorage.getItem("enemies")) // makes the array unreadable.
}
let isDefeated = (success) =>{
if (success == true) {
let currentEnemy = machine.enemies[0]
currentEnemy.css('display', 'none');
machine.enemies.splice(0,1);
levelUP();
console.log('The machine has '+ machine.hp +' health points remaining')
machine.hp = 50 * gameLevel;
}
}
let levelUP = () => {
if (machine.enemies.length == 5) {
$('#portal1').css('display','flex');
gameLevel++
} else if (machine.enemies.length == 3) {
$('#portal2').css('display','flex');
gameLevel++
} else if (machine.enemies.length == 0) {
$('#portal3').css('display','flex');
gameLevel++
}
console.log('levelUP ran. now on game level' + gameLevel)
}
let attack = (attacker,opponent) => {
let success = false
let diceRoll = Math.floor(Math.random() * 13);
if (diceRoll>=1) {
$('#spellStatus').text("Success!");
opponent.hp -= (50 * attacker.powerLevel)
if (machine.hp <= 0) {
success = true
isDefeated(success);
}
} else {
$('#spellStatus').text("A miss!");
}
saveStats();
}
const shift = () => {
let success = false
let diceRoll = Math.floor(Math.random() * 13);
if (diceRoll>=1) {
$('#spellStatus').text("Success!"); //pushes a message that says "success!" to screen
success = true;
mage.powerLevel++;
} else {
$('#spellStatus').text("A miss!");
}
console.log(success);
isDefeated(success);
saveStats();
}
// event listeners and click functions
$("#shiftButton").click(function(){
if (mage.spells['shift'] > 0){
mage.spells['shift']--
$('#shiftSpells').text('Shift Spells Left: ' + mage.spells['shift'])
shift();
}
});
$("#attackButton").click(function(){
if (mage.spells['attack'] > 0){
mage.spells['attack']--
$('#attackSpells').text('Attack Spells Left: ' + mage.spells['attack'])
if (mage.hp > 0 && machine.hp > 0){
attack(mage,machine);
}
if (machine.hp > 0){
attack(machine,mage)
$('#HP').text('Health: ' + mage.hp)
}
}
});
$("#portalButton").click(function(){
if (mage.hp > 0 ){
$('#winningMessage').css('display','flex')
}
localStorage.clear();
});
$(document).keydown(function(e){
let $player = $('#player')
if(($player.css('left') > '0px') && ($player.css('left') !== ((3000-750) +'px'))){
console.log('keydown firing')
switch (e.which){
case 37: //left arrow key
$player.finish().animate({
left: "-=60",
});
break;
case 39: //right arrow key
$player.finish().animate({
left: "+=60",
});
break;
}
}
});