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