/minild29

To get this branch, use:
bzr branch http://9ix.org/bzr/minild29
1 by Josh C
initial commit
1
#include "Dark.h"
2
3
namespace Dark
4
{
5
  Player::Player() : Entity(),
3 by Josh C
square moving around the screen
6
		     FRICTION(400),
9 by Josh C
creatures move less, make everything smaller
7
		     MAXSPEED(20.0f),
3 by Josh C
square moving around the screen
8
		     ACCELERATION(800)
1 by Josh C
initial commit
9
  {
10
    sprite = new SpriteAnimation("player.png", FILTER_NONE, 64, 64);
11
    sprite->Add("idle", 0, 0, 0.35f);
12
    sprite->Play("idle");
13
    SetGraphic(sprite);
9 by Josh C
creatures move less, make everything smaller
14
    scale = Vector2(0.25, 0.25);
1 by Josh C
initial commit
15
16
    AddTag("player");
7 by Josh C
footsteps
17
18
    footsteps = Audio::NewDeck(Assets::RequestAudio("footsteps.ogg"));
19
    footsteps->SetLoops(0); //loop indefinitely
1 by Josh C
initial commit
20
    
9 by Josh C
creatures move less, make everything smaller
21
    SetCollider(new RectangleCollider(16, 16));
1 by Josh C
initial commit
22
  }
23
24
  void Player::Update()
25
  {
26
    Entity::Update();
27
3 by Josh C
square moving around the screen
28
    if (Input::IsKeyMaskHeld("left"))
29
      {
30
	velocity.x -= ACCELERATION * Monocle::deltaTime;
31
      }
32
    if (Input::IsKeyMaskHeld("right"))
33
      {
34
	velocity.x += ACCELERATION * Monocle::deltaTime;
35
      }
36
    if (Input::IsKeyMaskHeld("up"))
37
      {
38
	velocity.y -= ACCELERATION * Monocle::deltaTime;
39
      }
40
    if (Input::IsKeyMaskHeld("down"))
41
      {
42
	velocity.y += ACCELERATION * Monocle::deltaTime;
43
      }
44
    
45
    // velocity will approach 0 in steps of FRICTION * dt
46
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
47
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
48
4 by Josh C
invisible walls and code to bump into them
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
7 by Josh C
footsteps
72
    if (velocity.GetSquaredMagnitude() > 20)
73
      footsteps->Play();
74
    else
75
      footsteps->Pause(); //Stop()?
3 by Josh C
square moving around the screen
76
1 by Josh C
initial commit
77
    //Scene::GetCamera()->position = position;
78
  }
79
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
80
  Creature::Creature() : Entity(),
6 by Josh C
skittering blue square
81
			 FRICTION(800),
9 by Josh C
creatures move less, make everything smaller
82
			 MAXSPEED(15.0f),
6 by Josh C
skittering blue square
83
			 ACCELERATION(1200)			 
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
84
  {
85
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
86
    sprite->Add("idle", 0, 0, 0.35f);
8 by Josh C
harder to see creatures
87
    sprite->Add("move", 1, 2, 4.0f);
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
88
    sprite->Play("idle");
89
    SetGraphic(sprite);
9 by Josh C
creatures move less, make everything smaller
90
    scale = Vector2(0.25, 0.25);
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
91
92
    AddTag("creature");
9 by Josh C
creatures move less, make everything smaller
93
    SetCollider(new RectangleCollider(16, 16));
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
94
6 by Josh C
skittering blue square
95
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
96
    skitter1->SetLoops(0); //loop indefinitely
97
9 by Josh C
creatures move less, make everything smaller
98
    velocity = Vector2::Random() * MAXSPEED;
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
99
100
    //set aiTime to random so creatures tick at different times
6 by Josh C
skittering blue square
101
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
102
  }
103
104
  void Creature::Update()
105
  {
106
    Entity::Update();
107
108
    aiTime += Monocle::deltaTime;
6 by Josh C
skittering blue square
109
    if (aiTime > 1.0f)
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
110
      {
9 by Josh C
creatures move less, make everything smaller
111
	switch (rand() % 3)
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
112
	  {
6 by Josh C
skittering blue square
113
	  case 0: // idle
114
	    //printf("idle\n");
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
115
	    direction = Vector2::zero;
116
	    state = "idle";
117
	    break;
6 by Josh C
skittering blue square
118
	  case 1: // move
119
	    if (state != "wander") {
120
	      //printf("wander\n");
121
	      direction = Vector2::Random();
122
	      state = "wander";
123
	      break;
124
	    }
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
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
6 by Josh C
skittering blue square
135
    bool xcol = false;
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
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;}
6 by Josh C
skittering blue square
157
7 by Josh C
footsteps
158
    if (velocity.GetSquaredMagnitude() > 20)
8 by Josh C
harder to see creatures
159
      {
160
	skitter1->Play();
161
	sprite->Play("move");
162
      }
6 by Josh C
skittering blue square
163
    else
8 by Josh C
harder to see creatures
164
      {
165
	skitter1->Pause(); //Stop()?
166
	sprite->Play("idle");
167
      }
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
168
  }
169
1 by Josh C
initial commit
170
  // Scene
171
172
  DarkScene::DarkScene() : Scene()
173
  {
174
  }
175
  
176
  void DarkScene::Begin()
177
  {
178
    Scene::Begin();
179
180
    Graphics::Set2D(1024, 768);
181
182
    Text *inst2 = new Text("Press ESC to quit");
183
    inst2->position = Vector2(-492, -354); // 20px h, 30px v from U-L corner
184
    Add(inst2);
185
186
    Input::DefineMaskKey("left", KEY_LEFT);
187
    Input::DefineMaskKey("right", KEY_RIGHT);
188
    Input::DefineMaskKey("up", KEY_UP);
189
    Input::DefineMaskKey("down", KEY_DOWN);
190
191
    Input::DefineMaskKey("up", KEY_W);
192
    Input::DefineMaskKey("left", KEY_A);
193
    Input::DefineMaskKey("down", KEY_S);
194
    Input::DefineMaskKey("right", KEY_D);
195
196
    // level editor
197
    Add( levelEditor = new LevelEditor() );
198
    levelEditor->Disable();
199
200
    // load level from files
201
    Level::LoadProject("project.xml");
202
    Level::Load("level.xml", this);
4 by Josh C
invisible walls and code to bump into them
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
      }
1 by Josh C
initial commit
211
   
212
    Player *player = new Player;
213
    player->position = Graphics::GetScreenCenter();
214
    Add(player);
215
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
216
    Creature *creature = new Creature;
217
    creature->position = Graphics::GetScreenCenter();
218
    Add(creature);
219
7 by Josh C
footsteps
220
    Creature *creature2 = new Creature;
221
    creature2->position = Graphics::GetScreenCenter();
222
    Add(creature2);
223
8 by Josh C
harder to see creatures
224
    Graphics::SetBackgroundColor(Color::black * 0.2f);
1 by Josh C
initial commit
225
226
  }
227
228
  void DarkScene::Update()
229
  {
230
    Scene::Update();
231
232
    if (Input::IsKeyPressed(KEY_ESCAPE))
233
      Game::Quit();
234
235
    if (isPaused)
236
      {
237
	if (Input::IsKeyPressed(KEY_S) && Input::IsKeyHeld(KEY_LCTRL))
238
	  Level::Save();
239
240
	if (Input::IsKeyPressed(KEY_A))
241
	  Scene::GetCamera()->position.x -= 600;
242
243
	if (Input::IsKeyPressed(KEY_D))
244
	  Scene::GetCamera()->position.x += 600;
245
      }
246
247
    if (Input::IsKeyPressed(KEY_TAB))
248
      {
249
        // if we're not doing anything in the levelEditor...
250
        if (levelEditor->GetState() == FTES_NONE)
251
          {
252
            isPaused = !isPaused;
253
            
254
            if (isPaused)
255
	      levelEditor->Enable();
256
            else
257
	      levelEditor->Disable();
258
          }
259
      }
260
261
  }// DarkScene::Update
262
263
  Text::Text(const std::string& text, FontAsset* font)
264
    : Entity(), text(text)
265
  {
266
    if (font == NULL)
267
      this->font = Assets::RequestFont("LiberationSans-Regular.ttf", 18.0f);
268
    else
269
      this->font = font;
270
  }
271
272
  void Text::Render()
273
  {
274
    Graphics::PushMatrix();
275
276
    // place text directly relative to camera
277
    Graphics::Translate(scene->GetCamera()->position + position);
278
279
    Graphics::SetBlend(BLEND_ALPHA);
280
    Graphics::SetColor(Color::white);
281
    Graphics::BindFont(font);
282
    
283
    Graphics::RenderText(*font, text, 0, 0);
284
    Graphics::PopMatrix();
285
  }
286
287
}