/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-17 16:54:23 UTC
  • Revision ID: josh@9ix.org-20110917165423-wflahgpej1hbjowe
harder to see creatures

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#include "Dark.h"
2
 
#include <cmath>
3
2
 
4
3
namespace Dark
5
4
{
6
5
  Player::Player() : Entity(),
7
 
                     FRICTION(150),
8
 
                     MAXSPEED(150.0f),
9
 
                     ACCELERATION(300)
 
6
                     FRICTION(400),
 
7
                     MAXSPEED(60.0f),
 
8
                     ACCELERATION(800)
10
9
  {
11
10
    sprite = new SpriteAnimation("player.png", FILTER_NONE, 64, 64);
12
11
    sprite->Add("idle", 0, 0, 0.35f);
13
12
    sprite->Play("idle");
14
13
    SetGraphic(sprite);
15
 
    scale = Vector2(0.25, 0.25);
16
14
 
17
15
    AddTag("player");
18
16
 
19
17
    footsteps = Audio::NewDeck(Assets::RequestAudio("footsteps.ogg"));
20
18
    footsteps->SetLoops(0); //loop indefinitely
21
 
    footsteps->SetVolume(0);
22
 
    footsteps->Play();
23
19
    
24
 
    SetCollider(new RectangleCollider(16, 16));
25
 
 
26
 
    dark = new Entity();
27
 
    dark->SetGraphic(new Sprite("dark.png", FILTER_NONE, 2048, 1536)); //alpha?
 
20
    SetCollider(new RectangleCollider(32, 32));
28
21
  }
29
22
 
30
23
  void Player::Update()
52
45
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
53
46
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
54
47
 
55
 
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
56
 
      velocity = velocity.GetNormalized() * MAXSPEED;
57
 
 
58
48
    bool xcol = false;
59
49
    bool ycol = false;
60
50
 
77
67
        position.y -= SIGN(velocity.y, 0.1);
78
68
      }
79
69
    if (ycol) {velocity.y = 0;}
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;
 
70
 
 
71
    if (velocity.GetSquaredMagnitude() > 20)
 
72
      footsteps->Play();
 
73
    else
 
74
      footsteps->Pause(); //Stop()?
90
75
 
91
76
    //Scene::GetCamera()->position = position;
92
77
  }
93
78
 
94
79
  Creature::Creature() : Entity(),
95
80
                         FRICTION(800),
96
 
                         ACCELERATION(1200),
97
 
                         DEFAULT_MAXSPEED(80.0f),
98
 
                         STALKSPEED(20.0f)
 
81
                         MAXSPEED(30.0f),
 
82
                         ACCELERATION(1200)                      
99
83
  {
100
84
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
101
85
    sprite->Add("idle", 0, 0, 0.35f);
102
86
    sprite->Add("move", 1, 2, 4.0f);
103
 
    sprite->Add("slowmove", 1, 2, 2.0f);
104
87
    sprite->Play("idle");
105
88
    SetGraphic(sprite);
106
 
    scale = Vector2(0.25, 0.25);
107
89
 
108
90
    AddTag("creature");
109
 
    AddTag("Solid");
110
 
    SetCollider(new RectangleCollider(16, 16));
 
91
    SetCollider(new RectangleCollider(32, 32));
111
92
 
112
 
    alert = Assets::RequestAudio("alert.ogg");
113
 
    freakout = Assets::RequestAudio("freakout.ogg");
114
93
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
115
94
    skitter1->SetLoops(0); //loop indefinitely
116
 
    sniff = Audio::NewDeck(Assets::RequestAudio("sniff.ogg"));
117
 
    sniff->SetLoops(0);
118
95
 
119
 
    maxspeed = DEFAULT_MAXSPEED;
 
96
    velocity = Vector2::Random() * ACCELERATION;
120
97
 
121
98
    //set aiTime to random so creatures tick at different times
122
99
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
123
 
 
124
 
    state = "idle";
125
100
  }
126
101
 
127
102
  void Creature::Update()
128
103
  {
129
104
    Entity::Update();
130
105
 
131
 
    if (state == "idle" || state == "wander") 
 
106
    aiTime += Monocle::deltaTime;
 
107
    if (aiTime > 1.0f)
132
108
      {
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();
 
109
        switch (rand() % 2)
 
110
          {
 
111
          case 0: // idle
 
112
            //printf("idle\n");
162
113
            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?
223
114
            state = "idle";
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;
 
115
            break;
 
116
          case 1: // move
 
117
            if (state != "wander") {
 
118
              //printf("wander\n");
 
119
              direction = Vector2::Random();
 
120
              state = "wander";
 
121
              break;
250
122
            }
251
123
          }
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
 
        }
 
124
        
 
125
        aiTime = 0.0f;
270
126
      }
271
 
 
 
127
    
272
128
    velocity += direction * ACCELERATION * Monocle::deltaTime;
273
129
    
274
130
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
275
131
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
276
132
 
277
 
    if (velocity.GetSquaredMagnitude() > pow(maxspeed, 2))
278
 
      velocity = velocity.GetNormalized() * maxspeed;
279
 
 
280
133
    bool xcol = false;
281
134
    bool ycol = false;
282
135
 
283
136
    position.x += velocity.x * Monocle::deltaTime;
284
 
    while (Collide("Solid") || Collide("creaturewall"))
 
137
    while (Collide("Solid"))
285
138
      {
286
139
        xcol = true;
287
140
        if (velocity.x == 0) { break; }
291
144
    if (xcol) {velocity.x = 0;}
292
145
 
293
146
    position.y += velocity.y * Monocle::deltaTime;
294
 
    while (Collide("Solid") || Collide("creaturewall"))
 
147
    while (Collide("Solid"))
295
148
      {
296
149
        ycol = true;
297
150
        if (velocity.y == 0) { break; }
300
153
      }
301
154
    if (ycol) {velocity.y = 0;}
302
155
 
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);
 
156
    if (velocity.GetSquaredMagnitude() > 20)
 
157
      {
316
158
        skitter1->Play();
317
159
        sprite->Play("move");
318
160
      }
323
165
      }
324
166
  }
325
167
 
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
 
 
359
168
  // Scene
360
169
 
361
170
  DarkScene::DarkScene() : Scene()
390
199
    Level::LoadProject("project.xml");
391
200
    Level::Load("level.xml", this);
392
201
 
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)
 
202
    std::list<Entity*> *inv = GetAllTag("invisible");
 
203
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
406
204
      {
407
205
        Entity *e = (*i);
408
206
        Vector2 s = e->scale;
409
207
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
410
208
      }
411
209
   
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;;
 
210
    Player *player = new Player;
 
211
    player->position = Graphics::GetScreenCenter();
436
212
    Add(player);
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
 
      }
 
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
 
449
224
  }
450
225
 
451
226
  void DarkScene::Update()
474
249
          {
475
250
            isPaused = !isPaused;
476
251
            
477
 
            if (isPaused) {
478
 
              player->dark->isVisible = false;
479
 
              ShowSpawners();
 
252
            if (isPaused)
480
253
              levelEditor->Enable();
481
 
            } else {
482
 
              HideSpawners();
 
254
            else
483
255
              levelEditor->Disable();
484
 
              player->dark->isVisible = true;
485
 
            }
486
256
          }
487
257
      }
488
258
 
495
265
      this->font = Assets::RequestFont("LiberationSans-Regular.ttf", 18.0f);
496
266
    else
497
267
      this->font = font;
498
 
 
499
 
    SetLayer(-2);
500
268
  }
501
269
 
502
270
  void Text::Render()