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 |
||
45 |
position += velocity * Monocle::deltaTime; |
|
46 |
||
1
by Josh C
initial commit |
47 |
//Scene::GetCamera()->position = position; |
48 |
||
49 |
//... |
|
50 |
} |
|
51 |
||
52 |
// Scene |
|
53 |
||
54 |
DarkScene::DarkScene() : Scene() |
|
55 |
{ |
|
56 |
} |
|
57 |
||
58 |
void DarkScene::Begin() |
|
59 |
{ |
|
60 |
Scene::Begin(); |
|
61 |
||
62 |
Graphics::Set2D(1024, 768); |
|
63 |
||
64 |
Text *inst2 = new Text("Press ESC to quit"); |
|
65 |
inst2->position = Vector2(-492, -354); // 20px h, 30px v from U-L corner |
|
66 |
Add(inst2); |
|
67 |
||
68 |
Input::DefineMaskKey("left", KEY_LEFT); |
|
69 |
Input::DefineMaskKey("right", KEY_RIGHT); |
|
70 |
Input::DefineMaskKey("up", KEY_UP); |
|
71 |
Input::DefineMaskKey("down", KEY_DOWN); |
|
72 |
||
73 |
Input::DefineMaskKey("up", KEY_W); |
|
74 |
Input::DefineMaskKey("left", KEY_A); |
|
75 |
Input::DefineMaskKey("down", KEY_S); |
|
76 |
Input::DefineMaskKey("right", KEY_D); |
|
77 |
||
78 |
// level editor |
|
79 |
Add( levelEditor = new LevelEditor() ); |
|
80 |
levelEditor->Disable(); |
|
81 |
||
82 |
// load level from files |
|
83 |
Level::LoadProject("project.xml"); |
|
84 |
Level::Load("level.xml", this); |
|
85 |
||
86 |
Player *player = new Player; |
|
87 |
player->position = Graphics::GetScreenCenter(); |
|
88 |
Add(player); |
|
89 |
||
90 |
Graphics::SetBackgroundColor(Color::green * 0.2f); |
|
91 |
||
92 |
} |
|
93 |
||
94 |
void DarkScene::Update() |
|
95 |
{ |
|
96 |
Scene::Update(); |
|
97 |
||
98 |
if (Input::IsKeyPressed(KEY_ESCAPE)) |
|
99 |
Game::Quit(); |
|
100 |
||
101 |
if (isPaused) |
|
102 |
{ |
|
103 |
if (Input::IsKeyPressed(KEY_S) && Input::IsKeyHeld(KEY_LCTRL)) |
|
104 |
Level::Save(); |
|
105 |
||
106 |
if (Input::IsKeyPressed(KEY_A)) |
|
107 |
Scene::GetCamera()->position.x -= 600; |
|
108 |
||
109 |
if (Input::IsKeyPressed(KEY_D)) |
|
110 |
Scene::GetCamera()->position.x += 600; |
|
111 |
} |
|
112 |
||
113 |
if (Input::IsKeyPressed(KEY_TAB)) |
|
114 |
{ |
|
115 |
// if we're not doing anything in the levelEditor... |
|
116 |
if (levelEditor->GetState() == FTES_NONE) |
|
117 |
{ |
|
118 |
isPaused = !isPaused; |
|
119 |
||
120 |
if (isPaused) |
|
121 |
levelEditor->Enable(); |
|
122 |
else |
|
123 |
levelEditor->Disable(); |
|
124 |
} |
|
125 |
} |
|
126 |
||
127 |
}// DarkScene::Update |
|
128 |
||
129 |
Text::Text(const std::string& text, FontAsset* font) |
|
130 |
: Entity(), text(text) |
|
131 |
{ |
|
132 |
if (font == NULL) |
|
133 |
this->font = Assets::RequestFont("LiberationSans-Regular.ttf", 18.0f); |
|
134 |
else |
|
135 |
this->font = font; |
|
136 |
} |
|
137 |
||
138 |
void Text::Render() |
|
139 |
{ |
|
140 |
Graphics::PushMatrix(); |
|
141 |
||
142 |
// place text directly relative to camera |
|
143 |
Graphics::Translate(scene->GetCamera()->position + position); |
|
144 |
||
145 |
Graphics::SetBlend(BLEND_ALPHA); |
|
146 |
Graphics::SetColor(Color::white); |
|
147 |
Graphics::BindFont(font); |
|
148 |
||
149 |
Graphics::RenderText(*font, text, 0, 0); |
|
150 |
Graphics::PopMatrix(); |
|
151 |
} |
|
152 |
||
153 |
} |