/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-18 16:12:57 UTC
  • Revision ID: josh@9ix.org-20110918161257-zshzdmi0e62idum1
some more stalk logic

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
 
    if (Collide("exit")) {
62
 
      Scene *s = GetScene();
63
 
      s->isPaused = true;
64
 
      
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);
68
 
      s->Add(win1);
69
 
 
70
 
      Text *win2 = new Text("Congratulations!", 
71
 
                           Assets::RequestFont("LiberationSans-Regular.ttf", 50.0f));
72
 
      win2->position = Vector2(-150,50);
73
 
      s->Add(win2);
74
 
    }
75
 
 
76
 
    Collider *collider = NULL;
77
 
 
78
61
    position.x += velocity.x * Monocle::deltaTime;
79
 
    while (Collide("Solid") || (collider = Collide("creature")))
 
62
    while (Collide("Solid"))
80
63
      {
81
 
        // Don't colide with hunters.  We should just die.
82
 
        if (collider) {
83
 
          Creature *c = (Creature *) collider->GetEntity();
84
 
          if (c->state == "hunt") { break; }
85
 
        }
86
 
 
87
64
        xcol = true;
88
65
        if (velocity.x == 0) { break; }
89
66
        //printf("collision1\n");
91
68
      }
92
69
    if (xcol) {velocity.x = 0;}
93
70
 
94
 
    collider = NULL;
95
71
    position.y += velocity.y * Monocle::deltaTime;
96
 
    while (Collide("Solid") || (collider = Collide("creature")))
 
72
    while (Collide("Solid"))
97
73
      {
98
 
        // Don't colide with hunters.  We should just die.
99
 
        if (collider) {
100
 
          Creature *c = (Creature *) collider->GetEntity();
101
 
          if (c->state == "hunt") { break; }
102
 
        }
103
 
 
104
74
        ycol = true;
105
75
        if (velocity.y == 0) { break; }
106
76
        //printf("collision2\n");
114
84
 
115
85
    // How far away can they hear your footsteps?
116
86
    // The circle is diameter 300 at MAXSPEED, 32 (2x your size) at 0 speed
117
 
    noisiness = ((magnitude / MAXSPEED *(400-64)) + 64) /2;
 
87
    noisiness = ((magnitude / MAXSPEED *(300-32)) + 32) /2;
118
88
 
119
89
    dark->position = position;
120
90
 
123
93
 
124
94
  Creature::Creature() : Entity(),
125
95
                         FRICTION(800),
 
96
                         MAXSPEED(80.0f),
126
97
                         ACCELERATION(1200),
127
 
                         DEFAULT_MAXSPEED(80.0f),
128
 
                         STALKSPEED(20.0f)
 
98
                         STALKSPEED(5.0f),
 
99
                         HUNTSPEED(80.0f)
129
100
  {
130
101
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
131
102
    sprite->Add("idle", 0, 0, 0.35f);
132
103
    sprite->Add("move", 1, 2, 4.0f);
133
 
    sprite->Add("slowmove", 1, 2, 2.0f);
134
104
    sprite->Play("idle");
135
105
    SetGraphic(sprite);
136
106
    scale = Vector2(0.25, 0.25);
138
108
    AddTag("creature");
139
109
    SetCollider(new RectangleCollider(16, 16));
140
110
 
141
 
    alert = Assets::RequestAudio("alert.ogg");
142
 
    freakout = Assets::RequestAudio("freakout.ogg");
143
 
    chomp = Assets::RequestAudio("chomp.ogg");
144
111
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
145
112
    skitter1->SetLoops(0); //loop indefinitely
146
 
    sniff = Audio::NewDeck(Assets::RequestAudio("sniff.ogg"));
147
 
    sniff->SetLoops(0);
 
113
    alert = Assets::RequestAudio("alert.ogg");
 
114
    stalk = Assets::RequestAudio("stalk.ogg");
148
115
 
149
 
    maxspeed = DEFAULT_MAXSPEED;
 
116
    velocity = Vector2::Random() * MAXSPEED;
150
117
 
151
118
    //set aiTime to random so creatures tick at different times
152
119
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
207
174
             pow(player->noisiness * 2, 2) )
208
175
          {
209
176
            state = "stalk";
 
177
            stalk->Play();
210
178
            Ping::NewPing(position, 8.0f, 32.0f, 0.15f);
211
 
            direction = (player->position - position).GetNormalized();
212
 
            maxspeed = STALKSPEED;
 
179
            direction = (player->position - position).GetNormalized() * STALKSPEED;
213
180
            aiTime = 0.0f;
 
181
            noiseTime = 0.0f;
214
182
          }
215
183
 
216
184
        // if we've been listening a long time, switch back to idle
223
191
    else if (state == "stalk")
224
192
      {
225
193
        aiTime += Monocle::deltaTime;
 
194
        noiseTime += Monocle::deltaTime;
226
195
 
227
196
        // Go to HUNT if:
228
197
        // * it's been >1s & we hear the player
234
203
            || Collide("player")
235
204
            )
236
205
          {
237
 
            state = "hunt";
238
 
            direction = (player->position - position).GetNormalized();
239
 
            maxspeed = 0; // give them a running start
240
 
            sniff->Stop();
241
 
            freakout->Play();
242
 
            Ping::NewPing(position, 8.0f, 64.0f, 0.15f); 
243
 
            aiTime = 0.0f;
244
 
            noiseTime = 0.0f;
245
 
          }
246
 
 
247
 
        // Go back to IDLE if:
248
 
        // * we hit a wall
249
 
        // * we stalk for more than 10-15s
250
 
        if ((aiTime > 15.0f) || Collide("wall") || Collide("creaturewall"))
251
 
          {
252
 
            // play frustrated noise?
253
 
            state = "idle";
254
 
            aiTime = 0.0f;
255
 
            sniff->Stop();
256
 
            maxspeed = DEFAULT_MAXSPEED;
257
 
            direction = Vector2::zero;
258
 
          }
259
 
      }
