/minild29

To get this branch, use:
bzr branch http://9ix.org/bzr/minild29
1 by Josh C
initial commit
1
#include "Dark.h"
15 by Josh C
make maxspeed work. footstep volume has to do with speed. skitter
2
#include <cmath>
1 by Josh C
initial commit
3
4
namespace Dark
5
{
6
  Player::Player() : Entity(),
15 by Josh C
make maxspeed work. footstep volume has to do with speed. skitter
7
		     FRICTION(150),
8
		     MAXSPEED(150.0f),
9
		     ACCELERATION(300)
1 by Josh C
initial commit
10
  {
11
    sprite = new SpriteAnimation("player.png", FILTER_NONE, 64, 64);
12
    sprite->Add("idle", 0, 0, 0.35f);
13
    sprite->Play("idle");
14
    SetGraphic(sprite);
9 by Josh C
creatures move less, make everything smaller
15
    scale = Vector2(0.25, 0.25);
1 by Josh C
initial commit
16
17
    AddTag("player");
7 by Josh C
footsteps
18
19
    footsteps = Audio::NewDeck(Assets::RequestAudio("footsteps.ogg"));
20
    footsteps->SetLoops(0); //loop indefinitely
15 by Josh C
make maxspeed work. footstep volume has to do with speed. skitter
21
    footsteps->SetVolume(0);
22
    footsteps->Play();
1 by Josh C
initial commit
23
    
9 by Josh C
creatures move less, make everything smaller
24
    SetCollider(new RectangleCollider(16, 16));
10 by Josh C
"light" in the dark
25
26
    dark = new Entity();
27
    dark->SetGraphic(new Sprite("dark.png", FILTER_NONE, 2048, 1536)); //alpha?
1 by Josh C
initial commit
28
  }
29
30
  void Player::Update()
31
  {
32
    Entity::Update();
33
3 by Josh C
square moving around the screen
34
    if (Input::IsKeyMaskHeld("left"))
35
      {
36
	velocity.x -= ACCELERATION * Monocle::deltaTime;
37
      }
38
    if (Input::IsKeyMaskHeld("right"))
39
      {
40
	velocity.x += ACCELERATION * Monocle::deltaTime;
41
      }
42
    if (Input::IsKeyMaskHeld("up"))
43
      {
44
	velocity.y -= ACCELERATION * Monocle::deltaTime;
45
      }
46
    if (Input::IsKeyMaskHeld("down"))
47
      {
48
	velocity.y += ACCELERATION * Monocle::deltaTime;
49
      }
50
    
51
    // velocity will approach 0 in steps of FRICTION * dt
52
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
53
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
54
15 by Josh C
make maxspeed work. footstep volume has to do with speed. skitter
55
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
56
      velocity = velocity.GetNormalized() * MAXSPEED;
57
4 by Josh C
invisible walls and code to bump into them
58
    bool xcol = false;
59
    bool ycol = false;
60
61
    position.x += velocity.x * Monocle::deltaTime;
62
    while (Collide("Solid"))
63
      {
64
	xcol = true;
65
	if (velocity.x == 0) { break; }
66
	//printf("collision1\n");
67
	position.x -= SIGN(velocity.x, 0.1);
68
      }
69
    if (xcol) {velocity.x = 0;}
70
71
    position.y += velocity.y * Monocle::deltaTime;
72
    while (Collide("Solid"))
73
      {
74
	ycol = true;
75
	if (velocity.y == 0) { break; }
76
	//printf("collision2\n");
77
	position.y -= SIGN(velocity.y, 0.1);
78
      }
79
    if (ycol) {velocity.y = 0;}
80
15 by Josh C
make maxspeed work. footstep volume has to do with speed. skitter
81
    float vol = velocity.GetMagnitude() / MAXSPEED;
82
    footsteps->SetVolume(vol);
3 by Josh C
square moving around the screen
83
10 by Josh C
"light" in the dark
84
    dark->position = position;
85
1 by Josh C
initial commit
86
    //Scene::GetCamera()->position = position;
87
  }
88
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
89
  Creature::Creature() : Entity(),
6 by Josh C
skittering blue square
90
			 FRICTION(800),
15 by Josh C
make maxspeed work. footstep volume has to do with speed. skitter
91
			 MAXSPEED(80.0f),
6 by Josh C
skittering blue square
92
			 ACCELERATION(1200)			 
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
93
  {
94
    sprite = new SpriteAnimation("creature.png", FILTER_NONE, 64, 64);
95
    sprite->Add("idle", 0, 0, 0.35f);
8 by Josh C
harder to see creatures
96
    sprite->Add("move", 1, 2, 4.0f);
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
97
    sprite->Play("idle");
98
    SetGraphic(sprite);
9 by Josh C
creatures move less, make everything smaller
99
    scale = Vector2(0.25, 0.25);
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
100
101
    AddTag("creature");
9 by Josh C
creatures move less, make everything smaller
102
    SetCollider(new RectangleCollider(16, 16));
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
103
6 by Josh C
skittering blue square
104
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
105
    skitter1->SetLoops(0); //loop indefinitely
106
9 by Josh C
creatures move less, make everything smaller
107
    velocity = Vector2::Random() * MAXSPEED;
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
108
109
    //set aiTime to random so creatures tick at different times
6 by Josh C
skittering blue square
110
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
111
  }
112
113
  void Creature::Update()
114
  {
115
    Entity::Update();
116
117
    aiTime += Monocle::deltaTime;
6 by Josh C
skittering blue square
118
    if (aiTime > 1.0f)
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
119
      {
9 by Josh C
creatures move less, make everything smaller
120
	switch (rand() % 3)
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
121
	  {
6 by Josh C
skittering blue square
122
	  case 0: // idle
123
	    //printf("idle\n");
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
124
	    direction = Vector2::zero;
125
	    state = "idle";
126
	    break;
6 by Josh C
skittering blue square
127
	  case 1: // move
128
	    if (state != "wander") {
129
	      //printf("wander\n");
130
	      direction = Vector2::Random();
131
	      state = "wander";
132
	      break;
133
	    }
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
134
	  }
135
	
136
	aiTime = 0.0f;
137
      }
138
    
139
    velocity += direction * ACCELERATION * Monocle::deltaTime;
140
    
141
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
142
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
143
15 by Josh C
make maxspeed work. footstep volume has to do with speed. skitter
144
    if (velocity.GetSquaredMagnitude() > pow(MAXSPEED, 2))
145
      velocity = velocity.GetNormalized() * MAXSPEED;
146
6 by Josh C
skittering blue square
147
    bool xcol = false;
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
148
    bool ycol = false;
149
150
    position.x += velocity.x * Monocle::deltaTime;
151
    while (Collide("Solid"))
152
      {
153
	xcol = true;
154
	if (velocity.x == 0) { break; }
155
	//printf("collision1\n");
156
	position.x -= SIGN(velocity.x, 0.1);
157
      }
158
    if (xcol) {velocity.x = 0;}
159
160
    position.y += velocity.y * Monocle::deltaTime;
161
    while (Collide("Solid"))
162
      {
163
	ycol = true;
164
	if (velocity.y == 0) { break; }
165
	//printf("collision2\n");
166
	position.y -= SIGN(velocity.y, 0.1);
167
      }
168
    if (ycol) {velocity.y = 0;}
6 by Josh C
skittering blue square
169
7 by Josh C
footsteps
170
    if (velocity.GetSquaredMagnitude() > 20)
8 by Josh C
harder to see creatures
171
      {
15 by Josh C
make maxspeed work. footstep volume has to do with speed. skitter
172
	Vector2 distance = ((DarkScene *)scene)->player->position - position;
173
	// at distance 0, vol should be 1.0.  at 800, it should be 0.
174
	float vol = (distance.GetMagnitude() / -800.0f) + 1.0f;
175
	skitter1->SetVolume(vol);
8 by Josh C
harder to see creatures
176
	skitter1->Play();
177
	sprite->Play("move");
178
      }
6 by Josh C
skittering blue square
179
    else
8 by Josh C
harder to see creatures
180
      {
181
	skitter1->Pause(); //Stop()?
182
	sprite->Play("idle");
183
      }
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
184
  }
185
1 by Josh C
initial commit
186
  // Scene
187
188
  DarkScene::DarkScene() : Scene()
189
  {
190
  }
191
  
192
  void DarkScene::Begin()
193
  {
194
    Scene::Begin();
195
196
    Graphics::Set2D(1024, 768);
197
198
    Text *inst2 = new Text("Press ESC to quit");
199
    inst2->position = Vector2(-492, -354); // 20px h, 30px v from U-L corner
200
    Add(inst2);
201
202
    Input::DefineMaskKey("left", KEY_LEFT);
203
    Input::DefineMaskKey("right", KEY_RIGHT);
204
    Input::DefineMaskKey("up", KEY_UP);
205
    Input::DefineMaskKey("down", KEY_DOWN);
206
207
    Input::DefineMaskKey("up", KEY_W);
208
    Input::DefineMaskKey("left", KEY_A);
209
    Input::DefineMaskKey("down", KEY_S);
210
    Input::DefineMaskKey("right", KEY_D);
211
212
    // level editor
213
    Add( levelEditor = new LevelEditor() );
214
    levelEditor->Disable();
215
216
    // load level from files
217
    Level::LoadProject("project.xml");
218
    Level::Load("level.xml", this);
4 by Josh C
invisible walls and code to bump into them
219
13 by Josh C
visible walls
220
    std::list<Entity*> *walls = GetAllTag("wall");
221
    for (std::list<Entity*>::iterator i = walls->begin(); i != walls->end(); ++i)
4 by Josh C
invisible walls and code to bump into them
222
      {
223
        Entity *e = (*i);
224
        Vector2 s = e->scale;
225
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
226
      }
1 by Josh C
initial commit
227
   
11 by Josh C
disappear darkness mask in editor mode
228
    player = new Player();
1 by Josh C
initial commit
229
    player->position = Graphics::GetScreenCenter();
230
    Add(player);
10 by Josh C
"light" in the dark
231
    Add(player->dark);
1 by Josh C
initial commit
232
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
233
    Creature *creature = new Creature;
234
    creature->position = Graphics::GetScreenCenter();
235
    Add(creature);
236
7 by Josh C
footsteps
237
    Creature *creature2 = new Creature;
238
    creature2->position = Graphics::GetScreenCenter();
239
    Add(creature2);
240
10 by Josh C
"light" in the dark
241
    Graphics::SetBackgroundColor(Color::green * 0.2f);
1 by Josh C
initial commit
242
243
  }
244
245
  void DarkScene::Update()
246
  {
247
    Scene::Update();
248
249
    if (Input::IsKeyPressed(KEY_ESCAPE))
250
      Game::Quit();
251
252
    if (isPaused)
253
      {
254
	if (Input::IsKeyPressed(KEY_S) && Input::IsKeyHeld(KEY_LCTRL))
255
	  Level::Save();
256
257
	if (Input::IsKeyPressed(KEY_A))
258
	  Scene::GetCamera()->position.x -= 600;
259
260
	if (Input::IsKeyPressed(KEY_D))
261
	  Scene::GetCamera()->position.x += 600;
262
      }
263
264
    if (Input::IsKeyPressed(KEY_TAB))
265
      {
266
        // if we're not doing anything in the levelEditor...
267
        if (levelEditor->GetState() == FTES_NONE)
268
          {
269
            isPaused = !isPaused;
270
            
11 by Josh C
disappear darkness mask in editor mode
271
            if (isPaused) {
12 by Josh C
drastically less hacky way to hide the darkness
272
	      player->dark->isVisible = false;
1 by Josh C
initial commit
273
	      levelEditor->Enable();
11 by Josh C
disappear darkness mask in editor mode
274
	    } else {
1 by Josh C
initial commit
275
	      levelEditor->Disable();
12 by Josh C
drastically less hacky way to hide the darkness
276
	      player->dark->isVisible = true;
11 by Josh C
disappear darkness mask in editor mode
277
	    }
1 by Josh C
initial commit
278
          }
279
      }
280
281
  }// DarkScene::Update
282
283
  Text::Text(const std::string& text, FontAsset* font)
284
    : Entity(), text(text)
285
  {
286
    if (font == NULL)
287
      this->font = Assets::RequestFont("LiberationSans-Regular.ttf", 18.0f);
288
    else
289
      this->font = font;
11 by Josh C
disappear darkness mask in editor mode
290
291
    SetLayer(-2);
1 by Josh C
initial commit
292
  }
293
294
  void Text::Render()
295
  {
296
    Graphics::PushMatrix();
297
298
    // place text directly relative to camera
299
    Graphics::Translate(scene->GetCamera()->position + position);
300
301
    Graphics::SetBlend(BLEND_ALPHA);
302
    Graphics::SetColor(Color::white);
303
    Graphics::BindFont(font);
304
    
305
    Graphics::RenderText(*font, text, 0, 0);
306
    Graphics::PopMatrix();
307
  }
308
309
}