/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:45:22 UTC
  • Revision ID: josh@9ix.org-20110919164522-dtd7of8u0ylmvs0s
harder maze, safe zone, they kill you

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(60.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);
12
13
    sprite->Play("idle");
13
14
    SetGraphic(sprite);
 
15
    scale = Vector2(0.25, 0.25);
14
16
 
15
17
    AddTag("player");
16
18
 
17
19
    footsteps = Audio::NewDeck(Assets::RequestAudio("footsteps.ogg"));
18
20
    footsteps->SetLoops(0); //loop indefinitely
 
21
    footsteps->SetVolume(0);
 
22
    footsteps->Play();
19
23
    
20
 
    SetCollider(new RectangleCollider(32, 32));
 
24
    SetCollider(new RectangleCollider(16, 16));
 
25
 
 
26
    dark = new Entity();
 
27
    dark->SetGraphic(new Sprite("dark.png", FILTER_NONE, 2048, 1536)); //alpha?
21
28
  }
22
29
 
23
30
  void Player::Update()
45
52
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
46
53
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
47
54
 
 
55
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
 
56
      velocity = velocity.GetNormalized() * MAXSPEED;
 
57
 
48
58
    bool xcol = false;
49
59
    bool ycol = false;
50
60
 
67
77
        position.y -= SIGN(velocity.y, 0.1);
68
78
      }
69
79
    if (ycol) {velocity.y = 0;}
70
 
 
71
 
    if (velocity.GetSquaredMagnitude() > 20)
72
 
      footsteps->Play();
73
 
    else
74
 
      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;
 
88
 
 
89
    dark->position = position;
75
90
 
76
91
    //Scene::GetCamera()->position = position;
77
92
  }
78
93
 
79
94
  Creature::Creature() : Entity(),
80
95
                         FRICTION(800),
81
 
                         MAXSPEED(30.0f),
82
 
                         ACCELERATION(1200)                      
 
96
                         ACCELERATION(1200),
 
97
                         DEFAULT_MAXSPEED(80.0f),
 
98
                         STALKSPEED(20.0f)
83
99
  {
84
100
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
85
101
    sprite->Add("idle", 0, 0, 0.35f);
86
102
    sprite->Add("move", 1, 2, 4.0f);
 
103
    sprite->Add("slowmove", 1, 2, 2.0f);
87
104
    sprite->Play("idle");
88
105
    SetGraphic(sprite);
 
106
    scale = Vector2(0.25, 0.25);
89
107
 
90
108
    AddTag("creature");
91
 
    SetCollider(new RectangleCollider(32, 32));
 
109
    AddTag("Solid");
 
110
    SetCollider(new RectangleCollider(16, 16));
92
111
 
 
112
    alert = Assets::RequestAudio("alert.ogg");
 
113
    freakout = Assets::RequestAudio("freakout.ogg");
93
114
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
94
115
    skitter1->SetLoops(0); //loop indefinitely
 
116
    sniff = Audio::NewDeck(Assets::RequestAudio("sniff.ogg"));
 
117
    sniff->SetLoops(0);
95
118
 
96
 
    velocity = Vector2::Random() * ACCELERATION;
 
119
    maxspeed = DEFAULT_MAXSPEED;
97
120
 
98
121
    //set aiTime to random so creatures tick at different times
99
122
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
 
123
 
 
124
    state = "idle";
100
125
  }
101
126
 
102
127
  void Creature::Update()
