game.coffee 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. URL = 'ws://localhost:8099'
  2. PLAYER_COLORS = ['blue', 'green', 'red', 'yellow']
  3. WALL_NONE = 0
  4. WALL_TOP = 1
  5. WALL_RIGHT = 2
  6. WALL_BOTTOM = 4
  7. WALL_LEFT = 8
  8. WALL_ALL = WALL_TOP | WALL_RIGHT | WALL_BOTTOM | WALL_LEFT
  9. div = (cls) ->
  10. elem = document.createElement 'div'
  11. elem.className = cls
  12. elem
  13. divin = (parent, cls) ->
  14. elem = div cls
  15. parent.appendChild elem
  16. elem
  17. class Game
  18. constructor: (@w, @h, elem) ->
  19. @render elem if elem
  20. get_wall: (x, y, direction) ->
  21. x *= 2
  22. y *= 2
  23. if direction == WALL_TOP
  24. x += 1
  25. else if direction == WALL_RIGHT
  26. x += 2
  27. y += 1
  28. else if direction == WALL_BOTTOM
  29. x += 1
  30. y += 2
  31. else if direction == WALL_LEFT
  32. y += 1
  33. @board.children[y].children[x]
  34. click_wall: (x, y, direction) ->
  35. wall = @get_wall x, y, direction
  36. wall.className += ' clicked'
  37. get_room: (x, y) ->
  38. @board.children[y * 2 + 1].children[x * 2 + 1]
  39. occupy: (x, y, player) ->
  40. room = @get_room x, y
  41. room.style.backgroundColor = PLAYER_COLORS[player]
  42. render: (elem) ->
  43. @board = div 'board'
  44. elem.appendChild @board
  45. row = divin @board, 'row'
  46. for x in [1..@w]
  47. divin row, 'dot'
  48. divin row, 'wall-h clicked'
  49. divin row, 'dot'
  50. for y in [1..@h]
  51. row = divin @board, 'row'
  52. for x in [1..@w]
  53. clicked = if x == 1 then ' clicked' else ''
  54. divin row, "wall-v#{clicked}"
  55. divin row, 'room'
  56. divin row, 'wall-v clicked'
  57. row = divin @board, 'row'
  58. clicked = if y == @h then ' clicked' else ''
  59. for x in [1..@w]
  60. divin row, 'dot'
  61. divin row, "wall-h#{clicked}"
  62. divin row, 'dot'
  63. game = new Game 6, 6
  64. game.render document.getElementById 'game'
  65. game.click_wall 2, 2, WALL_RIGHT
  66. game.click_wall 2, 2, WALL_BOTTOM
  67. game.click_wall 1, 2, WALL_RIGHT
  68. game.click_wall 2, 2, WALL_TOP
  69. game.occupy 2, 2, 2
  70. #ws = new WebSocket URL
  71. #
  72. #ws.onopen = ->
  73. # console.log 'open'
  74. #
  75. #ws.onclose = ->
  76. # console.log 'close'
  77. #
  78. #ws.onerror = (e) ->
  79. # console.log 'error', e
  80. #
  81. #ws.onmessage = (msg) ->
  82. # console.log 'msg', msg