12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- #include "Game.h"
- #include "rendering/Renderer.h"
- #include "rendering/wrapper/GLFWWrapper.h"
- #include "utils/String.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(const Controller& c, const Clock& fps, const Clock& tps) : controller(c), fps(fps), tps(tps) {
- }
- void Game::tick() {
- lastX = x;
- lastY = y;
- if(controller.isDown(Button::LEFT)) {
- x -= 7;
- }
- if(controller.isDown(Button::RIGHT)) {
- x += 7;
- }
- if(controller.isDown(Button::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).scale(4).update();
- for(uint i = 0; i < controller.getButtonAmount(); i++) {
- String s(controller.getName(i));
- s.append(": ").append(controller.wasReleased(i)).append(" ").append(controller.getDownTime(i));
- renderer.drawString(0, 10 * i, s);
- }
- renderer.translateTo(0.0f, 0.0f).update();
- renderer.drawRectangle(lastX + (x - lastX) * lag, lastY + (y - lastY) * lag, playerWidth, playerHeight, 0x0000FFFF);
- }
- bool Game::isRunning() const {
- return !controller.isDown(Button::SELECT);
- }
|