Commits

Mikhail Sidorov committed 92df883

Viewport and boxes

Comments (0)

Files changed (1)

     
     static:
         [
-            [[0, 480], [640, 480], [640, 400], [0, 400]]
+            [[0, 480], [920, 480], [920, 400], [0, 400]]
             [[0, 0], [0, 480], [30, 480], [30, 0]]
             [[0, 180], [100, 170], [100, 160], [95, 150], [85, 140], [30, 130]]
             [[0, 130], [0, 420], [80, 420], [50, 140]]
             [[30, 200], [30, 420], [170, 420]]
             [[30, 300], [30, 420], [250, 420]]
-            [[340, 420], [500, 420], [450, 370], [390, 380]]
-            [[500, 420], [750, 420], [750, 360]]
+            [[340, 410], [500, 410], [450, 370], [390, 380]]
+            
+            [[500, 410], [750, 410], [750, 380]]
+            [[640, 410], [860, 410], [860, 320]]
+            [[1200, 480], [1920, 480], [1920, 400], [1200, 400]]
         ]
     
-    dynamic:
-        []
+    boxes:
+        [
+            [870, 240, 20, 350, 5]
+        ]
 
 class Ball extends jaws.Sprite
     constructor: (x, y, space) ->
         @torque = 20000
         @body = new cp.Body(1, cp.momentForCircle(1, 0, @width / 2, cp.vzero))
         @body.setPos(cp.v(x, y))
-        @body.w_limit = 300
+        @body.w_limit = 100
         @shape = new cp.CircleShape(@body, @width / 2, cp.vzero)
         @shape.setFriction(1)
         space.addBody(@body)
         jaws.context.closePath()
         jaws.context.fill()
 
+class Box
+    constructor: (x, y, width, height, mass, space) ->
+        @body = new cp.Body(mass, cp.momentForBox(mass, width, height))
+        @body.setPos(cp.v(x, y))
+        @shape = cp.BoxShape(@body, width, height)
+        @shape.setFriction(1)
+        space.addBody(@body)
+        space.addShape(@shape)
+    
+    draw: ->
+        jaws.context.beginPath()
+        jaws.context.fillStyle = "black"
+        jaws.context.translate(@body.p.x, @body.p.y)
+        jaws.context.rotate(@body.a)
+        jaws.context.lineTo(x, @shape.verts[i + 1]) for x, i in @shape.verts by 2
+        jaws.context.closePath()
+        jaws.context.fill()
+
 class GameState
     constructor: ->
         @gradient = jaws.context.createLinearGradient(0, 0, 0, jaws.height)
         
         @ball = new Ball(level.ball.x, level.ball.y, @space)
         @static = (new StaticPlatform(verts, @space) for verts in level.static)
+        @boxes = (new Box(x, y, width, height, mass, @space)\
+                     for [x, y, width, height, mass] in level.boxes)
+        
+        @viewport = new jaws.Viewport({max_x: jaws.width * 3, max_y: jaws.height})
+    
+    updateViewport: ->
+        @viewport.moveTo(@ball.x - @ball.x % jaws.width, 0)
     
     update: ->
         if jaws.pressed("left") then @ball.body.t = -@ball.torque
         else @ball.body.t = 0
         @space.step(1 / 60)
         @ball.update()
+        @updateViewport()
     
     draw: ->
         jaws.context.fillStyle = @gradient
         jaws.context.fillRect(0, 0, jaws.width, jaws.height)
-        @ball.draw()
-        platform.draw() for platform in @static
+        @viewport.apply( =>
+            @ball.draw()
+            platform.draw() for platform in @static
+            box.draw() for box in @boxes
+        )
 
 window.onload = ->
     jaws.assets.add("images/ball.png")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.