-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.coffee
180 lines (128 loc) · 3.86 KB
/
game.coffee
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
Player = require('./entities/player')
World = require('./entities/world')
Array::remove = (e) -> @[t..t] = [] if (t = @.indexOf(e)) > -1
module.exports = class
constructor: (@io) ->
@players = []
@nextId = 1
go: ->
@world = new World 750, 750
@bullets = []
@io.sockets.on "connection", (client) =>
@.addClient client
@.nextCycle()
addClient: (client) ->
[x, y] = @world.nextSpawnPoint()
id = @nextId
@nextId += 1
player = new Player id, client, x, y
@players.push player
client.on "action.move", (direction) ->
player.addMoveDirection direction
client.on "action.stop", (direction) ->
player.removeMoveDirection direction
client.on "action.shoot", (weapon) ->
return unless ['laser', 'rocket'].indexOf(weapon) isnt -1
player.startShooting(weapon)
client.on "action.stopShooting", ->
player.stopShooting()
client.on "disconnect", =>
@players.remove(player)
# Send initial data
client.emit "game.init", {
world: @world.data()
id: player.id
count: @players.length
}
movePlayer: (player) ->
newX = player.x
newY = player.y
if player.move.left then newX -= 1
if player.move.right then newX += 1
if player.move.up then newY -= 1
if player.move.down then newY += 1
# X
free = true
for i in [-1..1]
for j in [-1..1]
free = false if @world.get( newX + i, player.y + j) > 0
for other in @players
unless other is player
free = false if Math.abs(other.x - newX) < 3 and Math.abs(other.y - player.y) < 3
if free
player.x = newX
# Y
free = true
for i in [-1..1]
for j in [-1..1]
free = false if @world.get( player.x + i, newY + j) > 0
for other in @players
unless other is player
free = false if Math.abs(other.x - player.x) < 3 and Math.abs(other.y - newY) < 3
if free
player.y = newY
resurrectPlayer: (player) ->
if player.cooldown == 0
[x, y] = @world.nextSpawnPoint()
player.resurrect x, y
nextCycle: ->
sounds =
shoot: false
hit: false
envhit: false
# Move & Shoot
for player in @players
player.doStep()
if player.dead
@.resurrectPlayer player
else
@.movePlayer player
if player.shooting and player.cooldown == 0
sounds.shoot = true
@bullets.push player.getBullet()
# Die
checkCollision = (bullet, playerId, x, y) =>
if @world.get(x,y) > 0
sounds.envhit = true
@world.hit(bullet, x, y)
return true
else
for player in @players
if playerId isnt player.id
for i in [-1..1]
for j in [-1..1]
if player.x == (x + i) and player.y == (y + j)
player.hit()
sounds.hit = true
return true
return false
remainingBullets = []
for bullet in @bullets
collision = false
if bullet.trajY == 0
for i in [bullet.x..(bullet.x + bullet.trajX)]
collision = true if checkCollision(bullet, bullet.playerId, i, bullet.y)
else
for i in [bullet.y..(bullet.y + bullet.trajY)]
collision = true if checkCollision(bullet, bullet.playerId, bullet.x, i)
remainingBullets.push( bullet ) unless collision
bulletData = []
for bullet in remainingBullets
bullet.x += bullet.trajX
bullet.y += bullet.trajY
bulletData.push bullet.data()
@bullets = remainingBullets
# Tell
playerData = []
for player in @players
playerData.push player.data()
@io.sockets.emit "game.step", {
players: playerData
bullets: bulletData
changes: @world.changelist
sounds: sounds
}
@world.resetChangeList()
setTimeout =>
@.nextCycle()
, 33