/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:13:10 UTC
  • Revision ID: josh@9ix.org-20110917031310-jb649nt8e2xy0i6k
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
namespace Dark
4
4
{
5
5
  Player::Player() : Entity(),
6
 
                     FRICTION(400),
7
 
                     MAXSPEED(60.0f),
8
 
                     ACCELERATION(800)
 
6
                     VELOCITY(150.0f)
9
7
  {
10
8
    sprite = new SpriteAnimation("player.png", FILTER_NONE, 64, 64);
11
9
    sprite->Add("idle", 0, 0, 0.35f);
21
19
  {
22
20
    Entity::Update();
23
21
 
24
 
    if (Input::IsKeyMaskHeld("left"))
25
 
      {
26
 
        velocity.x -= ACCELERATION * Monocle::deltaTime;
27
 
      }
28
 
    if (Input::IsKeyMaskHeld("right"))
29
 
      {
30
 
        velocity.x += ACCELERATION * Monocle::deltaTime;
31
 
      }
32
 
    if (Input::IsKeyMaskHeld("up"))
33
 
      {
34
 
        velocity.y -= ACCELERATION * Monocle::deltaTime;
35
 
      }
36
 
    if (Input::IsKeyMaskHeld("down"))
37
 
      {
38
 
        velocity.y += ACCELERATION * Monocle::deltaTime;
39
 
      }
40
 
    
41
 
    // velocity will approach 0 in steps of FRICTION * dt
42
 
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
43
 
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
44
 
 
45
 
    bool xcol = false;
46
 
    bool ycol = false;
47
 
 
48
 
    position.x += velocity.x * Monocle::deltaTime;
49
 
    while (Collide("Solid"))
50
 
      {
51
 
        xcol = true;
52
 
        if (velocity.x == 0) { break; }
53
 
        //printf("collision1\n");
54
 
        position.x -= SIGN(velocity.x, 0.1);
55
 
      }
56
 
    if (xcol) {velocity.x = 0;}
57
 
 
58
 
    position.y += velocity.y * Monocle::deltaTime;
59
 
    while (Collide("Solid"))
60
 
      {
61
 
        ycol = true;
62
 
        if (velocity.y == 0) { break; }
63
 
        //printf("collision2\n");
64
 
        position.y -= SIGN(velocity.y, 0.1);
65
 
      }
66
 
    if (ycol) {velocity.y = 0;}
67
 
 
68
 
 
69
22
    //Scene::GetCamera()->position = position;
70
 
  }
71
 
 
72
 
  Creature::Creature() : Entity(),
73
 
                         FRICTION(800),
74
 
                         MAXSPEED(30.0f),
75
 
                         ACCELERATION(1200)                      
76
 
  {
77
 
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
78
 
    sprite->Add("idle", 0, 0, 0.35f);
79
 
    sprite->Play("idle");
80
 
    SetGraphic(sprite);
81
 
 
82
 
    AddTag("creature");
83
 
    SetCollider(new RectangleCollider(32, 32));
84
 
 
85
 
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
86
 
    skitter1->SetLoops(0); //loop indefinitely
87
 
 
88
 
    velocity = Vector2::Random() * ACCELERATION;
89
 
 
90
 
    //set aiTime to random so creatures tick at different times
91
 
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
92
 
  }
93
 
 
94
 
  void Creature::Update()
95
 
  {
96
 
    Entity::Update();
97
 
 
98
 
    aiTime += Monocle::deltaTime;
99
 
    if (aiTime > 1.0f)
100
 
      {
101
 
        switch (rand() % 2)
102
 
          {
103
 
          case 0: // idle
104
 
            //printf("idle\n");
105
 
            direction = Vector2::zero;
106
 
            state = "idle";
107
 
            break;
108
 
          case 1: // move
109
 
            if (state != "wander") {
110
 
              //printf("wander\n");
111
 
              direction = Vector2::Random();
112
 
              state = "wander";
113
 
              break;
114
 
            }
115
 
          }
116
 
        
117
 
        aiTime = 0.0f;
118
 
      }
119
 
    
120
 
    velocity += direction * ACCELERATION * Monocle::deltaTime;
121
 
    
122
 
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
123
 
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
124
 
 
125
 
    bool xcol = false;
126
 
    bool ycol = false;
127
 
 
128
 
    position.x += velocity.x * Monocle::deltaTime;
129
 
    while (Collide("Solid"))
130
 
      {
131
 
        xcol = true;
132
 
        if (velocity.x == 0) { break; }
133
 
        //printf("collision1\n");
134
 
        position.x -= SIGN(velocity.x, 0.1);
135
 
      }
136
 
    if (xcol) {velocity.x = 0;}
137
 
 
138
 
    position.y += velocity.y * Monocle::deltaTime;
139
 
    while (Collide("Solid"))
140
 
      {
141
 
        ycol = true;
142
 
        if (velocity.y == 0) { break; }
143
 
        //printf("collision2\n");
144
 
        position.y -= SIGN(velocity.y, 0.1);
145
 
      }
146
 
    if (ycol) {velocity.y = 0;}
147
 
 
148
 
    if (velocity.GetSquaredMagnitude() > 0)
149
 
      skitter1->Play();
150
 
    else
151
 
      skitter1->Pause(); //Stop()?
 
23
 
 
24
    //...
152
25
  }
153
26
 
154
27
  // Scene
184
57
    // load level from files
185
58
    Level::LoadProject("project.xml");
186
59
    Level::Load("level.xml", this);
187
 
 
188
 
    std::list<Entity*> *inv = GetAllTag("invisible");
189
 
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
190
 
      {
191
 
        Entity *e = (*i);
192
 
        Vector2 s = e->scale;
193
 
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
194
 
      }
195
60
   
196
61
    Player *player = new Player;
197
62
    player->position = Graphics::GetScreenCenter();
198
63
    Add(player);
199
64
 
200
 
    Creature *creature = new Creature;
201
 
    creature->position = Graphics::GetScreenCenter();
202
 
    Add(creature);
203
 
 
204
65
    Graphics::SetBackgroundColor(Color::green * 0.2f);
205
66
 
206
67
  }