/minild29

To get this branch, use:
bzr branch http://9ix.org/bzr/minild29

« back to all changes in this revision

Viewing changes to Dark.cpp

  • Committer: Josh C
  • Date: 2011-09-19 18:02:20 UTC
  • Revision ID: josh@9ix.org-20110919180220-flnvccj5mdk3441x
chompy noise, make collisions with hunters work a little better

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
    bool xcol = false;
59
59
    bool ycol = false;
60
60
 
 
61
    Collider *collider = NULL;
 
62
 
61
63
    position.x += velocity.x * Monocle::deltaTime;
62
 
    while (Collide("Solid"))
 
64
    while (Collide("Solid") || (collider = Collide("creature")))
63
65
      {
 
66
        // Don't colide with hunters.  We should just die.
 
67
        if (collider) {
 
68
          Creature *c = (Creature *) collider->GetEntity();
 
69
          if (c->state == "hunt") { break; }
 
70
        }
 
71
 
64
72
        xcol = true;
65
73
        if (velocity.x == 0) { break; }
66
74
        //printf("collision1\n");
68
76
      }
69
77
    if (xcol) {velocity.x = 0;}
70
78
 
 
79
    collider = NULL;
71
80
    position.y += velocity.y * Monocle::deltaTime;
72
 
    while (Collide("Solid"))
 
81
    while (Collide("Solid") || (collider = Collide("creature")))
73
82
      {
 
83
        // Don't colide with hunters.  We should just die.
 
84
        if (collider) {
 
85
          Creature *c = (Creature *) collider->GetEntity();
 
86
          if (c->state == "hunt") { break; }
 
87
        }
 
88
 
74
89
        ycol = true;
75
90
        if (velocity.y == 0) { break; }
76
91
        //printf("collision2\n");
84
99
 
85
100
    // How far away can they hear your footsteps?
86
101
    // The circle is diameter 300 at MAXSPEED, 32 (2x your size) at 0 speed
87
 
    noisiness = ((magnitude / MAXSPEED *(300-32)) + 32) /2;
 
102
    noisiness = ((magnitude / MAXSPEED *(400-64)) + 64) /2;
88
103
 
89
104
    dark->position = position;
90
105
 
93
108
 
94
109
  Creature::Creature() : Entity(),
95
110
                         FRICTION(800),
96
 
                         MAXSPEED(80.0f),
97
111
                         ACCELERATION(1200),
98
 
                         STALKSPEED(5.0f),
99
 
                         HUNTSPEED(80.0f)
 
112
                         DEFAULT_MAXSPEED(80.0f),
 
113
                         STALKSPEED(20.0f)
100
114
  {
101
115
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
102
116
    sprite->Add("idle", 0, 0, 0.35f);
103
117
    sprite->Add("move", 1, 2, 4.0f);
 
118
    sprite->Add("slowmove", 1, 2, 2.0f);
104
119
    sprite->Play("idle");
105
120
    SetGraphic(sprite);
106
121
    scale = Vector2(0.25, 0.25);
108
123
    AddTag("creature");
109
124
    SetCollider(new RectangleCollider(16, 16));
110
125
 
 
126
    alert = Assets::RequestAudio("alert.ogg");
 
127
    freakout = Assets::RequestAudio("freakout.ogg");
 
128
    chomp = Assets::RequestAudio("chomp.ogg");
111
129
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
112
130
    skitter1->SetLoops(0); //loop indefinitely
113
 
    alert = Assets::RequestAudio("alert.ogg");
114
 
    stalk = Assets::RequestAudio("stalk.ogg");
 
131
    sniff = Audio::NewDeck(Assets::RequestAudio("sniff.ogg"));
 
132
    sniff->SetLoops(0);
115
133
 
116
 
    velocity = Vector2::Random() * MAXSPEED;
 
134
    maxspeed = DEFAULT_MAXSPEED;
117
135
 
118
136
    //set aiTime to random so creatures tick at different times
119
137
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
174
192
             pow(player->noisiness * 2, 2) )
175
193
          {
176
194
            state = "stalk";
177
 
            stalk->Play();
178
195
            Ping::NewPing(position, 8.0f, 32.0f, 0.15f);
179
 
            direction = (player->position - position).GetNormalized() * STALKSPEED;
 
196
            direction = (player->position - position).GetNormalized();
 
197
            maxspeed = STALKSPEED;
180
198
            aiTime = 0.0f;
181
 
            noiseTime = 0.0f;
182
199
          }
183
200
 
184
201
        // if we've been listening a long time, switch back to idle
191
208
    else if (state == "stalk")
192
209
      {
193
210
        aiTime += Monocle::deltaTime;
194
 
        noiseTime += Monocle::deltaTime;
195
211
 
196
212
        // Go to HUNT if:
197
213
        // * it's been >1s & we hear the player
203
219
            || Collide("player")
204
220
            )
205
221
          {
206
 
            state == "hunt";
207
 
            direction = (player->position - position).GetNormalized() * HUNTSPEED;
208
 
            // Play hunt noise (REEET!)
209
 
 
210
 
          }
211
 
          
212
 
          
213
 
        //after 6 or 7 seconds on the noiseTime clock, play sound and ping again
214
 
        // if we hit a wall, call off the search?
215
 
        //we'll want to play that sound again
216
 
        // do others go ALERT if we start stalking?
217
 
        //probably a timeout here too.
 
222
            state = "hunt";
 
223
            direction = (player->position - position).GetNormalized();
 
224
            maxspeed = 0; // give them a running start
 
225
            sniff->Stop();
 
226
            freakout->Play();
 
227
            Ping::NewPing(position, 8.0f, 64.0f, 0.15f); 
 
228
            aiTime = 0.0f;
 
229
            noiseTime = 0.0f;
 
230
          }
 
