Skip to content

Commit

Permalink
Flatten ball.pos struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Oregu committed May 4, 2013
1 parent 43a920e commit d54b106
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 73 deletions.
16 changes: 8 additions & 8 deletions game/game.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ module.exports = class Game extends GameCore
sendMove: (sid) ->
g = @gamers[sid]
@gs[g.side].updates = g.updates
@ball.t = @updateTime
g.socket.emit 'move', {gamers: @gs, ball: @ball}

sendMoveAll: ->
Expand All @@ -52,15 +51,15 @@ module.exports = class Game extends GameCore
@sendScore sid

updateState: (sid, dir, seq) ->
@gamers[sid].updates.push {dir: dir, seq: seq, t: @time()}
@gamers[sid].updates.push {dir: dir, seq: seq, t: @time()} # FIXME not serverTime this should be

placeBall: (side) ->
@ball.pos.y = @gs[side].pos + @racketHeight / 2 - @ballSize / 2
@ball.y = @gs[side].pos + @racketHeight / 2 - @ballSize / 2
if side == 0
@ball.pos.x = @ballResetOffset
@ball.x = @ballResetOffset
@ball.angle = Math.asin((@gs[1].pos - @gs[0].pos) / (@canvasWidth - 2 * @xOffset))
else
@ball.pos.x = @canvasWidth - @ballResetOffset - @ballSize
@ball.x = @canvasWidth - @ballResetOffset - @ballSize
@ball.angle = Math.PI + Math.asin((@gs[1].pos - @gs[0].pos) / (@canvasWidth - 2 * @xOffset))
@ball.v = @initBallV

Expand All @@ -76,12 +75,12 @@ module.exports = class Game extends GameCore
@gs[gamer.side].updates = [] # FIXME seems wrong, clear after updates sent only

checkScoreUpdate: ->
if @ball.pos.x < 0 or @ball.pos.x > @canvasWidth - @ballSize
if @ball.x < 0 or @ball.x > @canvasWidth - @ballSize
side = -1
if @ball.pos.x < 0
if @ball.x < 0
@scores[1] += 1
side = 0
if @ball.pos.x > @canvasWidth - @ballSize
if @ball.x > @canvasWidth - @ballSize
@scores[0] += 1
side = 1
@placeBall side
Expand All @@ -105,6 +104,7 @@ module.exports = class Game extends GameCore
@moveRackets time
@ball.t = @updateTime
@ball = @moveBall [@ball], time, (time - @updateTime)
@ball.t = time
@checkScoreUpdate()
@sendMoveAll()
@updateTime = time
Expand Down
35 changes: 17 additions & 18 deletions game/game.core.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ class GameCore
@speedUp = 0.9 # ball speed up coefficient, should be lte than 1

@ball =
pos:
x: (@canvasWidth / 2 - @ballSize / 2)
y: (@canvasHeight / 2 - @ballSize / 2)
x: (@canvasWidth / 2 - @ballSize / 2)
y: (@canvasHeight / 2 - @ballSize / 2)
angle: ((20 + Math.random()*50)*Math.PI/180)
v: 0.2 # speed in px per ms
t: 0
Expand Down Expand Up @@ -67,34 +66,34 @@ class GameCore

moveBallBit: (ball, dt) ->
ds = ball.v * dt
ball.pos.x += ds * Math.cos(ball.angle)
ball.pos.y += ds * Math.sin(ball.angle)
ball.x += ds * Math.cos(ball.angle)
ball.y += ds * Math.sin(ball.angle)
ball.t += dt
return @checkBallCollision ball

checkBallCollision: (ball) ->
if ball.pos.y < 0
ball.pos.y = 0
if ball.y < 0
ball.y = 0
ball.angle = - ball.angle
else if ball.pos.y > @canvasHeight - @ballSize
ball.pos.y = @canvasHeight - @ballSize
else if ball.y > @canvasHeight - @ballSize
ball.y = @canvasHeight - @ballSize
ball.angle = - ball.angle
else if ball.pos.x <= @xOffset
if ball.pos.y >= @gs[0].pos && ball.pos.y <= @gs[0].pos + @racketHeight - @ballSize
ball.pos.x = @xOffset
else if ball.x <= @xOffset
if ball.y >= @gs[0].pos && ball.y <= @gs[0].pos + @racketHeight - @ballSize
ball.x = @xOffset
ball.angle = Math.PI - ball.angle
ball.v = ball.v * (@speedUp + Math.abs(ball.pos.y - @gs[0].pos + @racketHeight / 2) / (@gs[0].pos + @racketHeight / 2))
ball.v = ball.v * (@speedUp + Math.abs(ball.y - @gs[0].pos + @racketHeight / 2) / (@gs[0].pos + @racketHeight / 2))
if ball.v >= @maxBallV
ball.v = @maxBallV
else if ball.v <= @minBallV
ball.v = @minBallV
else
ball.v = ball.v * (@speedUp + Math.abs(ball.pos.y - @gs[0].pos + @racketHeight / 2) / (@gs[0].pos + @racketHeight / 2))
else if ball.pos.x >= @canvasWidth - @xOffset - @ballSize
if ball.pos.y >= @gs[1].pos && ball.pos.y <= @gs[1].pos + @racketHeight - @ballSize
ball.pos.x = @canvasWidth - @xOffset - @ballSize
ball.v = ball.v * (@speedUp + Math.abs(ball.y - @gs[0].pos + @racketHeight / 2) / (@gs[0].pos + @racketHeight / 2))
else if ball.x >= @canvasWidth - @xOffset - @ballSize
if ball.y >= @gs[1].pos && ball.y <= @gs[1].pos + @racketHeight - @ballSize
ball.x = @canvasWidth - @xOffset - @ballSize
ball.angle = Math.PI - ball.angle
ball.v = ball.v * (@speedUp + Math.abs(ball.pos.y - @gs[0].pos + @racketHeight / 2) / (@gs[0].pos + @racketHeight / 2))
ball.v = ball.v * (@speedUp + Math.abs(ball.y - @gs[0].pos + @racketHeight / 2) / (@gs[0].pos + @racketHeight / 2))
if ball.v >= @maxBallV
ball.v = @maxBallV
else if ball.v <= @minBallV
Expand Down
36 changes: 17 additions & 19 deletions game/game.core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions game/game.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/javascripts/pong.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ window.Game = class Game extends GameCore

drawBall: (ball, color) ->
@ctx.fillStyle = color
@ctx.fillRect ball.pos.x, ball.pos.y, @ballSize, @ballSize
@ctx.fillRect ball.x, ball.y, @ballSize, @ballSize

drawBoard: ->
@ctx.clearRect 0, 0, @canvasWidth, @canvasHeight
Expand Down
38 changes: 18 additions & 20 deletions public/javascripts/pong.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d54b106

Please sign in to comment.