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