/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");
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
72
  // Scene
73
74
  DarkScene::DarkScene() : Scene()
75
  {
76
  }
77
  
78
  void DarkScene::Begin()
79
  {
80
    Scene::Begin();
81
82
    Graphics::Set2D(1024, 768);
83
84
    Text *inst2 = new Text("Press ESC to quit");
85
    inst2->position = Vector2(-492, -354); // 20px h, 30px v from U-L corner
86
    Add(inst2);
87
88
    Input::DefineMaskKey("left", KEY_LEFT);
89
    Input::DefineMaskKey("right", KEY_RIGHT);
90
    Input::DefineMaskKey("up", KEY_UP);
91
    Input::DefineMaskKey("down", KEY_DOWN);
92
93
    Input::DefineMaskKey("up", KEY_W);
94
    Input::DefineMaskKey("left", KEY_A);
95
    Input::DefineMaskKey("down", KEY_S);
96
    Input::DefineMaskKey("right", KEY_D);
97
98
    // level editor
99
    Add( levelEditor = new LevelEditor() );
100
    levelEditor->Disable();
101
102
    // load level from files
103
    Level::LoadProject("project.xml");
104
    Level::Load("level.xml", this);
4 by Josh C
invisible walls and code to bump into them
105
106
    std::list<Entity*> *inv = GetAllTag("invisible");
107
    for (std::list<Entity*>::iterator i = inv->begin(); i != inv->end(); ++i)
108
      {
109
        Entity *e = (*i);
110
        Vector2 s = e->scale;
111
        e->SetCollider(new RectangleCollider(s.x * 64, s.y * 64));
112
      }
1 by Josh C
initial commit
113
   
114
    Player *player = new Player;
115
    player->position = Graphics::GetScreenCenter();
116
    Add(player);
117
118
    Graphics::SetBackgroundColor(Color::green * 0.2f);
119
120
  }
121
122
  void DarkScene::Update()
123
  {
124
    Scene::Update();
125
126
    if (Input::IsKeyPressed(KEY_ESCAPE))
127
      Game::Quit();
128
129
    if (isPaused)
130
      {
131
	if (Input::IsKeyPressed(KEY_S) && Input::IsKeyHeld(KEY_LCTRL))
132
	  Level::Save();
133
134
	if (Input::IsKeyPressed(KEY_A))
135
	  Scene::GetCamera()->position.x -= 600;
136
137
	if (Input::IsKeyPressed(KEY_D))
138
	  Scene::GetCamera()->position.x += 600;
139
      }
140
141
    if (Input::IsKeyPressed(KEY_TAB))
142
      {
143
        // if we're not doing anything in the levelEditor...
144
        if (levelEditor->GetState() == FTES_NONE)
145
          {
146
            isPaused = !isPaused;
147
            
148
            if (isPaused)
149
	      levelEditor->Enable();
150
            else
151
	      levelEditor->Disable();
152
          }
153
      }
154
155
  }// DarkScene::Update
156
157
  Text::Text(const std::string& text, FontAsset* font)
158
    : Entity(), text(text)
159
  {
160
    if (font == NULL)
161
      this->font = Assets::RequestFont("LiberationSans-Regular.ttf", 18.0f);
162
    else
163
      this->font = font;
164
  }
165
166
  void Text::Render()
167
  {
168
    Graphics::PushMatrix();
169
170
    // place text directly relative to camera
171
    Graphics::Translate(scene->GetCamera()->position + position);
172
173
    Graphics::SetBlend(BLEND_ALPHA);
174
    Graphics::SetColor(Color::white);
175
    Graphics::BindFont(font);
176
    
177
    Graphics::RenderText(*font, text, 0, 0);
178
    Graphics::PopMatrix();
179
  }
180
181
}