/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 15:42:47 UTC
  • Revision ID: josh@9ix.org-20110917154247-vdk48n09rbnrmzgg
footsteps

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
 
78
68
      }
79
69
    if (ycol) {velocity.y = 0;}
80
70
 
81
 
    float vol = velocity.GetMagnitude() / MAXSPEED;
82
 
    footsteps->SetVolume(vol);
83
 
 
84
 
    dark->position = position;
 
71
    if (velocity.GetSquaredMagnitude() > 20)
 
72
      footsteps->Play();
 
73
    else
 
74
      footsteps->Pause(); //Stop()?
85
75
 
86
76
    //Scene::GetCamera()->position = position;
87
77
  }
88
78
 
89
79
  Creature::Creature() : Entity(),
90
80
                         FRICTION(800),
91
 
                         MAXSPEED(80.0f),
 
81
                         MAXSPEED(30.0f),
92
82
                         ACCELERATION(1200)                      
93
83
  {
94
84
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
95
85
    sprite->Add("idle", 0, 0, 0.35f);
96
 
    sprite->Add("move", 1, 2, 4.0f);
97
86
    sprite->Play("idle");
98
87
    SetGraphic(sprite);
99
 
    scale = Vector2(0.25, 0.25);
100
88
 
101
89
    AddTag("creature");
102
 
    SetCollider(new RectangleCollider(16, 16));
 
90
    SetCollider(new RectangleCollider(32, 32));
103
91
 
104
92
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
105
93
    skitter1->SetLoops(0); //loop indefinitely
106
94
 
107
 
    velocity = Vector2::Random() * MAXSPEED;
 
95
    velocity = Vector2::Random() * ACCELERATION;
108
96
 
109
97
    //set aiTime to random so creatures tick at different times
110
98
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
117
105
    aiTime += Monocle::deltaTime;
118
106
    if (aiTime > 1.0f)
119
107
      {
120
 
        switch (rand() % 3)
 
108
        switch (rand() % 2)
121
109
          {
122
110
          case 0: // idle
123
111
            //printf("idle\n");
141
129
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
142
130
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
143
131
 
144
 
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
145
 
      velocity = velocity.GetNormalized() * MAXSPEED;
146
 
 
147
132
    bool xcol = false;
148
133
    bool ycol = false;
149
134
 
168
153
    if (ycol) {velocity.y = 0;}
169
154
 
170
155
    if (velocity.GetSquaredMagnitude() > 20)
171
 
      {
172
 
        Vector2 distance = ((DarkScene *)scene)->player->position - position;
173
 
        // at distance 0, vol should be 1.0.  at 800, it should be 0.
174
 
        float vol = (distance.GetMagnitude() / -800.0f) + 1.0f;
175
 
        skitter1->SetVolume(vol);
176
 
        skitter1->Play();
177
 
        sprite->Play("move");
178
 
      }
 
156
      skitter1->Play();
179
157
    else
180
 
      {
181
 
        skitter1->Pause(); //Stop()?
182
 
        sprite->Play("idle");
183
 
      }
 
158
      skitter1->Pause(); //Stop()?
184
159
  }
185
160
 
186
161
  // Scene
217
192
    Level::LoadProject("project.xml");
218
193
    Level::Load("level.xml", this);
219
194
 
220
 
    std::list<Entity*> *walls = GetAllTag("wall");
221
 
    for (std::list<Entity*>::iterator i = walls->begin(); i != walls->end(); ++i)
 
195
    std::list<Entity*> *inv = GetAllTag("invisible");
 
196
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
222
197
      {
223
198
        Entity *e = (*i);
224
199
        Vector2 s = e->scale;
225
200
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
226
201
      }
227
202
   
228
 
    player = new Player();
 
203
    Player *player = new Player;
229
204
    player->position = Graphics::GetScreenCenter();
230
205
    Add(player);
231
 
    Add(player->dark);
232
206
 
233
207
    Creature *creature = new Creature;
234
208
    creature->position = Graphics::GetScreenCenter();
268
242
          {
269
243
            isPaused = !isPaused;
270
244
            
271
 
            if (isPaused) {
272
 
              player->dark->isVisible = false;
 
245
            if (isPaused)
273
246
              levelEditor->Enable();
274
 
            } else {
 
247
            else
275
248
              levelEditor->Disable();
276
 
              player->dark->isVisible = true;
277
 
            }
278
249
          }
279
250
      }
280
251
 
287
258
      this->font = Assets::RequestFont("LiberationSans-Regular.ttf", 18.0f);
288
259
    else
289
260
      this->font = font;
290
 
 
291
 
    SetLayer(-2);
292
261
  }
293
262
 
294
263
  void Text::Render()