12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #include "Game.h"
- #include "rendering/Renderer.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) {
- }
- void Game::tick() {
- lastX = x;
- lastY = y;
- if(controller.left.isDown()) {
- x -= 7;
- }
- if(controller.right.isDown()) {
- x += 7;
- }
- if(controller.a.isDown() && 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(const Button& b : controller.list) {
- s.clear().append(b.getName()).append(": ").append(b.getDownTime()).append(" ").append(b.wasReleased());
- y = renderer.drawString(0.0f, y, s);
- }
- }
- bool Game::isRunning() const {
- return !controller.select.isDown();
- }
|