Skip to content

Commit

Permalink
Updated to handle both mod.it and server code without needing to do a…
Browse files Browse the repository at this point in the history
…nything special
  • Loading branch information
Myztiq committed Mar 8, 2013
1 parent a269c34 commit 01565d5
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 98 deletions.
34 changes: 26 additions & 8 deletions public/coffeescripts/car.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,17 @@ class Car

revoluteJointDef.Initialize(axle2, wheel2, wheel2.GetWorldCenter())
@motor2 = world.CreateJoint(revoluteJointDef)
@tire = new Image()
@tire.src = '/images/tire.jpg'
@tire.onload = =>
if MODIT?
@tire = MODIT.getImage('tire')
@initGraphics(graphics)
else
@tire = new Image()
@tire.src = '/images/tire.jpg'
@tire.onload = =>
@initGraphics(graphics)


# @tire = MODIT.getImage('tire')
# @initGraphics(graphics)



initGraphics: (graphics)=>
body = new createjs.Shape();
Expand Down Expand Up @@ -136,13 +139,13 @@ class Car
body.graphics.beginFill("red").drawRoundRect(0, 0, 2.2*scale*2, 0.5*scale*2, 5);
body.regX = 2.2*scale;
body.regY = .5*scale;
graphics.trackObject(body, @carBody, true)
graphics.trackObject(body, @carBody, {isCameraObject:true})

body = new createjs.Shape();
body.graphics.beginFill("red").drawRoundRect(0, 0, .8*scale*2, 0.4*scale*2, 5);
body.regX = 1.2*scale;
body.regY = -.4*scale;
graphics.trackObject(body, @carBody, true)
graphics.trackObject(body, @carBody)



Expand All @@ -158,6 +161,17 @@ class Car
@wheel2.SetPositionAndAngle(new b2Vec2(startPosition.x-1.4, startPosition.y+1), startPosition.angle)
@wheel2.SetLinearVelocity(new b2Vec2(0,0))
@wheel2.SetAngularVelocity(0)


@axle1.SetPositionAndAngle(new b2Vec2(startPosition.x, startPosition.y), startPosition.angle)
@axle1.SetLinearVelocity(new b2Vec2(0,0))
@axle1.SetAngularVelocity(0)


@axle2.SetPositionAndAngle(new b2Vec2(startPosition.x, startPosition.y), startPosition.angle)
@axle2.SetLinearVelocity(new b2Vec2(0,0))
@axle2.SetAngularVelocity(0)


update: (controls)=>
#Springs
Expand All @@ -172,6 +186,8 @@ class Car
#z/x tilt
tiltTorque = 100



@spring1.SetMaxMotorForce(force+Math.abs(tension*Math.pow(@spring1.GetJointTranslation(), 2)));
@spring1.SetMotorSpeed((@spring1.GetMotorSpeed() - speed*@spring1.GetJointTranslation())*0.4);

Expand Down Expand Up @@ -199,6 +215,8 @@ class Car
if controls.leftTilt.down and controls.rightTilt.down
tilt = 0



@motor1.SetMotorSpeed(speed*Math.PI * direction);
@motor1.SetMaxMotorTorque(torque);

Expand Down
3 changes: 2 additions & 1 deletion public/coffeescripts/game.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Game

update: =>
controlStatus = @controls.getControlStatus()
if controlStatus.reset.down
if controlStatus.reset.pressed
@reset()
@$canvas.width = @$canvas.width;
@graphics.update(@car, @ctx)
Expand All @@ -38,6 +38,7 @@ class Game
@world.ClearForces()

reset: =>
@track.reset()
@car.reset()


Expand Down
13 changes: 11 additions & 2 deletions public/coffeescripts/graphics.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@ class Graphics
@tracking = {}
@stage = new createjs.Stage("drawing");

trackObject: (easelObject, box2dObject, isCameraObject=false)=>
trackObject: (easelObject, box2dObject, {isCameraObject, trackingID} = {})=>
isCameraObject or= false
trackingID or= Math.random()

if isCameraObject
@cameraObj = box2dObject
trackingID = Math.random()
@tracking[trackingID] =
easel: easelObject
box2d: box2dObject
@stage.addChild easelObject
trackingID