260
 
    else if (state == "hunt")
261
 
      {
262
 
        aiTime += Monocle::deltaTime;
263
 
        noiseTime += Monocle::deltaTime;
264
 
        graceTime += Monocle::deltaTime;
265
 
 
266
 
        if ((graceTime > 1.0f) && (maxspeed != DEFAULT_MAXSPEED))
267
 
          maxspeed = DEFAULT_MAXSPEED;
268
 
 
269
 
        // if we hear the player (
270
 
        Player *player = ((DarkScene *)scene)->player;
271
 
        if ( (player->position - position).GetSquaredMagnitude() < 
272
 
             pow(player->noisiness, 2) )
273
 
          {
274
 
            direction = (player->position - position).GetNormalized();
275
 
            aiTime = 0.0f;
276
 
            if (noiseTime > 2.0f) {
277
 
              freakout->Play();
278
 
              Ping::NewPing(position, 8.0f, 64.0f, 0.15f);
279
 
              noiseTime = 0.0f;
280
 
            }
281
 
          }
282
 
 
283
 
        // I guess chill out if it's been a while
284
 
        if (aiTime > 15.0f) {
285
 
            state = "alert";
286
 
            //alert->Play();
287
 
            direction = Vector2::zero;
288
 
            aiTime = 0.0f;
289
 
            noiseTime = 0.0f;
290
 
            graceTime = 0.0f;
291
 
        }
292
 
 
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?
296
 
          chomp->Play();
297
 
          DarkScene *scene = (DarkScene *) GetScene();
298
 
          Entity* playersp = scene->GetFirstEntityWithTag("playerspawner");
299
 
          scene->player->position = playersp->position;
300
 
        }
 
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.
301
218
      }
302
219
 
303
220
    velocity += direction * ACCELERATION * Monocle::deltaTime;
305
222
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
306
223
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
307
224
 
308
 
    if (velocity.GetSquaredMagnitude() > pow(maxspeed, 2))
309
 
      velocity = velocity.GetNormalized() * maxspeed;
 
225
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
 
226
      velocity = velocity.GetNormalized() * MAXSPEED;
310
227
 
311
228
    bool xcol = false;
312
229
    bool ycol = false;
313
230
 
314
231
    position.x += velocity.x * Monocle::deltaTime;
315
 
    while (Collide("Solid") || Collide("creaturewall"))
 
232
    while (Collide("Solid"))
