Compare commits

...

2 Commits

  1. 13
      src/Event.h
  2. 9
      src/Game.cpp
  3. 6
      src/Game.h
  4. 7
      src/Listener.cpp
  5. 3
      src/Listener.h
  6. 14
      src/Player.cpp
  7. 3
      src/Player.h
  8. 1
      src/Queue.h
  9. 8
      src/Spectator.cpp
  10. 4
      src/Spectator.h

@ -46,14 +46,13 @@ struct WheelEvent : InputEvent {
struct GameEvent { struct GameEvent {
int time = 0; // seconds
float time = 0;
virtual void print() {}; virtual void print() {};
}; };
struct CoinCollectEvent : GameEvent { class Player;
double value = 0; struct WallJumpEvent : GameEvent {
}; double wallHeight = 0;
Player * player = nullptr;
struct DamageEvent : GameEvent {
double amount = 0;
}; };

@ -5,16 +5,23 @@
void Game::draw(QPixmap &output) { void Game::draw(QPixmap &output) {
QPainter p(&output); QPainter p(&output);
for (auto & spectator : spectators)
spectator.draw(p);
player.draw(p); player.draw(p);
} }
void Game::update(float dTime) { void Game::update(float dTime) {
eTime += dTime;
player.update(dTime); player.update(dTime);
} }
Game::Game() { Game::Game() {
for (auto & spectator : spectators){
Game::eventQueue->registerListener(&spectator);
}
InputWindow::inputQueue->registerListener(&player); InputWindow::inputQueue->registerListener(&player);
instance = this;
} }
GameQueue * Game::eventQueue = new GameQueue; GameQueue * Game::eventQueue = new GameQueue;
Game * Game::instance = nullptr;

@ -6,11 +6,15 @@
#define WIDTH 1000 #define WIDTH 1000
#define HEIGHT 500 #define HEIGHT 500
#define GROUND_Y 400
#define SPEC_Y 150
class Game { class Game {
Player player; Player player;
std::vector<Spectator> spectators; std::vector<Spectator> spectators {5};
public: public:
float eTime = 0;
static Game * instance;
static GameQueue * eventQueue; static GameQueue * eventQueue;
explicit Game(); explicit Game();
void draw(QPixmap & output); void draw(QPixmap & output);

@ -1,11 +1,8 @@
#include "Listener.h" #include "Listener.h"
void GameListener::accept(GameEvent *event) { void GameListener::accept(GameEvent *event) {
if (auto coinEvent = dynamic_cast<CoinCollectEvent *>(event)) if (auto wallJumpEvent = dynamic_cast<WallJumpEvent *>(event))
OnCoinCollect(coinEvent); OnWallJumped(wallJumpEvent);
if (auto dmgEvent = dynamic_cast<DamageEvent *>(event))
OnDamage(dmgEvent);
} }
void InputListener::accept(InputEvent *event) { void InputListener::accept(InputEvent *event) {

@ -26,6 +26,5 @@ class GameListener : public Listener<GameEvent> {
public: public:
void accept(GameEvent * event) override; void accept(GameEvent * event) override;
protected: protected:
virtual void OnCoinCollect(CoinCollectEvent * event) {}; virtual void OnWallJumped(WallJumpEvent * event) {};
virtual void OnDamage(DamageEvent * event) {};
}; };

@ -1,14 +1,20 @@
#include "Player.h" #include "Player.h"
#include "Audio.h" #include "Audio.h"
#include <iostream> #include <iostream>
#include "Game.h"
void Player::mousePressed(MouseEvent *event) { void Player::mousePressed(MouseEvent *event) {
if (pos.y() == ground && event->button == Qt::MouseButton::LeftButton){ if (pos.y() == GROUND_Y && event->button == Qt::MouseButton::LeftButton){
jump(); jump();
auto e = new WallJumpEvent;
e->time = Game::instance->eTime;
e->player = this;
e->wallHeight = 100;
Game::eventQueue->submitEvent(e);
} }
} }
void Player::draw(QPainter &painter) { void Player::draw(QPainter &painter) const {
painter.save(); painter.save();
painter.translate(pos.toPoint()); painter.translate(pos.toPoint());
@ -21,8 +27,8 @@ void Player::update(float dTime) {
pos += vel * dTime; pos += vel * dTime;
vel += acc * dTime; vel += acc * dTime;
if (pos.y() > ground){ if (pos.y() > GROUND_Y){
pos.setY(ground); pos.setY(GROUND_Y);
vel = {}; vel = {};
} }
} }

@ -10,10 +10,9 @@ class Player : public Entity, public InputListener {
QVector2D vel {}; QVector2D vel {};
QVector2D acc {0, 9.81 * 30}; QVector2D acc {0, 9.81 * 30};
float ground = 400;
void jump(); void jump();
public: public:
void draw(QPainter &painter); void draw(QPainter &painter) const;
void update(float dTime); void update(float dTime);
protected: protected:

@ -10,7 +10,6 @@
template<typename T> template<typename T>
class Queue { class Queue {
private:
std::thread thread; std::thread thread;
std::mutex mutex; std::mutex mutex;
void run() { void run() {

@ -2,6 +2,10 @@
#include <iostream> #include <iostream>
void Spectator::OnCoinCollect(CoinCollectEvent *event) { void Spectator::OnWallJumped(WallJumpEvent *event) {
std::cout << "Someone collected a coin\n"; std::cout << "Wall jump at " << event->time << std::endl;
}
void Spectator::draw(QPainter &painter) const {
} }

@ -5,5 +5,7 @@
class Spectator : public Entity, public GameListener { class Spectator : public Entity, public GameListener {
protected: protected:
void OnCoinCollect(CoinCollectEvent *event) override; void OnWallJumped(WallJumpEvent * event) override;
public:
void draw(QPainter &painter) const;
}; };

Loading…
Cancel
Save