Compare commits
6 Commits
cc27b166e1
...
89598f3cc8
Author | SHA1 | Date |
---|---|---|
Benjamin Kraft | 89598f3cc8 | 2 years ago |
Benjamin Kraft | 75af403e27 | 2 years ago |
Benjamin Kraft | f03e03101b | 2 years ago |
Benjamin Kraft | 20e5edb82c | 2 years ago |
Benjamin Kraft | 4a98d250d7 | 2 years ago |
Benjamin Kraft | 4d448de222 | 2 years ago |
20 changed files with 266 additions and 20 deletions
@ -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); |
||||||
|
}; |
@ -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; |
||||||
|
}; |
@ -0,0 +1,26 @@ |
|||||||
|
#include "RecordTracker.h" |
||||||
|
#include "FileManager.h" |
||||||
|
|
||||||
|
void RecordTracker::OnWallJumped(WallJumpEvent *event) { |
||||||
|
jumped++; |
||||||
|
} |
||||||
|
|
||||||
|
void RecordTracker::OnWallCrashed(WallCrashEvent *event) { |
||||||
|
checkJumpRecord(); |
||||||
|
jumped = 0; |
||||||
|
} |
||||||
|
|
||||||
|
void RecordTracker::checkJumpRecord() const { |
||||||
|
const uint64_t newRecord = jumped; |
||||||
|
FileManager::instance->readFile("record_jump.txt", [newRecord](const std::string& content){ |
||||||
|
if (content.empty()){ |
||||||
|
FileManager::instance->writeFile("record_jump.txt", std::to_string(newRecord)); |
||||||
|
return; |
||||||
|
} |
||||||
|
uint64_t oldRecord = std::stoull(content); |
||||||
|
if (newRecord > oldRecord){ |
||||||
|
std::cout << "New jump record!" << std::endl; |
||||||
|
FileManager::instance->writeFile("record_jump.txt", std::to_string(newRecord)); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
@ -0,0 +1,11 @@ |
|||||||
|
#pragma once |
||||||
|
|
||||||
|
#include "Listener.h" |
||||||
|
|
||||||
|
class RecordTracker : public GameListener { |
||||||
|
uint64_t jumped = 0; |
||||||
|
void checkJumpRecord() const; |
||||||
|
protected: |
||||||
|
void OnWallJumped(WallJumpEvent * event) override; |
||||||
|
void OnWallCrashed(WallCrashEvent * event) override; |
||||||
|
}; |
Loading…
Reference in new issue