/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:
4
4
{
5
5
  Player::Player() : Entity(),
6
6
                     FRICTION(400),
7
 
                     MAXSPEED(20.0f),
 
7
                     MAXSPEED(60.0f),
8
8
                     ACCELERATION(800)
9
9
  {
10
10
    sprite = new SpriteAnimation("player.png", FILTER_NONE, 64, 64);
11
11
    sprite->Add("idle", 0, 0, 0.35f);
12
12
    sprite->Play("idle");
13
13
    SetGraphic(sprite);
14
 
    scale = Vector2(0.25, 0.25);
15
14
 
16
15
    AddTag("player");
17
 
 
18
 
    footsteps = Audio::NewDeck(Assets::RequestAudio("footsteps.ogg"));
19
 
    footsteps->SetLoops(0); //loop indefinitely
20
16
    
21
 
    SetCollider(new RectangleCollider(16, 16));
22
 
 
23
 
    dark = new Entity();
24
 
    dark->SetGraphic(new Sprite("dark.png", FILTER_NONE, 2048, 1536)); //alpha?
 
17
    SetCollider(new RectangleCollider(32, 32));
25
18
  }
26
19
 
27
20
  void Player::Update()
72
65
      }
73
66
    if (ycol) {velocity.y = 0;}
74
67
 
75
 
    if (velocity.GetSquaredMagnitude() > 20)
76
 
      footsteps->Play();
77
 
    else
78
 
      footsteps->Pause(); //Stop()?
79
 
 
80
 
    dark->position = position;
81
68
 
82
69
    //Scene::GetCamera()->position = position;
83
70
  }
84
71
 
85
72
  Creature::Creature() : Entity(),
86
 
                         FRICTION(800),
87
 
                         MAXSPEED(15.0f),
88
 
                         ACCELERATION(1200)                      
 
73
                         FRICTION(400),
 
74
                         MAXSPEED(60.0f),
 
75
                         ACCELERATION(800)                       
89
76
  {
90
77
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
91
78
    sprite->Add("idle", 0, 0, 0.35f);
92
 
    sprite->Add("move", 1, 2, 4.0f);
93
79
    sprite->Play("idle");
94
80
    SetGraphic(sprite);
95
 
    scale = Vector2(0.25, 0.25);
96
81
 
97
82
    AddTag("creature");
98
 
    SetCollider(new RectangleCollider(16, 16));
99
 
 
100
 
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
101
 
    skitter1->SetLoops(0); //loop indefinitely
102
 
 
103
 
    velocity = Vector2::Random() * MAXSPEED;
 
83
    SetCollider(new RectangleCollider(32, 32));
 
84
 
 
85
    velocity = Vector2::Random() * ACCELERATION;
104
86
 
105
87
    //set aiTime to random so creatures tick at different times
106
 
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
 
88
    aiTime= (float(rand()) / float(RAND_MAX)) * 2.0f;
107
89
  }
108
90
 
109
91
  void Creature::Update()
111
93
    Entity::Update();
112
94
 
113
95
    aiTime += Monocle::deltaTime;
114
 
    if (aiTime > 1.0f)
 
96
    if (aiTime > 2.0f)
115
97
      {
116
98
        switch (rand() % 3)
117
99
          {
118
 
          case 0: // idle
119
 
            //printf("idle\n");
 
100
          case 1: // idle
120
101
            direction = Vector2::zero;
121
102
            state = "idle";
122
103
            break;
123
 
          case 1: // move
124
 
            if (state != "wander") {
125
 
              //printf("wander\n");
126
 
              direction = Vector2::Random();
127
 
              state = "wander";
128
 
              break;
129
 
            }
 
104
          case 2: // move
 
105
            direction = Vector2::Random();
 
106
            state = "wander";
 
107
            break;
130
108
          }
131
109
        
132
110
        aiTime = 0.0f;
137
115
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
138
116
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
139
117
 
140
 
    bool xcol = false;
 
118
        bool xcol = false;
141
119
    bool ycol = false;
142
120
 
143
121
    position.x += velocity.x * Monocle::deltaTime;
159
137
        position.y -= SIGN(velocity.y, 0.1);
160
138
      }
161
139
    if (ycol) {velocity.y = 0;}
162
 
 
163
 
    if (velocity.GetSquaredMagnitude() > 20)
164
 
      {
165
 
        skitter1->Play();
166
 
        sprite->Play("move");
167
 
      }
168
 
    else
169
 
      {
170
 
        skitter1->Pause(); //Stop()?
171
 
        sprite->Play("idle");
172
 
      }
173
140
  }
174
141
 
175
142
  // Scene
206
173
    Level::LoadProject("project.xml");
207
174
    Level::Load("level.xml", this);
208
175
 
209
 
    std::list<Entity*> *walls = GetAllTag("wall");
210
 
    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)
211
178
      {
212
179
        Entity *e = (*i);
213
180
        Vector2 s = e->scale;
214
181
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
215
182
      }
216
183
   
217
 
    player = new Player();
 
184
    Player *player = new Player;
218
185
    player->position = Graphics::GetScreenCenter();
219
186
    Add(player);
220
 
    Add(player->dark);
221
187
 
222
188
    Creature *creature = new Creature;
223
189
    creature->position = Graphics::GetScreenCenter();
224
190
    Add(creature);
225
191
 
226
 
    Creature *creature2 = new Creature;
227
 
    creature2->position = Graphics::GetScreenCenter();
228
 
    Add(creature2);
229
 
 
230
192
    Graphics::SetBackgroundColor(Color::green * 0.2f);
231
193
 
232
194
  }
257
219
          {
258
220
            isPaused = !isPaused;
259
221
            
260
 
            if (isPaused) {
261
 
              player->dark->isVisible = false;
 
222
            if (isPaused)
262
223
              levelEditor->Enable();
263
 
            } else {
 
224
            else
264
225
              levelEditor->Disable();
265
 
              player->dark->isVisible = true;
266
 
            }
267
226
          }
268
227
      }
269
228
 
276
235
      this->font = Assets::RequestFont("LiberationSans-Regular.ttf", 18.0f);
277
236
    else
278
237
      this->font = font;
279
 
 
280
 
    SetLayer(-2);
281
238
  }
282
239
 
283
240
  void Text::Render()