added coin plus event

main
Benjamin Kraft 2 years ago
parent 4a98d250d7
commit 20e5edb82c
  1. 28
      src/Coin.cpp
  2. 16
      src/Coin.h
  3. 5
      src/Event.h
  4. 22
      src/Game.cpp
  5. 4
      src/Game.h
  6. 2
      src/Listener.cpp
  7. 1
      src/Listener.h
  8. 16
      src/Player.cpp
  9. 3
      src/Player.h
  10. 21
      src/Spectator.cpp
  11. 2
      src/Spectator.h
  12. 2
      src/Wall.cpp

@ -0,0 +1,28 @@
#include "Coin.h"
#include "Game.h"
void Coin::draw(QPainter &painter) const {
painter.save();
painter.translate(pos);
painter.setBrush(QBrush(Qt::yellow, Qt::SolidPattern));
painter.drawEllipse(QPoint(), radius, radius);
painter.drawText(QRect(-radius, -radius, radius * 2, radius * 2), Qt::AlignCenter, QString::fromStdString(std::to_string(value)));
painter.restore();
}
bool Coin::isLost() const {
return pos.x() < -100;
}
Coin::Coin() {
int y = int(Game::Random(SPEC_Y, GROUND_Y));
pos = {WIDTH, y};
value = int(Game::Random(30, 37));
}
void Coin::update(float dTime) {
pos.setX(float(pos.x()) - velocity * dTime);
}

@ -0,0 +1,16 @@
#pragma once
#include <QPainter>
class Coin {
public:
explicit Coin();
bool collected = false;
QPoint pos;
float velocity = 150;
int value;
int radius = 15;
bool isLost() const;
void draw(QPainter &painter) const;
void update(float dTime);
};