316
233
      {
317
234
        xcol = true;
318
235
        if (velocity.x == 0) { break; }
322
239
    if (xcol) {velocity.x = 0;}
323
240
 
324
241
    position.y += velocity.y * Monocle::deltaTime;
325
 
    while (Collide("Solid") || Collide("creaturewall"))
 
242
    while (Collide("Solid"))
326
243
      {
327
244
        ycol = true;
328
245
        if (velocity.y == 0) { break; }
331
248
      }
332
249
    if (ycol) {velocity.y = 0;}
333
250
 
334
 
    Vector2 distance = ((DarkScene *)scene)->player->position - position;
335
 
    if (state == "stalk")
336
 
      {
337
 
        float vol = (distance.GetMagnitude() / -250.0f) + 1.0f;
338
 
        sniff->SetVolume(vol);
339
 
        sniff->Play();
340
 
        sprite->Play("slowmove");
341
 
      }
342
 
    else if (velocity.GetSquaredMagnitude() > 20)
343
 
      {
 
251
    if (velocity.GetSquaredMagnitude() > 20)
 
252
      {
 
253
        Vector2 distance = ((DarkScene *)scene)->player->position - position;
344
254
        // at distance 0, vol should be 1.0.  at 800, it should be 0.
345
 
        float vol = (distance.GetMagnitude() / -600.0f) + 1.0f;
 
255
        float vol = (distance.GetMagnitude() / -800.0f) + 1.0f;
346
256
        skitter1->SetVolume(vol);
347
257
        skitter1->Play();
348
258
        sprite->Play("move");
399
309
 
400
310
    Graphics::Set2D(1024, 768);
401
311
 
402
 
    Text *inst1 = new Text("Use arrow keys to move");
403
 
    inst1->position = Vector2(-492, -354);
404
 
    Add(inst1);
405
 
 
406
312
    Text *inst2 = new Text("Press ESC to quit");
407
 
    inst2->position = Vector2(-492, -334); // 20px h, 30px v from U-L corner
 
313
    inst2->position = Vector2(-492, -354); // 20px h, 30px v from U-L corner
408
314
    Add(inst2);
409
315
 
410
316
    Input::DefineMaskKey("left", KEY_LEFT);
425
331
    Level::LoadProject("project.xml");
426
332
    Level::Load("level.xml", this);
427
333
 
428
 
    HideSpawners();
429
 
    Spawn();
430
 
 
431
334
    std::list<Entity*> *walls = GetAllTag("wall");
432
335
    for (std::list<Entity*>::iterator i = walls->begin(); i != walls->end(); ++i)
433
336
      {
435
338
        Vector2 s = e->scale;
436
339
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
437
340
      }
438
 
 
439
 
    std::list<Entity*> *creaturewalls = GetAllTag("creaturewall");
440
 
    for (std::list<Entity*>::iterator i = creaturewalls->begin(); i != creaturewalls->end(); ++i)
441
 
      {
442
 
        Entity *e = (*i);
443
 
        Vector2 s = e->scale;
444
 
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
445
 
      }
446
 
 
447
 
    Entity* exit = GetFirstEntityWithTag("exit");
448
 
    Vector2 s = exit->scale;
449
 
    exit->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
450
 
 
451
341
   
452
 
    Graphics::SetBackgroundColor(Color::green * 0.2f);
453
 
 
454
 
  }
455
 
 
456
 
  void DarkScene::HideSpawners()
457
 
  {
458
 
    std::list<Entity*> *spawners = GetAllTag("spawner");
459
 
    if (spawners == NULL) {printf("NO SPAWNERS!!!\n");}
460
 
    for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
461
 
      (*i)->isVisible = false;
462
 
  }
463
 
 
464
 
  void DarkScene::ShowSpawners()
465
 
  {
466
 
    std::list<Entity*> *spawners = GetAllTag("spawner");
467
 
    for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
468
 
      (*i)->isVisible = true;
469
 
  }
470
 
  
471
 
  void DarkScene::Spawn()
472
 
  {
473
 
    Entity* playersp = GetFirstEntityWithTag("playerspawner");
474
342
    player = new Player();
475
 
    player->position = playersp->position;;
 
343
    player->position = Graphics::GetScreenCenter();
476
344
    Add(player);
477
345
    Add(player->dark);
478
346
 
479
 
    std::list<Entity*> *spawners = GetAllTag("creaturespawner");
480
 
    // Ugh, monocle is double-entering all the tags...
481
 
    spawners->sort();
482
 
    spawners->unique();
483
 
    for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
484
 
      {
485
 
        Creature *creature = new Creature();
486
 
        creature->position = (*i)->position;
487
 
        Add(creature);
488
 
      }
 
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
 
489
357
  }
490
358
 
491
359
  void DarkScene::Update()
516
384
            
517
385
            if (isPaused) {
518
386
              player->dark->isVisible = false;
519
 
              ShowSpawners();
520
387
              levelEditor->Enable();
521
388
            } else {
522
 
              HideSpawners();
523
389
              levelEditor->Disable();
524
390
              player->dark->isVisible = true;
525
391
            }