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