/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:32:26 UTC
  • Revision ID: josh@9ix.org-20110917033226-h7qh4w1plypy7wk0
square moving around the screen

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()
46
42
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
47
43
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
48
44
 
49
 
    bool xcol = false;
50
 
    bool ycol = false;
51
 
 
52
 
    position.x += velocity.x * Monocle::deltaTime;
53
 
    while (Collide("Solid"))
54
 
      {
55
 
        xcol = true;
56
 
        if (velocity.x == 0) { break; }
57
 
        //printf("collision1\n");
58
 
        position.x -= SIGN(velocity.x, 0.1);
59
 
      }
60
 
    if (xcol) {velocity.x = 0;}
61
 
 
62
 
    position.y += velocity.y * Monocle::deltaTime;
63
 
    while (Collide("Solid"))
64
 
      {
65
 
        ycol = true;
66
 
        if (velocity.y == 0) { break; }
67
 
        //printf("collision2\n");
68
 
        position.y -= SIGN(velocity.y, 0.1);
69
 
      }
70
 
    if (ycol) {velocity.y = 0;}
71
 
 
72
 
    if (velocity.GetSquaredMagnitude() > 20)
73
 
      footsteps->Play();
74
 
    else
75
 
      footsteps->Pause(); //Stop()?
 
45
    position += velocity * Monocle::deltaTime;
76
46
 
77
47
    //Scene::GetCamera()->position = position;
78
 
  }
79
 
 
80
 
  Creature::Creature() : Entity(),
81
 
                         FRICTION(800),
82
 
                         MAXSPEED(15.0f),
83
 
                         ACCELERATION(1200)                      
84
 
  {
85
 
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
86
 
    sprite->Add("idle", 0, 0, 0.35f);
87
 
    sprite->Add("move", 1, 2, 4.0f);
88
 
    sprite->Play("idle");
89
 
    SetGraphic(sprite);
90
 
    scale = Vector2(0.25, 0.25);
91
 
 
92
 
    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;
99
 
 
100
 
    //set aiTime to random so creatures tick at different times
101
 
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
102
 
  }
103
 
 
104
 
  void Creature::Update()
105
 
  {
106
 
    Entity::Update();
107
 
 
108
 
    aiTime += Monocle::deltaTime;
109
 
    if (aiTime > 1.0f)
110
 
      {
111
 
        switch (rand() % 3)
112
 
          {
113
 
          case 0: // idle
114
 
            //printf("idle\n");
115
 
            direction = Vector2::zero;
116
 
            state = "idle";
117
 
            break;
118
 
          case 1: // move
119
 
            if (state != "wander") {
120
 
              //printf("wander\n");
121
 
              direction = Vector2::Random();
122
 
              state = "wander";
123
 
              break;
124
 
            }
125
 
          }
126
 
        
127
 
        aiTime = 0.0f;
128
 
      }
129
 
    
130
 
    velocity += direction * ACCELERATION * Monocle::deltaTime;
131
 
    
132
 
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
133
 
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
134
 
 
135
 
    bool xcol = false;
136
 
    bool ycol = false;
137
 
 
138
 
    position.x += velocity.x * Monocle::deltaTime;
139
 
    while (Collide("Solid"))
140
 
      {
141
 
        xcol = true;
142
 
        if (velocity.x == 0) { break; }
143
 
        //printf("collision1\n");
144
 
        position.x -= SIGN(velocity.x, 0.1);
145
 
      }
146
 
    if (xcol) {velocity.x = 0;}
147
 
 
148
 
    position.y += velocity.y * Monocle::deltaTime;
149
 
    while (Collide("Solid"))
150
 
      {
151
 
        ycol = true;
152
 
        if (velocity.y == 0) { break; }
153
 
        //printf("collision2\n");
154
 
        position.y -= SIGN(velocity.y, 0.1);
155
 
      }
156
 
    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
 
      }
 
48
 
 
49
    //...
168
50
  }
169
51
 
170
52
  // Scene
200
82
    // load level from files
201
83
    Level::LoadProject("project.xml");
202
84
    Level::Load("level.xml", this);
203
 
 
204
 
    std::list<Entity*> *inv = GetAllTag("invisible");
205
 
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
206
 
      {
207
 
        Entity *e = (*i);
208
 
        Vector2 s = e->scale;
209
 
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
210
 
      }
211
85
   
212
86
    Player *player = new Player;
213
87
    player->position = Graphics::GetScreenCenter();
214
88
    Add(player);
215
89
 
216
 
    Creature *creature = new Creature;
217
 
    creature->position = Graphics::GetScreenCenter();
218
 
    Add(creature);
219
 
 
220
 
    Creature *creature2 = new Creature;
221
 
    creature2->position = Graphics::GetScreenCenter();
222
 
    Add(creature2);
223
 
 
224
 
    Graphics::SetBackgroundColor(Color::black * 0.2f);
 
90
    Graphics::SetBackgroundColor(Color::green * 0.2f);
225
91
 
226
92
  }
227
93