/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 17:12:23 UTC
  • Revision ID: josh@9ix.org-20110917171223-hsvrl6q7mxxbc1sv
creatures move less, make everything smaller

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(60.0f),
 
7
                     MAXSPEED(20.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);
14
15
 
15
16
    AddTag("player");
 
17
 
 
18
    footsteps = Audio::NewDeck(Assets::RequestAudio("footsteps.ogg"));
 
19
    footsteps->SetLoops(0); //loop indefinitely
16
20
    
17
 
    SetCollider(new RectangleCollider(32, 32));
 
21
    SetCollider(new RectangleCollider(16, 16));
18
22
  }
19
23
 
20
24
  void Player::Update()
42
46
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
43
47
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
44
48
 
45
 
    position += velocity * Monocle::deltaTime;
 
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()?
46
76
 
47
77
    //Scene::GetCamera()->position = position;
48
 
 
49
 
    //...
 
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
      }
50
168
  }
51
169
 
52
170
  // Scene
82
200
    // load level from files
83
201
    Level::LoadProject("project.xml");
84
202
    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
      }
85
211
   
86
212
    Player *player = new Player;
87
213
    player->position = Graphics::GetScreenCenter();
88
214
    Add(player);
89
215
 
90
 
    Graphics::SetBackgroundColor(Color::green * 0.2f);
 
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);
91
225
 
92
226
  }
93
227