Permalink
Browse files

More livescript conversion

  • Loading branch information...
1 parent 830a725 commit 02cde883477134adb5363bbc47c155fc0bd18187 @lessandro committed Nov 28, 2012
Showing with 148 additions and 151 deletions.
  1. +1 −2 README.md
  2. +10 −10 src/_entity.ls
  3. +5 −5 src/bullet.ls
  4. +28 −28 src/canvas.ls
  5. +10 −10 src/game.ls
  6. +10 −10 src/input.ls
  7. +43 −43 src/level.ls
  8. +33 −35 src/player.ls
  9. +2 −2 src/sprite.ls
  10. +6 −6 src/tile.ls
View
3 README.md
@@ -1,2 +1 @@
-2D platformer written in CoffeeScript
-
+2D platformer written in LiveScript
View
20 src/_entity.ls
@@ -5,24 +5,24 @@ class Entity
tick: ->
- touchingTiles: ->
- getTile = (x, y) ~>
- tile: @game.level.getTile x, y
+ touching-tiles: ->
+ get-tile = (x, y) ~>
+ tile: @game.level.get-tile x, y
x: x
y: y
xs = [@x, @x + @width - 1]
ys = [@y, @y + @height - 1]
- return [getTile x, y for y in ys for x in xs]
+ return [get-tile x, y for y in ys for x in xs]
clipped: (direction) ->
- tiles = @touchingTiles!
+ tiles = @touching-tiles!
map =
- 'up': [tiles[0+0].tile, tiles[0+1].tile]
- 'down': [tiles[2+0].tile, tiles[2+1].tile]
- 'left': [tiles[0+0].tile, tiles[2+0].tile]
- 'right': [tiles[0+1].tile, tiles[2+1].tile]
+ \up : [tiles[0+0].tile, tiles[0+1].tile]
+ \down : [tiles[2+0].tile, tiles[2+1].tile]
+ \left : [tiles[0+0].tile, tiles[2+0].tile]
+ \right : [tiles[0+1].tile, tiles[2+1].tile]
- return _.any _.map map[direction], Tile.isSolid
+ return _.any _.map map[direction], Tile.is-solid
View
10 src/bullet.ls
@@ -2,16 +2,16 @@ class Bullet extends Entity
(@game, x, y, @direction) ->
@width = 14
@height = 6
- @x = Math.round(x - @width/2)
- @y = Math.round(y - @height/2)
- @sprite = if @direction == 1 then 'bulletr' else 'bulletl'
+ @x = Math.round x - @width / 2
+ @y = Math.round y - @height / 2
+ @sprite = if @direction == 1 then \bulletr else \bulletl
tick: ->
vel = 10
@x += @direction * vel
- if @clipped (if @direction == 1 then 'right' else 'left')
+ if @clipped (if @direction == 1 then \right else \left)
@dead = true
draw: ->
- @game.canvas.drawSprite @x, @y, @sprite
+ @game.canvas.draw-sprite @x, @y, @sprite
View
56 src/canvas.ls
@@ -1,43 +1,43 @@
class Canvas
->
- canvas = $("canvas").get(0)
+ canvas = $ \canvas .get 0
@scroll = 0
- @ctx = canvas.getContext("2d")
- @ctx.mozImageSmoothingEnabled = false;
- @ctx.webkitImageSmoothingEnabled = false;
+ @ctx = canvas.get-context \2d
+ @ctx.moz-image-smoothing-enabled = false;
+ @ctx.webkit-image-smoothing-enabled = false;
@sprites =
- 'player0r': new Sprite(0, 0, 14, 16)
- 'player0l': new Sprite(1, 0, 14, 16)
- 'player1r': new Sprite(2, 0, 14, 16)
- 'player1l': new Sprite(3, 0, 14, 16)
- 'bulletr': new Sprite(0, 1, 7, 3)
- 'bulletl': new Sprite(1, 1, 7, 3)
- '*': new Sprite(0, 2)
- 'P': new Sprite(1, 2)
- 'R': new Sprite(2, 2)
- 'D': new Sprite(3, 2)
- 'B': new Sprite(0, 3)
- 'G': new Sprite(1, 3)
- '=': new Sprite(2, 3)
- '+': new Sprite(3, 3)
- 'F': new Sprite(4, 3)
- 'W': new Sprite(5, 3)
- '-': new Sprite(6, 3)
+ \player0r : new Sprite(0, 0, 14, 16)
+ \player0l : new Sprite(1, 0, 14, 16)
+ \player1r : new Sprite(2, 0, 14, 16)
+ \player1l : new Sprite(3, 0, 14, 16)
+ \bulletr : new Sprite(0, 1, 7, 3)
+ \bulletl : new Sprite(1, 1, 7, 3)
+ \* : new Sprite(0, 2)
+ \P : new Sprite(1, 2)
+ \R : new Sprite(2, 2)
+ \D : new Sprite(3, 2)
+ \B : new Sprite(0, 3)
+ \G : new Sprite(1, 3)
+ \= : new Sprite(2, 3)
+ \+ : new Sprite(3, 3)
+ \F : new Sprite(4, 3)
+ \W : new Sprite(5, 3)
+ \- : new Sprite(6, 3)
- setScroll: (dx) ->
+ set-scroll: (dx) ->
@scroll = dx
- drawTile: (tile, i, j) ->
+ draw-tile: (tile, i, j) ->
x = i * Tile.size
y = j * Tile.size
if @sprites[tile]
- @drawSprite x, y, tile
+ @draw-sprite x, y, tile
return
- @ctx.fillStyle = 'black'
- @ctx.fillRect x - @scroll, y, Tile.size, Tile.size
+ @ctx.fill-style = \black
+ @ctx.fill-rect x - @scroll, y, Tile.size, Tile.size
- drawSprite: (x, y, sprite) ->
- @sprites[sprite].draw(@ctx, x - @scroll, y)
+ draw-sprite: (x, y, sprite) ->
+ @sprites[sprite].draw @ctx, x - @scroll, y
View
20 src/game.ls
@@ -3,28 +3,28 @@ class Game
@input = new Input()
@canvas = new Canvas()
@restart = true
- @currentLevel = 0
+ @current-level = 0
run: ->
- console.log "running"
+ console.log \running
- window.setInterval @step, 1000 / 30 # fixed fps
+ window.set-interval @step, 1000 / 30 # fixed fps
step: ~>
@tick!
@draw!
tick: ->
- if @nextLevel
- @currentLevel++
+ if @next-level
+ @current-level++
- if @currentLevel >= Level.maps.length
- @currentLevel = 0
+ if @current-level >= Level.maps.length
+ @current-level = 0
- if @restart or @nextLevel
- @nextLevel = false
+ if @restart or @next-level
+ @next-level = false
@restart = false
- @level = new Level(this, @currentLevel)
+ @level = new Level(this, @current-level)
@input.clear!
@input.tick!
View
20 src/input.ls
@@ -1,32 +1,32 @@
class Input
keymap =
- 37: "left"
- 38: "up"
- 39: "right"
- 40: "down"
- 90: "z"
+ 37: \left
+ 38: \up
+ 39: \right
+ 40: \down
+ 90: \z
->
@clear!
handler = _.bind @handler, this
# assign handler
- $(document.body)
- .on("keydown", handler)
- .on("keyup", handler)
+ $ document.body
+ .on \keydown, handler
+ .on \keyup, handler
handler: (e) ->
key = keymap[e.which]
return true unless key
- if e.type is "keydown"
+ if e.type is \keydown
# disable keydown repeat
unless @pressed[key]
@pressed[key] = true
@down[key] = true
- if e.type is "keyup"
+ if e.type is \keyup
@pressed[key] = false
@up[key] = true
View
86 src/level.ls
@@ -6,16 +6,16 @@ class Level
y: 8
tiles: [
- "BBBBBBBBBBBBBBBBBBBG"
- "BP RBG"
- "B D D * D BG"
- "B B B B B BG"
- "BD D D D DBG"
- "BB B B B BBG"
- "BD D BG"
- "B BBBB BBBBBB BG"
- "B+ B= BG"
- "BBBBBBBBBBBBBBBBBBBG"
+ 'BBBBBBBBBBBBBBBBBBBG'
+ 'BP RBG'
+ 'B D D * D BG'
+ 'B B B B B BG'
+ 'BD D D D DBG'
+ 'BB B B B BBG'
+ 'BD D BG'
+ 'B BBBB BBBBBB BG'
+ 'B+ B= BG'
+ 'BBBBBBBBBBBBBBBBBBBG'
]
},
{
@@ -24,16 +24,16 @@ class Level
y: 8
tiles: [
- "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB "
- "BR D P BB =BB "
- "B BBBBB BB BBBBBBBBBBBBBBBBB "
- "B- - - BB BB B "
- "B --- B BB BBBBBBBBB B "
- "B -- B *B ----- B BB PB B B "
- "B B - B B B B B B BB "
- "B --- RB B DDDDD B B BB B BB B "
- "B BD -B B BP PB BPB "
- "BBBFFFFFFBFFFFBWWWWWWWBBBBBBBBBBBBBBBBFFFFFFFFFFFFFFFFFF"
+ 'BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB '
+ 'BR D P BB =BB '
+ 'B BBBBB BB BBBBBBBBBBBBBBBBB '
+ 'B- - - BB BB B '
+ 'B --- B BB BBBBBBBBB B '
+ 'B -- B *B ----- B BB PB B B '
+ 'B B - B B B B B B BB '
+ 'B --- RB B DDDDD B B BB B BB B '
+ 'B BD -B B BP PB BPB '
+ 'BBBFFFFFFBFFFFBWWWWWWWBBBBBBBBBBBBBBBBFFFFFFFFFFFFFFFFFF'
]
},
{
@@ -42,16 +42,16 @@ class Level
y: 5
tiles: [
- "GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG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GG '
+ 'GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGFFFFFFFFF'
]
}
]
@@ -64,30 +64,30 @@ class Level
@player = new Player(@game, @start)
@entities.push @player
- getCoords: (x, y) ->
+ get-coords: (x, y) ->
i = Math.floor x / Tile.size
j = Math.floor y / Tile.size
return [i, j]
- inBounds: (i, j) ->
+ in-bounds: (i, j) ->
if j < 0 or j >= @tiles.length
return false
if i < 0 or i >= @tiles[j].length
return false
return true
- getTile: (x, y) ->
- [i, j] = @getCoords x, y
+ get-tile: (x, y) ->
+ [i, j] = @get-coords x, y
- unless @inBounds i, j
+ unless @in-bounds i, j
return ' '
return @tiles[j][i]
- clearTile: (x, y) ->
- [i, j] = @getCoords x, y
+ clear-tile: (x, y) ->
+ [i, j] = @get-coords x, y
- if @inBounds i, j
+ if @in-bounds i, j
@tiles[j][i] = ' '
tick: ->
@@ -101,15 +101,15 @@ class Level
draw: ->
w = 18 * Tile.size
- dx = Math.floor(@player.x / w) * w
- @game.canvas.setScroll dx
+ dx = (Math.floor @player.x / w) * w
+ @game.canvas.set-scroll dx
for line, j in @tiles
for tile, i in line
- @drawTile tile, i, j
+ @draw-tile tile, i, j
for entity in @entities
entity.draw()
- drawTile: (tile, i, j) ->
- @game.canvas.drawTile tile, i, j
+ draw-tile: (tile, i, j) ->
+ @game.canvas.draw-tile tile, i, j
View
68 src/player.ls
@@ -14,73 +14,73 @@ class Player extends Entity
vel = 4
if keys.up.hold
- if not @jumping and @canJump!
+ if not @jumping and @can-jump!
@t = 5
@jumping = true
@vely = vel
- @jumpGoal = @y - 2 * Tile.size
+ @jump-goal = @y - 2 * Tile.size
if keys.z.pulse
@shoot!
- if !@jumping and !@canJump!
+ if !@jumping and !@can-jump!
@y += @vely
@vely += 0.2
if @vely >= vel
@vely = vel
- @adjustFall!
+ @adjust-fall!
if @jumping
@y -= @vely
@vely -= 0.01
- if @y <= @jumpGoal
- @y = @jumpGoal
+ if @y <= @jump-goal
+ @y = @jump-goal
@jumping = false
@vely = 0
- @adjustJump!
+ @adjust-jump!
if keys.right.hold
@x += vel
@direction = 1
- @adjustWalk 'right'
+ @adjust-walk \right
if keys.left.hold
@x -= vel
@direction = -1
- @adjustWalk 'left'
+ @adjust-walk \left
- @touchTiles!
+ @touch-tiles!
- canJump: ->
+ can-jump: ->
@y++
- ret = @clipped 'down'
+ ret = @clipped \down
@y--
ret
- adjustWalk: (direction) ->
+ adjust-walk: (direction) ->
if @clipped direction
- if direction == 'left'
+ if direction == \left
@x += @width - 1
@x = Tile.size * Math.floor @x / Tile.size
- if direction == 'right'
+ if direction == \right
@x += Tile.size - @width
else
if @canJump()
@t++
- adjustJump: ->
- if @clipped 'up'
+ adjust-jump: ->
+ if @clipped \up
@y += Tile.size
@y = Tile.size * Math.floor @y / Tile.size
@jumping = false
@vely = 0
- adjustFall: ->
- if @clipped 'down'
+ adjust-fall: ->
+ if @clipped \down
@y = Tile.size * Math.floor @y / Tile.size
shoot: ->
@@ -90,27 +90,25 @@ class Player extends Entity
@game.level.entities.push bullet
draw: ->
- sprite = "player"
+ sprite = \player
sprite += (Math.floor @t / 5) % 2
- sprite += (if @direction == 1 then 'r' else 'l')
- @game.canvas.drawSprite @x, @y, sprite
+ sprite += (if @direction == 1 then \r else \l)
+ @game.canvas.draw-sprite @x, @y, sprite
- touchTiles: ->
- tiles = _.flatten @touchingTiles!
- for tile in tiles
+ touch-tiles: ->
+ for tile in @touching-tiles!
- if tile.tile == '*'
- @hasTrophy = true
+ if tile.tile == \*
+ @has-trophy = true
- if tile.tile == '='
- if @hasTrophy
- @game.nextLevel = true
- @hasTrophy = false
+ if tile.tile == \=
+ if @has-trophy
+ @game.next-level = true
+ @has-trophy = false
- if Tile.isLethal tile.tile
+ if Tile.is-lethal tile.tile
@dead = true
@game.restart = true
- if Tile.isPickable tile.tile
- @game.level.clearTile tile.x, tile.y
-
+ if Tile.is-pickable tile.tile
+ @game.level.clear-tile tile.x, tile.y
View
4 src/sprite.ls
@@ -9,7 +9,7 @@ class Sprite
unless Sprite.image
Sprite.image = new Image()
- Sprite.image.src = 'images.png'
+ Sprite.image.src = \images.png
draw: (ctx, x, y) ->
- ctx.drawImage Sprite.image, @sx, @sy, @sw, @sh, x, y, @dw, @dh
+ ctx.draw-image Sprite.image, @sx, @sy, @sw, @sh, x, y, @dw, @dh
View
12 src/tile.ls
@@ -1,11 +1,11 @@
class Tile
@size = 32
- @isSolid = (tile) ->
- return tile in ['B', 'G', '+', '-']
+ @is-solid = (tile) ->
+ return tile in [\B \G \+ \-]
- @isPickable = (tile) ->
- return tile in ['*', 'P', 'D', 'R']
+ @is-pickable = (tile) ->
+ return tile in [\* \P \D \R]
- @isLethal = (tile) ->
- return tile in ['W', 'F']
+ @is-lethal = (tile) ->
+ return tile in [\W \F]

0 comments on commit 02cde88

Please sign in to comment.