Compare commits

..

No commits in common. '3a1e1ce094d51da925842288beaf12109acfb09a' and 'ff3bd0990f2c03d0eb99d90c4025a656e3a6e642' have entirely different histories.

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

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

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

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

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

@ -1,20 +1,14 @@
#include "Player.h"
#include "Audio.h"
#include <iostream>
#include "Game.h"
void Player::mousePressed(MouseEvent *event) {
if (pos.y() == GROUND_Y && event->button == Qt::MouseButton::LeftButton){
if (pos.y() == ground && event->button == Qt::MouseButton::LeftButton){
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) const {
void Player::draw(QPainter &painter) {
painter.save();
painter.translate(pos.toPoint());
@ -27,8 +21,8 @@ void Player::update(float dTime) {
pos += vel * dTime;
vel += acc * dTime;
if (pos.y() > GROUND_Y){
pos.setY(GROUND_Y);
if (pos.y() > ground){
pos.setY(ground);
vel = {};
}
}

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

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

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

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

Loading…
Cancel
Save