@ -60,3 +60,8 @@ struct WallJumpEvent : GameEvent {
struct WallCrashEvent : GameEvent { struct WallCrashEvent : GameEvent {
Player * player = nullptr; Player * player = nullptr;
}; };
struct CoinCollectEvent : GameEvent {
Player * player = nullptr;
int value = 0;
};

@ -12,22 +12,38 @@ void Game::draw(QPixmap &output) {
spectator.draw(p); spectator.draw(p);
for (auto & wall : walls) for (auto & wall : walls)
wall.draw(p); wall.draw(p);
for (auto & coin : coins)
coin.draw(p);
player.draw(p); player.draw(p);
} }
void Game::update(float dTime) { void Game::update(float dTime) {
eTime += dTime; eTime += dTime;
player.update(dTime, walls); player.update(dTime, walls, coins);
for (auto & wall : walls) for (auto & wall : walls)
wall.update(dTime); wall.update(dTime);
for (auto & coin : coins)
coin.update(dTime);
tryCreateCoin();
removeCoins();
tryCreateWall(); tryCreateWall();
removeWalls(); removeWalls();
} }
void Game::tryCreateCoin() {
if (Game::Random(0, 1000) > 995)
coins.emplace_back();
}
void Game::removeCoins() {
erase_if(coins, [](const Coin& coin){
return coin.isLost() || coin.collected;
});
}
void Game::tryCreateWall() { void Game::tryCreateWall() {
if (Game::Random(0, 1000) > 990){ if (Game::Random(0, 1000) > 990)
walls.emplace_back(); walls.emplace_back();
}
} }
void Game::removeWalls() { void Game::removeWalls() {

@ -4,6 +4,7 @@
#include "Player.h" #include "Player.h"
#include "Spectator.h" #include "Spectator.h"
#include "Wall.h" #include "Wall.h"
#include "Coin.h"
#define WIDTH 1000 #define WIDTH 1000
#define HEIGHT 500 #define HEIGHT 500
@ -15,6 +16,9 @@ class Game {
Player player; Player player;
std::vector<Spectator> spectators; std::vector<Spectator> spectators;
std::vector<Wall> walls; std::vector<Wall> walls;
std::vector<Coin> coins;
void tryCreateCoin();
void removeCoins();
void tryCreateWall(); void tryCreateWall();
void removeWalls(); void removeWalls();
public: public:

@ -5,6 +5,8 @@ void GameListener::accept(GameEvent *event) {
OnWallJumped(wallJumpEvent); OnWallJumped(wallJumpEvent);
if (auto wallCrashEvent = dynamic_cast<WallCrashEvent *>(event)) if (auto wallCrashEvent = dynamic_cast<WallCrashEvent *>(event))
OnWallCrashed(wallCrashEvent); OnWallCrashed(wallCrashEvent);
if (auto coinCollectEvent = dynamic_cast<CoinCollectEvent *>(event))
OnCoinCollected(coinCollectEvent);
} }
void InputListener::accept(InputEvent *event) { void InputListener::accept(InputEvent *event) {

@ -28,4 +28,5 @@ public:
protected: protected:
virtual void OnWallJumped(WallJumpEvent * event) {}; virtual void OnWallJumped(WallJumpEvent * event) {};
virtual void OnWallCrashed(WallCrashEvent * event) {}; virtual void OnWallCrashed(WallCrashEvent * event) {};
virtual void OnCoinCollected(CoinCollectEvent * event) {};
}; };

@ -12,12 +12,12 @@ void Player::draw(QPainter &painter) const {
painter.save(); painter.save();
painter.translate(pos.toPoint()); painter.translate(pos.toPoint());
painter.fillRect(-5, -10, 10, 10, Qt::black); painter.fillRect(-5, -10, 10, 10, Qt::darkGreen);
painter.restore(); painter.restore();
} }
void Player::update(float dTime, std::vector<Wall> &walls) { void Player::update(float dTime, std::vector<Wall> &walls, std::vector<Coin>& coins) {
pos += vel * dTime; pos += vel * dTime;
vel += acc * dTime; vel += acc * dTime;
@ -47,6 +47,18 @@ void Player::update(float dTime, std::vector<Wall> &walls) {
} }
} }
} }
for (auto &coin : coins){
if (coin.collected)
continue;
if (pos.distanceToPoint(QVector2D(coin.pos.x(), coin.pos.y())) <= coin.radius){
coin.collected = true;
auto e = new CoinCollectEvent;
e->time = Game::instance->eTime;
e->player = this;
e->value = coin.value;
Game::eventQueue->submitEvent(e);
}
}
} }
void Player::jump() { void Player::jump() {

@ -3,6 +3,7 @@
#include "Listener.h" #include "Listener.h"
#include "Event.h" #include "Event.h"
#include "Wall.h" #include "Wall.h"
#include "Coin.h"
#include <QPainter> #include <QPainter>
class Player : public InputListener { class Player : public InputListener {
@ -15,7 +16,7 @@ class Player : public InputListener {
void jump(); void jump();
public: public:
void draw(QPainter &painter) const; void draw(QPainter &painter) const;
void update(float dTime, std::vector<Wall>&); void update(float dTime, std::vector<Wall>&, std::vector<Coin>&);
protected: protected:
void mousePressed(MouseEvent *) override; void mousePressed(MouseEvent *) override;

@ -6,11 +6,11 @@
void Spectator::OnWallJumped(WallJumpEvent *event) { void Spectator::OnWallJumped(WallJumpEvent *event) {
float timeDiff = Game::instance->eTime - event->time; float timeDiff = Game::instance->eTime - event->time;
if (timeDiff > 2){ if (timeDiff > 2){
currentMessage = "Ignored event"; currentMessage = "Ignored jump";
return; return;
} }
currentMessage = "Calculating prime..."; currentMessage = "Calculating Prime...";
int prime = getHighPrime(); int prime = getHighPrime();
currentMessage = std::to_string(prime); currentMessage = std::to_string(prime);
} }
@ -19,6 +19,14 @@ void Spectator::OnWallCrashed(WallCrashEvent *event) {
currentMessage = "Crash!"; currentMessage = "Crash!";
} }
void Spectator::OnCoinCollected(CoinCollectEvent *event) {
int value = event->value;
currentMessage = "Calculating Fibo(" + std::to_string(value) + ")";
int fib = getHighFibonacci(value);
currentMessage = std::to_string(fib);
}
void Spectator::draw(QPainter &painter) const { void Spectator::draw(QPainter &painter) const {
painter.save(); painter.save();
@ -67,4 +75,13 @@ int Spectator::getHighPrime() {
return 2; return 2;
} }
int Spectator::getHighFibonacci(int val) {
std::function<int(int)> fib = [&fib](int n){
if (n == 1 || n == 2)
return n;
return fib(n - 1) + fib(n - 2);
};
return fib(val);
}

@ -6,8 +6,10 @@ class Spectator : public GameListener {
protected: protected:
void OnWallJumped(WallJumpEvent * event) override; void OnWallJumped(WallJumpEvent * event) override;
void OnWallCrashed(WallCrashEvent * event) override; void OnWallCrashed(WallCrashEvent * event) override;
void OnCoinCollected(CoinCollectEvent * event) override;
std::string currentMessage; std::string currentMessage;
static int getHighPrime(); static int getHighPrime();
static int getHighFibonacci(int val);
public: public:
QPoint pos; QPoint pos;
QSize size; QSize size;

@ -7,7 +7,7 @@ void Wall::draw(QPainter &painter) const {
int w = size.width(); int w = size.width();
int h = size.height(); int h = size.height();
painter.translate(pos.toPoint()); painter.translate(pos.toPoint());
painter.fillRect(w / 2, -h, w, h, Qt::black); painter.fillRect(w / 2, -h, w, h, Qt::red);
painter.restore(); painter.restore();
} }

Loading…
Cancel
Save