added file write

main
Benjamin Kraft 2 years ago
parent 20e5edb82c
commit f03e03101b
  1. 1
      src/Audio.cpp
  2. 1
      src/Event.h
  3. 12
      src/FileManager.cpp
  4. 4
      src/FileManager.h
  5. 2
      src/Game.cpp
  6. 23
      src/Logger.cpp
  7. 10
      src/Logger.h
  8. 1
      src/Player.cpp

@ -5,7 +5,6 @@
void Audio::update() {
auto cmd = pop();
std::cout << "Playing sound: " + std::to_string(cmd->soundId) << std::endl;
// use volume, soundId to play sound
delete cmd;

@ -58,6 +58,7 @@ struct WallJumpEvent : GameEvent {
};
struct WallCrashEvent : GameEvent {
double wallHeight = 0;
Player * player = nullptr;
};

@ -1,5 +1,6 @@
#include "FileManager.h"
#include <iostream>
#include <fstream>
void FileManager::update() {
auto cmd = pop();
@ -14,17 +15,24 @@ void FileManager::readFile(const std::string &path, const std::function<void(std
push(cmd);
}
void FileManager::writeFile(const std::string &path, const std::string &content) {
void FileManager::writeFile(const std::string &path, const std::string &content, bool append) {
auto cmd = new FileWriteCommand;
cmd->path = path;
cmd->content = content;
cmd->append = append;
push(cmd);
}
FileManager * FileManager::instance = new FileManager;
void FileReadCommand::execute() {
}
void FileWriteCommand::execute() {
std::ofstream out;
auto mode = append ? std::ios_base::app : std::ios_base::out;
out.open(path, mode);
out << content;
out.close();
}

@ -14,12 +14,14 @@ struct FileReadCommand : FileCommand {
struct FileWriteCommand : FileCommand {
std::string content;
bool append;
void execute() override;
};
class FileManager : private Queue<FileCommand>{
void update() override;
public:
static FileManager * instance;
void readFile(const std::string &path, const std::function<void(std::string)> &readCallback);
void writeFile(const std::string &path, const std::string &content);
void writeFile(const std::string &path, const std::string &content, bool append=false);
};

@ -2,6 +2,7 @@
#include "Game.h"
#include "InputWindow.h"
#include "Window.h"
#include "Logger.h"
#include <random>
void Game::draw(QPixmap &output) {
@ -64,6 +65,7 @@ Game::Game() {
for (auto & spec : spectators)
Game::eventQueue->registerListener(&spec);
InputWindow::inputQueue->registerListener(&player);
Game::eventQueue->registerListener(new Logger);
instance = this;
}

@ -0,0 +1,23 @@
#include "Logger.h"
#include "FileManager.h"
#define MAX_STRING 100
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);
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);
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);
FileManager::instance->writeFile("log.txt", buffer, true);
}

@ -0,0 +1,10 @@
#pragma once
#include "Listener.h"
class Logger : public GameListener{
protected:
void OnCoinCollected(CoinCollectEvent * event) override;
void OnWallJumped(WallJumpEvent * event) override;
void OnWallCrashed(WallCrashEvent * event) override;
};

@ -41,6 +41,7 @@ void Player::update(float dTime, std::vector<Wall> &walls, std::vector<Coin>& co
wall.failed = true;
auto e = new WallCrashEvent;
e->time = Game::instance->eTime;
e->wallHeight = wall.size.height();
e->player = this;
Game::eventQueue->submitEvent(e);
}

Loading…
Cancel
Save