/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 04:24:47 UTC
  • Revision ID: josh@9ix.org-20110917042447-pht93k6g9j22wtar
added a "creature" moving around w/ the same dynamics as you do 
(acceleration, collision)

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
 
                         FRICTION(800),
91
 
                         MAXSPEED(80.0f),
92
 
                         ACCELERATION(1200)                      
 
73
                         FRICTION(400),
 
74
                         MAXSPEED(60.0f),
 
75
                         ACCELERATION(800)                       
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));
103
 
 
104
 
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
105
 
    skitter1->SetLoops(0); //loop indefinitely
106
 
 
107
 
    velocity = Vector2::Random() * MAXSPEED;
 
83
    SetCollider(new RectangleCollider(32, 32));
 
84
 
 
85
    velocity = Vector2::Random() * ACCELERATION;
108
86
 
109
87
    //set aiTime to random so creatures tick at different times
110
 
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
 
88
    aiTime= (float(rand()) / float(RAND_MAX)) * 2.0f;
111
89
  }
112
90
 
113
91
  void Creature::Update()
115
93
    Entity::Update();
116
94
 
117
95
    aiTime += Monocle::deltaTime;
118
 
    if (aiTime > 1.0f)
 
96
    if (aiTime > 2.0f)
119
97
      {
120
98
        switch (rand() % 3)
121
99
          {
122
 
          case 0: // idle
123
 
            //printf("idle\n");
 
100
          case 1: // idle
124
101
            direction = Vector2::zero;
125
102
            state = "idle";
126
103
            break;
127
 
          case 1: // move
128
 
            if (state != "wander") {
129
 
              //printf("wander\n");
130
 
              direction = Vector2::Random();
131
 
              state = "wander";
132
 
              break;
133
 
            }
 
104
          case 2: // move
 
105
            direction = Vector2::Random();
 
106
            state = "wander";
 
107
            break;
134
108
          }
135
109
        
136
110
        aiTime = 0.0f;
141
115
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
142
116
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
143
117
 
144
 
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
145
 
      velocity = velocity.GetNormalized() * MAXSPEED;
146
 
 
147
 
    bool xcol = false;
 
118
        bool xcol = false;
148
119
    bool ycol = false;
149
120
 
150
121
    position.x += velocity.x * Monocle::deltaTime;
166
137
        position.y -= SIGN(velocity.y, 0.1);
167
138
      }
168
139
    if (ycol) {velocity.y = 0;}
169
 
 
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
 
      }
179
 
    else
180
 
      {
181
 
        skitter1->Pause(); //Stop()?
182
 
        sprite->Play("idle");
183
 
      }
184
140
  }
185
141
 
186
142
  // Scene
217
173
    Level::LoadProject("project.xml");
218
174
    Level::Load("level.xml", this);
219
175
 
220
 
    std::list<Entity*> *walls = GetAllTag("wall");
221
 
    for (std::list<Entity*>::iterator i = walls->begin(); i != walls->end(); ++i)
 
176
    std::list<Entity*> *inv = GetAllTag("invisible");
 
177
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
222
178
      {
223
179
        Entity *e = (*i);
224
180
        Vector2 s = e->scale;
225
181
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
226
182
      }
227
183
   
228
 
    player = new Player();
 
184
    Player *player = new Player;
229
185
    player->position = Graphics::GetScreenCenter();
230
186
    Add(player);
231
 
    Add(player->dark);
232
187
 
233
188
    Creature *creature = new Creature;
234
189
    creature->position = Graphics::GetScreenCenter();
235
190
    Add(creature);
236
191
 
237
 
    Creature *creature2 = new Creature;
238
 
    creature2->position = Graphics::GetScreenCenter();
239
 
    Add(creature2);
240
 
 
241
192
    Graphics::SetBackgroundColor(Color::green * 0.2f);
242
193
 
243
194
  }
268
219
          {
269
220
            isPaused = !isPaused;
270
221
            
271
 
            if (isPaused) {
272
 
              player->dark->isVisible = false;
 
222
            if (isPaused)
273
223
              levelEditor->Enable();
274
 
            } else {
 
224
            else
275
225
              levelEditor->Disable();
276
 
              player->dark->isVisible = true;
277
 
            }
278
226
          }
279
227
      }
280
228
 
287
235
      this->font = Assets::RequestFont("LiberationSans-Regular.ttf", 18.0f);
288
236
    else
289
237
      this->font = font;
290
 
 
291
 
    SetLayer(-2);
292
238
  }
293
239
 
294
240
  void Text::Render()