5
6
Player::Player() : Entity(),
8
11
sprite = new SpriteAnimation("player.png", FILTER_NONE, 64, 64);
9
12
sprite->Add("idle", 0, 0, 0.35f);
10
13
sprite->Play("idle");
11
14
SetGraphic(sprite);
15
scale = Vector2(0.25, 0.25);
19
footsteps = Audio::NewDeck(Assets::RequestAudio("footsteps.ogg"));
20
footsteps->SetLoops(0); //loop indefinitely
21
footsteps->SetVolume(0);
15
SetCollider(new RectangleCollider(32, 32));
24
SetCollider(new RectangleCollider(16, 16));
27
dark->SetGraphic(new Sprite("dark.png", FILTER_NONE, 2048, 1536)); //alpha?
18
30
void Player::Update()
34
if (Input::IsKeyMaskHeld("left"))
36
velocity.x -= ACCELERATION * Monocle::deltaTime;
38
if (Input::IsKeyMaskHeld("right"))
40
velocity.x += ACCELERATION * Monocle::deltaTime;
42
if (Input::IsKeyMaskHeld("up"))
44
velocity.y -= ACCELERATION * Monocle::deltaTime;
46
if (Input::IsKeyMaskHeld("down"))
48
velocity.y += ACCELERATION * Monocle::deltaTime;
51
// velocity will approach 0 in steps of FRICTION * dt
52
velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
53
velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
55
if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
56
velocity = velocity.GetNormalized() * MAXSPEED;
61
Collider *collider = NULL;
63
position.x += velocity.x * Monocle::deltaTime;
64
while (Collide("Solid") || (collider = Collide("creature")))
66
// Don't colide with hunters. We should just die.
68
Creature *c = (Creature *) collider->GetEntity();
69
if (c->state == "hunt") { break; }
73
if (velocity.x == 0) { break; }
74
//printf("collision1\n");
75
position.x -= SIGN(velocity.x, 0.1);
77
if (xcol) {velocity.x = 0;}
80
position.y += velocity.y * Monocle::deltaTime;
81
while (Collide("Solid") || (collider = Collide("creature")))
83
// Don't colide with hunters. We should just die.
85
Creature *c = (Creature *) collider->GetEntity();
86
if (c->state == "hunt") { break; }
90
if (velocity.y == 0) { break; }
91
//printf("collision2\n");
92
position.y -= SIGN(velocity.y, 0.1);
94
if (ycol) {velocity.y = 0;}
96
float magnitude = velocity.GetMagnitude();
98
footsteps->SetVolume(magnitude / MAXSPEED);
100
// How far away can they hear your footsteps?
101
// The circle is diameter 300 at MAXSPEED, 32 (2x your size) at 0 speed
102
noisiness = ((magnitude / MAXSPEED *(400-64)) + 64) /2;
104
dark->position = position;
22
106
//Scene::GetCamera()->position = position;
109
Creature::Creature() : Entity(),
112
DEFAULT_MAXSPEED(80.0f),
115
sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
116
sprite->Add("idle", 0, 0, 0.35f);
117
sprite->Add("move", 1, 2, 4.0f);
118
sprite->Add("slowmove", 1, 2, 2.0f);
119
sprite->Play("idle");
121
scale = Vector2(0.25, 0.25);
124
SetCollider(new RectangleCollider(16, 16));
126
alert = Assets::RequestAudio("alert.ogg");
127
freakout = Assets::RequestAudio("freakout.ogg");
128
chomp = Assets::RequestAudio("chomp.ogg");
129
skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
130
skitter1->SetLoops(0); //loop indefinitely
131
sniff = Audio::NewDeck(Assets::RequestAudio("sniff.ogg"));
134
maxspeed = DEFAULT_MAXSPEED;
136
//set aiTime to random so creatures tick at different times
137
aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
142
void Creature::Update()
146
if (state == "idle" || state == "wander")
148
aiTime += Monocle::deltaTime;
155
direction = Vector2::zero;
159
if (state != "wander") {
160
//printf("wander\n");
161
direction = Vector2::Random();
170
Player *player = ((DarkScene *)scene)->player;
171
if ( (player->position - position).GetSquaredMagnitude() <
172
pow(player->noisiness, 2) )
177
direction = Vector2::zero;
178
Ping::NewPing(position, 8.0f, 32.0f, 0.15f); // ping diameter 16-64 over .5s
179
aiTime = 0.0f; // diff variable? stateTime?
181
} // if idle or wander
182
else if (state == "alert")
184
aiTime += Monocle::deltaTime;
186
// if we've been listening past the grace period and hear
187
// something, switch state again
188
// We hear the player at DOUBLE noisiness distance, cuz WE'RE LISNING!
189
Player *player = ((DarkScene *)scene)->player;
190
if ( aiTime > 1.0f &&
191
(player->position - position).GetSquaredMagnitude() <
192
pow(player->noisiness * 2, 2) )
195
Ping::NewPing(position, 8.0f, 32.0f, 0.15f);
196
direction = (player->position - position).GetNormalized();
197
maxspeed = STALKSPEED;
201
// if we've been listening a long time, switch back to idle
203
//printf("end alert\n");
208
else if (state == "stalk")
210
aiTime += Monocle::deltaTime;
213
// * it's been >1s & we hear the player
214
// * we collide with the player
215
Player *player = ((DarkScene *)scene)->player;
216
if ((aiTime > 1.0f &&
217
(player->position - position).GetSquaredMagnitude() <
218
pow(player->noisiness, 2) )
223
direction = (player->position - position).GetNormalized();
224
maxspeed = 0; // give them a running start
227
Ping::NewPing(position, 8.0f, 64.0f, 0.15f);
232
// Go back to IDLE if:
234
// * we stalk for more than 10-15s
235
if ((aiTime > 15.0f) || Collide("wall") || Collide("creaturewall"))
237
// play frustrated noise?
241
maxspeed = DEFAULT_MAXSPEED;
242
direction = Vector2::zero;
245
else if (state == "hunt")
247
aiTime += Monocle::deltaTime;
248
noiseTime += Monocle::deltaTime;
249
graceTime += Monocle::deltaTime;
251
if ((graceTime > 1.0f) && (maxspeed != DEFAULT_MAXSPEED))
252
maxspeed = DEFAULT_MAXSPEED;
254
// if we hear the player (
255
Player *player = ((DarkScene *)scene)->player;
256
if ( (player->position - position).GetSquaredMagnitude() <
257
pow(player->noisiness, 2) )
259
direction = (player->position - position).GetNormalized();
261
if (noiseTime > 2.0f) {
263
Ping::NewPing(position, 8.0f, 64.0f, 0.15f);
268
// I guess chill out if it's been a while
269
if (aiTime > 15.0f) {
272
direction = Vector2::zero;
278
// if we collide with you, move you back to the starting point
279
if (Collide("player")) {
280
// TODO: first wait a second or 3?
282
DarkScene *scene = (DarkScene *) GetScene();
283
Entity* playersp = scene->GetFirstEntityWithTag("playerspawner");
284
scene->player->position = playersp->position;
288
velocity += direction * ACCELERATION * Monocle::deltaTime;
290
velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
291
velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
293
if (velocity.GetSquaredMagnitude() > pow(maxspeed, 2))
294
velocity = velocity.GetNormalized() * maxspeed;
299
position.x += velocity.x * Monocle::deltaTime;
300
while (Collide("Solid") || Collide("creaturewall"))
303
if (velocity.x == 0) { break; }
304
//printf("collision1\n");
305
position.x -= SIGN(velocity.x, 0.1);
307
if (xcol) {velocity.x = 0;}
309
position.y += velocity.y * Monocle::deltaTime;
310
while (Collide("Solid") || Collide("creaturewall"))
313
if (velocity.y == 0) { break; }
314
//printf("collision2\n");
315
position.y -= SIGN(velocity.y, 0.1);
317
if (ycol) {velocity.y = 0;}
319
Vector2 distance = ((DarkScene *)scene)->player->position - position;
320
if (state == "stalk")
322
float vol = (distance.GetMagnitude() / -250.0f) + 1.0f;
323
sniff->SetVolume(vol);
325
sprite->Play("slowmove");
327
else if (velocity.GetSquaredMagnitude() > 20)
329
// at distance 0, vol should be 1.0. at 800, it should be 0.
330
float vol = (distance.GetMagnitude() / -600.0f) + 1.0f;
331
skitter1->SetVolume(vol);
333
sprite->Play("move");
337
skitter1->Pause(); //Stop()?
338
sprite->Play("idle");
342
Ping::Ping() : Entity(),
343
d1(0.0f), d2(0.0f), t(0.0f), t2(0.0f)
345
sprite = new Sprite("yellow.png", FILTER_LINEAR, 64, 64);
347
scale = Vector2::zero;
350
Ping *Ping::NewPing(Vector2 pos, float d1, float d2, float t2)
352
Ping *ping = new Ping();
356
ping->position = pos;
358
Game::GetScene()->Add(ping);
366
// once we're done LERPing, get gone
370
t += Monocle::deltaTime;
371
float scaleFactor = LERP(d1/64, d2/64, t/t2);
372
scale = Vector2(scaleFactor, scaleFactor);