231
 
 
232
        // Go back to IDLE if:
 
233
        // * we hit a wall
 
234
        // * we stalk for more than 10-15s
 
235
        if ((aiTime > 15.0f) || Collide("wall") || Collide("creaturewall"))
 
236
          {
 
237
            // play frustrated noise?
 
238
            state = "idle";
 
239
            aiTime = 0.0f;
 
240
            sniff->Stop();
 
241
            maxspeed = DEFAULT_MAXSPEED;
 
242
            direction = Vector2::zero;
 
243
          }
 
244
      }
 
245
    else if (state == "hunt")
 
246
      {
 
247
        aiTime += Monocle::deltaTime;
 
248
        noiseTime += Monocle::deltaTime;
 
249
        graceTime += Monocle::deltaTime;
 
250
 
 
251
        if ((graceTime > 1.0f) && (maxspeed != DEFAULT_MAXSPEED))
 
252
          maxspeed = DEFAULT_MAXSPEED;
 
253
 
 
254
        // if we hear the player (
 
255
        Player *player = ((DarkScene *)scene)->player;
 
256
        if ( (player->position - position).GetSquaredMagnitude() < 
 
257
             pow(player->noisiness, 2) )
 
258
          {
 
259
            direction = (player->position - position).GetNormalized();
 
260
            aiTime = 0.0f;
 
261
            if (noiseTime > 2.0f) {
 
262
              freakout->Play();
 
263
              Ping::NewPing(position, 8.0f, 64.0f, 0.15f);
 
264
              noiseTime = 0.0f;
 
265
            }
 
266
          }
 
267
 
 
268
        // I guess chill out if it's been a while
 
269
        if (aiTime > 15.0f) {
 
270
            state = "alert";
 
271
            //alert->Play();
 
272
            direction = Vector2::zero;
 
273
            aiTime = 0.0f;
 
274
            noiseTime = 0.0f;
 
275
            graceTime = 0.0f;
 
276
        }
 
277
 
 
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?
 
281
          chomp->Play();
 
282
          DarkScene *scene = (DarkScene *) GetScene();
 
283
          Entity* playersp = scene->GetFirstEntityWithTag("playerspawner");
 
284
          scene->player->position = playersp->position;
 
285
        }
218
286
      }
219
287
 
220
288
    velocity += direction * ACCELERATION * Monocle::deltaTime;
222
290
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
223
291
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
224
292
 
225
 
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
226
 
      velocity = velocity.GetNormalized() * MAXSPEED;
 
293
    if (velocity.GetSquaredMagnitude() > pow(maxspeed, 2))
 
294
      velocity = velocity.GetNormalized() * maxspeed;
227
295
 
228
296
    bool xcol = false;
229
297
    bool ycol = false;
230
298
 
231
299
    position.x += velocity.x * Monocle::deltaTime;
232
 
    while (Collide("Solid"))
 
300
    while (Collide("Solid") || Collide("creaturewall"))
