Advertisement
PifyZ

Exercice du lundi n°5 : Chasse aux zombies

Nov 16th, 2014
631
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SIZE = 20
  2.  
  3. class Person
  4.     constructor: (x, y) ->
  5.         @x = x
  6.         @y = y
  7.  
  8.         @color = rgb(255, 255, 255)
  9.  
  10.     draw: (ctx) ->
  11.         ctx.fillStyle = @color
  12.         ctx.fillRect(@x * SIZE, @y * SIZE, SIZE, SIZE)
  13.  
  14.     move: ->
  15.  
  16.     move_cross: ->
  17.         switch Math.rand(0, 3)
  18.             when 0 then @go_right()
  19.             when 1 then @go_left()
  20.             when 2 then @go_down()
  21.             when 3 then @go_up()
  22.  
  23.     move_diagonal: ->
  24.         switch Math.rand(0, 7)
  25.             when 0 then @go_right()
  26.             when 1 then @go_left()
  27.             when 2 then @go_down()
  28.             when 3 then @go_up()
  29.             when 4
  30.                 @go_right()
  31.                 @go_down()
  32.             when 5
  33.                 @go_left()
  34.                 @go_up()
  35.             when 6
  36.                 @go_right()
  37.                 @go_up()
  38.             when 7
  39.                 @go_left()
  40.                 @go_down()
  41.  
  42.     go_up: ->
  43.         if @y > 0
  44.             new_y = @y - 1
  45.         else
  46.             new_y = @y + 1
  47.  
  48.         if is_free_cell(@x, new_y)
  49.             @y = new_y
  50.  
  51.     go_down: ->
  52.         if @y < SIZE - 1
  53.             new_y = @y + 1
  54.         else
  55.             new_y = @y - 1
  56.  
  57.         if is_free_cell(@x, new_y)
  58.             @y = new_y
  59.  
  60.     go_left: ->
  61.         if @x > 0
  62.             new_x = @x - 1
  63.         else
  64.             new_x = @x + 1
  65.  
  66.         if is_free_cell(new_x, @y)
  67.             @x = new_x
  68.  
  69.     go_right: ->
  70.         if @x < SIZE - 1
  71.             new_x = @x + 1
  72.         else
  73.             new_x = @x - 1
  74.  
  75.         if is_free_cell(new_x, @y)
  76.             @x = new_x
  77.  
  78. class Zombie extends Person
  79.     constructor: (x, y) ->
  80.         super(x, y)
  81.         @color = rgb(200, 0, 0)
  82.  
  83.     draw: (ctx) ->
  84.         super(ctx)
  85.  
  86.     move: ->
  87.         @move_cross()
  88.  
  89.     bite: ->
  90.         humans = []
  91.         pos = [
  92.             [ @x, @y - 1 ],
  93.             [ @x + 1, @y ],
  94.             [ @x, @y + 1 ],
  95.             [ @x - 1, @y ]
  96.         ]
  97.  
  98.         for human in persons when human instanceof Hunter or human instanceof Victim
  99.             if pos.has([ human.x, human.y ])
  100.                 humans.push(human)
  101.  
  102.         for human in humans
  103.             persons.push(new Zombie(human.x, human.y))
  104.             persons.splice(persons.indexOf(human), 1)
  105.  
  106. class Victim extends Person
  107.     constructor: (x, y) ->
  108.         super(x, y)
  109.         @color = rgb(0, 0, 200)
  110.  
  111.     draw: (ctx) ->
  112.         super(ctx)
  113.  
  114.     move: ->
  115.         zombies = []
  116.         pos = [
  117.             [ @x, @y - 1 ],
  118.             [ @x + 1, @y ],
  119.             [ @x, @y + 1 ],
  120.             [ @x - 1, @y ]
  121.         ]
  122.  
  123.         have_zombies = false
  124.  
  125.         for zombie in persons when zombie instanceof Zombie
  126.             if pos.has([ zombie.x, zombie.y ])
  127.                 have_zombies = true
  128.                 break
  129.  
  130.         if have_zombies
  131.             @move_cross()
  132.  
  133. class Hunter extends Person
  134.     constructor: (x, y) ->
  135.         super(x, y)
  136.         @color = rgb(200, 200, 0)
  137.  
  138.     draw: (ctx) ->
  139.         super(ctx)
  140.  
  141.     move: ->
  142.         @move_diagonal()
  143.  
  144.     hunt: ->
  145.         zombies = []
  146.         pos = [
  147.             [ @x, @y ],
  148.             [ @x, @y - 1 ],
  149.             [ @x + 1, @y ],
  150.             [ @x, @y + 1 ],
  151.             [ @x - 1, @y ]
  152.         ]
  153.  
  154.         for zombie in persons when zombie instanceof Zombie
  155.             if pos.has([ zombie.x, zombie.y ])
  156.                 zombies.push(zombie)
  157.  
  158.         i = 0
  159.         for zombie in zombies when i++ < 2
  160.             persons.splice(persons.indexOf(zombie), 1)
  161.  
  162. nbZombies = 0
  163. nbVictims = 0
  164. nbHunters = 0
  165.  
  166. can = document.getElementById('game')
  167. ctx = can.getContext('2d')
  168.  
  169. can.width  = 400
  170. can.height = 400
  171.  
  172. persons = mouse = sw = timer = null
  173.  
  174. if nbZombies + nbVictims + nbHunters > 20 * 20
  175.     alert 'STOP!'
  176.  
  177. init = ->
  178.     persons = []
  179.  
  180.     for i in [ 0 ... 160 ]
  181.         loop
  182.             x = Math.rand(0, 19)
  183.             y = Math.rand(0, 19)
  184.  
  185.             break if is_free_cell(x, y)
  186.  
  187.         switch Math.rand(0, 4)
  188.             when 0 then persons.push(new Zombie(x, y))
  189.             when 1 then persons.push(new Victim(x, y))
  190.             when 2,3,4 then persons.push(new Hunter(x, y))
  191.  
  192.     mouse = new Mouse(can)
  193.  
  194.     sw = new Stopwatch()
  195.  
  196.     timer = new Timer()
  197.  
  198.     img = preload_images({}, create)
  199.  
  200. create = ->
  201.     setInterval(->
  202.         for hunter in persons when hunter instanceof Hunter
  203.             hunter.move()
  204.             hunter.hunt()
  205.  
  206.         for victim in persons when victim instanceof Victim
  207.             victim.move()
  208.  
  209.         for zombie in persons when zombie instanceof Zombie
  210.             zombie.move()
  211.             zombie.bite()
  212.  
  213.         nbHunters = (persons.filter (person) -> person instanceof Hunter).length
  214.         nbVictims = (persons.filter (person) -> person instanceof Victim).length
  215.         nbZombies = (persons.filter (person) -> person instanceof Zombie).length
  216.  
  217.         stats = document.getElementById('stats')
  218.         stats.innerHTML = """
  219.             Nb. chasseurs : #{nbHunters}<br />
  220.             Nb. victimes : #{nbVictims}<br />
  221.             Nb. zombies : #{nbZombies}
  222.         """
  223.  
  224.     , 100)
  225.  
  226.     update()
  227.  
  228. update = ->
  229.     draw()
  230.  
  231.     mouse.update()
  232.     sw.update()
  233.     timer.update()
  234.  
  235.     requestAnimationFrame(update)
  236.  
  237. draw = ->
  238.     ctx.fillStyle = rgb(0, 0, 0)
  239.     ctx.fillRect(0, 0, can.width, can.height)
  240.  
  241.     for person in persons
  242.         person.draw(ctx)
  243.  
  244.     return
  245.  
  246. is_free_cell = (x, y) ->
  247.     for person in persons
  248.         if x == person.x and y == person.y
  249.             return false
  250.  
  251.     return true
  252.  
  253. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement