-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.coffee
101 lines (68 loc) · 1.99 KB
/
index.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
moves = new Rx.Subject()
make2dArray = (w, h, f) ->
a = []
for x in [0...w]
for y in [0...h]
a[x] ?= []
a[x][y] = f x, y
a
Grid = (w, h) ->
grid = {}
grid.units = make2dArray w, h, (x, y) -> new GridUnit grid, x, y
grid.each = (f) ->
f grid.units[x][y], x, y for x in [0...w] for y in [0...h]
grid
GridView = (grid) ->
dom = $('<div>').addClass('grid')
grid.each (unit) ->
dom.append new GridUnitView unit
dom
GridUnit = (grid, x, y) ->
unit =
grid: grid
x: x
y: y
piece: new Rx.BehaviorSubject null
unit.piece.where((x) -> x? and x.unit.value != unit).subscribe (x) ->
x.unit.onNext unit
unit
GridUnitView = (gridUnit) ->
dom = $('<div>').addClass('grid-unit')
# dom.removeClass (i, cls) -> cls.replace /(col|row)-[0-9]+/g, ''
dom.addClass "col-#{gridUnit.x}"
dom.addClass "row-#{gridUnit.y}"
dom.toggleClass 'odd', gridUnit.x % 2 isnt 0 or gridUnit.y % 2 isnt 0
dom.toggleClass 'even', gridUnit.x % 2 is 0 or gridUnit.y % 2 is 0
gridUnit.piece.subscribe (value) ->
dom.toggleClass 'occupied', value isnt null
dom
Piece = (team, unit = null) ->
piece =
team: team
kinged: new Rx.BehaviorSubject false
unit: new Rx.BehaviorSubject unit
piece.unit.where((x) -> x? and x.piece.value != piece).subscribe (x) ->
x.piece.onNext piece
piece
PieceView = (piece) ->
dom = $('<div>').addClass('piece').addClass("team-#{piece.team}")
dom.onAsObservable('click').subscribe (e) ->
unit = piece.unit.value
next = unit.grid.units[unit.x][unit.y+1]
piece.unit.onNext next
piece.kinged.subscribe (value) ->
dom.toggleClass 'kinged', value
piece.unit.subscribe (value) ->
# dom.removeClass (i, cls) -> 'hey'
dom.addClass "col-#{value.x}"
dom.addClass "row-#{value.y}"
dom
grid = new Grid 8, 8
$ ->
body = $ 'body'
body.append gridView = new GridView grid
grid.each (unit, x, y) ->
return if (x + y) % 2 == 0 or (y > 1 and y < 6)
team = if y >= 4 then 'black' else 'red'
piece = new Piece team, unit
gridView.append new PieceView piece