removeTracking: (id)->
obj = @tracking[id]
if obj?
@stage.removeChild @tracking[id].easel
delete @tracking[id]
else
console.log "Tracking object with ID #{id} does not exist"


update: (objToTrack, ctx)=>
Expand Down
82 changes: 53 additions & 29 deletions public/coffeescripts/track.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,38 @@ class Track
constructor: (@world, @graphics)->
@obstacles = []
@loadQueue = []
@img = new Image();
@img.src="/images/brick.jpg";
@img.onload = =>
if MODIT?
@loaded = true
for queued in @loadQueue
@drawObj(queued.obj, queued.track)
# @loaded = true
# @img = MODIT.getImage('brick')

customDrawData =
x: 0
y: 13
angle: 0
length: 20
right:
x: 20
y: 0
left:
x: 0
y: 0
@img = MODIT.getImage('brick')
else
@img = new Image();
@img.src="/images/brick.jpg";
@img.onload = =>
@loaded = true
for queued in @loadQueue
@drawObj(queued.obj, queued.track, queued.drawID)
@reset()

ramp = @addPhysicsObj(customDrawData)
@addObstacle(customDrawData)
@drawObj(customDrawData, ramp)


drawObj: (obj, track)=>
drawObj: (obj, track, forceID)=>
if @loaded
body = new createjs.Shape();
body.graphics.beginBitmapFill(@img).drawRoundRect(0, 0, obj.length*scale*2, .2*scale*2, 5);
body.regX = obj.length*scale;
body.regY = .2*scale;
@graphics.trackObject(body, track)
if forceID
id = @graphics.trackObject(body, track, {trackingID: forceID})
else
id = @graphics.trackObject(body, track)
id
else
id = Math.random()
@loadQueue.push
obj: obj
track: track
drawID: id
id

addPhysicsObj: (customDrawData)->
# Create the shape!
Expand All @@ -71,20 +67,47 @@ class Track

removePhysicsObj: (obj)->
@world.DestroyBody(obj)

removeGraphicsObj: (id)->
@graphics.removeTracking id

reset: ->
for obstacle in @obstacles
@removePhysicsObj(obstacle.physicsObj)
@removeGraphicsObj(obstacle.graphicsID)

@obstacles = []
customDrawData =
x: 0
y: 13
angle: 0
length: 20
right:
x: 20
y: 0
left:
x: 0
y: 0

ramp = @addPhysicsObj(customDrawData)
@addObstacle(customDrawData)
@drawObj(customDrawData, ramp)


cleanupTrack: (currentX)->
cleanupThreshold = 100
cleanupIndex = 0
cleanupIndex = -1

for obstacle, i in @obstacles
if obstacle.physicsObj.GetWorldCenter().x+cleanupThreshold < currentX
@removePhysicsObj(obstacle.physicsObj)
@removeGraphicsObj(obstacle.graphicsID)
cleanupIndex = i
else
break

if cleanupIndex > 0
@obstacles.splice 0, cleanupIndex
if cleanupIndex >= 0
@obstacles.splice 0, cleanupIndex+1


addObstacle: (lastObjData)->
Expand Down Expand Up @@ -126,7 +149,7 @@ class Track


ramp = @addPhysicsObj(customDrawData)
@drawObj(customDrawData, ramp)
customDrawData.graphicsID = @drawObj(customDrawData, ramp)
customDrawData.physicsObj = ramp
@obstacles.push customDrawData

Expand All @@ -140,9 +163,10 @@ class Track
lastObstacle = @obstacles[@obstacles.length-1]
if carPosition.x+20 > lastObstacle.x
@addObstacle(lastObstacle)

if carPosition.x+200 > @obstacles[0].x
@cleanupTrack(carPosition.x)


window.Track = Track


29 changes: 21 additions & 8 deletions public/javascripts/car.js

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

3 changes: 2 additions & 1 deletion public/javascripts/game.js

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

25 changes: 17 additions & 8 deletions public/javascripts/graphics.js

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

Loading

0 comments on commit 01565d5

Please sign in to comment.