-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.js
More file actions
258 lines (234 loc) · 9.76 KB
/
Copy pathentity.js
File metadata and controls
258 lines (234 loc) · 9.76 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
var antsApp = {};
antsApp.pixiApp = {};
antsApp.width = 640; //this gets updated on load
antsApp.height = 480;
antsApp.center = {x:antsApp.width/2, y:antsApp.height/2}; //updated on load
antsApp.gameEntities = [];
antsApp.cooldown = 30;
antsApp.worldSpeed = 1;
antsApp.worldSize = 1;
antsApp.antBugRange = 10;
antsApp.StartingAnts = 10;
antsApp.StartingBugs = 180;
antsApp.StartingFood = 20;
antsApp.zoomRate = .1;
antsApp.food = 0;
antsApp.foodCostAnt = 5;
antsApp.baseSize = 20;
antsApp.entity = {};
antsApp.entity.moods = [
{name:"confused",speed:.80,antColor:antColorBlue,rest:.6,moveDirection:true,randomDirection:true,attack:false},
{name:"bored",speed:.25,antColor:antColorDarkGreen,rest:.7,moveDirection:true,randomDirection:true,attack:false},
{name:"calm",speed:.5,antColor:antColorDarkRed,rest:.25,moveDirection:true,randomDirection:true,attack:false},
{name:"happy",speed:1.2,antColor:antColorYellow,rest:.1,moveDirection:true,randomDirection:true,attack:false},
{name:"excited",speed:1,antColor:antColorGreen,rest:.05,moveDirection:true,randomDirection:true,attack:false},
{name:"hungry",speed:.7,antColor:antColorYellow,rest:.25,moveDirection:true,randomDirection:true,attack:false},
{name:"scared",speed:1.25,antColor:antColorDarkBlue,rest:.75,moveDirection:false,randomDirection:false,attack:false},
{name:"surprised",speed:1.25,antColor:antColorYellow,rest:.2,moveDirection:true,randomDirection:true,attack:false},
{name:"angry",speed:1.5,antColor:antColorRed,rest:0,moveDirection:true,randomDirection:false,attack:true},
{name:"mad",speed:2,antColor:antColorRed2,rest:0,moveDirection:true,randomDirection:false,attack:true},
{name:"sad",speed:2.25,antColor:antColorDarkYellow,rest:.5,moveDirection:false,randomDirection:true,attack:false},
{name:"sick",speed:.25,antColor:antColorDarkYellow,rest:.8,moveDirection:false,randomDirection:false,attack:false},
{name:"food",speed:0,antColor:foodColorOrange,rest:1,moveDirection:false,randomDirection:false,attack:false},
{name:"worried",speed:1,antColor:bugColorDarkBrown,rest:0,moveDirection:true,randomDirection:true,attack:false},
{name:"withFood",speed:.55,antColor:antWithFoodColor,rest:0,moveDirection:true,randomDirection:false,attack:false},
{name:"home",speed:0,antColor:homeColor,rest:1,moveDirection:false,randomDirection:false,attack:false}
];//,"hungry","sleepy","happy","sad","angry","excited","bored","confused","scared","surprised","sick","silly","shy","tired","worried","lonely","proud","puzzled"];
antsApp.entity.startingMoods = ["calm","happy","excited","hungry"];
antsApp.entity.moodsBugs = ["confused","bored","calm","scared","sick"];
antsApp.entity.AntAttackProgression = ["calm","surprised","angry","mad"];
//ant
antsApp.entity.getAnt = function () {
let ant = antsApp.entity.getBase();
ant.mood = antsApp.entity.getMoodByName(antsApp.entity.startingMoods[Math.floor(Math.random() * antsApp.entity.startingMoods.length)]);
//getMoodByName(startingMoods[Math.floor(Math.random() * startingMoods.length)]);
//getMoodByName("happy");
ant.beginFill(ant.mood.antColor);
ant.size = 3 * antsApp.worldSize;
ant.drawRect(0, 0, ant.size, ant.size);
ant.endFill();
ant.eType = "ant";
//ants have a different starting position than default
ant.home = {x:antsApp.center.x + 9, y:antsApp.center.y + 9};
ant.x = ant.home.x;
ant.y = ant.home.y;
return ant;
}
antsApp.entity.getBase = function (){
let base = new PIXI.Graphics();
base.x = Math.random() * document.body.clientWidth;
base.y = Math.random() * document.body.clientHeight;
base.direction = Math.random() * 2 * Math.PI;
base.moodCooldown = 0;
base.moveCooldown = Math.floor(Math.random() * antsApp.cooldown);
base.eType = "none";
//ant is tracking a bug
base.track = {};
//bug is being tracked by an ant(s)
base.tracking = [];
base.life = 100;
base.hitpoints = 1;
base.delete = false;
return base;
}
//bug
antsApp.entity.getBug = function() {
let bug = antsApp.entity.getBase();
//get random mood name from moodsBugs array;
let mood = antsApp.entity.moodsBugs[Math.floor(Math.random() * antsApp.entity.moodsBugs.length)];
bug.mood = antsApp.entity.getMoodByName(mood);
bug.beginFill(bugColorBrown);
bug.size = 7 * antsApp.worldSize;
bug.drawRect(0, 0, bug.size, bug.size);
bug.endFill();
bug.eType = "bug";
return bug;
}
//home
antsApp.entity.getHome = function() {
let home = antsApp.entity.getBase();
home.mood = antsApp.entity.getMoodByName("home");
home.beginFill(home.mood.antColor);
home.size = antsApp.baseSize * antsApp.worldSize;
home.drawRect(0, 0, home.size, home.size);
home.endFill();
home.eType = "home";
home.x = antsApp.center.x; // - home.size / 2;
home.y = antsApp.center.y; // - home.size / 2;
return home;
}
antsApp.entity.changeMood = function(ent, mood) {
ent.mood = mood;
ent.beginFill(ent.mood.antColor);
ent.drawRect(0, 0, ent.size, ent.size);
ent.endFill();
ent.moodCooldown = antsApp.cooldown;
}
antsApp.entity.move = function(ent, time){
let speed = (ent.mood.speed * antsApp.worldSpeed * time);
let ant = false;
if (ent.eType == "ant"){
ant = true;
}
if (Math.random() > ent.mood.rest){
antsApp.entity.moveRandom(ent, speed);
if (ent.mood.name == "withFood"){
//if the ant has food move it towards the home
antsApp.entity.moveDirection(ent, speed, ent.home);
}else{
antsApp.entity.moveDirection(ent, speed, ent.track);
}
}else if (ent.mood.randomDirection){
if (ent.moveCooldown < antsApp.cooldown) {
ent.moveCooldown++;
}else{
ent.direction = antsApp.entity.getRandomDirection();
ent.moveCooldown = Math.floor(Math.random() * antsApp.cooldown);;
}
}
/* if the ent goes off the screen move it to the other side
if (ent.x > getWidth()) {
ent.x = 0;
} else if (ent.x < 0) {
ent.x = getWidth();
}
if (ent.y > getHeight()) {
ent.y = 0;
} else if (ent.y < 0) {
ent.y = getHeight();
}
*/
}
antsApp.entity.moveRandom = function(ent, speed) {
//using the deta time to move the ant randomly
ent.x += Math.random() * speed - (speed/2);
ent.y += Math.random() * speed - (speed/2);
}
antsApp.entity.moveDirection = function(ent, speed, location) {
if (ent.mood.moveDirection){
//if tracking point towards the tracking point
if (location.x != undefined) {
let dx = (location.x - ent.x);
let dy = (location.y - ent.y);
ent.direction = Math.atan2(dy, dx);
//antsApp.entity.moveRandom(ent, 5);
}
ent.x += Math.cos(ent.direction) * speed;
ent.y += Math.sin(ent.direction) * speed;
}
}
antsApp.entity.getRandomDirection = function() {
return Math.random() * 2 * Math.PI;
}
antsApp.entity.getRandomMood = function(start) {
if(start){
return antsApp.entity.moods[Math.floor(Math.random() * 7)]; //only the first 6 moods
} else {
return antsApp.entity.moods[Math.floor(Math.random() * (antsApp.entity.moods.length))];
}
}
antsApp.entity.getMoodByName = function(name) {
return antsApp.entity.moods.find(mood => mood.name == name);
}
antsApp.entity.getAntsByMood = function(mood) {
return antsApp.gameEntities.filter(ant => ant.mood.name == mood);
}
//ant is hunting a bug, if food then change to exited.
antsApp.entity.AntHuntingBug = function(bug, ant) {
let maxProgression = antsApp.entity.AntAttackProgression.length - 1;
let currentProgression = antsApp.entity.AntAttackProgression.indexOf(ant.mood.name);
//if the ant is excited and the bug is food
if (bug.mood.name == "food" && ant.mood.name != "excited"){
antsApp.entity.changeMood(ant, antsApp.entity.getMoodByName("excited"));
}else if (currentProgression < maxProgression && ant.mood.name != "mad" && bug.mood.name != "food"){
//if not at max progression
antsApp.entity.changeMood(ant, antsApp.entity.getMoodByName(antsApp.entity.AntAttackProgression[currentProgression + 1]));
currentProgression++;
}
}
//ant is attacking a bug
antsApp.entity.AntAttackingBug = function(bug, ant) {
if (bug.mood.name != "worried"){
antsApp.entity.changeMood(bug, antsApp.entity.getMoodByName("worried"));
}
bug.life -= ant.hitpoints;
if (bug.life < 1){
antsApp.entity.changeMood(bug, antsApp.entity.getMoodByName("food"));
antsApp.StartingBugs--;
bug.life = 100;
antsApp.StartingFood++;
//Now that the bug is food the ants need to change to excited
bug.tracking.forEach(a => {
antsApp.entity.changeMood(a, antsApp.entity.getMoodByName("excited"));
//a.tracking = {};
});
bug.scale.x = 1.25;
bug.scale.y = 1.25;
};//else, the bug is still alive
}
//ant attaching food
antsApp.entity.AntEatingFood = function(food, ant) {
if (ant.mood.name != "withFood"){
if(food.life > 0){
//ant picks up some food
food.life--;
antsApp.entity.changeMood(ant, antsApp.entity.getMoodByName("withFood"));
}else{
//food is gone, remove ants and dispose of food
try{
food.tracking.forEach(a => {
antsApp.entity.changeMood(a, antsApp.entity.getMoodByName("happy"));
a.tracking = {};
});
//remove the food
food.delete = true;
}catch(e){
}
}
}
}
//ant returning home
antsApp.entity.AntFoodDrop = function(ant, home) {
antsApp.entity.changeMood(ant, antsApp.entity.getMoodByName("happy"));
antsApp.food++;
}