52
49
velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
53
50
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
55
position.x += velocity.x * Monocle::deltaTime;
64
while (Collide("Solid") || (collider = Collide("creature")))
56
while (Collide("Solid"))
66
// Don't colide with hunters. We should just die.
68
Creature *c = (Creature *) collider->GetEntity();
69
if (c->state == "hunt") { break; }
73
59
if (velocity.x == 0) { break; }
74
60
//printf("collision1\n");
77
63
if (xcol) {velocity.x = 0;}
80
65
position.y += velocity.y * Monocle::deltaTime;
81
while (Collide("Solid") || (collider = Collide("creature")))
66
while (Collide("Solid"))
83
// Don't colide with hunters. We should just die.
85
Creature *c = (Creature *) collider->GetEntity();
86
if (c->state == "hunt") { break; }
90
69
if (velocity.y == 0) { break; }
91
70
//printf("collision2\n");
92
71
position.y -= SIGN(velocity.y, 0.1);
94
73
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;
75
if (velocity.GetSquaredMagnitude() > 20)
78
footsteps->Pause(); //Stop()?
104
80
dark->position = position;
123
97
AddTag("creature");
124
98
SetCollider(new RectangleCollider(16, 16));
126
alert = Assets::RequestAudio("alert.ogg");
127
freakout = Assets::RequestAudio("freakout.ogg");
128
chomp = Assets::RequestAudio("chomp.ogg");
129
100
skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
130
101
skitter1->SetLoops(0); //loop indefinitely
131
sniff = Audio::NewDeck(Assets::RequestAudio("sniff.ogg"));
134
maxspeed = DEFAULT_MAXSPEED;
103
velocity = Vector2::Random() * MAXSPEED;
136
105
//set aiTime to random so creatures tick at different times
137
106
aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
142
109
void Creature::Update()
144
111
Entity::Update();
146
if (state == "idle" || state == "wander")
113
aiTime += Monocle::deltaTime;
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
120
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);
124
if (state != "wander") {
125
//printf("wander\n");
126
direction = Vector2::Random();
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
135
velocity += direction * ACCELERATION * Monocle::deltaTime;
290
137
velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
291
138
velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
293
if (velocity.GetSquaredMagnitude() > pow(maxspeed, 2))
294
velocity = velocity.GetNormalized() * maxspeed;
296
140
bool xcol = false;
297
141
bool ycol = false;
299
143
position.x += velocity.x * Monocle::deltaTime;
300
while (Collide("Solid") || Collide("creaturewall"))
144
while (Collide("Solid"))
303
147
if (velocity.x == 0) { break; }
406
206
Level::LoadProject("project.xml");
407
207
Level::Load("level.xml", this);
412
std::list<Entity*> *walls = GetAllTag("wall");
413
for (std::list<Entity*>::iterator i = walls->begin(); i != walls->end(); ++i)
416
Vector2 s = e->scale;
417
e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
420
std::list<Entity*> *creaturewalls = GetAllTag("creaturewall");
421
for (std::list<Entity*>::iterator i = creaturewalls->begin(); i != creaturewalls->end(); ++i)
209
std::list<Entity*> *inv = GetAllTag("invisible");
210
for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
423
212
Entity *e = (*i);
424
213
Vector2 s = e->scale;
425
214
e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
428
Graphics::SetBackgroundColor(Color::green * 0.2f);
432
void DarkScene::HideSpawners()
434
std::list<Entity*> *spawners = GetAllTag("spawner");
435
if (spawners == NULL) {printf("NO SPAWNERS!!!\n");}
436
for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
437
(*i)->isVisible = false;
440
void DarkScene::ShowSpawners()
442
std::list<Entity*> *spawners = GetAllTag("spawner");
443
for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
444
(*i)->isVisible = true;
447
void DarkScene::Spawn()
449
Entity* playersp = GetFirstEntityWithTag("playerspawner");
450
player = new Player();
451
player->position = playersp->position;;
217
Player *player = new Player;
218
player->position = Graphics::GetScreenCenter();
453
220
Add(player->dark);
455
std::list<Entity*> *spawners = GetAllTag("creaturespawner");
456
// Ugh, monocle is double-entering all the tags...
459
for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
461
Creature *creature = new Creature();
462
creature->position = (*i)->position;
222
Creature *creature = new Creature;
223
creature->position = Graphics::GetScreenCenter();
226
Creature *creature2 = new Creature;
227
creature2->position = Graphics::GetScreenCenter();
230
Graphics::SetBackgroundColor(Color::green * 0.2f);
467
234
void DarkScene::Update()