-
Notifications
You must be signed in to change notification settings - Fork 2
/
gameLogic.js
257 lines (239 loc) · 7.33 KB
/
gameLogic.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
//ship logic
//ideally: set up some sort of export where we can acquire board features within gameLogic, but that is too much work. :3
var Ship = function (id, x, y, facing, playerId, fleet, maxX, maxY) {
this.shipId = id;
this.position = {x : x, y : y};
this.type = 'Ship';
this.facing = facing;
this.lastPosition = {x : x, y : y};
this.playerId = playerId
this.fleet = fleet
this.health = 2
this.damage = 1
this.range = 3
this.maxX = maxX
this.maxY = maxY
return this
}
Ship.prototype.turn = function (direction) {
if (this.facing == "up") {
if (direction == "left"){
this.facing = "left"
}
else if (direction == "right"){
this.facing = "right"
}
else if (direction == "flip"){
this.facing = "down"
}
}
else if (this.facing == "right") {
if (direction == "left"){
this.facing = "up"
}
else if (direction == "right"){
this.facing = "down"
}
else if (direction == "flip"){
this.facing = "left"
}
}
else if (this.facing == "left") {
if (direction == "left"){
this.facing = "down"
}
else if (direction == "right"){
this.facing = "up"
}
else if (direction == "flip"){
this.facing = "right"
}
}
else if (this.facing == "down") {
if (direction == "left"){
this.facing = "right"
}
else if (direction == "right"){
this.facing = "left"
}
else if (direction == "flip"){
this.facing = "up"
}
}
else {
console.log("wat");
}
}
Ship.prototype.move = function (distance) {
var maxX = this.maxX
var maxY = this.maxY
var hasMoved = true
this.lastPosition = this.position;
var xPos = this.position.x;
var yPos = this.position.y;
if (this.facing == "up") {
this.position = {x: xPos, y: yPos - distance}
} else if (this.facing == "right") {
this.position = {x: xPos + distance, y: yPos}
} else if (this.facing == "left") {
this.position = {x: xPos - distance, y: yPos}
} else if (this.facing == "down") {
this.position = {x: xPos, y: yPos + distance}
} else {
console.log("wat");
}
var oldPos = this.lastPosition;
var oldX = oldPos.x;
var oldY = oldPos.y;
var newPos = this.position;
var newX = newPos.x;
var newY = newPos.y;
if (newX < 0 || newX > maxX || newY < 0 || newY > maxY) {
this.position = this.lastPosition
newX = this.position.x;
newY = this.position.y;
hasMoved = false
}
return hasMoved
}
Ship.prototype.takeDamage = function(gameBoard, playerLeftShips, playerRightShips, destroyedShips, damage){
console.log("TAKING DAMAGE")
console.log(this.shipId)
this.health = this.health - damage
if (this.health <= 0){
destroyedShips.destroyed.push({'shipId': this.shipId, 'manner': 'shots'})
//this.destroy(gameBoard, playerLeftShips, playerRightShips, destroyedShips, 'shots')
}
}
Ship.prototype.shoot = function(gameBoard, playerLeftShips, playerRightShips, destroyedShips){
console.log(this.shipId + " " + this.position.x + " " + this.position.y)
var maxX = this.maxX
var maxY = this.maxY
var xPos = this.position.x
var yPos = this.position.y
var bulletDest = {}
if (this.facing == "up"){
console.log("this ship is facing up")
var i = yPos - 1
for (var j = 0 ; j < this.range ; j = j + 1){
console.log("looping once")
console.log(i)
if (i >= 0){ //grid boundaries
console.log(gameBoard[xPos][i])
if (gameBoard[xPos][i] instanceof Ship){
console.log("FOUND A SHIP!!!!!!!!!!!!!!!!!!!!!!!!!!!")
ship = gameBoard[xPos][i]
ship.takeDamage(gameBoard, playerLeftShips, playerRightShips, destroyedShips, this.damage)
bulletDest.x = xPos
bulletDest.y = i
break
}
}
else{
bulletDest.x = xPos
bulletDest.y = 0
break
}
i = i - 1
}
if (bulletDest.x == null){
bulletDest.x = xPos
bulletDest.y = yPos - this.range
}
}
if (this.facing == "right"){
var i = xPos + 1
for (var j = 0 ; j < this.range ; j = j + 1){
if (i <= maxX ){ //grid boundaries
if (gameBoard[i][yPos] instanceof Ship){
ship = gameBoard[i][yPos]
ship.takeDamage(gameBoard, playerLeftShips, playerRightShips, destroyedShips, this.damage)
bulletDest.x = i
bulletDest.y = yPos
break
}
}
else{
bulletDest.x = maxX
bulletDest.y = yPos
break
}
i = i + 1
}
if (bulletDest.x == null){
bulletDest.x = xPos + this.range
bulletDest.y = yPos
}
}
if (this.facing == "left"){
var i = xPos - 1
for (var j = 0 ; j < this.range ; j = j + 1){
if(i >= 0){
if (gameBoard[i][yPos] instanceof Ship){
ship = gameBoard[i][yPos]
ship.takeDamage(gameBoard, playerLeftShips, playerRightShips, destroyedShips, this.damage)
bulletDest.x = i
bulletDest.y = yPos
break
}
}
else{
bulletDest.x = 0
bulletDest.y = yPos
break
}
i = i - 1
}
if (bulletDest.x == null){
bulletDest.x = xPos - this.range
bulletDest.y = yPos
}
}
if (this.facing == "down"){
var i = yPos+1
for (var j = 0 ; j < this.range ; j = j +1){
if (i <= maxY){
if (gameBoard[xPos][i] instanceof Ship){
ship = gameBoard[xPos][i]
ship.takeDamage(gameBoard, playerLeftShips, playerRightShips, destroyedShips, this.damage)
bulletDest.x = xPos
bulletDest.y = i
break
}
}
else{
bulletDest.x = xPos
bulletDest.y = maxY
break
}
i = i + 1
}
if (bulletDest.x == null){
bulletDest.x = xPos
bulletDest.y = yPos + this.range
}
}
return bulletDest
}
Ship.prototype.destroy = function (gameBoard, playerLeftShips, playerRightShips, changes, manner){
if (this.fleet === 'playerLeftShips'){
delete playerLeftShips[this.shipId]
}
else if(this.fleet === 'playerRightShips'){
delete playerRightShips[this.shipId]
}
if ('manner' === 'crashed'){
changes.destroyed.push({'shipId': this.shipId, 'manner': manner})
}
gameBoard[this.position.x][this.position.y] = new Space(this.position.x,this.position.y);
console.log("done destroying")
}
var Space = function (x,y) {
this.shipId = null;
this.position = {x: x, y: y};
this.type = "Space"
this.bounced = false
return this
}
module.exports.Ship = Ship
module.exports.Space = Space