Compare commits

..

No commits in common. '2890ec0d0ab4e32112760d55f8b9ac2c82bc26fd' and '78252c94e3a3c67b173b84e60517a8a86b939211' have entirely different histories.

  1. 4
      src/Event.h
  2. 6
      src/Game.cpp
  3. 16
      src/Listener.cpp
  4. 4
      src/Listener.h
  5. 9
      src/Logger.cpp
  6. 3
      src/Player.cpp
  7. 38
      src/Queue.h
  8. 2
      src/main.cpp

@ -51,14 +51,18 @@ struct GameEvent {
virtual void print() {};
};
class Player;
struct WallJumpEvent : GameEvent {
double wallHeight = 0;
Player * player = nullptr;
};
struct WallCrashEvent : GameEvent {
double wallHeight = 0;
Player * player = nullptr;
};
struct CoinCollectEvent : GameEvent {
Player * player = nullptr;
int value = 0;
};

@ -66,12 +66,8 @@ Game::Game() {
for (auto & spec : spectators)
Game::eventQueue->registerListener(&spec);
InputWindow::inputQueue->registerListener(&player);
auto logger = new Logger;
Game::eventQueue->registerListener(logger);
Game::eventQueue->unregisterListener(logger);
Game::eventQueue->registerListener(logger);
Game::eventQueue->registerListener(new Logger);
Game::eventQueue->registerListener(new RecordTracker);
instance = this;
}

@ -3,25 +3,25 @@
void GameListener::accept(GameEvent *event) {
if (auto wallJumpEvent = dynamic_cast<WallJumpEvent *>(event))
OnWallJumped(wallJumpEvent);
else if (auto wallCrashEvent = dynamic_cast<WallCrashEvent *>(event))
if (auto wallCrashEvent = dynamic_cast<WallCrashEvent *>(event))
OnWallCrashed(wallCrashEvent);
else if (auto coinCollectEvent = dynamic_cast<CoinCollectEvent *>(event))
if (auto coinCollectEvent = dynamic_cast<CoinCollectEvent *>(event))
OnCoinCollected(coinCollectEvent);
}
void InputListener::accept(InputEvent *event) {
if (auto mousePress = dynamic_cast<MousePressEvent *>(event))
mousePressed(mousePress);
else if (auto mouseRelease = dynamic_cast<MouseReleaseEvent *>(event))
if (auto mouseRelease = dynamic_cast<MouseReleaseEvent *>(event))
mouseReleased(mouseRelease);
else if (auto mouseDoubleClick = dynamic_cast<MouseDoubleClickEvent *>(event))
if (auto mouseDoubleClick = dynamic_cast<MouseDoubleClickEvent *>(event))
mouseDoubleClicked(mouseDoubleClick);
else if (auto mouseMove = dynamic_cast<MouseMoveEvent *>(event))
if (auto mouseMove = dynamic_cast<MouseMoveEvent *>(event))
mouseMoved(mouseMove);
else if (auto keyPress = dynamic_cast<KeyPressEvent *>(event))
if (auto keyPress = dynamic_cast<KeyPressEvent *>(event))
keyPressed(keyPress);
else if (auto keyRelease = dynamic_cast<KeyReleaseEvent *>(event))
if (auto keyRelease = dynamic_cast<KeyReleaseEvent *>(event))
keyReleased(keyRelease);
else if (auto wheel = dynamic_cast<WheelEvent *>(event))
if (auto wheel = dynamic_cast<WheelEvent *>(event))
mouseWheel(wheel);
}

