-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch.js
132 lines (117 loc) · 4 KB
/
branch.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
(function() {
var Branch;
Branch = (function() {
Branch.ids = 0;
function Branch(props) {
var _base, _base1, _base2, _base3;
this.props = props;
this.vertices = [];
this.branches = [];
this.savePosition();
this.id = Branch.ids++;
(_base = this.props).initial_health || (_base.initial_health = 100);
(_base1 = this.props).color || (_base1.color = "#000000");
(_base2 = this.props).branch_thickness || (_base2.branch_thickness = 7);
this.props.end_branch_thickness = this.props.branch_thickness * .8;
this.health = this.props.initial_health;
(_base3 = this.props).post_step || (_base3.post_step = function(branch) {
var left, new_props, right;
if (branch.props.initial_health > 10 && branch.branches.length < 1 && branch.health < branch.props.initial_health / 2) {
new_props = jQuery.extend(true, {}, branch.props);
new_props.initial_health *= .8;
new_props.branch_thickness = branch.props.end_branch_thickness;
new_props.color = averageColors(branch.props.color, "#" + (0xFFFFFF * Math.random()).toString(12), 10, 1);
left = new Branch(jQuery.extend(true, {}, new_props));
right = new Branch(jQuery.extend(true, {}, new_props));
branch.spawn(left);
branch.spawn(right);
left.savePosition();
right.savePosition();
left.rotate(-Math.PI / 12);
return right.rotate(Math.PI / 12);
}
});
}
Branch.prototype.spawn = function(branch) {
return this.branches.push(branch);
};
Branch.prototype.rotate = function(amount) {
var ang, mag;
ang = this.get_angle();
mag = this.get_speed();
this.props.velocity[0] = Math.cos(ang + amount) * mag;
return this.props.velocity[1] = Math.sin(ang + amount) * mag;
};
Branch.prototype.get_angle = function() {
return Math.atan2(this.props.velocity[1], this.props.velocity[0]);
};
Branch.prototype.get_speed = function() {
return Math.sqrt((this.props.velocity[0] * this.props.velocity[0]) + (this.props.velocity[1] * this.props.velocity[1]));
};
Branch.prototype.step = function(count) {
var i, lng;
if (count == null) {
count = 1;
}
this.rotate((Math.random() - .5) / 12);
i = 0;
lng = this.branches.length;
while (i < lng) {
this.branches[i].step();
i++;
}
if (this.health <= 0) {
return;
}
i = 0;
lng = this.props.position.length;
while (i < lng) {
this.props.position[i] += this.props.velocity[i];
i++;
}
this.savePosition();
this.health--;
this.percDone = 1 - this.health / this.props.initial_health;
this.props.post_step(this);
if (this.health <= 0) {
return this.on_branch_dead();
}
};
Branch.prototype.on_branch_dead = function() {};
Branch.prototype.draw = function(ctx, drawLast) {
var i, lng, prevVertex, tempS, vertex, _results;
if (drawLast == null) {
drawLast = 1;
}
i = 0;
lng = this.branches.length;
while (i < lng) {
this.branches[i].draw(ctx, drawLast);
i++;
}
if (this.health <= 0) {
return;
}
i = this.vertices.length - drawLast;
tempS = this.vertices.length;
_results = [];
while (i < tempS) {
vertex = this.vertices[i];
prevVertex = this.vertices[i - 1];
ctx.lineWidth = this.props.branch_thickness - this.props.end_branch_thickness * this.percDone;
ctx.beginPath();
ctx.strokeStyle = this.props.color;
ctx.moveTo(prevVertex[0], prevVertex[1]);
ctx.lineTo(vertex[0], vertex[1]);
ctx.stroke();
_results.push(i++);
}
return _results;
};
Branch.prototype.savePosition = function() {
return this.vertices.push(this.props.position.concat());
};
return Branch;
})();
window.Branch = Branch;
}).call(this);