/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 16:17:11 UTC
  • Revision ID: josh@9ix.org-20110919161711-uy03f0x3g8l0waxu
much level changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "Dark.h"
 
2
#include <cmath>
2
3
 
3
4
namespace Dark
4
5
{
5
6
  Player::Player() : Entity(),
6
 
                     FRICTION(400),
7
 
                     MAXSPEED(20.0f),
8
 
                     ACCELERATION(800)
 
7
                     FRICTION(150),
 
8
                     MAXSPEED(150.0f),
 
9
                     ACCELERATION(300)
9
10
  {
10
11
    sprite = new SpriteAnimation("player.png", FILTER_NONE, 64, 64);
11
12
    sprite->Add("idle", 0, 0, 0.35f);
17
18
 
18
19
    footsteps = Audio::NewDeck(Assets::RequestAudio("footsteps.ogg"));
19
20
    footsteps->SetLoops(0); //loop indefinitely
 
21
    footsteps->SetVolume(0);
 
22
    footsteps->Play();
20
23
    
21
24
    SetCollider(new RectangleCollider(16, 16));
22
25
 
49
52
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
50
53
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
51
54
 
 
55
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
 
56
      velocity = velocity.GetNormalized() * MAXSPEED;
 
57
 
52
58
    bool xcol = false;
53
59
    bool ycol = false;
54
60
 
71
77
        position.y -= SIGN(velocity.y, 0.1);
72
78
      }
73
79
    if (ycol) {velocity.y = 0;}
74
 
 
75
 
    if (velocity.GetSquaredMagnitude() > 20)
76
 
      footsteps->Play();
77
 
    else
78
 
      footsteps->Pause(); //Stop()?
 
80
    
 
81
    float magnitude = velocity.GetMagnitude();
 
82
 
 
83
    footsteps->SetVolume(magnitude / MAXSPEED);
 
84
 
 
85
    // How far away can they hear your footsteps?
 
86
    // The circle is diameter 300 at MAXSPEED, 32 (2x your size) at 0 speed
 
87
    noisiness = ((magnitude / MAXSPEED *(400-64)) + 64) /2;
79
88
 
80
89
    dark->position = position;
81
90
 
84
93
 
85
94
  Creature::Creature() : Entity(),
86
95
                         FRICTION(800),
87
 
                         MAXSPEED(15.0f),
88
 
                         ACCELERATION(1200)                      
 
96
                         ACCELERATION(1200),
 
97
                         DEFAULT_MAXSPEED(80.0f),
 
98
                         STALKSPEED(20.0f)
89
99
  {
90
100
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
91
101
    sprite->Add("idle", 0, 0, 0.35f);
92
102
    sprite->Add("move", 1, 2, 4.0f);
 
103
    sprite->Add("slowmove", 1, 2, 2.0f);
93
104
    sprite->Play("idle");
94
105
    SetGraphic(sprite);
95
106
    scale = Vector2(0.25, 0.25);
97
108
    AddTag("creature");
98
109
    SetCollider(new RectangleCollider(16, 16));
99
110
 
 
111
    alert = Assets::RequestAudio("alert.ogg");
 
112
    freakout = Assets::RequestAudio("freakout.ogg");
100
113
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
101
114
    skitter1->SetLoops(0); //loop indefinitely
 
115
    sniff = Audio::NewDeck(Assets::RequestAudio("sniff.ogg"));
 
116
    sniff->SetLoops(0);
102
117
 
103
 
    velocity = Vector2::Random() * MAXSPEED;
 
118
    maxspeed = DEFAULT_MAXSPEED;
104
119
 
105
120
    //set aiTime to random so creatures tick at different times
106
121
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
 
122
 
 
123
    state = "idle";
107
124
  }
108
125
 
109
126
  void Creature::Update()
110
127
  {
111
128
    Entity::Update();
112
129
 
113
 
    aiTime += Monocle::deltaTime;
114
 
    if (aiTime > 1.0f)
 
130
    if (state == "idle" || state == "wander") 
115
131
      {
116
 
        switch (rand() % 3)
117
 
          {
118
 
          case 0: // idle
119
 
            //printf("idle\n");
 
132
        aiTime += Monocle::deltaTime;
 
133
        if (aiTime > 1.0f)
 
134
          {
 
135
            switch (rand() % 3)
 
136
              {
 
137
              case 0: // idle
 
138
                //printf("idle\n");
 
139
                direction = Vector2::zero;
 
140
                state = "idle";
 
141
                break;
 
142
              case 1: // move
 
143
                if (state != "wander") {
 
144
                  //printf("wander\n");
 
145
                  direction = Vector2::Random();
 
146
                  state = "wander";
 
147
                  break;
 
148
                }
 
149
              }
 
150
            
 
151
            aiTime = 0.0f;
 
152
          }
 
153
        
 
154
        Player *player = ((DarkScene *)scene)->player;
 
155
        if ( (player->position - position).GetSquaredMagnitude() < 
 
156
             pow(player->noisiness, 2) )
 
157
          {
 
158
            //printf("alert\n");
 
159
            state = "alert";
 
160
            alert->Play();
120
161
            direction = Vector2::zero;
 
162
            Ping::NewPing(position, 8.0f, 32.0f, 0.15f); // ping diameter 16-64 over .5s
 
163
            aiTime = 0.0f; // diff variable?  stateTime?
 
164
          }
 
165
      } // if idle or wander
 
166
    else if (state == "alert")
 
167
      {
 
168
        aiTime += Monocle::deltaTime;
 
169
        
 
170
        // if we've been listening past the grace period and hear
 
171
        // something, switch state again
 
172
        // We hear the player at DOUBLE noisiness distance, cuz WE'RE LISNING!
 
173
        Player *player = ((DarkScene *)scene)->player;
 
174
        if ( aiTime > 1.0f && 
 
175
             (player->position - position).GetSquaredMagnitude() < 
 
176
             pow(player->noisiness * 2, 2) )
 
177
          {
 
178
            state = "stalk";
 
179
            Ping::NewPing(position, 8.0f, 32.0f, 0.15f);
 
180
            direction = (player->position - position).GetNormalized();
 
181
            maxspeed = STALKSPEED;
 
182
            aiTime = 0.0f;
 
183
          }
 
184
 
 
185
        // if we've been listening a long time, switch back to idle
 
186
        if (aiTime > 5.0f) {
 
187
          //printf("end alert\n");
 
188
          state = "idle";
 
189
          aiTime = 0.0f;
 
190
        }
 
191
      }
 
192
    else if (state == "stalk")
 
193
      {
 
194
        aiTime += Monocle::deltaTime;
 
195
 
 
196
        // Go to HUNT if:
 
197
        // * it's been >1s & we hear the player
 
198
        // * we collide with the player
 
199
        Player *player = ((DarkScene *)scene)->player;
 
200
        if ((aiTime > 1.0f &&  
 
201
             (player->position - position).GetSquaredMagnitude() < 
 
202
             pow(player->noisiness, 2) )
 
203
            || Collide("player")
 
204
            )
 
205
          {
 
206
            state = "hunt";
 
207
            direction = (player->position - position).GetNormalized();
 
208
            maxspeed = 0; // give them a running start
 
209
            sniff->Stop();
 
210
            freakout->Play();
 
211
            Ping::NewPing(position, 8.0f, 64.0f, 0.15f); 
 
212
            aiTime = 0.0f;
 
213
            noiseTime = 0.0f;
 
214
          }
 
215
 
 
216
        // Go back to IDLE if:
 
217
        // * we hit a wall
 
218
        // * we stalk for more than 10-15s
 
219
        if ((aiTime > 15.0f) || Collide("wall"))
 
220
          {
 
221
            // play frustrated noise?
121
222
            state = "idle";
122
 
            break;
123
 
          case 1: // move
124
 
            if (state != "wander") {
125
 
              //printf("wander\n");
126
 
              direction = Vector2::Random();
127
 
              state = "wander";
128
 
              break;
 
223
            aiTime = 0.0f;
 
224
            sniff->Stop();
 
225
            maxspeed = DEFAULT_MAXSPEED;
 
226
            direction = Vector2::zero;
 
227
          }
 
228
      }
 
229
    else if (state == "hunt")
 
230
      {
 
231
        aiTime += Monocle::deltaTime;
 
232
        noiseTime += Monocle::deltaTime;
 
233
        graceTime += Monocle::deltaTime;
 
234
 
 
235
        if ((graceTime > 1.0f) && (maxspeed != DEFAULT_MAXSPEED))
 
236
          maxspeed = DEFAULT_MAXSPEED;
 
237
 
 
238
        // if we hear the player (
 
239
        Player *player = ((DarkScene *)scene)->player;
 
240
        if ( (player->position - position).GetSquaredMagnitude() < 
 
241
             pow(player->noisiness, 2) )
 
242
          {
 
243
            direction = (player->position - position).GetNormalized();
 
244
            aiTime = 0.0f;
 
245
            if (noiseTime > 2.0f) {
 
246
              freakout->Play();
 
247
              Ping::NewPing(position, 8.0f, 64.0f, 0.15f);
 
248
              noiseTime = 0.0f;
129
249
            }
130
250
          }
131
 
        
132
 
        aiTime = 0.0f;
 
251
 
 
252
        // I guess chill out if it's been a while
 
253
        if (aiTime > 15.0f) {
 
254
            state = "alert";
 
255
            //alert->Play();
 
256
            direction = Vector2::zero;
 
257
            aiTime = 0.0f;
 
258
            noiseTime = 0.0f;
 
259
            graceTime = 0.0f;
 
260
        }
 
261
 
 
262
        // if we collide with you... game over?
133
263
      }
134
 
    
 
264
 
135
265
    velocity += direction * ACCELERATION * Monocle::deltaTime;
136
266
    
137
267
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
138
268
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
139
269
 
 
270
    if (velocity.GetSquaredMagnitude() > pow(maxspeed, 2))
 
271
      velocity = velocity.GetNormalized() * maxspeed;
 
272
 
140
273
    bool xcol = false;
141
274
    bool ycol = false;
142
275
 
160
293
      }
161
294
    if (ycol) {velocity.y = 0;}
162
295
 
163
 
    if (velocity.GetSquaredMagnitude() > 20)
164
 
      {
 
296
    Vector2 distance = ((DarkScene *)scene)->player->position - position;
 
297
    if (state == "stalk")
 
298
      {
 
299
        float vol = (distance.GetMagnitude() / -250.0f) + 1.0f;
 
300
        sniff->SetVolume(vol);
 
301
        sniff->Play();
 
302
        sprite->Play("slowmove");
 
303
      }
 
304
    else if (velocity.GetSquaredMagnitude() > 20)
 
305
      {
 
306
        // at distance 0, vol should be 1.0.  at 800, it should be 0.
 
307
        float vol = (distance.GetMagnitude() / -600.0f) + 1.0f;
 
308
        skitter1->SetVolume(vol);
165
309
        skitter1->Play();
166
310
        sprite->Play("move");
167
311
      }
172
316
      }
173
317
  }
174
318
 
 
319
  Ping::Ping() : Entity(),
 
320
                 d1(0.0f), d2(0.0f), t(0.0f), t2(0.0f)
 
321
  {
 
322
    sprite = new Sprite("yellow.png", FILTER_LINEAR, 64, 64);
 
323
    SetGraphic(sprite);
 
324
    scale = Vector2::zero;
 
325
  }
 
326
 
 
327
  Ping *Ping::NewPing(Vector2 pos, float d1, float d2, float t2)
 
328
  {
 
329
    Ping *ping = new Ping();
 
330
    ping->d1 = d1;
 
331
    ping->d2 = d2;
 
332
    ping->t2 = t2;
 
333
    ping->position = pos;
 
334
 
 
335
    Game::GetScene()->Add(ping);
 
336
    return ping;
 
337
  }
 
338
 
 
339
  void Ping::Update()
 
340
  {
 
341
    Entity::Update();
 
342
 
 
343
    // once we're done LERPing, get gone
 
344
    if (t > t2)
 
345
      RemoveSelf();
 
346
 
 
347
    t += Monocle::deltaTime;
 
348
    float scaleFactor = LERP(d1/64, d2/64, t/t2);
 
349
    scale = Vector2(scaleFactor, scaleFactor);
 
350
  }
 
351
 
175
352
  // Scene
176
353
 
177
354
  DarkScene::DarkScene() : Scene()
206
383
    Level::LoadProject("project.xml");
207
384
    Level::Load("level.xml", this);
208
385
 
209
 
    std::list<Entity*> *inv = GetAllTag("invisible");
210
 
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
 
386
    HideSpawners();
 
387
    Spawn();
 
388
 
 
389
    std::list<Entity*> *walls = GetAllTag("wall");
 
390
    for (std::list<Entity*>::iterator i = walls->begin(); i != walls->end(); ++i)
211
391
      {
212
392
        Entity *e = (*i);
213
393
        Vector2 s = e->scale;
214
394
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
215
395
      }
216
396
   
 
397
    Graphics::SetBackgroundColor(Color::green * 0.2f);
 
398
 
 
399
  }
 
400
 
 
401
  void DarkScene::HideSpawners()
 
402
  {
 
403
    std::list<Entity*> *spawners = GetAllTag("spawner");
 
404
    if (spawners == NULL) {printf("NO SPAWNERS!!!\n");}
 
405
    for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
 
406
      (*i)->isVisible = false;
 
407
  }
 
408
 
 
409
  void DarkScene::ShowSpawners()
 
410
  {
 
411
    std::list<Entity*> *spawners = GetAllTag("spawner");
 
412
    for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
 
413
      (*i)->isVisible = true;
 
414
  }
 
415
  
 
416
  void DarkScene::Spawn()
 
417
  {
 
418
    Entity* playersp = GetFirstEntityWithTag("playerspawner");
217
419
    player = new Player();
218
 
    player->position = Graphics::GetScreenCenter();
 
420
    player->position = playersp->position;;
219
421
    Add(player);
220
422
    Add(player->dark);
221
423
 
222
 
    Creature *creature = new Creature;
223
 
    creature->position = Graphics::GetScreenCenter();
224
 
    Add(creature);
225
 
 
226
 
    Creature *creature2 = new Creature;
227
 
    creature2->position = Graphics::GetScreenCenter();
228
 
    Add(creature2);
229
 
 
230
 
    Graphics::SetBackgroundColor(Color::green * 0.2f);
231
 
 
 
424
    std::list<Entity*> *spawners = GetAllTag("creaturespawner");
 
425
    // Ugh, monocle is double-entering all the tags...
 
426
    spawners->sort();
 
427
    spawners->unique();
 
428
    for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
 
429
      {
 
430
        Creature *creature = new Creature();
 
431
        creature->position = (*i)->position;
 
432
        Add(creature);
 
433
      }
232
434
  }
233
435
 
234
436
  void DarkScene::Update()
259
461
            
260
462
            if (isPaused) {
261
463
              player->dark->isVisible = false;
 
464
              ShowSpawners();
262
465
              levelEditor->Enable();
263
466
            } else {
 
467
              HideSpawners();
264
468
              levelEditor->Disable();
265
469
              player->dark->isVisible = true;
266
470
            }