-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.coffee
92 lines (81 loc) · 2.62 KB
/
world.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
class World
@RADIUS = 50
@CIRCUMFERENCE = 35
@HEIGHT = 11
@ANGLE = Math.TAU / @CIRCUMFERENCE
@SIZE = @ANGLE * @RADIUS
@CENTER = new THREE.Vector3(0,0,0)
constructor: (game) ->
@game = game
@createMesh()
@initialize()
@alive = []
@maxAlive = 0
@maxAge = 0
initialize: () ->
@world = @createEmptyWorld()
debug: (world) ->
for row in (world || @world)
line = ""
for c in row
line += c.kind
console.debug line
createEmptyWorld: () ->
@world = []
createRow = (r)=>
row = (new Cell(@world,r,c) for c in [0..(World.CIRCUMFERENCE - 1)])
@world.push row
row
(createRow(r) for r in [0..(World.HEIGHT - 1)])
saveState: () ->
state = []
state.push(((if cell.kind == Cell.Life then Cell.Earth else cell.kind) for cell,c in row)) for row,r in @world
state
loadState: (state) ->
for row, r in state
for kind, c in row
@changeTile(r,c,kind)
reset: () ->
@maxAlive = 0
@maxAge = 0
for row,r in @world
for cell,c in row
@changeTile(r,c, Cell.Water)
step: () ->
oldAlive = @alive.length
@alive = []
for row in @world
for cell in row
cell.step()
for row in @world
for cell in row
cell.finishStep()
if cell.kind == Cell.Life
@maxAge = cell.age if cell.age > @maxAge
@alive.push cell
@finished = @alive.length == 0
@maxAlive = @alive.length if @alive.length > @maxAlive
diff = Math.abs(oldAlive - @alive.length)
sounds = Math.ceil(diff/10)
for i in [0..sounds]
@game.audio.playSound(Math.floor(Math.random() * Audio.Sounds).toString(),0)
changeTile: (r, c, kind) ->
cell = @world[r][c]
dirty = kind != cell.kind
if dirty
@world[r][c].kind = @world[r][c].newKind = kind
@world[r][c].updateMesh()
createMesh: () ->
# create the sphere's material
texture = THREE.ImageUtils.loadTexture('assets/poles.png')
sphereMaterial = new THREE.MeshLambertMaterial(map: texture)
# create a new mesh with
# sphere geometry - we will cover
# the sphereMaterial next!
sphere = new THREE.Mesh(
new THREE.SphereGeometry(
World.RADIUS,
100,
100),
sphereMaterial);
@mesh = sphere