/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:
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
  }
45
42
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
46
43
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
47
44
 
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()?
 
45
    position += velocity * Monocle::deltaTime;
75
46
 
76
47
    //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->Add("move", 1, 2, 4.0f);
87
 
    sprite->Play("idle");
88
 
    SetGraphic(sprite);
89
 
 
90
 
    AddTag("creature");
91
 
    SetCollider(new RectangleCollider(32, 32));
92
 
 
93
 
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
94
 
    skitter1->SetLoops(0); //loop indefinitely
95
 
 
96
 
    velocity = Vector2::Random() * ACCELERATION;
97
 
 
98
 
    //set aiTime to random so creatures tick at different times
99
 
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
100
 
  }
101
 
 
102
 
  void Creature::Update()
103
 
  {
104
 
    Entity::Update();
105
 
 
106
 
    aiTime += Monocle::deltaTime;
107
 
    if (aiTime > 1.0f)
108
 
      {
109
 
        switch (rand() % 2)
110
 
          {
111
 
          case 0: // idle
112
 
            //printf("idle\n");
113
 
            direction = Vector2::zero;
114
 
            state = "idle";
115
 
            break;
116
 
          case 1: // move
117
 
            if (state != "wander") {
118
 
              //printf("wander\n");
119
 
              direction = Vector2::Random();
120
 
              state = "wander";
121
 
              break;
122
 
            }
123
 
          }
124
 
        
125
 
        aiTime = 0.0f;
126
 
      }
127
 
    
128
 
    velocity += direction * ACCELERATION * Monocle::deltaTime;
129
 
    
130
 
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
131
 
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
132
 
 
133
 
    bool xcol = false;
134
 
    bool ycol = false;
135
 
 
136
 
    position.x += velocity.x * Monocle::deltaTime;
137
 
    while (Collide("Solid"))
138
 
      {
139
 
        xcol = true;
140
 
        if (velocity.x == 0) { break; }
141
 
        //printf("collision1\n");
142
 
        position.x -= SIGN(velocity.x, 0.1);
143
 
      }
144
 
    if (xcol) {velocity.x = 0;}
145
 
 
146
 
    position.y += velocity.y * Monocle::deltaTime;
147
 
    while (Collide("Solid"))
148
 
      {
149
 
        ycol = true;
150
 
        if (velocity.y == 0) { break; }
151
 
        //printf("collision2\n");
152
 
        position.y -= SIGN(velocity.y, 0.1);
153
 
      }
154
 
    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
 
      }
 
48
 
 
49
    //...
166
50
  }
167
51
 
168
52
  // Scene
198
82
    // load level from files
199
83
    Level::LoadProject("project.xml");
200
84
    Level::Load("level.xml", this);
201
 
 
202
 
    std::list<Entity*> *inv = GetAllTag("invisible");
203
 
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
204
 
      {
205
 
        Entity *e = (*i);
206
 
        Vector2 s = e->scale;
207
 
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
208
 
      }
209
85
   
210
86
    Player *player = new Player;
211
87
    player->position = Graphics::GetScreenCenter();
212
88
    Add(player);
213
89
 
214
 
    Creature *creature = new Creature;
215
 
    creature->position = Graphics::GetScreenCenter();
216
 
    Add(creature);
217
 
 
218
 
    Creature *creature2 = new Creature;
219
 
    creature2->position = Graphics::GetScreenCenter();
220
 
    Add(creature2);
221
 
 
222
 
    Graphics::SetBackgroundColor(Color::black * 0.2f);
 
90
    Graphics::SetBackgroundColor(Color::green * 0.2f);
223
91
 
224
92
  }
225
93