-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spider.pde
142 lines (102 loc) · 3.19 KB
/
Spider.pde
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
/*
For Utility:
When severely hungry, goes outside of web hunting for food
When full maturaty, searches for mate (female eats male, gender randomly chosen)
*/
public enum SpiderBehaviour{
CHASING,
CALM
}
class Spider extends Creature{
static final float CHASE_SPEED = 80;
static final float CALM_SPEED = 30;
static final float NEXT_POSITION_TIME = 5;
Web web;
SpiderBehaviour behaviour;
float[] legOffset;
float legTime = 0;
Object target;
Vec2 desiredLocation;
float dec_t;
Spider(float x, float y, float m, PVector c){
super(x,y,m,c);
behaviour = SpiderBehaviour.CALM;
//Build body using Box2D
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(box2d.coordPixelsToWorld(x,y));
body = box2d.createBody(bd);
body.setUserData(this);
//cephalothorax is the part of a spider in which the legs are connected
CircleShape cephalothorax = new CircleShape();
cephalothorax.m_radius = box2d.scalarPixelsToWorld(5);
FixtureDef fd = new FixtureDef();
fd.shape=cephalothorax;
fd.density = 1;
fd.friction = 1;
fd.restitution = 1f;
body.createFixture(fd);
//Abdomen
CircleShape abdomen = new CircleShape();
abdomen.m_radius = box2d.scalarPixelsToWorld(10);
abdomen.m_p.set(box2d.scalarPixelsToWorld(-8),0);
fd = new FixtureDef();
fd.shape=abdomen;
fd.density = 1;
fd.friction = 0.3;
fd.restitution = 0.5f;
body.createFixture(fd);
//Offset to give the legs a more natural imperfect position
legOffset = new float[8];
for(int i =0; i < 8; i++){
legOffset[i] = random(-PI/4,PI/4);
}
//Leg creation
appendages = new Appendage[8];
float legAngle = PI/6;
float legLength = 12;
for(int i = 1; i < 5; i++){
appendages[i-1] = new Appendage(box2d.getBodyPixelCoord(body), i*legAngle, 5, legLength, legTime+legOffset[i-1], 3);
appendages[i+3] = new Appendage(box2d.getBodyPixelCoord(body), -i*legAngle, 5, legLength, -(legTime+legOffset[i+3]), 3);
}
//Web creation
web = new Web(new Vec2(x,y));
//web.addInside(this);
terrain.add(web);
desiredLocation = web.getRandomLocation();
}
void update(){
super.update();
int dirSwitch = 1;
for(Appendage a : appendages){
a.rate = dirSwitch*0.007*box2d.vectorWorldToPixels(body.getLinearVelocity()).length();
a.pos = box2d.getBodyPixelCoord(body);
a.angle = getAngle();
a.update();
dirSwitch = dirSwitch*-1;
}
//AI Behaviour
if(target != null && web.inside.contains(target)){
topSpeed = CHASE_SPEED;
Vec2 tPos = box2d.getBodyPixelCoord(target.body);
arrive(tPos);
return;
}
target = web.getCreatureInside();
if(target == this){
target = null;
}
topSpeed = CALM_SPEED;
if(dec_t > NEXT_POSITION_TIME){
desiredLocation = web.getRandomLocation();
dec_t = 0;
}
arrive(desiredLocation);
dec_t+=0.006;
}
void display(){
super.display();
for(Appendage a : appendages)
a.display();
}
}