-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameObjects.js
224 lines (197 loc) · 5.45 KB
/
gameObjects.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
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
class gameObject {
constructor(id, x, y, img) {
this.id = id
this.pos = createVector(x, y)
this.img = img
}
update(){
return
}
show(){
fill(255,100)
ellipse(this.pos.x,this.pos.y,5)
}
}
//CREATURES
class Creature extends gameObject {
constructor(id, x, y, img, dna = [0.3, 50, 0]) {
super(id, x, y, img)
this.dna = dna;
this.speed = dna[0];
this.viewingRadius = dna[1];
this.hunger = dna[2];
this.age = 0;
this.health = 100
this.target = this;
this.lastTarget = this;
this.dead = false;
}
move(target) {
//check Terrain
let viscosity = 1
this.pos.add(p5.Vector.sub(this.target.pos, this.pos).limit(this.speed * viscosity));
}
chooseTarget(qt) {
let angle = noise(frameCount / 1000 + this.id * 10) * TWO_PI * 4
let v = p5.Vector.fromAngle(angle).setMag(10)
v.add(this.pos)
let obj = { pos: v }
if (!(MainBoundary.contains(obj))) {
return this
}
return obj
}
update(qt) {
//dies
if (this.health <= 0) {
this.die()
}
//dead
if (this.dead) {
return
}
//aging
this.age += 1
//hunger
this.hunger += 0.1
this.hunger = constrain(this.hunger, 0, 300)
//hunger removes health
if (this.hunger > 100) {
this.health -= this.hunger / 100
}
//choosing new target after reaching last one
if (this.pos.dist(this.target.pos) < 3 || this.target.dead) {
this.lastTarget = this.target;
this.target = this.chooseTarget(qt);
}
//moving towards target if existing
if (this.target) {
this.move(this.target);
}
}
die() {
this.dead = true
this.deathTime = frameCount
this.deathAge = this.age
}
show() {
noStroke()
if (this.dead) {
if (DebugOptions["Debug CreatureInfo"] || this.info) {
let c = this.c || color(255, 100)
c.setAlpha(100)
fill(255,0,0, 50)
ellipse(this.pos.x, this.pos.y, this.viewingRadius * 2)
fill(c)
noStroke()
this.img ? image(this.img, this.pos.x - 8, this.pos.y - 8) : ellipse(this.pos.x, this.pos.y, 7)
textSize(10)
fill(255, 150)
text("health: " + str(round(this.health)), this.pos.x, this.pos.y)
text("hunger: " + str(round(this.hunger)), this.pos.x, this.pos.y + 10)
}
else {
return
}
}
else {
let c = this.c || color(255)
if (DebugOptions["Debug CreatureInfo"] || this.info) {
fill(255, 70)
ellipse(this.pos.x, this.pos.y, this.viewingRadius * 2)
fill(c)
noStroke()
this.img ? image(this.img, this.pos.x - 8, this.pos.y - 8) : ellipse(this.pos.x, this.pos.y, 7)
textSize(10)
fill(255)
text("health: " + str(round(this.health)), this.pos.x, this.pos.y)
text("hunger: " + str(round(this.hunger)), this.pos.x, this.pos.y + 10)
}
else {
fill(c)
noStroke()
this.img ? image(this.img, this.pos.x - 8, this.pos.y - 8, 10,10) : ellipse(this.pos.x, this.pos.y, 7)
}
}
}
}
class Human extends Creature {
constructor(id, x, y, img, dna) {
super(id, x, y, img, dna);
this.c = color(0, 0, 200)
this.speed = 0.15
}
chooseTarget(qt) {
let others = qt.instancesInView(this.pos.x, this.pos.y, this.viewingRadius)
let nearestDist = Infinity
let nearest
for (let i = 0; i < others.length; i++) {
let other = others[i]
if (other.constructor.name === "Food") {
if (this.pos.dist(other.pos) < nearestDist) {
nearest = other
nearestDist = this.pos.dist(other.pos)
}
}
}
return nearest || this
}
update(qt) {
if (this.pos.dist(this.target.pos) < 3) {
if (!(this.target == this)) {
this.target.amount -= 1
this.health += 200
this.hunger = 0
}
}
super.update(qt)
}
breed(otherHuman, id) {
let dna = this.mutate(this.dna, otherHuman.dna);
let baby = new Human(id, this.x, this.y, dna);
}
}
class Bear extends Creature {
constructor(id, x, y, img, dna) {
super(id, x, y, img, dna)
this.c = color(200, 0, 0)
this.speed = 0.3
}
chooseTarget(qt) {
let others = qt.instancesInView(this.pos.x, this.pos.y, this.viewingRadius)
let nearestDist = Infinity
let nearest
for (let i = 0; i < others.length; i++) {
let other = others[i]
if (other.constructor.name === "Human") {
if (this.pos.dist(other.pos) < nearestDist) {
nearest = other
nearestDist = this.pos.dist(other.pos)
}
}
}
return nearest || this
}
update(qt) {
if (this.pos.dist(this.target.pos) < 3) {
if (!(this.target == this)) {
this.target.die()
this.health += 200
this.hunger = 0
}
}
super.update(qt)
}
}
class Food extends gameObject {
constructor(id, x, y, amount) {
super(id, x, y)
this.amount = amount
}
update(){
if(this.amount <= 0){
this.dead = true
delete WorldObjects[this.id]
}
}
}