Created
September 25, 2011 00:47
-
-
Save itsbth/1240065 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WORLD_SIZE = [0..31] | |
BLOCKS = _([1, 1, 33, 34, 50]).reverse() | |
class Renderer | |
constructor: (@ctx, @grid, @terrain) -> | |
render: -> | |
for x in WORLD_SIZE | |
for y in WORLD_SIZE | |
@grid[x][y].render @, {x: x, y: y} | |
null | |
drawBlock: (pos, id) -> | |
[sx, sy] = [(id % 16) * 16, Math.floor(id / 16) * 16] | |
@ctx.drawImage(@terrain, sx, sy, 16, 16, pos.x * 16, pos.y * 16, 16, 16) | |
class World | |
constructor: (@renderer) -> | |
class Block | |
constructor: (@id) -> | |
@damage = 0 | |
render: (renderer, pos) -> | |
renderer.drawBlock(pos, @id) | |
if @damage > 0 | |
renderer.drawBlock(pos, 239 + @damage) | |
click: -> | |
if @damage < 10 | |
@damage += 1 | |
terrain = new Image | |
terrain.src = 'terrain.png' | |
noise = new SimplexNoise | |
grid = [] | |
for x in WORLD_SIZE | |
grid[x] = [] | |
for y in WORLD_SIZE | |
grid[x][y] = new Block BLOCKS[Math.floor (1 + noise.noise(x / 10, y / 10)) * BLOCKS.length / 2] | |
#for x in WORLD_SIZE | |
# for y in WORLD_SIZE | |
# grid[x][y] = new Block 1 | |
document.addEventListener 'DOMContentLoaded', -> | |
canvas = document.getElementById 'cv' | |
canvas.addEventListener 'click', (evt) -> | |
x = Math.floor(evt.offsetX / 16) | |
y = Math.floor(evt.offsetY / 16) | |
grid[x][y].click() | |
ctx = canvas.getContext '2d' | |
renderer = new Renderer(ctx, grid, terrain) | |
draw = -> | |
renderer.render() | |
setTimeout arguments.callee, 100 | |
setTimeout draw, 100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment