-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.coffee
116 lines (106 loc) · 3.46 KB
/
cell.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Cell
@Size = World.ANGLE * World.RADIUS
@Height = 1
@Distance = World.RADIUS + Cell.Height / 2
@AgeMaximum = 20
@Water = 0
@Earth = 1
@Grass = 2
@Forest = 3
@Life = 4
@Kinds =
0: 'Water',
1: 'Earth',
2: 'Grass',
3: 'Forest',
4: 'Life'
@KindsAmount = 5
constructor: (world, r, c, kind) ->
distanceFromCenter = Math.abs((World.HEIGHT - 1) / 2 - r)
tempRadius = World.RADIUS * Math.sin(Math.TAU / 4 - distanceFromCenter * World.ANGLE)
@length = World.ANGLE * tempRadius
@world = world
@row = r
@column = c
@age = 0
@kind = kind || Cell.Water
@newKind = @kind
@mesh = new THREE.Mesh(
new THREE.CubeGeometry(@length,
Cell.Size,
Cell.Height))
@materials = {}
@materials[Cell.Water] = new THREE.MeshLambertMaterial(color: 0x00BFFF)
@materials[Cell.Earth] = new THREE.MeshLambertMaterial(color: 0xA0522D)
@materials[Cell.Grass] = new THREE.MeshLambertMaterial(color: 0x7CFC00)
@materials[Cell.Forest] = new THREE.MeshLambertMaterial(color: 0x006400)
@materials[Cell.Life] = new THREE.MeshLambertMaterial(color: 0xFFA500)
@updateMesh()
updateMesh: () ->
@mesh.material = @materials[@kind]
neighbours: () ->
left = @column - 1
right = @column + 1
if @column == 0
left = World.CIRCUMFERENCE - 1
if @column == World.CIRCUMFERENCE - 1
right = 0
above = @row + 1
under = @row - 1
coordinates = [
[@row, left],
[@row, right],
[above, left],
[above, right],
[above, @column],
[under, @column],
[under, left],
[under, right]
]
neighbours = {}
for coord in coordinates
cell = null
row = @world[coord[0]]
if row?
cell = row[coord[1]]
if cell?
neighbours[cell.kind] ?= []
neighbours[cell.kind].push cell
neighbours
step: () ->
n = @neighbours()
age = 0
if n[Cell.Life]?.length > 0
for c in n[Cell.Life]
if c.age > age
age = c.age
if @kind == Cell.Life
if @age < Cell.AgeMaximum then @age++
else
@age = 0
switch @kind
when Cell.Water
if n[Cell.Life]?.length > 0 and age > 4
@newKind = Cell.Earth
when Cell.Earth
if n[Cell.Water]?.length > 0
@newKind = Cell.Grass
if n[Cell.Earth]?.length == 8
@newKind = Cell.Water
when Cell.Grass
if n[Cell.Life]?.length > 0 and n[Cell.Water]?.length > 0
@newKind = Cell.Forest
else if n[Cell.Water] is undefined
@newKind = Cell.Earth
when Cell.Forest
if n[Cell.Life]?.length > 0 and age > 2
@newKind = Cell.Life
when Cell.Life
if n[Cell.Water] is undefined
@newKind = Cell.Earth
if n[Cell.Forest] is undefined
@newKind = Cell.Grass
finishStep: () ->
if @kind != @newKind
@kind = @newKind
@updateMesh()