/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 03:16:50 UTC
  • Revision ID: josh@9ix.org-20110917031650-z4zm6jm68luxs400
bare necessary assets

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
namespace Dark
4
4
{
5
5
  Player::Player() : Entity(),
6
 
                     FRICTION(400),
7
 
                     MAXSPEED(20.0f),
8
 
                     ACCELERATION(800)
 
6
                     VELOCITY(150.0f)
9
7
  {
10
8
    sprite = new SpriteAnimation("player.png", FILTER_NONE, 64, 64);
11
9
    sprite->Add("idle", 0, 0, 0.35f);
12
10
    sprite->Play("idle");
13
11
    SetGraphic(sprite);
14
 
    scale = Vector2(0.25, 0.25);
15
12
 
16
13
    AddTag("player");
17
 
 
18
 
    footsteps = Audio::NewDeck(Assets::RequestAudio("footsteps.ogg"));
19
 
    footsteps->SetLoops(0); //loop indefinitely
20
14
    
21
 
    SetCollider(new RectangleCollider(16, 16));
22
 
 
23
 
    dark = new Entity();
24
 
    dark->SetGraphic(new Sprite("dark.png", FILTER_NONE, 2048, 1536)); //alpha?
 
15
    SetCollider(new RectangleCollider(32, 32));
25
16
  }
26
17
 
27
18
  void Player::Update()
28
19
  {
29
20
    Entity::Update();
30
21
 
31
 
    if (Input::IsKeyMaskHeld("left"))
32
 
      {
33
 
        velocity.x -= ACCELERATION * Monocle::deltaTime;
34
 
      }
35
 
    if (Input::IsKeyMaskHeld("right"))
36
 
      {
37
 
        velocity.x += ACCELERATION * Monocle::deltaTime;
38
 
      }
39
 
    if (Input::IsKeyMaskHeld("up"))
40
 
      {
41
 
        velocity.y -= ACCELERATION * Monocle::deltaTime;
42
 
      }
43
 
    if (Input::IsKeyMaskHeld("down"))
44
 
      {
45
 
        velocity.y += ACCELERATION * Monocle::deltaTime;
46
 
      }
47
 
    
48
 
    // velocity will approach 0 in steps of FRICTION * dt
49
 
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
50
 
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
51
 
 
52
 
    bool xcol = false;
53
 
    bool ycol = false;
54
 
 
55
 
    position.x += velocity.x * Monocle::deltaTime;
56
 
    while (Collide("Solid"))
57
 
      {
58
 
        xcol = true;
59
 
        if (velocity.x == 0) { break; }
60
 
        //printf("collision1\n");
61
 
        position.x -= SIGN(velocity.x, 0.1);
62
 
      }
63
 
    if (xcol) {velocity.x = 0;}
64
 
 
65
 
    position.y += velocity.y * Monocle::deltaTime;
66
 
    while (Collide("Solid"))
67
 
      {
68
 
        ycol = true;
69
 
        if (velocity.y == 0) { break; }
70
 
        //printf("collision2\n");
71
 
        position.y -= SIGN(velocity.y, 0.1);
72
 
      }
73
 
    if (ycol) {velocity.y = 0;}
74
 
 
75
 
    if (velocity.GetSquaredMagnitude() > 20)
76
 
      footsteps->Play();
77
 
    else
78
 
      footsteps->Pause(); //Stop()?
79
 
 
80
 
    dark->position = position;
81
 
 
82
22
    //Scene::GetCamera()->position = position;
83
 
  }
84
 
 
85
 
  Creature::Creature() : Entity(),
86
 
                         FRICTION(800),
87
 
                         MAXSPEED(15.0f),
88
 
                         ACCELERATION(1200)                      
89
 
  {
90
 
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
91
 
    sprite->Add("idle", 0, 0, 0.35f);
92
 
    sprite->Add("move", 1, 2, 4.0f);
93
 
    sprite->Play("idle");
94
 
    SetGraphic(sprite);
95
 
    scale = Vector2(0.25, 0.25);
96
 
 
97
 
    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;
104
 
 
105
 
    //set aiTime to random so creatures tick at different times
106
 
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
107
 
  }
108
 
 
109
 
  void Creature::Update()
110
 
  {
111
 
    Entity::Update();
112
 
 
113
 
    aiTime += Monocle::deltaTime;
114
 
    if (aiTime > 1.0f)
115
 
      {
116
 
        switch (rand() % 3)
117
 
          {
118
 
          case 0: // idle
119
 
            //printf("idle\n");
120
 
            direction = Vector2::zero;
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
 
            }
130
 
          }
131
 
        
132
 
        aiTime = 0.0f;
133
 
      }
134
 
    
135
 
    velocity += direction * ACCELERATION * Monocle::deltaTime;
136
 
    
137
 
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
138
 
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
139
 
 
140
 
    bool xcol = false;
141
 
    bool ycol = false;
142
 
 
143
 
    position.x += velocity.x * Monocle::deltaTime;
144
 
    while (Collide("Solid"))
145
 
      {
146
 
        xcol = true;
147
 
        if (velocity.x == 0) { break; }
148
 
        //printf("collision1\n");
149
 
        position.x -= SIGN(velocity.x, 0.1);
150
 
      }
151
 
    if (xcol) {velocity.x = 0;}
152
 
 
153
 
    position.y += velocity.y * Monocle::deltaTime;
154
 
    while (Collide("Solid"))
155
 
      {
156
 
        ycol = true;
157
 
        if (velocity.y == 0) { break; }
158
 
        //printf("collision2\n");
159
 
        position.y -= SIGN(velocity.y, 0.1);
160
 
      }
161
 
    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
 
      }
 
23
 
 
24
    //...
173
25
  }
174
26
 
175
27
  // Scene
205
57
    // load level from files
206
58
    Level::LoadProject("project.xml");
207
59
    Level::Load("level.xml", this);
208
 
 
209
 
    std::list<Entity*> *walls = GetAllTag("wall");
210
 
    for (std::list<Entity*>::iterator i = walls->begin(); i != walls->end(); ++i)
211
 
      {
212
 
        Entity *e = (*i);
213
 
        Vector2 s = e->scale;
214
 
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
215
 
      }
216
60
   
217
 
    player = new Player();
 
61
    Player *player = new Player;
218
62
    player->position = Graphics::GetScreenCenter();
219
63
    Add(player);
220
 
    Add(player->dark);
221
 
 
222
 
    Creature *creature = new Creature;
223
 
    creature->position = Graphics::GetScreenCenter();
224
 
    Add(creature);
225
 
 
226
 
    Creature *creature2 = new Creature;
227
 
    creature2->position = Graphics::GetScreenCenter();
228
 
    Add(creature2);
229
64
 
230
65
    Graphics::SetBackgroundColor(Color::green * 0.2f);
231
66
 
257
92
          {
258
93
            isPaused = !isPaused;
259
94
            
260
 
            if (isPaused) {
261
 
              player->dark->isVisible = false;
 
95
            if (isPaused)
262
96
              levelEditor->Enable();
263
 
            } else {
 
97
            else
264
98
              levelEditor->Disable();
265
 
              player->dark->isVisible = true;
266
 
            }
267
99
          }
268
100
      }
269
101
 
276
108
      this->font = Assets::RequestFont("LiberationSans-Regular.ttf", 18.0f);
277
109
    else
278
110
      this->font = font;
279
 
 
280
 
    SetLayer(-2);
281
111
  }
282
112
 
283
113
  void Text::Render()