/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:03:52 UTC
  • Revision ID: josh@9ix.org-20110917150352-j6nh9t61px2eitdu
skittering blue square

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
 
 
19
 
    footsteps = Audio::NewDeck(Assets::RequestAudio("footsteps.ogg"));
20
 
    footsteps->SetLoops(0); //loop indefinitely
21
 
    footsteps->SetVolume(0);
22
 
    footsteps->Play();
23
16
    
24
 
    SetCollider(new RectangleCollider(16, 16));
25
 
 
26
 
    dark = new Entity();
27
 
    dark->SetGraphic(new Sprite("dark.png", FILTER_NONE, 2048, 1536)); //alpha?
 
17
    SetCollider(new RectangleCollider(32, 32));
28
18
  }
29
19
 
30
20
  void Player::Update()
52
42
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
53
43
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
54
44
 
55
 
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
56
 
      velocity = velocity.GetNormalized() * MAXSPEED;
57
 
 
58
45
    bool xcol = false;
59
46
    bool ycol = false;
60
47
 
78
65
      }
79
66
    if (ycol) {velocity.y = 0;}
80
67
 
81
 
    float vol = velocity.GetMagnitude() / MAXSPEED;
82
 
    footsteps->SetVolume(vol);
83
 
 
84
 
    dark->position = position;
85
68
 
86
69
    //Scene::GetCamera()->position = position;
87
70
  }
88
71
 
89
72
  Creature::Creature() : Entity(),
90
73
                         FRICTION(800),
91
 
                         MAXSPEED(80.0f),
 
74
                         MAXSPEED(30.0f),
92
75
                         ACCELERATION(1200)                      
93
76
  {
94
77
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
95
78
    sprite->Add("idle", 0, 0, 0.35f);
96
 
    sprite->Add("move", 1, 2, 4.0f);
97
79
    sprite->Play("idle");
98
80
    SetGraphic(sprite);
99
 
    scale = Vector2(0.25, 0.25);
100
81
 
101
82
    AddTag("creature");
102
 
    SetCollider(new RectangleCollider(16, 16));
 
83
    SetCollider(new RectangleCollider(32, 32));
103
84
 
104
85
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
105
86
    skitter1->SetLoops(0); //loop indefinitely
106
87
 
107
 
    velocity = Vector2::Random() * MAXSPEED;
 
88
    velocity = Vector2::Random() * ACCELERATION;
108
89
 
109
90
    //set aiTime to random so creatures tick at different times
110
91
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
117
98
    aiTime += Monocle::deltaTime;
118
99
    if (aiTime > 1.0f)
119
100
      {
120
 
        switch (rand() % 3)
 
101
        switch (rand() % 2)
121
102
          {
122
103
          case 0: // idle
123
104
            //printf("idle\n");
141
122
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
142
123
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
143
124
 
144
 
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
145
 
      velocity = velocity.GetNormalized() * MAXSPEED;
146
 
 
147
125
    bool xcol = false;
148
126
    bool ycol = false;
149
127
 
167
145
      }
168
146
    if (ycol) {velocity.y = 0;}
169
147
 
170
 
    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
 
      }
 
148
    if (velocity.GetSquaredMagnitude() > 0)
 
149
      skitter1->Play();
179
150
    else
180
 
      {
181
 
        skitter1->Pause(); //Stop()?
182
 
        sprite->Play("idle");
183
 
      }
 
151
      skitter1->Pause(); //Stop()?
184
152
  }
185
153
 
186
154
  // Scene
217
185
    Level::LoadProject("project.xml");
218
186
    Level::Load("level.xml", this);
219
187
 
220
 
    std::list<Entity*> *walls = GetAllTag("wall");
221
 
    for (std::list<Entity*>::iterator i = walls->begin(); i != walls->end(); ++i)
 
188
    std::list<Entity*> *inv = GetAllTag("invisible");
 
189
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
222
190
      {
223
191
        Entity *e = (*i);
224
192
        Vector2 s = e->scale;
225
193
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
226
194
      }
227
195
   
228
 
    player = new Player();
 
196
    Player *player = new Player;
229
197
    player->position = Graphics::GetScreenCenter();
230
198
    Add(player);
231
 
    Add(player->dark);
232
199
 
233
200
    Creature *creature = new Creature;
234
201
    creature->position = Graphics::GetScreenCenter();
235
202
    Add(creature);
236
203
 
237
 
    Creature *creature2 = new Creature;
238
 
    creature2->position = Graphics::GetScreenCenter();
239
 
    Add(creature2);
240
 
 
241
204
    Graphics::SetBackgroundColor(Color::green * 0.2f);
242
205
 
243
206
  }
268
231
          {
269
232
            isPaused = !isPaused;
270
233
            
271
 
            if (isPaused) {
272
 
              player->dark->isVisible = false;
 
234
            if (isPaused)
273
235
              levelEditor->Enable();
274
 
            } else {
 
236
            else
275
237
              levelEditor->Disable();
276
 
              player->dark->isVisible = true;
277
 
            }
278
238
          }
279
239
      }
280
240
 
287
247
      this->font = Assets::RequestFont("LiberationSans-Regular.ttf", 18.0f);
288
248
    else
289
249
      this->font = font;
290
 
 
291
 
    SetLayer(-2);
292
250
  }
293
251
 
294
252
  void Text::Render()