6
5
Player::Player() : Entity(),
11
8
sprite = new SpriteAnimation("player.png", FILTER_NONE, 64, 64);
12
9
sprite->Add("idle", 0, 0, 0.35f);
13
10
sprite->Play("idle");
14
11
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);
24
SetCollider(new RectangleCollider(16, 16));
27
dark->SetGraphic(new Sprite("dark.png", FILTER_NONE, 2048, 1536)); //alpha?
15
SetCollider(new RectangleCollider(32, 32));
30
18
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
if (Collide("exit")) {
62
Scene *s = GetScene();
65
Text *win1 = new Text("You made it safely through the dark!",
66
Assets::RequestFont("LiberationSans-Regular.ttf", 50.0f));
67
win1->position = Vector2(-350,0);
70
Text *win2 = new Text("Congratulations!",
71
Assets::RequestFont("LiberationSans-Regular.ttf", 50.0f));
72
win2->position = Vector2(-150,50);
76
Collider *collider = NULL;
78
position.x += velocity.x * Monocle::deltaTime;
79
while (Collide("Solid") || (collider = Collide("creature")))
81
// Don't colide with hunters. We should just die.
83
Creature *c = (Creature *) collider->GetEntity();
84
if (c->state == "hunt") { break; }
88
if (velocity.x == 0) { break; }
89
//printf("collision1\n");
90
position.x -= SIGN(velocity.x, 0.1);
92
if (xcol) {velocity.x = 0;}
95
position.y += velocity.y * Monocle::deltaTime;
96
while (Collide("Solid") || (collider = Collide("creature")))
98
// Don't colide with hunters. We should just die.
100
Creature *c = (Creature *) collider->GetEntity();
101
if (c->state == "hunt") { break; }
105
if (velocity.y == 0) { break; }
106
//printf("collision2\n");
107
position.y -= SIGN(velocity.y, 0.1);
109
if (ycol) {velocity.y = 0;}
111
float magnitude = velocity.GetMagnitude();
113
footsteps->SetVolume(magnitude / MAXSPEED);
115
// How far away can they hear your footsteps?
116
// The circle is diameter 300 at MAXSPEED, 32 (2x your size) at 0 speed
117
noisiness = ((magnitude / MAXSPEED *(400-64)) + 64) /2;
119
dark->position = position;
121
22
//Scene::GetCamera()->position = position;
124
Creature::Creature() : Entity(),
127
DEFAULT_MAXSPEED(80.0f),
130
sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
131
sprite->Add("idle", 0, 0, 0.35f);
132
sprite->Add("move", 1, 2, 4.0f);
133
sprite->Add("slowmove", 1, 2, 2.0f);
134
sprite->Play("idle");
136
scale = Vector2(0.25, 0.25);
139
SetCollider(new RectangleCollider(16, 16));
141
alert = Assets::RequestAudio("alert.ogg");
142
freakout = Assets::RequestAudio("freakout.ogg");
143
chomp = Assets::RequestAudio("chomp.ogg");
144
skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
145
skitter1->SetLoops(0); //loop indefinitely
146
sniff = Audio::NewDeck(Assets::RequestAudio("sniff.ogg"));
149
maxspeed = DEFAULT_MAXSPEED;
151
//set aiTime to random so creatures tick at different times
152
aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
157
void Creature::Update()
161
if (state == "idle" || state == "wander")
163
aiTime += Monocle::deltaTime;
170
direction = Vector2::zero;
174
if (state != "wander") {
175
//printf("wander\n");
176
direction = Vector2::Random();
185
Player *player = ((DarkScene *)scene)->player;
186
if ( (player->position - position).GetSquaredMagnitude() <
187
pow(player->noisiness, 2) )
192
direction = Vector2::zero;
193
Ping::NewPing(position, 8.0f, 32.0f, 0.15f); // ping diameter 16-64 over .5s
194
aiTime = 0.0f; // diff variable? stateTime?
196
} // if idle or wander
197
else if (state == "alert")
199
aiTime += Monocle::deltaTime;
201
// if we've been listening past the grace period and hear
202
// something, switch state again
203
// We hear the player at DOUBLE noisiness distance, cuz WE'RE LISNING!
204
Player *player = ((DarkScene *)scene)->player;
205
if ( aiTime > 1.0f &&
206
(player->position - position).GetSquaredMagnitude() <
207
pow(player->noisiness * 2, 2) )
210
Ping::NewPing(position, 8.0f, 32.0f, 0.15f);
211
direction = (player->position - position).GetNormalized();
212
maxspeed = STALKSPEED;
216
// if we've been listening a long time, switch back to idle
218
//printf("end alert\n");
223
else if (state == "stalk")
225
aiTime += Monocle::deltaTime;
228
// * it's been >1s & we hear the player
229
// * we collide with the player
230
Player *player = ((DarkScene *)scene)->player;
231
if ((aiTime > 1.0f &&
232
(player->position - position).GetSquaredMagnitude() <
233
pow(player->noisiness, 2) )
238
direction = (player->position - position).GetNormalized();
239
maxspeed = 0; // give them a running start
242
Ping::NewPing(position, 8.0f, 64.0f, 0.15f);
247
// Go back to IDLE if:
249
// * we stalk for more than 10-15s
250
if ((aiTime > 15.0f) || Collide("wall") || Collide("creaturewall"))
252
// play frustrated noise?
256
maxspeed = DEFAULT_MAXSPEED;
257
direction = Vector2::zero;
260
else if (state == "hunt")
262
aiTime += Monocle::deltaTime;
263
noiseTime += Monocle::deltaTime;
264
graceTime += Monocle::deltaTime;
266
if ((graceTime > 1.0f) && (maxspeed != DEFAULT_MAXSPEED))
267
maxspeed = DEFAULT_MAXSPEED;
269
// if we hear the player (
270
Player *player = ((DarkScene *)scene)->player;
271
if ( (player->position - position).GetSquaredMagnitude() <
272
pow(player->noisiness, 2) )
274
direction = (player->position - position).GetNormalized();
276
if (noiseTime > 2.0f) {
278
Ping::NewPing(position, 8.0f, 64.0f, 0.15f);
283
// I guess chill out if it's been a while
284
if (aiTime > 15.0f) {
287
direction = Vector2::zero;
293
// if we collide with you, move you back to the starting point
294
if (Collide("player")) {
295
// TODO: first wait a second or 3?
297
DarkScene *scene = (DarkScene *) GetScene();
298
Entity* playersp = scene->GetFirstEntityWithTag("playerspawner");
299
scene->player->position = playersp->position;
303
velocity += direction * ACCELERATION * Monocle::deltaTime;
305
velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
306
velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
308
if (velocity.GetSquaredMagnitude() > pow(maxspeed, 2))
309
velocity = velocity.GetNormalized() * maxspeed;
314
position.x += velocity.x * Monocle::deltaTime;
315
while (Collide("Solid") || Collide("creaturewall"))
318
if (velocity.x == 0) { break; }
319
//printf("collision1\n");
320
position.x -= SIGN(velocity.x, 0.1);
322
if (xcol) {velocity.x = 0;}
324
position.y += velocity.y * Monocle::deltaTime;
325
while (Collide("Solid") || Collide("creaturewall"))
328
if (velocity.y == 0) { break; }
329
//printf("collision2\n");
330
position.y -= SIGN(velocity.y, 0.1);
332
if (ycol) {velocity.y = 0;}
334
Vector2 distance = ((DarkScene *)scene)->player->position - position;
335
if (state == "stalk")
337
float vol = (distance.GetMagnitude() / -250.0f) + 1.0f;
338
sniff->SetVolume(vol);
340
sprite->Play("slowmove");
342
else if (velocity.GetSquaredMagnitude() > 20)
344
// at distance 0, vol should be 1.0. at 800, it should be 0.
345
float vol = (distance.GetMagnitude() / -600.0f) + 1.0f;
346
skitter1->SetVolume(vol);
348
sprite->Play("move");
352
skitter1->Pause(); //Stop()?
353
sprite->Play("idle");
357
Ping::Ping() : Entity(),
358
d1(0.0f), d2(0.0f), t(0.0f), t2(0.0f)
360
sprite = new Sprite("yellow.png", FILTER_LINEAR, 64, 64);
362
scale = Vector2::zero;
365
Ping *Ping::NewPing(Vector2 pos, float d1, float d2, float t2)
367
Ping *ping = new Ping();
371
ping->position = pos;
373
Game::GetScene()->Add(ping);
381
// once we're done LERPing, get gone
385
t += Monocle::deltaTime;
386
float scaleFactor = LERP(d1/64, d2/64, t/t2);
387
scale = Vector2(scaleFactor, scaleFactor);