-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathItem.js
132 lines (108 loc) · 3.42 KB
/
Item.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
class Item {
constructor(x, y, type) {
this.pos = createVector(x, y)
this.vel = createVector(1, 0)
this.acc = createVector(0, 0)
this.xoff = 0
this.maxForce = 0.2
this.maxSpeed = 4
this.type = type
}
nearestTarget() {
let nearestTarget = null;
let nearestDistance = Infinity;
for (let item of items) {
if (
(this.type === "rock" && item.type === "scissors") ||
(this.type === "scissors" && item.type === "paper") ||
(this.type === "paper" && item.type === "rock")
) {
const distance = p5.Vector.dist(this.pos, item.pos);
if (distance < nearestDistance) {
nearestDistance = distance;
nearestTarget = item;
}
}
}
return nearestTarget;
}
seek() {
const target = this.nearestTarget()
if (target) {
const desired = p5.Vector.sub(target.pos, this.pos);
desired.setMag(this.maxSpeed);
const steer = p5.Vector.sub(desired, this.vel);
steer.limit(this.maxForce / 4);
this.applyForce(steer);
}
this.update()
}
update() {
this.vel.add(this.acc)
this.vel.limit(this.maxSpeed)
this.pos.add(this.vel)
this.acc.set(0, 0)
}
wander() {
noiseSeed()
let angle = noise(this.xoff) * TWO_PI * 4;
let steer = p5.Vector.fromAngle(angle);
steer.setMag(this.maxForce);
this.applyForce(steer);
this.xoff += 0.01;
}
applyForce(force) {
this.acc.add(force)
}
checkEdge() {
if (this.pos.x > width - 16) {
this.pos.x = width - 16
this.vel.mult(-1)
}
if (this.pos.x < 16) {
this.pos.x = 16
this.vel.mult(-1)
}
if (this.pos.y > height - statsBar.height - 16) {
this.pos.y = height - statsBar.height - 16
this.vel.mult(-1)
}
if (this.pos.y < 16) {
this.pos.y = 16
this.vel.mult(-1)
}
}
eat(items) {
for (let item of items) {
if (item.type == this.type) continue
const distance = p5.Vector.dist(this.pos, item.pos);
if (distance <= 32) {
if (this.type === "scissors" && item.type === "paper") {
item.type = "scissors"
}
if (this.type === "rock" && item.type === "scissors") {
item.type = "rock"
}
if (this.type === "paper" && item.type === "rock") {
item.type = "paper"
}
}
}
}
show() {
imageMode(CENTER)
switch (this.type) {
case 'rock':
image(rockImage, this.pos.x, this.pos.y, 32, 32)
break
case 'paper':
image(paperImage, this.pos.x, this.pos.y, 32, 32)
break
case 'scissors':
image(scissorsImage, this.pos.x, this.pos.y, 32, 32)
break
default:
throw new Error("unknown item type")
}
}
}