restructured the code

This commit is contained in:
Ruben Müller 2013-06-13 21:15:18 +02:00
parent c1ac81a24b
commit 1132deb5e6
17 changed files with 300 additions and 328 deletions

5
Cakefile Normal file
View file

@ -0,0 +1,5 @@
{exec} = require 'child_process'
task 'sbuild', 'Build project from ./src/*.coffee to ./build/*.js', ->
exec 'coffee --output ./build/ --join game.js --compile ./src/', (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr

1
docs/README Normal file
View file

@ -0,0 +1 @@
README

View file

@ -1,28 +1,9 @@
+ levelweschel start
+
+ loose screen
+ böses licht
+ items
- knicklicht
- feuerzeug
- fackel
+ schatten addieren
+ schatten addieren
+ include reihenfolge

View file

@ -1 +0,0 @@
[3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0,0,0,6,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,0,0,0,1,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,0,0,3,3,0,3,3,0,3,3,0,3,3,0,3,3,0,3,3,0,3,3,0,0,3,3,0,0,0,0,0,0,0,3,0,0,0,3,3,0,0,0,0,0,0,3,3,0,0,0,0,0,0,3,3,0,0,0,3,3,0,0,3,3,0,3,3,0,3,3,0,3,3,0,3,3,0,3,3,0,3,3,0,0,3,3,0,0,0,0,0,0,0,3,0,0,3,3,3,3,0,0,0,0,3,3,3,3,0,0,0,0,3,3,3,3,0,0,3,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,0,0,0,0,0,0,0,3,0,0,3,3,3,3,0,0,0,0,3,3,3,3,0,0,0,0,3,3,3,3,0,0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,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3]

BIN
src/.DS_Store vendored Normal file

Binary file not shown.

View file

@ -1,5 +0,0 @@
{exec} = require 'child_process'
task 'sbuild', 'Build project from ./*.coffee to ../build/*.js', ->
exec 'coffee --output ../build/ --join game.js --compile ./', (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr

View file

@ -1,70 +0,0 @@
canvas = document.getElementById 'canvas'
context = canvas.getContext '2d'
context.textAlign = 'center';
# the config
config =
gamewin: false
gameover: false
pixelsize: 15
debug: false
lights: true
map:
width: 60
height: 40
origin:
x: 0
y: 50
player =
position: 0
lightrange: 10
blocks =
walkable: [1, 0, 4, 6, 7, 8]
entities: [1, 2, 7, 5, 6, 8]
colors =
floor: '#eeeeee'
wall: '#333333'
player: '#850091'
key: '#ffe91a'
door: '#0883fd'
exit: '#07fd0f'
breakable: '#bf7d0b'
levels = []
current_level = 0
level = null
# preparing the canvas
update_canvas = () ->
canvas.width = config.map.width*config.pixelsize+config.map.origin.x
canvas.height = config.map.height*config.pixelsize+config.map.origin.y
update_canvas()
toxy = (position)->
x: position%config.map.width
y: Math.floor(position/config.map.width)
# function: rect drawing
rect = (x, y, w, h, color) ->
context.fillStyle = color
context.fillRect x, y, w, h
pixel = (position, w, h, color) ->
x = config.pixelsize*toxy(position).x+config.map.origin.x
y = config.pixelsize*toxy(position).y+config.map.origin.y
rect(x, y, w, h, color)
write = (x, y, text, size, color) ->
context.textAlign = 'center';
context.fillStyle = color
context.font = 'normal '+size+'pt Verdana'
context.fillText(text, x, y)
add_level = (map) ->
levels.push map
# tmp: level
shadow_layer = enemy_layer = level = null

47
src/config.coffee Normal file
View file

@ -0,0 +1,47 @@
canvas = document.getElementById 'canvas'
context = canvas.getContext '2d'
context.textAlign = 'center';
# the config
config =
gamewin: false
gameover: false
pixelsize: 15
debug: false
lights: true
map:
width: 60
height: 40
origin:
x: 0
y: 50
player =
position: 0
lightrange: 10
blocks =
walkable: [1, 0, 4, 6, 7, 8]
entities: [1, 2, 7, 5, 6, 8]
colors =
floor: '#eeeeee'
wall: '#333333'
player: '#850091'
key: '#ffe91a'
door: '#0883fd'
exit: '#07fd0f'
breakable: '#bf7d0b'
inventar =
key: false
#candles: 0
#sticks: 0
#lighter: false
levels = []
current_level = 0
level = null
# tmp: level
shadow_layer = enemy_layer = null

View file

@ -1,9 +1,3 @@
inventar =
key: false
#candles: 0
#sticks: 0
#lighter: false
draw_hud = ->
rect(0, 0, config.pixelsize*config.map.width, config.map.origin.y, '#222222')
rect(0, config.map.origin.y-2, config.pixelsize*config.map.width, 2, '#333333')

23
src/input.coffee Normal file
View file

@ -0,0 +1,23 @@
# get input
keymap = []
addEventListener 'keydown', (event) ->
keymap[event.keyCode] = true
#console.log event.keyCode
#if event.keyCode == 187 then player.lightrange++
if event.keyCode == 187
config.pixelsize += 2
update_canvas()
#current_level++
#load_level(current_level)
#if event.keyCode == 189 then player.lightrange--
if event.keyCode == 189
config.pixelsize -= 2
update_canvas()
if event.keyCode == 68 then config.debug = !config.debug
if event.keyCode == 32 then break_walls(player.position)
if event.keyCode == 76 then config.lights = !config.lights
addEventListener 'keyup', (event) ->
keymap[event.keyCode] = false

38
src/level.coffee Normal file
View file

@ -0,0 +1,38 @@
add_level = (map) ->
levels.push map
draw_level = (level) ->
for block, position in level
switch block
when 0 then color = colors.floor
when 3 then color = colors.wall
when 1 then color = colors.key
when 2 then color = colors.door
when 7 then color = colors.exit
when 5 then color = colors.breakable
pixel(position, config.pixelsize, config.pixelsize, color)
reset_layers = () ->
shadow_layer = [0..(config.map.width*config.map.height)].map -> 0
enemy_layer = [0..(config.map.width*config.map.height)].map -> 0
init_level = () ->
level = [0..(config.map.width*config.map.height)].map -> 0
inventar.key = false
load_level = (number) ->
if number is levels.length
config.gamewin = true
else
init_level()
level = levels[number]
parse_levelentities(level)
parse_levelentities = ->
for block, position in level
switch block
when 4
player.position = position
level[position] = 0

19
src/lib/2d.coffee Normal file
View file

@ -0,0 +1,19 @@
toxy = (position)->
x: position%config.map.width
y: Math.floor(position/config.map.width)
# function: rect drawing
rect = (x, y, w, h, color) ->
context.fillStyle = color
context.fillRect x, y, w, h
pixel = (position, w, h, color) ->
x = config.pixelsize*toxy(position).x+config.map.origin.x
y = config.pixelsize*toxy(position).y+config.map.origin.y
rect(x, y, w, h, color)
write = (x, y, text, size, color) ->
context.textAlign = 'center';
context.fillStyle = color
context.font = 'normal '+size+'pt Verdana'
context.fillText(text, x, y)

8
src/lib/helpers.coffee Normal file
View file

@ -0,0 +1,8 @@
# preparing the canvas
update_canvas = () ->
canvas.width = config.map.width*config.pixelsize+config.map.origin.x
canvas.height = config.map.height*config.pixelsize+config.map.origin.y
update_canvas()
map_range = (from, to, s) ->
to[0] + (s-from[0]) * (to[1] - to[0]) / (from[1] - from[0])

111
src/light.coffee Normal file
View file

@ -0,0 +1,111 @@
calculate_lightray = (x1, y1, x2, y2, type) ->
switch type
when 'enemy' then selected_layer = enemy_layer
else selected_layer = shadow_layer
pixel_x1 = x1*config.pixelsize + (config.pixelsize/2)
pixel_y1 = y1*config.pixelsize + (config.pixelsize/2)
pixel_x2 = x2*config.pixelsize + (config.pixelsize/2)
pixel_y2 = y2*config.pixelsize + (config.pixelsize/2)
dx = Math.abs(pixel_x1 - pixel_x2)
dy = Math.abs(pixel_y1 - pixel_y2)
s = 0.99 / (if dx>dy then dx else dy)
t = 0.0
# show me the rays
if config.debug
context.beginPath()
context.strokeStyle = 'blue'
context.moveTo(pixel_x1+config.map.origin.x,pixel_y1+config.map.origin.y)
context.lineTo(pixel_x2+config.map.origin.x,pixel_y2+config.map.origin.y)
context.stroke()
hit_wall = false
while t < 1.0
dx = Math.round((1.0-t)*pixel_x1 + t*pixel_x2)
dy = Math.round((1.0-t)*pixel_y1 + t*pixel_y2)
dx = Math.floor(dx/config.pixelsize)
dy = Math.floor(dy/config.pixelsize)
# FLOOR?
if level[dy*config.map.width + dx] is 0
break if hit_wall and !config.debug
if selected_layer[dy*config.map.width + dx] == 0
if t > 0.5
selected_layer[dy*config.map.width + dx] += map_range([0.5, 1], [1, 0], t)
else
selected_layer[dy*config.map.width + dx] += 1
# WALL?
else if level[dy*config.map.width + dx] is 3
break if type is 'enemy'
if t > 0.5
selected_layer[dy*config.map.width + dx] += map_range([0.5, 1], [1, 0], t)
else
selected_layer[dy*config.map.width + dx] += 1
hit_wall = true
# ENTITIY?
else if level[dy*config.map.width + dx] in blocks.entities
if t > 0.5
selected_layer[dy*config.map.width + dx] += map_range([0.5, 1], [1, 0], t)
else
selected_layer[dy*config.map.width + dx] += 1
break if !config.debug and level[dy*config.map.width + dx] isnt 1 and level[dy*config.map.width + dx] isnt 8
else
selected_layer[dy*config.map.width + dx] = .5
break if !config.debug
t += s
illuminate = (x1, y1, range, type = 'shadow') ->
r = 0
while r < Math.PI*2
x2 = range*Math.cos(r) + x1
y2 = range*Math.sin(r) + y1
# bug: edge blurry stuff
#x2 = config.map.width-1 if x2 > config.map.width-1
#y2 = config.map.height-1 if y2 > config.map.height-1
#
#x2 = 0 if x2 < 0
#y2 = 0 if y2 < 0
calculate_lightray(x1, y1, x2, y2, type)
r += 0.03
calculate_lights = () ->
#illuminate(8, 8, 5)
illuminate(toxy(player.position).x, toxy(player.position).y, player.lightrange)
for block, position in level
switch block
# 6 = LIGHT
# CAMERA
when 8
illuminate(toxy(position).x, toxy(position).y, 4, 'enemy')
draw_lights = (type) ->
switch type
when 'enemy'
color = '255, 0, 0'
selected_layer = enemy_layer
else
color = '0, 0, 0'
selected_layer = shadow_layer
for brightness, position in selected_layer
switch type
when 'enemy'
alpha = brightness*.5
else
alpha = 1-brightness
if level[position] in blocks.entities
if alpha < 0.4 then alpha = 0
pixel(position, config.pixelsize, config.pixelsize, 'rgba('+color+', '+alpha+')')

View file

@ -1,226 +1,3 @@
draw_level = (level) ->
for block, position in level
switch block
when 0 then color = colors.floor
when 3 then color = colors.wall
when 1 then color = colors.key
when 2 then color = colors.door
when 7 then color = colors.exit
when 5 then color = colors.breakable
pixel(position, config.pixelsize, config.pixelsize, color)
reset_layers = () ->
shadow_layer = [0..(config.map.width*config.map.height)].map -> 0
enemy_layer = [0..(config.map.width*config.map.height)].map -> 0
init_level = () ->
level = [0..(config.map.width*config.map.height)].map -> 0
inventar.key = false
load_level = (number) ->
if number is levels.length
config.gamewin = true
else
init_level()
level = levels[number]
parse_levelentities(level)
# function: update player position
update_player_position = () ->
new_position = player.position
if keymap[37] and toxy(player.position).x > 0 # left
new_position = new_position-1
if keymap[39] and toxy(player.position).x < config.map.width-1 # right
new_position = new_position+1
if keymap[38] and toxy(player.position).y > 0 # up
new_position = new_position - config.map.width
if keymap[40] and toxy(player.position).y < config.map.height-1 # down
new_position = new_position + config.map.width
switch level[new_position]
# EXIT
when 7
current_level++
load_level(current_level)
return
# DOOR
when 2
if inventar.key
inventar.key = false
level[new_position] = 0
# KEY
when 1
inventar.key = true
level[new_position] = 0
#console.log enemy_layer
if enemy_layer[new_position] > 0 and !config.debug
config.gameover = true
if level[new_position] in blocks.walkable then player.position = new_position
# function: draw player
draw_player = (position) ->
pixel(position, config.pixelsize, config.pixelsize, colors.player)
map_range = (from, to, s) ->
to[0] + (s-from[0]) * (to[1] - to[0]) / (from[1] - from[0])
calculate_lightray = (x1, y1, x2, y2, type) ->
switch type
when 'enemy' then selected_layer = enemy_layer
else selected_layer = shadow_layer
pixel_x1 = x1*config.pixelsize + (config.pixelsize/2)
pixel_y1 = y1*config.pixelsize + (config.pixelsize/2)
pixel_x2 = x2*config.pixelsize + (config.pixelsize/2)
pixel_y2 = y2*config.pixelsize + (config.pixelsize/2)
dx = Math.abs(pixel_x1 - pixel_x2)
dy = Math.abs(pixel_y1 - pixel_y2)
s = 0.99 / (if dx>dy then dx else dy)
t = 0.0
# show me the rays
if config.debug
context.beginPath()
context.strokeStyle = 'blue'
context.moveTo(pixel_x1+config.map.origin.x,pixel_y1+config.map.origin.y)
context.lineTo(pixel_x2+config.map.origin.x,pixel_y2+config.map.origin.y)
context.stroke()
hit_wall = false
while t < 1.0
dx = Math.round((1.0-t)*pixel_x1 + t*pixel_x2)
dy = Math.round((1.0-t)*pixel_y1 + t*pixel_y2)
dx = Math.floor(dx/config.pixelsize)
dy = Math.floor(dy/config.pixelsize)
# FLOOR?
if level[dy*config.map.width + dx] is 0
break if hit_wall and !config.debug
if selected_layer[dy*config.map.width + dx] == 0
if t > 0.5
selected_layer[dy*config.map.width + dx] += map_range([0.5, 1], [1, 0], t)
else
selected_layer[dy*config.map.width + dx] += 1
# WALL?
else if level[dy*config.map.width + dx] is 3
break if type is 'enemy'
if t > 0.5
selected_layer[dy*config.map.width + dx] += map_range([0.5, 1], [1, 0], t)
else
selected_layer[dy*config.map.width + dx] += 1
hit_wall = true
# ENTITIY?
else if level[dy*config.map.width + dx] in blocks.entities
if t > 0.5
selected_layer[dy*config.map.width + dx] += map_range([0.5, 1], [1, 0], t)
else
selected_layer[dy*config.map.width + dx] += 1
break if !config.debug and level[dy*config.map.width + dx] isnt 1 and level[dy*config.map.width + dx] isnt 8
else
selected_layer[dy*config.map.width + dx] = .5
break if !config.debug
t += s
illuminate = (x1, y1, range, type = 'shadow') ->
r = 0
while r < Math.PI*2
x2 = range*Math.cos(r) + x1
y2 = range*Math.sin(r) + y1
# bug: edge blurry stuff
#x2 = config.map.width-1 if x2 > config.map.width-1
#y2 = config.map.height-1 if y2 > config.map.height-1
#
#x2 = 0 if x2 < 0
#y2 = 0 if y2 < 0
calculate_lightray(x1, y1, x2, y2, type)
r += 0.03
calculate_lights = () ->
#illuminate(8, 8, 5)
illuminate(toxy(player.position).x, toxy(player.position).y, player.lightrange)
for block, position in level
switch block
# 6 = LIGHT
# CAMERA
when 8
illuminate(toxy(position).x, toxy(position).y, 4, 'enemy')
draw_lights = (type) ->
switch type
when 'enemy'
color = '255, 0, 0'
selected_layer = enemy_layer
else
color = '0, 0, 0'
selected_layer = shadow_layer
for brightness, position in selected_layer
switch type
when 'enemy'
alpha = brightness*.5
else
alpha = 1-brightness
if level[position] in blocks.entities
if alpha < 0.4 then alpha = 0
pixel(position, config.pixelsize, config.pixelsize, 'rgba('+color+', '+alpha+')')
break_walls = (position) ->
level[position-1] = 0 if level[position-1] is 5 or config.debug
level[position+1] = 0 if level[position+1] is 5 or config.debug
level[position-config.map.width] = 0 if level[position-config.map.width] is 5 or config.debug
level[position+config.map.width] = 0 if level[position+config.map.width] is 5 or config.debug
# get input
keymap = []
addEventListener 'keydown', (event) ->
keymap[event.keyCode] = true
#console.log event.keyCode
#if event.keyCode == 187 then player.lightrange++
if event.keyCode == 187
config.pixelsize += 2
update_canvas()
#current_level++
#load_level(current_level)
#if event.keyCode == 189 then player.lightrange--
if event.keyCode == 189
config.pixelsize -= 2
update_canvas()
if event.keyCode == 68 then config.debug = !config.debug
if event.keyCode == 32 then break_walls(player.position)
if event.keyCode == 76 then config.lights = !config.lights
addEventListener 'keyup', (event) ->
keymap[event.keyCode] = false
parse_levelentities = ->
for block, position in level
switch block
when 4
player.position = position
level[position] = 0
# startup config
load_level(current_level)
# the awesome main loop
main_loop = ->
if config.gamewin
rect(0, 0, config.map.width*config.pixelsize, config.map.height*config.pixelsize+config.map.origin.y, '#ffffff')
@ -244,4 +21,5 @@ main_loop = ->
setTimeout main_loop, 50
load_level(current_level)
main_loop()

43
src/player.coffee Normal file
View file

@ -0,0 +1,43 @@
# function: update player position
update_player_position = () ->
new_position = player.position
if keymap[37] and toxy(player.position).x > 0 # left
new_position = new_position-1
if keymap[39] and toxy(player.position).x < config.map.width-1 # right
new_position = new_position+1
if keymap[38] and toxy(player.position).y > 0 # up
new_position = new_position - config.map.width
if keymap[40] and toxy(player.position).y < config.map.height-1 # down
new_position = new_position + config.map.width
switch level[new_position]
# EXIT
when 7
current_level++
load_level(current_level)
return
# DOOR
when 2
if inventar.key
inventar.key = false
level[new_position] = 0
# KEY
when 1
inventar.key = true
level[new_position] = 0
#console.log enemy_layer
if enemy_layer[new_position] > 0 and !config.debug
config.gameover = true
if level[new_position] in blocks.walkable then player.position = new_position
# function: draw player
draw_player = (position) ->
pixel(position, config.pixelsize, config.pixelsize, colors.player)
break_walls = (position) ->
level[position-1] = 0 if level[position-1] is 5 or config.debug
level[position+1] = 0 if level[position+1] is 5 or config.debug
level[position-config.map.width] = 0 if level[position-config.map.width] is 5 or config.debug
level[position+config.map.width] = 0 if level[position+config.map.width] is 5 or config.debug