233
301
      {
234
302
        xcol = true;
235
303
        if (velocity.x == 0) { break; }
239
307
    if (xcol) {velocity.x = 0;}
240
308
 
241
309
    position.y += velocity.y * Monocle::deltaTime;
242
 
    while (Collide("Solid"))
 
310
    while (Collide("Solid") || Collide("creaturewall"))
243
311
      {
244
312
        ycol = true;
245
313
        if (velocity.y == 0) { break; }
248
316
      }
249
317
    if (ycol) {velocity.y = 0;}
250
318
 
251
 
    if (velocity.GetSquaredMagnitude() > 20)
252
 
      {
253
 
        Vector2 distance = ((DarkScene *)scene)->player->position - position;
 
319
    Vector2 distance = ((DarkScene *)scene)->player->position - position;
 
320
    if (state == "stalk")
 
321
      {
 
322
        float vol = (distance.GetMagnitude() / -250.0f) + 1.0f;
 
323
        sniff->SetVolume(vol);
 
324
        sniff->Play();
 
325
        sprite->Play("slowmove");
 
326
      }
 
327
    else if (velocity.GetSquaredMagnitude() > 20)
 
328
      {
254
329
        // at distance 0, vol should be 1.0.  at 800, it should be 0.
255
 
        float vol = (distance.GetMagnitude() / -800.0f) + 1.0f;
 
330
        float vol = (distance.GetMagnitude() / -600.0f) + 1.0f;
256
331
        skitter1->SetVolume(vol);
257
332
        skitter1->Play();
258
333
        sprite->Play("move");
331
406
    Level::LoadProject("project.xml");
332
407
    Level::Load("level.xml", this);
333
408
 
 
409
    HideSpawners();
 
410
    Spawn();
 
411
 
334
412
    std::list<Entity*> *walls = GetAllTag("wall");
335
413
    for (std::list<Entity*>::iterator i = walls->begin(); i != walls->end(); ++i)
336
414
      {
338
416
        Vector2 s = e->scale;
339
417
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
340
418
      }
 
419
 
 
420
    std::list<Entity*> *creaturewalls = GetAllTag("creaturewall");
 
421
    for (std::list<Entity*>::iterator i = creaturewalls->begin(); i != creaturewalls->end(); ++i)
 
422
      {
 
423
        Entity *e = (*i);
 
424
        Vector2 s = e->scale;
 
425
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
 
426
      }
341
427
   
 
428
    Graphics::SetBackgroundColor(Color::green * 0.2f);
 
429
 
 
430
  }
 
431
 
 
432
  void DarkScene::HideSpawners()
 
433
  {
 
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;
 
438
  }
 
439
 
 
440
  void DarkScene::ShowSpawners()
 
441
  {
 
442
    std::list<Entity*> *spawners = GetAllTag("spawner");
 
443
    for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
 
444
      (*i)->isVisible = true;
 
445
  }
 
446
  
 
447
  void DarkScene::Spawn()
 
448
  {
 
449
    Entity* playersp = GetFirstEntityWithTag("playerspawner");
342
450
    player = new Player();
343
 
    player->position = Graphics::GetScreenCenter();
 
451
    player->position = playersp->position;;
344
452
    Add(player);
345
453
    Add(player->dark);
346
454
 
347
 
    Creature *creature = new Creature;
348
 
    creature->position = Graphics::GetScreenCenter();
349
 
    Add(creature);
350
 
 
351
 
    Creature *creature2 = new Creature;
352
 
    creature2->position = Graphics::GetScreenCenter();
353
 
    Add(creature2);
354
 
 
355
 
    Graphics::SetBackgroundColor(Color::green * 0.2f);
356
 
 
 
455
    std::list<Entity*> *spawners = GetAllTag("creaturespawner");
 
456
    // Ugh, monocle is double-entering all the tags...
 
457
    spawners->sort();
 
458
    spawners->unique();
 
459
    for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
 
460
      {
 
461
        Creature *creature = new Creature();
 
462
        creature->position = (*i)->position;
 
463
        Add(creature);
 
464
      }
357
465
  }
358
466
 
359
467
  void DarkScene::Update()
384
492
            
385
493
            if (isPaused) {
386
494
              player->dark->isVisible = false;
 
495
              ShowSpawners();
387
496
              levelEditor->Enable();
388
497
            } else {
 
498
              HideSpawners();
389
499
              levelEditor->Disable();
390
500
              player->dark->isVisible = true;
391
501
            }