/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(60.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);
13
11
    SetGraphic(sprite);
14
12
 
15
13
    AddTag("player");
16
 
 
17
 
    footsteps = Audio::NewDeck(Assets::RequestAudio("footsteps.ogg"));
18
 
    footsteps->SetLoops(0); //loop indefinitely
19
14
    
20
15
    SetCollider(new RectangleCollider(32, 32));
21
16
  }
24
19
  {
25
20
    Entity::Update();
26
21
 
27
 
    if (Input::IsKeyMaskHeld("left"))
28
 
      {
29
 
        velocity.x -= ACCELERATION * Monocle::deltaTime;
30
 
      }
31
 
    if (Input::IsKeyMaskHeld("right"))
32
 
      {
33
 
        velocity.x += ACCELERATION * Monocle::deltaTime;
34
 
      }
35
 
    if (Input::IsKeyMaskHeld("up"))
36
 
      {
37
 
        velocity.y -= ACCELERATION * Monocle::deltaTime;
38
 
      }
39
 
    if (Input::IsKeyMaskHeld("down"))
40
 
      {
41
 
        velocity.y += ACCELERATION * Monocle::deltaTime;
42
 
      }
43
 
    
44
 
    // velocity will approach 0 in steps of FRICTION * dt
45
 
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
46
 
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
47
 
 
48
 
    bool xcol = false;
49
 
    bool ycol = false;
50
 
 
51
 
    position.x += velocity.x * Monocle::deltaTime;
52
 
    while (Collide("Solid"))
53
 
      {
54
 
        xcol = true;
55
 
        if (velocity.x == 0) { break; }
56
 
        //printf("collision1\n");
57
 
        position.x -= SIGN(velocity.x, 0.1);
58
 
      }
59
 
    if (xcol) {velocity.x = 0;}
60
 
 
61
 
    position.y += velocity.y * Monocle::deltaTime;
62
 
    while (Collide("Solid"))
63
 
      {
64
 
        ycol = true;
65
 
        if (velocity.y == 0) { break; }
66
 
        //printf("collision2\n");
67
 
        position.y -= SIGN(velocity.y, 0.1);
68
 
      }
69
 
    if (ycol) {velocity.y = 0;}
70
 
 
71
 
    if (velocity.GetSquaredMagnitude() > 20)
72
 
      footsteps->Play();
73
 
    else
74
 
      footsteps->Pause(); //Stop()?
75
 
 
76
22
    //Scene::GetCamera()->position = position;
77
 
  }
78
 
 
79
 
  Creature::Creature() : Entity(),
80
 
                         FRICTION(800),
81
 
                         MAXSPEED(30.0f),
82
 
                         ACCELERATION(1200)                      
83
 
  {
84
 
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
85
 
    sprite->Add("idle", 0, 0, 0.35f);
86
 
    sprite->Play("idle");
87
 
    SetGraphic(sprite);
88
 
 
89
 
    AddTag("creature");
90
 
    SetCollider(new RectangleCollider(32, 32));
91
 
 
92
 
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
93
 
    skitter1->SetLoops(0); //loop indefinitely
94
 
 
95
 
    velocity = Vector2::Random() * ACCELERATION;
96
 
 
97
 
    //set aiTime to random so creatures tick at different times
98
 
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
99
 
  }
100
 
 
101
 
  void Creature::Update()
102
 
  {
103
 
    Entity::Update();
104
 
 
105
 
    aiTime += Monocle::deltaTime;
106
 
    if (aiTime > 1.0f)
107
 
      {
108
 
        switch (rand() % 2)
109
 
          {
110
 
          case 0: // idle
111
 
            //printf("idle\n");
112
 
            direction = Vector2::zero;
113
 
            state = "idle";
114
 
            break;
115
 
          case 1: // move
116
 
            if (state != "wander") {
117
 
              //printf("wander\n");
118
 
              direction = Vector2::Random();
119
 
              state = "wander";
120
 
              break;
121
 
            }
122
 
          }
123
 
        
124
 
        aiTime = 0.0f;
125
 
      }
126
 
    
127
 
    velocity += direction * ACCELERATION * Monocle::deltaTime;
128
 
    
129
 
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
130
 
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
131
 
 
132
 
    bool xcol = false;
133
 
    bool ycol = false;
134
 
 
135
 
    position.x += velocity.x * Monocle::deltaTime;
136
 
    while (Collide("Solid"))
137
 
      {
138
 
        xcol = true;
139
 
        if (velocity.x == 0) { break; }
140
 
        //printf("collision1\n");
141
 
        position.x -= SIGN(velocity.x, 0.1);
142
 
      }
143
 
    if (xcol) {velocity.x = 0;}
144
 
 
145
 
    position.y += velocity.y * Monocle::deltaTime;
146
 
    while (Collide("Solid"))
147
 
      {
148
 
        ycol = true;
149
 
        if (velocity.y == 0) { break; }
150
 
        //printf("collision2\n");
151
 
        position.y -= SIGN(velocity.y, 0.1);
152
 
      }
153
 
    if (ycol) {velocity.y = 0;}
154
 
 
155
 
    if (velocity.GetSquaredMagnitude() > 20)
156
 
      skitter1->Play();
157
 
    else
158
 
      skitter1->Pause(); //Stop()?
 
23
 
 
24
    //...
159
25
  }
160
26
 
161
27
  // Scene
191
57
    // load level from files
192
58
    Level::LoadProject("project.xml");
193
59
    Level::Load("level.xml", this);
194
 
 
195
 
    std::list<Entity*> *inv = GetAllTag("invisible");
196
 
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
197
 
      {
198
 
        Entity *e = (*i);
199
 
        Vector2 s = e->scale;
200
 
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
201
 
      }
202
60
   
203
61
    Player *player = new Player;
204
62
    player->position = Graphics::GetScreenCenter();
205
63
    Add(player);
206
64
 
207
 
    Creature *creature = new Creature;
208
 
    creature->position = Graphics::GetScreenCenter();
209
 
    Add(creature);
210
 
 
211
 
    Creature *creature2 = new Creature;
212
 
    creature2->position = Graphics::GetScreenCenter();
213
 
    Add(creature2);
214
 
 
215
65
    Graphics::SetBackgroundColor(Color::green * 0.2f);
216
66
 
217
67
  }