@ -3,10 +3,10 @@
#include <iostream>
#include "Event.h"
template <typename T>
template <typename EventType>
class Listener {
public:
virtual void accept(T *) {}
virtual void accept(EventType *) {}
};
class InputListener : public Listener<InputEvent> {

@ -6,21 +6,18 @@ constexpr int eventStringMax = 6;
void Logger::OnCoinCollected(CoinCollectEvent *event) {
char buffer[MAX_STRING];
sprintf(buffer, "Game event: %*s | time: %6.2f | value: %d\n",
eventStringMax, "Coin", event->time, event->value);
sprintf(buffer, "Game event: %*s | time: %6.2f | value: %d\n", eventStringMax, "Coin", event->time, event->value);
FileManager::instance->writeFile("log.txt", buffer, true);
}
void Logger::OnWallJumped(WallJumpEvent *event) {
char buffer[MAX_STRING];
sprintf(buffer, "Game event: %*s | time: %6.2f | wall_height: %6.2f\n",
eventStringMax, "Jump", event->time, event->wallHeight);
sprintf(buffer, "Game event: %*s | time: %6.2f | wall_height: %6.2f\n", eventStringMax, "Jump", event->time, event->wallHeight);
FileManager::instance->writeFile("log.txt", buffer, true);
}
void Logger::OnWallCrashed(WallCrashEvent *event) {
char buffer[MAX_STRING];
sprintf(buffer, "Game event: %*s | time: %6.2f | wall_height: %6.2f\n",
eventStringMax, "Crash", event->time, event->wallHeight);
sprintf(buffer, "Game event: %*s | time: %6.2f | wall_height: %6.2f\n", eventStringMax, "Crash", event->time, event->wallHeight);
FileManager::instance->writeFile("log.txt", buffer, true);
}

@ -35,6 +35,7 @@ void Player::update(float dTime, std::vector<Wall> &walls, std::vector<Coin>& co
wall.jumped = true;
auto e = new WallJumpEvent;
e->time = Game::instance->eTime;
e->player = this;
e->wallHeight = wall.size.height();
Game::eventQueue->submitEvent(e);
} else {
@ -42,6 +43,7 @@ void Player::update(float dTime, std::vector<Wall> &walls, std::vector<Coin>& co
auto e = new WallCrashEvent;
e->time = Game::instance->eTime;
e->wallHeight = wall.size.height();
e->player = this;
Game::eventQueue->submitEvent(e);
}
}
@ -54,6 +56,7 @@ void Player::update(float dTime, std::vector<Wall> &walls, std::vector<Coin>& co
coin.collected = true;
auto e = new CoinCollectEvent;
e->time = Game::instance->eTime;
e->player = this;
e->value = coin.value;
Game::eventQueue->submitEvent(e);
}

@ -17,18 +17,29 @@ class Queue {
std::mutex mutex;
void run() {
while (!QApplication::closingDown()){
if (tail != head)
if (!isEmpty())
update();
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
};
virtual void update() {}
// First in: points to next free slot
// First in
size_t tail = 0;
// First out: points to leaving element's position
// First out
size_t head = 0;
T * container[MAX_QUEUE_SIZE];
protected:
bool isEmpty() const {
return tail == head;
}
size_t size() const {
if (tail > head)
return tail - head;
return MAX_QUEUE_SIZE - head + tail;
}
T * pop(){
mutex.lock();
T * element = container[head];
@ -42,21 +53,18 @@ public:
}
void push(T * element){
mutex.lock();
if ((tail + 1) % MAX_QUEUE_SIZE != head){
container[tail] = element;
tail = (tail + 1) % MAX_QUEUE_SIZE;
} else
std::cout << "Queue is full!\n";
container[tail] = element;
tail = (tail + 1) % MAX_QUEUE_SIZE;
mutex.unlock();
}
};
template <typename T>
class EventQueue : private Queue<T>{
std::set<Listener<T> *> listeners;
template <typename EventType>
class EventQueue : protected Queue<EventType>{
std::set<Listener<EventType> *> listeners;
void update() override {
T * event = this->pop();
EventType * event = this->pop();
for (auto listener : listeners)
listener->accept(event);
@ -64,13 +72,13 @@ class EventQueue : private Queue<T>{
delete event;
}
public:
void submitEvent(T * event){
void submitEvent(EventType * event){
this->push(event);
}
void registerListener(Listener<T> * listener){
void registerListener(Listener<EventType> * listener){
listeners.insert(listener);
}
void unregisterListener(Listener<T> * listener){
void unregisterListener(Listener<EventType> * listener){
listeners.erase(listener);
}
};

@ -8,7 +8,7 @@ int main(int argc, char * argv[]){
QApplication app(argc, argv);
Window w;
w.resize(WIDTH, HEIGHT);
w.resize(800, 400);
w.show();
return QApplication::exec();

Loading…
Cancel
Save