-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathEcoSystem.js
More file actions
182 lines (161 loc) · 4.53 KB
/
EcoSystem.js
File metadata and controls
182 lines (161 loc) · 4.53 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* @class EcoSystem
*/
class EcoSystem {
constructor() {
this.groups = {}; // agents
this.entities = {}; // generic container (food, poison)
this.agents = {}; // agent classes
this.behaviors = {}; // calculated behaviors
}
/**
* @method addEntities
* @param {Object} names
*/
addEntities(names) {
for (const i in names) {
this.entities[i] = names[i];
}
}
/**
* @method registerAgents
* @param {Object} agents
* agents object is a Object of BaseAgent Constructor
*/
registerAgents(agents) {
this.agents = agents;
for (const i in agents) {
this.groups[i] = []
}
}
/**
* @method initialPopulation
* @param {Object} init
*/
initialPopulation(init) {
this.initPopulation = init;
for (const i in this.initPopulation) {
if (this.groups[i] !== undefined) {
this.addAgent(this.agents[i], this.groups[i], this.initPopulation[i]);
}
}
}
/**
* @method add
* @param {String} type
* @param {Number} x
* @param {Number} y
* @param {Nmber} radius
* adds creatures to groups object
*/
add(type, x, y, radius = 5) {
let name = this.agents[type]
this.groups[type].push(name.setPos(x, y).setRadius(radius).build());
}
/**
* @metho addAgent
* @param {AgentBuilder} name
* @param {Array} list
* @param {Number} max
* adds agents to specific list in random pos
*/
addAgent(name, list, max) {
for (let i = 0; i < max; i++) {
let x = random(WIDTH);
let y = random(HEIGHT);
const radius = random(4, 5);
if (isInsideWall(x, y, radius)) {
x = random(WIDTH);
y = random(HEIGHT);
}
if (name instanceof AgentBuilder) {
list.push(name.setPos(x, y).setRadius(radius).build());
}
}
}
/**
* @method addBehavior
* @param {Object} config
* @param {String} config.name
* @param {String} config.like
* @param {String} config.dislike
* @param {Array} config.likeDislikeWeight
* @param {Number} config.cloneItSelf
* @param {Object} config.fear
* @param {Function} config.callback
*/
addBehavior(config) {
const agents = this.groups[config.name];
const foodPoison = [this.entities[config.like], this.entities[config.dislike]];
const likeDislikeWeight = config.likeDislikeWeight;
const callback = config.callback;
const fear = [];
const cloneItSelf = config.cloneItSelf;
if (!agents) return;
// PARSE FEAR ARRAY
for (const i in config.fear) {
fear.push([this.groups[i], config.fear[i][0], config.fear[i][1], config.fear[i][2]]);
}
this.behaviors[config.name] = { agents, foodPoison, likeDislikeWeight, callback, fear, cloneItSelf }
}
/**
* @method update
* updates all the behaviors
*/
update() {
for (const a in this.behaviors) {
const behave = this.behaviors[a];
this.batchUpdateAgents(behave.agents, behave.foodPoison, behave.likeDislikeWeight, (list, i) => {
let current = list[i];
// CLONE (i can do that with callback but just experimenting)
if (behave.cloneItSelf) {
let child = list[i].clone(behave.cloneItSelf);
if (child !== null) {
list.push(child);
}
}
// APPLY FEAR
for (let i = 0; i < behave.fear.length; i++) {
current.defineFear.apply(current, behave.fear[i]);
}
behave.callback && behave.callback.call(current);
})
}
}
/**
* @method batchUpdateAgents
* @param {Array} list
* @param {Array} foodPoison
* @param {Number} weight
* @param {Function} callback
*/
batchUpdateAgents(list, foodPoison, weight, callback) {
for (let i = list.length - 1; i >= 0; i--) {
list[i].update();
list[i].updateFlockBehavior(flk_slider_separate.value, flk_slider_align.value, flk_slider_cohesion.value);
list[i].applyFlock(list);
if (foodPoison[0] !== undefined && foodPoison[1] !== undefined) {
list[i].Behavior(foodPoison[0], foodPoison[1], weight);
}
list[i].boundaries();
callback && callback.call(list[i], list, i);
// Kill the agent
if (list[i].isDead()) {
const x = list[i].pos.x;
const y = list[i].pos.y;
foodPoison && foodPoison[0].push({ pos: new Vector(x, y) });
list.splice(i, 1);
}
}
}
/**
* @method render
*/
render() {
for (const i in this.groups) {
if (this.groups[i][0] instanceof BaseAgent) {
batchRenderAgents(this.groups[i]);
}
}
}
}