/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));
 
17
    SetCollider(new RectangleCollider(32, 32));
22
18
  }
23
19
 
24
20
  void Player::Update()
69
65
      }
70
66
    if (ycol) {velocity.y = 0;}
71
67
 
72
 
    if (velocity.GetSquaredMagnitude() > 20)
73
 
      footsteps->Play();
74
 
    else
75
 
      footsteps->Pause(); //Stop()?
76
68
 
77
69
    //Scene::GetCamera()->position = position;
78
70
  }
79
71
 
80
72
  Creature::Creature() : Entity(),
81
 
                         FRICTION(800),
82
 
                         MAXSPEED(15.0f),
83
 
                         ACCELERATION(1200)                      
 
73
                         FRICTION(400),
 
74
                         MAXSPEED(60.0f),
 
75
                         ACCELERATION(800)                       
84
76
  {
85
77
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
86
78
    sprite->Add("idle", 0, 0, 0.35f);
87
 
    sprite->Add("move", 1, 2, 4.0f);
88
79
    sprite->Play("idle");
89
80
    SetGraphic(sprite);
90
 
    scale = Vector2(0.25, 0.25);
91
81
 
92
82
    AddTag("creature");
93
 
    SetCollider(new RectangleCollider(16, 16));
94
 
 
95
 
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
96
 
    skitter1->SetLoops(0); //loop indefinitely
97
 
 
98
 
    velocity = Vector2::Random() * MAXSPEED;
 
83
    SetCollider(new RectangleCollider(32, 32));
 
84
 
 
85
    velocity = Vector2::Random() * ACCELERATION;
99
86
 
100
87
    //set aiTime to random so creatures tick at different times
101
 
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
 
88
    aiTime= (float(rand()) / float(RAND_MAX)) * 2.0f;
102
89
  }
103
90
 
104
91
  void Creature::Update()
106
93
    Entity::Update();
107
94
 
108
95
    aiTime += Monocle::deltaTime;
109
 
    if (aiTime > 1.0f)
 
96
    if (aiTime > 2.0f)
110
97
      {
111
98
        switch (rand() % 3)
112
99
          {
113
 
          case 0: // idle
114
 
            //printf("idle\n");
 
100
          case 1: // idle
115
101
            direction = Vector2::zero;
116
102
            state = "idle";
117
103
            break;
118
 
          case 1: // move
119
 
            if (state != "wander") {
120
 
              //printf("wander\n");
121
 
              direction = Vector2::Random();
122
 
              state = "wander";
123
 
              break;
124
 
            }
 
104
          case 2: // move
 
105
            direction = Vector2::Random();
 
106
            state = "wander";
 
107
            break;
125
108
          }
126
109
        
127
110
        aiTime = 0.0f;
132
115
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
133
116
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
134
117
 
135
 
    bool xcol = false;
 
118
        bool xcol = false;
136
119
    bool ycol = false;
137
120
 
138
121
    position.x += velocity.x * Monocle::deltaTime;
154
137
        position.y -= SIGN(velocity.y, 0.1);
155
138
      }
156
139
    if (ycol) {velocity.y = 0;}
157
 
 
158
 
    if (velocity.GetSquaredMagnitude() > 20)
159
 
      {
160
 
        skitter1->Play();
161
 
        sprite->Play("move");
162
 
      }
163
 
    else
164
 
      {
165
 
        skitter1->Pause(); //Stop()?
166
 
        sprite->Play("idle");
167
 
      }
168
140
  }
169
141
 
170
142
  // Scene
217
189
    creature->position = Graphics::GetScreenCenter();
218
190
    Add(creature);
219
191
 
220
 
    Creature *creature2 = new Creature;
221
 
    creature2->position = Graphics::GetScreenCenter();
222
 
    Add(creature2);
223
 
 
224
 
    Graphics::SetBackgroundColor(Color::black * 0.2f);
 
192
    Graphics::SetBackgroundColor(Color::green * 0.2f);
225
193
 
226
194
  }
227
195