/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 19:59:09 UTC
  • Revision ID: josh@9ix.org-20110917195909-v02b0cco0yri61qc
disappear darkness mask in editor mode

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(20.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);
18
17
 
19
18
    footsteps = Audio::NewDeck(Assets::RequestAudio("footsteps.ogg"));
20
19
    footsteps->SetLoops(0); //loop indefinitely
21
 
    footsteps->SetVolume(0);
22
 
    footsteps->Play();
23
20
    
24
21
    SetCollider(new RectangleCollider(16, 16));
25
22
 
52
49
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
53
50
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
54
51
 
55
 
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
56
 
      velocity = velocity.GetNormalized() * MAXSPEED;
57
 
 
58
52
    bool xcol = false;
59
53
    bool ycol = false;
60
54
 
77
71
        position.y -= SIGN(velocity.y, 0.1);
78
72
      }
79
73
    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 *(300-32)) + 32) /2;
 
74
 
 
75
    if (velocity.GetSquaredMagnitude() > 20)
 
76
      footsteps->Play();
 
77
    else
 
78
      footsteps->Pause(); //Stop()?
88
79
 
89
80
    dark->position = position;
90
81
 
93
84
 
94
85
  Creature::Creature() : Entity(),
95
86
                         FRICTION(800),
96
 
                         MAXSPEED(80.0f),
 
87
                         MAXSPEED(15.0f),
97
88
                         ACCELERATION(1200)                      
98
89
  {
99
90
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
119
110
  {
120
111
    Entity::Update();
121
112
 
122
 
    if (state == "idle" || state == "wander") 
 
113
    aiTime += Monocle::deltaTime;
 
114
    if (aiTime > 1.0f)
123
115
      {
124
 
        aiTime += Monocle::deltaTime;
125
 
        if (aiTime > 1.0f)
126
 
          {
127
 
            switch (rand() % 3)
128
 
              {
129
 
              case 0: // idle
130
 
                //printf("idle\n");
131
 
                direction = Vector2::zero;
132
 
                state = "idle";
133
 
                break;
134
 
              case 1: // move
135
 
                if (state != "wander") {
136
 
                  //printf("wander\n");
137
 
                  direction = Vector2::Random();
138
 
                  state = "wander";
139
 
                  break;
140
 
                }
141
 
              }
142
 
            
143
 
            aiTime = 0.0f;
144
 
          }
145
 
        
146
 
        Player *player = ((DarkScene *)scene)->player;
147
 
        if ( (player->position - position).GetSquaredMagnitude() < 
148
 
             pow(player->noisiness, 2) )
149
 
          {
150
 
            state = "alert";
151
 
            // TODO: Play startled noise
 
116
        switch (rand() % 3)
 
117
          {
 
118
          case 0: // idle
 
119
            //printf("idle\n");
152
120
            direction = Vector2::zero;
153
 
            // TODO: Play pulse animation (yellow?)
154
 
            aiTime = 0.0f; // diff variable?  stateTime?
 
121
            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;
 
129
            }
155
130
          }
156
 
      } // if idle or wander
157
 
    else if (state == "alert")
158
 
      {
159
 
        aiTime += Monocle::deltaTime;
160
131
        
161
 
        // if we've been listening past the grace period and hear
162
 
        // something, switch state again
163
 
 
164
 
        // if we've been listening a long time, switch back to idle
 
132
        aiTime = 0.0f;
165
133
      }
166
 
 
 
134
    
167
135
    velocity += direction * ACCELERATION * Monocle::deltaTime;
168
136
    
169
137
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
170
138
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
171
139
 
172
 
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
173
 
      velocity = velocity.GetNormalized() * MAXSPEED;
174
 
 
175
140
    bool xcol = false;
176
141
    bool ycol = false;
177
142
 
197
162
 
198
163
    if (velocity.GetSquaredMagnitude() > 20)
199
164
      {
200
 
        Vector2 distance = ((DarkScene *)scene)->player->position - position;
201
 
        // at distance 0, vol should be 1.0.  at 800, it should be 0.
202
 
        float vol = (distance.GetMagnitude() / -800.0f) + 1.0f;
203
 
        skitter1->SetVolume(vol);
204
165
        skitter1->Play();
205
166
        sprite->Play("move");
206
167
      }
245
206
    Level::LoadProject("project.xml");
246
207
    Level::Load("level.xml", this);
247
208
 
248
 
    std::list<Entity*> *walls = GetAllTag("wall");
249
 
    for (std::list<Entity*>::iterator i = walls->begin(); i != walls->end(); ++i)
 
209
    std::list<Entity*> *inv = GetAllTag("invisible");
 
210
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
250
211
      {
251
212
        Entity *e = (*i);
252
213
        Vector2 s = e->scale;
297
258
            isPaused = !isPaused;
298
259
            
299
260
            if (isPaused) {
300
 
              player->dark->isVisible = false;
 
261
              player->dark->position = Vector2(2048, 1600);
301
262
              levelEditor->Enable();
302
263
            } else {
303
264
              levelEditor->Disable();
304
 
              player->dark->isVisible = true;
305
265
            }
306
266
          }
307
267
      }