SIZE = 20
class Person
constructor: (x, y) ->
@x = x
@y = y
@color = rgb(255, 255, 255)
draw: (ctx) ->
ctx.fillStyle = @color
ctx.fillRect(@x * SIZE, @y * SIZE, SIZE, SIZE)
move: ->
move_cross: ->
switch Math.rand(0, 3)
when 0 then @go_right()
when 1 then @go_left()
when 2 then @go_down()
when 3 then @go_up()
move_diagonal: ->
switch Math.rand(0, 7)
when 0 then @go_right()
when 1 then @go_left()
when 2 then @go_down()
when 3 then @go_up()
when 4
@go_right()
@go_down()
when 5
@go_left()
@go_up()
when 6
@go_right()
@go_up()
when 7
@go_left()
@go_down()
go_up: ->
if @y > 0
new_y = @y - 1
else
new_y = @y + 1
if is_free_cell(@x, new_y)
@y = new_y
go_down: ->
if @y < SIZE - 1
new_y = @y + 1
else
new_y = @y - 1
if is_free_cell(@x, new_y)
@y = new_y
go_left: ->
if @x > 0
new_x = @x - 1
else
new_x = @x + 1
if is_free_cell(new_x, @y)
@x = new_x
go_right: ->
if @x < SIZE - 1
new_x = @x + 1
else
new_x = @x - 1
if is_free_cell(new_x, @y)
@x = new_x
class Zombie extends Person
constructor: (x, y) ->
super(x, y)
@color = rgb(200, 0, 0)
draw: (ctx) ->
super(ctx)
move: ->
@move_cross()
bite: ->
humans = []
pos = [
[ @x, @y - 1 ],
[ @x + 1, @y ],
[ @x, @y + 1 ],
[ @x - 1, @y ]
]
for human in persons when human instanceof Hunter or human instanceof Victim
if pos.has([ human.x, human.y ])
humans.push(human)
for human in humans
persons.push(new Zombie(human.x, human.y))
persons.splice(persons.indexOf(human), 1)
class Victim extends Person
constructor: (x, y) ->
super(x, y)
@color = rgb(0, 0, 200)
draw: (ctx) ->
super(ctx)
move: ->
zombies = []
pos = [
[ @x, @y - 1 ],
[ @x + 1, @y ],
[ @x, @y + 1 ],
[ @x - 1, @y ]
]
have_zombies = false
for zombie in persons when zombie instanceof Zombie
if pos.has([ zombie.x, zombie.y ])
have_zombies = true
break
if have_zombies
@move_cross()
class Hunter extends Person
constructor: (x, y) ->
super(x, y)
@color = rgb(200, 200, 0)
draw: (ctx) ->
super(ctx)
move: ->
@move_diagonal()
hunt: ->
zombies = []
pos = [
[ @x, @y ],
[ @x, @y - 1 ],
[ @x + 1, @y ],
[ @x, @y + 1 ],
[ @x - 1, @y ]
]
for zombie in persons when zombie instanceof Zombie
if pos.has([ zombie.x, zombie.y ])
zombies.push(zombie)
i = 0
for zombie in zombies when i++ < 2
persons.splice(persons.indexOf(zombie), 1)
nbZombies = 0
nbVictims = 0
nbHunters = 0
can = document.getElementById('game')
ctx = can.getContext('2d')
can.width = 400
can.height = 400
persons = mouse = sw = timer = null
if nbZombies + nbVictims + nbHunters > 20 * 20
alert 'STOP!'
init = ->
persons = []
for i in [ 0 ... 160 ]
loop
x = Math.rand(0, 19)
y = Math.rand(0, 19)
break if is_free_cell(x, y)
switch Math.rand(0, 4)
when 0 then persons.push(new Zombie(x, y))
when 1 then persons.push(new Victim(x, y))
when 2,3,4 then persons.push(new Hunter(x, y))
mouse = new Mouse(can)
sw = new Stopwatch()
timer = new Timer()
img = preload_images({}, create)
create = ->
setInterval(->
for hunter in persons when hunter instanceof Hunter
hunter.move()
hunter.hunt()
for victim in persons when victim instanceof Victim
victim.move()
for zombie in persons when zombie instanceof Zombie
zombie.move()
zombie.bite()
nbHunters = (persons.filter (person) -> person instanceof Hunter).length
nbVictims = (persons.filter (person) -> person instanceof Victim).length
nbZombies = (persons.filter (person) -> person instanceof Zombie).length
stats = document.getElementById('stats')
stats.innerHTML = """
Nb. chasseurs : #{nbHunters}
Nb. victimes : #{nbVictims}
Nb. zombies : #{nbZombies}
"""
, 100)
update()
update = ->
draw()
mouse.update()
sw.update()
timer.update()
requestAnimationFrame(update)
draw = ->
ctx.fillStyle = rgb(0, 0, 0)
ctx.fillRect(0, 0, can.width, can.height)
for person in persons
person.draw(ctx)
return
is_free_cell = (x, y) ->
for person in persons
if x == person.x and y == person.y
return false
return true
init()