/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:59:41 UTC
  • Revision ID: josh@9ix.org-20110918165941-waswg2xidvf0v04x
player and creature spawners in level editor

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
 
 
63
61
    position.x += velocity.x * Monocle::deltaTime;
64
 
    while (Collide("Solid") || (collider = Collide("creature")))
 
62
    while (Collide("Solid"))
65
63
      {
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
 
 
72
64
        xcol = true;
73
65
        if (velocity.x == 0) { break; }
74
66
        //printf("collision1\n");
76
68
      }
77
69
    if (xcol) {velocity.x = 0;}
78
70
 
79
 
    collider = NULL;
80
71
    position.y += velocity.y * Monocle::deltaTime;
81
 
    while (Collide("Solid") || (collider = Collide("creature")))
 
72
    while (Collide("Solid"))
82
73
      {
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
 
 
89
74
        ycol = true;
90
75
        if (velocity.y == 0) { break; }
91
76
        //printf("collision2\n");
99
84
 
100
85
    // How far away can they hear your footsteps?
101
86
    // The circle is diameter 300 at MAXSPEED, 32 (2x your size) at 0 speed
102
 
    noisiness = ((magnitude / MAXSPEED *(400-64)) + 64) /2;
 
87
    noisiness = ((magnitude / MAXSPEED *(300-32)) + 32) /2;
103
88
 
104
89
    dark->position = position;
105
90
 
108
93
 
109
94
  Creature::Creature() : Entity(),
110
95
                         FRICTION(800),
 
96
                         MAXSPEED(80.0f),
111
97
                         ACCELERATION(1200),
112
 
                         DEFAULT_MAXSPEED(80.0f),
113
 
                         STALKSPEED(20.0f)
 
98
                         STALKSPEED(5.0f),
 
99
                         HUNTSPEED(80.0f)
114
100
  {
115
101
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
116
102
    sprite->Add("idle", 0, 0, 0.35f);
117
103
    sprite->Add("move", 1, 2, 4.0f);
118
 
    sprite->Add("slowmove", 1, 2, 2.0f);
119
104
    sprite->Play("idle");
120
105
    SetGraphic(sprite);
121
106
    scale = Vector2(0.25, 0.25);
123
108
    AddTag("creature");
124
109
    SetCollider(new RectangleCollider(16, 16));
125
110
 
126
 
    alert = Assets::RequestAudio("alert.ogg");
127
 
    freakout = Assets::RequestAudio("freakout.ogg");
128
 
    chomp = Assets::RequestAudio("chomp.ogg");
129
111
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
130
112
    skitter1->SetLoops(0); //loop indefinitely
131
 
    sniff = Audio::NewDeck(Assets::RequestAudio("sniff.ogg"));
132
 
    sniff->SetLoops(0);
 
113
    alert = Assets::RequestAudio("alert.ogg");
 
114
    stalk = Assets::RequestAudio("stalk.ogg");
133
115
 
134
 
    maxspeed = DEFAULT_MAXSPEED;
 
116
    velocity = Vector2::Random() * MAXSPEED;
135
117
 
136
118
    //set aiTime to random so creatures tick at different times
137
119
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
192
174
             pow(player->noisiness * 2, 2) )
193
175
          {
194
176
            state = "stalk";
 
177
            stalk->Play();
195
178
            Ping::NewPing(position, 8.0f, 32.0f, 0.15f);
196
 
            direction = (player->position - position).GetNormalized();
197
 
            maxspeed = STALKSPEED;
 
179
            direction = (player->position - position).GetNormalized() * STALKSPEED;
198
180
            aiTime = 0.0f;
 
181
            noiseTime = 0.0f;
199
182
          }
200
183
 
201
184
        // if we've been listening a long time, switch back to idle
208
191
    else if (state == "stalk")
209
192
      {
210
193
        aiTime += Monocle::deltaTime;
 
194
        noiseTime += Monocle::deltaTime;
211
195
 
212
196
        // Go to HUNT if:
213
197
        // * it's been >1s & we hear the player
219
203
            || Collide("player")
220
204
            )
221
205
          {
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
 
        }
 
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.
286
218
      }
287
219
 
288
220
    velocity += direction * ACCELERATION * Monocle::deltaTime;
290
222
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
291
223
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
292
224
 
293
 
    if (velocity.GetSquaredMagnitude() > pow(maxspeed, 2))
294
 
      velocity = velocity.GetNormalized() * maxspeed;
 
225
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
 
226
      velocity = velocity.GetNormalized() * MAXSPEED;
295
227
 
296
228
    bool xcol = false;
297
229
    bool ycol = false;
298
230
 
299
231
    position.x += velocity.x * Monocle::deltaTime;
300
 
    while (Collide("Solid") || Collide("creaturewall"))
 
232
    while (Collide("Solid"))
301
233
      {
302
234
        xcol = true;
303
235
        if (velocity.x == 0) { break; }
307
239
    if (xcol) {velocity.x = 0;}
308
240
 
309
241
    position.y += velocity.y * Monocle::deltaTime;
310
 
    while (Collide("Solid") || Collide("creaturewall"))
 
242
    while (Collide("Solid"))
311
243
      {
312
244
        ycol = true;
313
245
        if (velocity.y == 0) { break; }
316
248
      }
317
249
    if (ycol) {velocity.y = 0;}
318
250
 
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
 
      {
 
251
    if (velocity.GetSquaredMagnitude() > 20)
 
252
      {
 
253
        Vector2 distance = ((DarkScene *)scene)->player->position - position;
329
254
        // at distance 0, vol should be 1.0.  at 800, it should be 0.
330
 
        float vol = (distance.GetMagnitude() / -600.0f) + 1.0f;
 
255
        float vol = (distance.GetMagnitude() / -800.0f) + 1.0f;
331
256
        skitter1->SetVolume(vol);
332
257
        skitter1->Play();
333
258
        sprite->Play("move");
416
341
        Vector2 s = e->scale;
417
342
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
418
343
      }
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
 
      }
427
344
   
428
345
    Graphics::SetBackgroundColor(Color::green * 0.2f);
429
346