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