/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);
86
    sprite->Play("idle");
87
    SetGraphic(sprite);
88
89
    AddTag("creature");
90
    SetCollider(new RectangleCollider(32, 32));
91
6 by Josh C
skittering blue square
92
    skitter1 = Audio::NewDeck(Assets::RequestAudio("skitter1.ogg"));
93
    skitter1->SetLoops(0); //loop indefinitely
94
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
95
    velocity = Vector2::Random() * ACCELERATION;
96
97
    //set aiTime to random so creatures tick at different times
6 by Josh C
skittering blue square
98
    aiTime= (float(rand()) / float(RAND_MAX)) * 1.0f;
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
99
  }
100
101
  void Creature::Update()
102
  {
103
    Entity::Update();
104
105
    aiTime += Monocle::deltaTime;
6 by Josh C
skittering blue square
106
    if (aiTime > 1.0f)
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
107
      {
6 by Josh C
skittering blue square
108
	switch (rand() % 2)
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
109
	  {
6 by Josh C
skittering blue square
110
	  case 0: // idle
111
	    //printf("idle\n");
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
112
	    direction = Vector2::zero;
113
	    state = "idle";
114
	    break;
6 by Josh C
skittering blue square
115
	  case 1: // move
116
	    if (state != "wander") {
117
	      //printf("wander\n");
118
	      direction = Vector2::Random();
119
	      state = "wander";
120
	      break;
121
	    }
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
122
	  }
123
	
124
	aiTime = 0.0f;
125
      }
126
    
127
    velocity += direction * ACCELERATION * Monocle::deltaTime;
128
    
129
    velocity.x = APPROACH(velocity.x, 0, FRICTION * Monocle::deltaTime);
130
    velocity.y = APPROACH(velocity.y, 0, FRICTION * Monocle::deltaTime);
131
6 by Josh C
skittering blue square
132
    bool xcol = false;
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
133
    bool ycol = false;
134
135
    position.x += velocity.x * Monocle::deltaTime;
136
    while (Collide("Solid"))
137
      {
138
	xcol = true;
139
	if (velocity.x == 0) { break; }
140
	//printf("collision1\n");
141
	position.x -= SIGN(velocity.x, 0.1);
142
      }
143
    if (xcol) {velocity.x = 0;}
144
145
    position.y += velocity.y * Monocle::deltaTime;
146
    while (Collide("Solid"))
147
      {
148
	ycol = true;
149
	if (velocity.y == 0) { break; }
150
	//printf("collision2\n");
151
	position.y -= SIGN(velocity.y, 0.1);
152
      }
153
    if (ycol) {velocity.y = 0;}
6 by Josh C
skittering blue square
154
7 by Josh C
footsteps
155
    if (velocity.GetSquaredMagnitude() > 20)
6 by Josh C
skittering blue square
156
      skitter1->Play();
157
    else
158
      skitter1->Pause(); //Stop()?
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
159
  }
160
1 by Josh C
initial commit
161
  // Scene
162
163
  DarkScene::DarkScene() : Scene()
164
  {
165
  }
166
  
167
  void DarkScene::Begin()
168
  {
169
    Scene::Begin();
170
171
    Graphics::Set2D(1024, 768);
172
173
    Text *inst2 = new Text("Press ESC to quit");
174
    inst2->position = Vector2(-492, -354); // 20px h, 30px v from U-L corner
175
    Add(inst2);
176
177
    Input::DefineMaskKey("left", KEY_LEFT);
178
    Input::DefineMaskKey("right", KEY_RIGHT);
179
    Input::DefineMaskKey("up", KEY_UP);
180
    Input::DefineMaskKey("down", KEY_DOWN);
181
182
    Input::DefineMaskKey("up", KEY_W);
183
    Input::DefineMaskKey("left", KEY_A);
184
    Input::DefineMaskKey("down", KEY_S);
185
    Input::DefineMaskKey("right", KEY_D);
186
187
    // level editor
188
    Add( levelEditor = new LevelEditor() );
189
    levelEditor->Disable();
190
191
    // load level from files
192
    Level::LoadProject("project.xml");
193
    Level::Load("level.xml", this);
4 by Josh C
invisible walls and code to bump into them
194
195
    std::list<Entity*> *inv = GetAllTag("invisible");
196
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
197
      {
198
        Entity *e = (*i);
199
        Vector2 s = e->scale;
200
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
201
      }
1 by Josh C
initial commit
202
   
203
    Player *player = new Player;
204
    player->position = Graphics::GetScreenCenter();
205
    Add(player);
206
5 by Josh C
added a "creature" moving around w/ the same dynamics as you do
207
    Creature *creature = new Creature;
208
    creature->position = Graphics::GetScreenCenter();
209
    Add(creature);
210
7 by Josh C
footsteps
211
    Creature *creature2 = new Creature;
212
    creature2->position = Graphics::GetScreenCenter();
213
    Add(creature2);
214
1 by Josh C
initial commit
215
    Graphics::SetBackgroundColor(Color::green * 0.2f);
216
217
  }
218
219
  void DarkScene::Update()
220
  {
221
    Scene::Update();
222
223
    if (Input::IsKeyPressed(KEY_ESCAPE))
224
      Game::Quit();
225
226
    if (isPaused)
227
      {
228
	if (Input::IsKeyPressed(KEY_S) && Input::IsKeyHeld(KEY_LCTRL))
229
	  Level::Save();
230
231
	if (Input::IsKeyPressed(KEY_A))
232
	  Scene::GetCamera()->position.x -= 600;
233
234
	if (Input::IsKeyPressed(KEY_D))
235
	  Scene::GetCamera()->position.x += 600;
236
      }
237
238
    if (Input::IsKeyPressed(KEY_TAB))
239
      {
240
        // if we're not doing anything in the levelEditor...
241
        if (levelEditor->GetState() == FTES_NONE)
242
          {
243
            isPaused = !isPaused;
244
            
245
            if (isPaused)
246
	      levelEditor->Enable();
247
            else
248
	      levelEditor->Disable();
249
          }
250
      }
251
252
  }// DarkScene::Update
253
254
  Text::Text(const std::string& text, FontAsset* font)
255
    : Entity(), text(text)
256
  {
257
    if (font == NULL)
258
      this->font = Assets::RequestFont("LiberationSans-Regular.ttf", 18.0f);
259
    else
260
      this->font = font;
261
  }
262
263
  void Text::Render()
264
  {
265
    Graphics::PushMatrix();
266
267
    // place text directly relative to camera
268
    Graphics::Translate(scene->GetCamera()->position + position);
269
270
    Graphics::SetBlend(BLEND_ALPHA);
271
    Graphics::SetColor(Color::white);
272
    Graphics::BindFont(font);
273
    
274
    Graphics::RenderText(*font, text, 0, 0);
275
    Graphics::PopMatrix();
276
  }
277
278
}