1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- #include "Game.h"
- #include "rendering/Renderer.h"
- #include "rendering/wrapper/GLFW.h"
- #include "utils/StringBuffer.h"
- float x = 0.0f;
- float y = 0.0f;
- float lastX = 0.0f;
- float lastY = 0.0f;
- float playerWidth = 80.0f;
- float playerHeight = 80.0f;
- float width = 800.0f;
- float height = 480.0f;
- float motionY = 0.0f;
- bool onGround = false;
- Game::Game(Controller& c, const Clock& fps, const Clock& tps) : controller(c), fps(fps), tps(tps) {
- c.mapKey(GLFW_KEY_A, Controller::A);
- c.mapKey(GLFW_KEY_S, Controller::B);
- c.mapKey(GLFW_KEY_X, Controller::X);
- c.mapKey(GLFW_KEY_Z, Controller::Y);
- c.mapKey(GLFW_KEY_Q, Controller::L);
- c.mapKey(GLFW_KEY_W, Controller::R);
- c.mapKey(GLFW_KEY_E, Controller::START);
- c.mapKey(GLFW_KEY_D, Controller::SELECT);
- c.mapKey(GLFW_KEY_LEFT, Controller::LEFT);
- c.mapKey(GLFW_KEY_RIGHT, Controller::RIGHT);
- c.mapKey(GLFW_KEY_UP, Controller::UP);
- c.mapKey(GLFW_KEY_DOWN, Controller::DOWN);
- }
- void Game::tick() {
- lastX = x;
- lastY = y;
- if(controller.isDown(Controller::LEFT)) {
- x -= 7;
- }
- if(controller.isDown(Controller::RIGHT)) {
- x += 7;
- }
- if(controller.isDown(Controller::A) && onGround) {
- motionY = -15.0f;
- onGround = false;
- }
- motionY += 0.5f;
- y += motionY;
- if(y > height - playerHeight) {
- y = height - playerHeight;
- motionY = 0.0f;
- onGround = true;
- }
- while(x > width) {
- x -= width + playerWidth;
- lastX -= width + playerWidth;
- }
- while(x < -playerWidth) {
- x += width + playerWidth;
- lastX += width + playerWidth;
- }
- }
- void Game::render(float lag, Renderer& renderer) const {
- renderer.translateTo(0.0f, 0.0f).update();
- renderer.drawRectangle(lastX + (x - lastX) * lag, lastY + (y - lastY) * lag, playerWidth, playerHeight, Color4(255, 0, 0, 255));
- renderer.translateTo(0.0f, 0.0f).update();
- StringBuffer<100> s;
- s.append("FPS: ").append(fps.getUpdatesPerSecond());
- float y = 0.0f;
- renderer.setStringSize(2);
- y = renderer.drawString(0.0f, y, s);
- for(int i = 0; i < controller.getButtonAmount(); i++) {
- s.clear().append(controller.getName(i)).append(": ").append(controller.getDownTime(i)).append(" ").append(controller.wasReleased(i));
- y = renderer.drawString(0.0f, y, s);
- }
- }
- bool Game::isRunning() const {
- return !controller.isDown(Controller::SELECT);
- }
|