forked from perscolas-rtt-se/Space_Battle_starter_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
175 lines (158 loc) · 5.86 KB
/
Copy pathscript.js
File metadata and controls
175 lines (158 loc) · 5.86 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
//creates an enemy class
class enemyValues {
constructor(hp = (Math.floor(Math.random() * 4) + 2),fp = (Math.floor(Math.random() * 2) + 1), ac = (Math.floor(Math.random()) * .4) + .2) {
this.hp = hp;
this.fp = fp;
this.ac = ac;
this.status = true;
}
}
//initiate list of enemies
let enemyList = [];
let enemyStats = document.querySelector(".enemyStats")
let enemyAttributes = enemyStats.innerText.split("\n")
let enemyAttributes2 = enemyAttributes[0].split(" ")
let enemyHull = enemyAttributes2[2];
enemyAttributes2 = enemyAttributes[1].split(" ")
let enemyFP = enemyAttributes2[2];
enemyAttributes2 = enemyAttributes[2].split(" ")
let enemyAC = enemyAttributes2[2];
// console.log(enemyAttributes2)
//set original enemy to object and pushes it to total enemies
let newEnemy = new enemyValues(enemyHull,enemyFP,enemyAC);
enemyList.push(newEnemy)
console.log(enemyList);
//makes multiple enemies
function createEnemies() {
let numEnemies = Math.floor(Math.random()*3) + 4;
for(let i = 1;i<=numEnemies;i++){
newEnemy = new enemyValues()
enemyList.push(newEnemy)
}
}
//calls the function
createEnemies()
console.log(enemyList[0].hp);
//create player
let playerStats = document.querySelector(".playerStats")
let playerAttributes = playerStats.innerText.split("\n")
let playerAttributes2 = playerAttributes[0].split(" ")
let playerHull = playerAttributes2[2];
playerAttributes2 = playerAttributes[1].split(" ")
let playerFP = playerAttributes2[2];
playerAttributes2 = playerAttributes[2].split(" ")
let playerAC = playerAttributes2[2];
//missles
let missles = 3;
playerStats.innerText = `Hull : ${playerHull}\nFirepower : ${playerFP}\nAccuracy : ${playerAC}\nMissles :${missles}`;
// console.log(playerAttributes2[2])
alert("Click on the ship to fight off the invader!")
//get id of img to set add event listener
let ship = document.querySelector(".playerImage");
let shipStatus = true;
console.log(enemyList.length);
let attackTarget;
//No megaship in the beginning
let megaShip = false;
ship.addEventListener("click", function(){
//ship status determines if the game is on
if(shipStatus==true){
//random shield function
if(Math.random()>.7){
alert("SHIELDS ACTIVATE\nHull's looking stronger");
playerHull+=3;
}
//set prompt for targetting attack command
let targetNum = (prompt(`There are ${enemyList.length}, which do you want to target?`))-1;
while(megaShip==true && enemyList.length>1 && targetNum==0){
alert("The Ship's weapons are in the way, try again")
targetNum = (prompt(`There are ${enemyList.length}, which do you want to target?`))-1;
}
console.log(targetNum)
attackTarget = enemyList[targetNum]
console.log(attackTarget)
attackCommand(attackTarget)
//checks if there are still enemues
if(playerHull<=0) {
alert("COMMANDER, CAN YOU HEAR ME?! COMAAAAANDEEER!!")
shipStatus=false;
} else if(attackTarget.hp<=0){
enemyList.splice(targetNum,1)
//if player defeats an enemy, let the player know
if(enemyList.length>0){
alert("Now's not the time to celebrate, more incoming")
console.log(enemyList)
} else {
alert("Congratulations soldier, you're going home!")
//if the player has enough health and enough missles, it starts the boss fight
if(megaShip==false && playerHull>10 && missles>2){
alert("Wait, What is that!");
alert("Oh no, it's the Mega-Ship!")
alert("You have to get through the weapons before you can attack the main hull (Enemy 1)")
//creates the boss
newEnemy = new enemyValues(10,8,0.1);
enemyList.push(newEnemy)
//creates the weapons
for(let i =0; i<4;i++){
newEnemy = new enemyValues (10, 2, 0.5)
enemyList.push(newEnemy)
}
enemyList.push()
console.log(enemyList[0])
attackTarget = enemyList[0];
megaShip = true;
} else {
shipStatus=false;
}
}
//if the player loses
}
//ask if the player wants to play again
} else {
let playAgain = confirm("Ready for another go trooper?");
if(playAgain == true){
playerHull = 15;
createEnemies();
shipStatus = true;
alert("Click on the ship to fight off the invader!")
attackTarget = enemyList[0]
} else {
alert('GAME OVER')
}
}
//resets the dom
playerStats.innerText = `Hull : ${playerHull}\nFirepower : ${playerFP}\nAccuracy : ${playerAC}\nMissles :${missles}`;
enemyStats.innerText = `Hull : ${attackTarget.hp}\nFirepower : ${attackTarget.fp}\nAccuracy : ${attackTarget.ac}`;
})
//Math damage
function attackCommand(target){
//missles
let useMissle;
if(missles>0){
useMissle = confirm(`Use Missle?`)
if(useMissle){
missles--;
}
}
//player attacks
if((Math.random())<playerAC){
if(useMissle){
target.hp-= ((playerFP*1)+3)
console.log(playerFP)
}else{
target.hp-= playerFP;
}
alert("What a Hit!")
} else {
alert("You'll get them next time")
}
//enemy attacks
for(let i in enemyList){
if((Math.random())<enemyList[i].ac && enemyList[i].hp>0){
playerHull-= enemyList[i].fp;
alert("Looks like the enemy got you")
} else {
alert(`That was close! Enemy ${(i*1)+1} almost got you.`)
}
}
}