103
128
  {
104
129
    Entity::Update();
105
130
 
106
 
    aiTime += Monocle::deltaTime;
107
 
    if (aiTime > 1.0f)
 
131
    if (state == "idle" || state == "wander") 
108
132
      {
109
 
        switch (rand() % 2)
110
 
          {
111
 
          case 0: // idle
112
 
            //printf("idle\n");
 
133
        aiTime += Monocle::deltaTime;
 
134
        if (aiTime > 1.0f)
 
135
          {
 
136
            switch (rand() % 3)
 
137
              {
 
138
              case 0: // idle
 
139
                //printf("idle\n");
 
140
                direction = Vector2::zero;
 
141
                state = "idle";
 
142
                break;
 
143
              case 1: // move
 
144
                if (state != "wander") {
 
145
                  //printf("wander\n");
 
146
                  direction = Vector2::Random();
 
147
                  state = "wander";
 
148
                  break;
 
149
                }
 
150
              }
 
151
            
 
152
            aiTime = 0.0f;
 
153
          }
 
154
        
 
155
        Player *player = ((DarkScene *)scene)->player;
 
156
        if ( (player->position - position).GetSquaredMagnitude() < 
 
157
             pow(player->noisiness, 2) )
 
158
          {
 
159
            //printf("alert\n");
 
160
            state = "alert";
 
161
            alert->Play();
113
162
            direction = Vector2::zero;
 
163
            Ping::NewPing(position, 8.0f, 32.0f, 0.15f); // ping diameter 16-64 over .5s
 
164
            aiTime = 0.0f; // diff variable?  stateTime?
 
165
          }
 
166
      } // if idle or wander
 
167
    else if (state == "alert")
 
168
      {
 
169
        aiTime += Monocle::deltaTime;
 
170
        
 
171
        // if we've been listening past the grace period and hear
 
172
        // something, switch state again
 
173
        // We hear the player at DOUBLE noisiness distance, cuz WE'RE LISNING!
 
174
        Player *player = ((DarkScene *)scene)->player;
 
175
        if ( aiTime > 1.0f && 
 
176
             (player->position - position).GetSquaredMagnitude() < 
 
177
             pow(player->noisiness * 2, 2) )
 
178
          {
 
179
            state = "stalk";
 
180
            Ping::NewPing(position, 8.0f, 32.0f, 0.15f);
 
181
            direction = (player->position - position).GetNormalized();
 
182
            maxspeed = STALKSPEED;
 
183
            aiTime = 0.0f;
 
184
          }
 
185
 
 
186
        // if we've been listening a long time, switch back to idle
 
187
        if (aiTime > 5.0f) {
 
188
          //printf("end alert\n");
 
189
          state = "idle";
 
190
          aiTime = 0.0f;
 
191
        }
 
192
      }
 
193
    else if (state == "stalk")
 
194
      {
 
195
        aiTime += Monocle::deltaTime;
 
196
 
 
197
        // Go to HUNT if:
 
198
        // * it's been >1s & we hear the player
 
199
        // * we collide with the player
 
200
        Player *player = ((DarkScene *)scene)->player;
 
201
        if ((aiTime > 1.0f &&  
 
202
             (player->position - position).GetSquaredMagnitude() < 
 
203
             pow(player->noisiness, 2) )
 
204
            || Collide("player")
 
205
            )
 
206
          {
 
207
            state = "hunt";
 
208
            direction = (player->position - position).GetNormalized();
 
209
            maxspeed = 0; // give them a running start
 
210
            sniff->Stop();
 
211
            freakout->Play();
 
212
            Ping::NewPing(position, 8.0f, 64.0f, 0.15f); 
 
213
            aiTime = 0.0f;
 
214
            noiseTime = 0.0f;
 
215
          }
 
216
 
 
217
        // Go back to IDLE if:
 
218
        // * we hit a wall
 
219
        // * we stalk for more than 10-15s
 
220
        if ((aiTime > 15.0f) || Collide("wall") || Collide("creaturewall"))
 
221
          {
 
222
            // play frustrated noise?
114
223
            state = "idle";
115
 
            break;
116
 
          case 1: // move
117
 
            if (state != "wander") {
118
 
              //printf("wander\n");
119
 
              direction = Vector2::Random();
120
 
              state = "wander";
121
 
              break;
 
224
            aiTime = 0.0f;
 
225
            sniff->Stop();
 
226
            maxspeed = DEFAULT_MAXSPEED;
 
227
            direction = Vector2::zero;
 
228
          }
 
229
      }
 
230
    else if (state == "hunt")
 
231
      {
 
232
        aiTime += Monocle::deltaTime;
 
233
        noiseTime += Monocle::deltaTime;
 
234
        graceTime += Monocle::deltaTime;
 
235
 
 
236
        if ((graceTime > 1.0f) && (maxspeed != DEFAULT_MAXSPEED))
 
237
          maxspeed = DEFAULT_MAXSPEED;
 
238
 
 
239
        // if we hear the player (
 
240
        Player *player = ((DarkScene *)scene)->player;
 
241
        if ( (player->position - position).GetSquaredMagnitude() < 
 
242
             pow(player->noisiness, 2) )
 
243
          {
 
244
            direction = (player->position - position).GetNormalized();
 
245
            aiTime = 0.0f;
 
246
            if (noiseTime > 2.0f) {
 
247
              freakout->Play();
 
248
              Ping::NewPing(position, 8.0f, 64.0f, 0.15f);
 
249
              noiseTime = 0.0f;
122
250
            }
123
251
          }
124
 
        
125
 
        aiTime = 0.0f;
 
252
 
 
253
        // I guess chill out if it's been a while
 
254
        if (aiTime > 15.0f) {
 
255
            state = "alert";
 
256
            //alert->Play();
 
257
            direction = Vector2::zero;
 
258
            aiTime = 0.0f;
 
259
            noiseTime = 0.0f;
 
260
            graceTime = 0.0f;
 
261
        }
 
262
 
 
263
        // if we collide with you, move you back to the starting point
 
264
        if (Collide("player")) {
 
265
          // TODO: first, make chompy noises and wait a second or 3?
 
266
          DarkScene *scene = (DarkScene *) GetScene();
 
267
          Entity* playersp = scene->GetFirstEntityWithTag("playerspawner");
 
268
          scene->player->position = playersp->position;
 
269
        }
126
270
      }
127
 
    
 
271
 
128
272
    velocity += direction * ACCELERATION * Monocle::deltaTime;
129
273
    
130
274
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
131
275
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
132
276
 
 
277
    if (velocity.GetSquaredMagnitude() > pow(maxspeed, 2))
 
278
      velocity = velocity.GetNormalized() * maxspeed;
 
279
 
133
280
    bool xcol = false;
134
281
    bool ycol = false;
135
282
 
136
283
    position.x += velocity.x * Monocle::deltaTime;
137
 
    while (Collide("Solid"))
 
284
    while (Collide("Solid") || Collide("creaturewall"))
138
285
      {
139
286
        xcol = true;
140
287
        if (velocity.x == 0) { break; }
144
291
    if (xcol) {velocity.x = 0;}
145
292
 
146
293
    position.y += velocity.y * Monocle::deltaTime;
147
 
    while (Collide("Solid"))
 
294
    while (Collide("Solid") || Collide("creaturewall"))
148
295
      {
149
296
        ycol = true;
150
297
        if (velocity.y == 0) { break; }
153
300
      }
154
301
    if (ycol) {velocity.y = 0;}
155
302
 
156
 
    if (velocity.GetSquaredMagnitude() > 20)
157
 
      {
 
303
    Vector2 distance = ((DarkScene *)scene)->player->position - position;
 
304
    if (state == "stalk")
 
305
      {
 
306
        float vol = (distance.GetMagnitude() / -250.0f) + 1.0f;
 
307
        sniff->SetVolume(vol);
 
308
        sniff->Play();
 
309
        sprite->Play("slowmove");
 
310
      }
 
311
    else if (velocity.GetSquaredMagnitude() > 20)
 
312
      {
 
313
        // at distance 0, vol should be 1.0.  at 800, it should be 0.
 
314
        float vol = (distance.GetMagnitude() / -600.0f) + 1.0f;
 
315
        skitter1->SetVolume(vol);
158
316
        skitter1->Play();
159
317
        sprite->Play("move");
160
318
      }
165
323
      }
166
324
  }
167
325
 
 
326
  Ping::Ping() : Entity(),
 
327
                 d1(0.0f), d2(0.0f), t(0.0f), t2(0.0f)
 
328
  {
 
329
    sprite = new Sprite("yellow.png", FILTER_LINEAR, 64, 64);
 
330
    SetGraphic(sprite);
 
331
    scale = Vector2::zero;
 
332
  }
 
333
 
 
334
  Ping *Ping::NewPing(Vector2 pos, float d1, float d2, float t2)
 
335
  {
 
336
    Ping *ping = new Ping();
 
337
    ping->d1 = d1;
 
338
    ping->d2 = d2;
 
339
    ping->t2 = t2;
 
340
    ping->position = pos;
 
341
 
 
342
    Game::GetScene()->Add(ping);
 
343
    return ping;
 
344
  }
 
345
 
 
346
  void Ping::Update()
 
347
  {
 
348
    Entity::Update();
 
349
 
 
350
    // once we're done LERPing, get gone
 
351
    if (t > t2)
 
352
      RemoveSelf();
 
353
 
 
354
    t += Monocle::deltaTime;
 
355
    float scaleFactor = LERP(d1/64, d2/64, t/t2);
 
356
    scale = Vector2(scaleFactor, scaleFactor);
 
357
  }
 
358
 
168
359
  // Scene
169
360
 
170
361
  DarkScene::DarkScene() : Scene()
199
390
    Level::LoadProject("project.xml");
200
391
    Level::Load("level.xml", this);
201
392
 
202
 
    std::list<Entity*> *inv = GetAllTag("invisible");
203
 
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
 
393
    HideSpawners();
 
394
    Spawn();
 
395
 
 
396
    std::list<Entity*> *walls = GetAllTag("wall");
 
397
    for (std::list<Entity*>::iterator i = walls->begin(); i != walls->end(); ++i)
 
398
      {
 
399
        Entity *e = (*i);
 
400
        Vector2 s = e->scale;
 
401
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
 
402
      }
 
403
 
 
404
    std::list<Entity*> *creaturewalls = GetAllTag("creaturewall");
 
405
    for (std::list<Entity*>::iterator i = creaturewalls->begin(); i != creaturewalls->end(); ++i)
204
406
      {
205
407
        Entity *e = (*i);
206
408
        Vector2 s = e->scale;
207
409
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
208
410
      }
209
411
   
210
 
    Player *player = new Player;
211
 
    player->position = Graphics::GetScreenCenter();
 
412
    Graphics::SetBackgroundColor(Color::green * 0.2f);
 
413
 
 
414
  }
 
415
 
 
416
  void DarkScene::HideSpawners()
 
417
  {
 
418
    std::list<Entity*> *spawners = GetAllTag("spawner");
 
419
    if (spawners == NULL) {printf("NO SPAWNERS!!!\n");}
 
420
    for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
 
421
      (*i)->isVisible = false;
 
422
  }
 
423
 
 
424
  void DarkScene::ShowSpawners()
 
425
  {
 
426
    std::list<Entity*> *spawners = GetAllTag("spawner");
 
427
    for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
 
428
      (*i)->isVisible = true;
 
429
  }
 
430
  
 
431
  void DarkScene::Spawn()
 
432
  {
 
433
    Entity* playersp = GetFirstEntityWithTag("playerspawner");
 
434
    player = new Player();
 
435
    player->position = playersp->position;;
212
436
    Add(player);
213
 
 
214
 
    Creature *creature = new Creature;
215
 
    creature->position = Graphics::GetScreenCenter();
216
 
    Add(creature);
217
 
 
218
 
    Creature *creature2 = new Creature;
219
 
    creature2->position = Graphics::GetScreenCenter();
220
 
    Add(creature2);
221
 
 
222
 
    Graphics::SetBackgroundColor(Color::black * 0.2f);
223
 
 
 
437
    Add(player->dark);
 
438
 
 
439
    std::list<Entity*> *spawners = GetAllTag("creaturespawner");
 
440
    // Ugh, monocle is double-entering all the tags...
 
441
    spawners->sort();
 
442
    spawners->unique();
 
443
    for (std::list<Entity*>::iterator i = spawners->begin(); i != spawners->end(); ++i)
 
444
      {
 
445
        Creature *creature = new Creature();
 
446
        creature->position = (*i)->position;
 
447
        Add(creature);
 
448
      }
224
449
  }
225
450
 
226
451
  void DarkScene::Update()
249
474
          {
250
475
            isPaused = !isPaused;
251
476
            
252
 
            if (isPaused)
 
477
            if (isPaused) {
 
478
              player->dark->isVisible = false;
 
479
              ShowSpawners();
253
480
              levelEditor->Enable();
254
 
            else
 
481
            } else {
 
482
              HideSpawners();
255
483
              levelEditor->Disable();
 
484
              player->dark->isVisible = true;
 
485
            }
256
486
          }
257
487
      }
258
488
 
265
495
      this->font = Assets::RequestFont("LiberationSans-Regular.ttf", 18.0f);
266
496
    else
267
497
      this->font = font;
 
498
 
 
499
    SetLayer(-2);
268
500
  }
269
501
 
270
502
  void Text::Render()