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