-
Notifications
You must be signed in to change notification settings - Fork 1
Project base and demonstration of some features of that base #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bdfb7d1
create basic project structure, model structure, add some graphics
jansenin 7985372
improve code style
jansenin 430c52f
Refactored Model
parfen01 c30018c
return to qt6
jansenin 60075a1
add test mob,tower,tower slot,projectile, add resources
jansenin 91c019c
improve code style
jansenin 9931902
Refactoring
jansenin ddc0bc5
supress warning
jansenin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,3 +50,7 @@ compile_commands.json | |
|
|
||
| # QtCreator local machine specific files for imported projects | ||
| *creator.user* | ||
|
|
||
|
|
||
|
|
||
| /.idea/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| cmake_minimum_required(VERSION 3.20) | ||
| project(Game) | ||
|
|
||
| set(CMAKE_CXX_STANDARD 17) | ||
| set(CMAKE_AUTOMOC ON) | ||
| set(CMAKE_AUTORCC ON) | ||
| set(CMAKE_AUTOUIC ON) | ||
|
|
||
| find_package(Qt6 COMPONENTS | ||
| Core | ||
| Gui | ||
| Widgets | ||
| REQUIRED) | ||
|
|
||
| set(RESOURCES | ||
| Resources/resources.qrc) | ||
|
|
||
| set(CMAKE_INCLUDE_CURRENT_DIR ON) | ||
|
|
||
| set(GAME_OBJECTS | ||
| GameObjects/BasicObjects/Interface/damageable.cpp | ||
| GameObjects/BasicObjects/Interface/entity.cpp | ||
| GameObjects/BasicObjects/Entities/Mobs/Basis/mob.cpp | ||
| GameObjects/BasicObjects/Entities/Mobs/test_mob.cpp | ||
| GameObjects/BasicObjects/Entities/Towers/tower.cpp | ||
| GameObjects/BasicObjects/Entities/Towers/TowerSlots/tower_slot.cpp | ||
| GameObjects/BasicObjects/Entities/Towers/TowerSlots/test_tower_slot.cpp | ||
| GameObjects/BasicObjects/Entities/Towers/test_tower.cpp | ||
| GameObjects/BasicObjects/Entities/Projectiles/autoguided_projectile.cpp | ||
| GameObjects/BasicObjects/Entities/Projectiles/test_projectile.cpp) | ||
|
|
||
| set(UTILITIES | ||
| Utilities/damage.cpp | ||
| Utilities/time.cpp | ||
| Utilities/vector_f.cpp | ||
| Utilities/Resources/pixmap_loader.cpp | ||
| Utilities/timer.cpp) | ||
|
|
||
| add_executable(Game | ||
| ${RESOURCES} | ||
| ${GAME_OBJECTS} | ||
| ${UTILITIES} | ||
| main.cpp | ||
| main_window.cpp | ||
| Controller/controller.cpp game_view.cpp game_view.h constants.cpp) | ||
|
|
||
| target_link_libraries(Game Qt::Core Qt::Gui Qt::Widgets) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| #include "Controller/controller.h" | ||
|
|
||
| #include <QPushButton> | ||
| #include <QGraphicsProxyWidget> | ||
| #include <QApplication> | ||
| #include <QTimer> | ||
|
|
||
| #include "GameObjects/BasicObjects/Entities/Mobs/test_mob.h" | ||
| #include "GameObjects/BasicObjects/Entities/Towers/TowerSlots/test_tower_slot.h" | ||
| #include "constants.h" | ||
|
|
||
| Controller* Controller::instance; | ||
|
|
||
| Controller::Controller() : | ||
| scene_(new QGraphicsScene(kSceneRect)), | ||
| view_(new GameView(scene_)), | ||
| tick_timer_(new QTimer(this)) { | ||
| SetupScene(); | ||
| LaunchTickTimer(); | ||
| } | ||
|
|
||
| GameView* Controller::GetView() const { | ||
| return view_; | ||
| } | ||
|
|
||
| void Controller::SetupScene() { | ||
| { // temporary code | ||
| QPushButton* close_button = new QPushButton(); | ||
| QGraphicsProxyWidget* close_button_proxy = scene_->addWidget(close_button); | ||
| close_button_proxy->setGeometry(QRectF( | ||
| scene_->sceneRect().topRight() - VectorF{100, 0}, | ||
| scene_->sceneRect().topRight() + VectorF{0, 100})); | ||
|
|
||
| close_button->setText("Close"); | ||
| QObject::connect(close_button, &QPushButton::clicked, &QApplication::exit); | ||
|
|
||
| Entity* entity = new TestMob(); | ||
| scene_->addItem(entity); | ||
| scene_->setFocusItem(entity); | ||
|
|
||
| TestTowerSlot* test_tower_slot = new TestTowerSlot(VectorF{400, 400}); | ||
| scene_->addItem(test_tower_slot); | ||
|
|
||
| QRectF sceneRect = scene_->sceneRect(); | ||
| qreal x = sceneRect.x(); | ||
| qreal y = sceneRect.y(); | ||
| qreal width = sceneRect.width(); | ||
| qreal height = sceneRect.height(); | ||
|
|
||
| scene_->addLine( | ||
| x + width / 2, | ||
| y, | ||
| x + width / 2, | ||
| y + height, | ||
| QPen(Qt::blue)); | ||
|
|
||
| scene_->addLine( | ||
| x, | ||
| y + 1, | ||
| x + width, | ||
| y + 1, | ||
| QPen(Qt::blue)); | ||
|
|
||
| scene_->addLine( | ||
| x, | ||
| y + height / 2, | ||
| x + width, | ||
| y + height / 2, | ||
| QPen(Qt::blue)); | ||
| } // temporary code end | ||
| } | ||
|
|
||
| void Controller::LaunchTickTimer() { | ||
| tick_timer_->setInterval(1000 / 30); | ||
|
jansenin marked this conversation as resolved.
|
||
| tick_timer_->start(); | ||
| connect(tick_timer_, &QTimer::timeout, this, &Controller::TickAllTickables); | ||
| } | ||
|
|
||
| Controller* Controller::Instance() { | ||
| if (instance == nullptr) { | ||
| instance = new Controller(); | ||
| } | ||
| return instance; | ||
| } | ||
|
|
||
| void Controller::TickAllTickables() { | ||
| for (QGraphicsItem* graphics_item : scene_->items()) { | ||
| if (Tickable* tickable = dynamic_cast<Tickable*>(graphics_item)) { | ||
| // TODO(jansenin): make time dependency(it | ||
| // could have been more than 1000/30 ms) | ||
| tickable->Tick(Time(1000 / 30)); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #pragma once | ||
|
|
||
| #include <QObject> | ||
| #include <QGraphicsScene> | ||
| #include <QGraphicsView> | ||
|
|
||
| #include "GameObjects/BasicObjects/Interface/entity.h" | ||
| #include "game_view.h" | ||
|
|
||
| class Controller : public QObject { | ||
|
jansenin marked this conversation as resolved.
|
||
| Q_OBJECT | ||
| public: | ||
| static Controller* Instance(); | ||
|
|
||
| [[nodiscard]] GameView* GetView() const; | ||
|
|
||
| public slots: | ||
| void TickAllTickables(); | ||
|
|
||
| private: | ||
| static Controller* instance; | ||
|
|
||
| Controller(); | ||
|
|
||
| void SetupScene(); | ||
| void LaunchTickTimer(); | ||
|
|
||
| QGraphicsScene* scene_; | ||
| GameView* view_; | ||
| QTimer* tick_timer_; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| #include "mob.h" | ||
|
|
||
| Mob::Mob(const VectorF& coordinates, QPixmap* pixmap, int health) | ||
| : Entity(coordinates, pixmap, health) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #pragma once | ||
|
|
||
| #include "GameObjects/BasicObjects/Interface/entity.h" | ||
|
|
||
| #include <QPixmap> | ||
|
|
||
| #include "Utilities/vector_f.h" | ||
|
|
||
| class Mob : public Entity { | ||
| public: | ||
| Mob(const VectorF& coordinates, QPixmap* pixmap, int health = 0); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| #include "test_mob.h" | ||
|
|
||
| #include <QGraphicsScene> | ||
|
|
||
| #include "Utilities/Resources/pixmap_loader.h" | ||
| #include "constants.h" | ||
|
|
||
| void TestMob::Tick(Time delta) { | ||
| MoveBy(VectorF{delta.seconds() * Entities::TestMob::kPassiveMoveSpeed, 0}); | ||
| } | ||
|
|
||
| void TestMob::keyPressEvent(QKeyEvent* event) { | ||
| if (health_ == 0) return; | ||
| QPointF velocity_vector = mapToParent(pos() + | ||
| VectorF{0, -Entities::TestMob::kActiveMoveSpeed}) - mapToParent(pos()); | ||
| if (event->key() == Qt::Key::Key_Left) { | ||
| setRotation(rotation() - Entities::TestMob::kRotationSpeed); | ||
| } else if (event->key() == Qt::Key::Key_Right) { | ||
| setRotation(rotation() + Entities::TestMob::kRotationSpeed); | ||
| } else if (event->key() == Qt::Key::Key_Up) { | ||
| setPos(pos() + velocity_vector); | ||
| } else if (event->key() == Qt::Key::Key_Down) { | ||
| setPos(pos() - velocity_vector); | ||
| } | ||
| } | ||
|
jansenin marked this conversation as resolved.
|
||
|
|
||
| void TestMob::mousePressEvent(QGraphicsSceneMouseEvent* event) { | ||
| scene()->addItem(new TestMob(pos() + VectorF{10, 30})); | ||
| } | ||
|
|
||
| TestMob::TestMob(const VectorF& coordinates) | ||
| : Mob( | ||
| coordinates, | ||
| PixmapLoader::Pixmaps::kTestMob, | ||
| Entities::TestMob::kHealth) { | ||
| setFlag(QGraphicsItem::ItemIsFocusable, true); | ||
| } | ||
| void TestMob::paint(QPainter* painter, | ||
| const QStyleOptionGraphicsItem* option, | ||
| QWidget* widget) { | ||
| Entity::paint(painter, option, widget); | ||
| if (health_ == 0) { | ||
| painter->drawLine(-50, -50, 50, 50); | ||
| painter->drawLine(50, -50, -50, 50); | ||
|
Wind-Eagle marked this conversation as resolved.
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #pragma once | ||
|
|
||
| #include <QPixmap> | ||
|
|
||
| #include "Basis/mob.h" | ||
| #include "Utilities/vector_f.h" | ||
|
|
||
| class TestMob : public Mob { | ||
| public: | ||
| explicit TestMob(const VectorF& coordinates = VectorF{0, 0}); | ||
|
|
||
| void Tick(Time delta) override; | ||
|
|
||
| void paint(QPainter* painter, | ||
| const QStyleOptionGraphicsItem* option, | ||
| QWidget* widget) override; | ||
|
|
||
| protected: | ||
| void keyPressEvent(QKeyEvent* event) override; | ||
| void mousePressEvent(QGraphicsSceneMouseEvent* event) override; | ||
| }; |
29 changes: 29 additions & 0 deletions
29
GameObjects/BasicObjects/Entities/Projectiles/autoguided_projectile.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #include <QGraphicsScene> | ||
|
|
||
| #include "autoguided_projectile.h" | ||
|
|
||
| AutoguidedProjectile::AutoguidedProjectile( | ||
| const VectorF& coordinates, | ||
| QPixmap* pixmap, | ||
| Entity* target, | ||
| qreal speed, | ||
| Damage damage) | ||
| : Entity(coordinates, pixmap), | ||
| target_(target), speed_(speed), damage_(damage) {} | ||
|
|
||
| void AutoguidedProjectile::Tick(Time delta) { | ||
| Move(delta); | ||
|
|
||
| if (target_->collidesWithItem(this)) { | ||
| target_->ApplyDamage(damage_); | ||
| deleteLater(); | ||
| } | ||
| } | ||
|
jansenin marked this conversation as resolved.
|
||
|
|
||
| void AutoguidedProjectile::Move(Time delta) { | ||
| VectorF target_point = target_->scenePos(); | ||
| VectorF delta_pos = target_point - scenePos(); | ||
| VectorF velocity = delta_pos.normalized() * speed_; | ||
|
|
||
| MoveBy(velocity * delta.seconds()); | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
GameObjects/BasicObjects/Entities/Projectiles/autoguided_projectile.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #pragma once | ||
|
|
||
| #include "GameObjects/BasicObjects/Interface/entity.h" | ||
|
|
||
| class AutoguidedProjectile : public Entity { | ||
| public: | ||
| AutoguidedProjectile(const VectorF& coordinates, | ||
| QPixmap* pixmap, | ||
| Entity* target, qreal speed, Damage damage); | ||
|
|
||
| void Tick(Time delta) override; | ||
|
|
||
| protected: | ||
| void Move(Time delta); | ||
|
|
||
| Entity* target_; | ||
| qreal speed_; | ||
| Damage damage_; | ||
| }; |
16 changes: 16 additions & 0 deletions
16
GameObjects/BasicObjects/Entities/Projectiles/test_projectile.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #include "test_projectile.h" | ||
|
|
||
| #include "Utilities/Resources/pixmap_loader.h" | ||
| #include "constants.h" | ||
|
|
||
| TestProjectile::TestProjectile(const VectorF& coordinates, Entity* target) | ||
| : AutoguidedProjectile( | ||
| coordinates, | ||
| PixmapLoader::Pixmaps::kTestBullet, | ||
| target, | ||
| Entities::TestProjectile::kSpeed, | ||
| Entities::TestProjectile::kDamage) {} | ||
|
|
||
| void TestProjectile::Tick(Time delta) { | ||
| AutoguidedProjectile::Tick(delta); | ||
| } |
10 changes: 10 additions & 0 deletions
10
GameObjects/BasicObjects/Entities/Projectiles/test_projectile.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #pragma once | ||
|
|
||
| #include "autoguided_projectile.h" | ||
|
|
||
| class TestProjectile : public AutoguidedProjectile { | ||
| public: | ||
| TestProjectile(const VectorF& coordinates, Entity* target); | ||
|
|
||
| void Tick(Time delta) override; | ||
| }; |
29 changes: 29 additions & 0 deletions
29
GameObjects/BasicObjects/Entities/Towers/TowerSlots/test_tower_slot.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| #include "test_tower_slot.h" | ||
|
|
||
| #include <QGraphicsScene> | ||
|
|
||
| #include "GameObjects/BasicObjects/Entities/Towers/test_tower.h" | ||
| #include <Utilities/Resources/pixmap_loader.h> | ||
|
|
||
| TestTowerSlot::TestTowerSlot(const VectorF& coordinates) : TowerSlot( | ||
| coordinates, PixmapLoader::Pixmaps::kTestTowerSlot) {} | ||
|
|
||
| void TestTowerSlot::mousePressEvent(QGraphicsSceneMouseEvent* event) { | ||
| if (event->button() != Qt::MouseButton::LeftButton) { | ||
| return TowerSlot::mousePressEvent(event); | ||
| } | ||
| if (!IsTakenUp()) { | ||
| TestTower* tower = new TestTower(scenePos()); | ||
| scene()->addItem(tower); | ||
| TakeUpArea(tower); | ||
| } | ||
| QGraphicsItem::mousePressEvent(event); | ||
| } | ||
|
|
||
| void TestTowerSlot::paint( | ||
| QPainter* painter, | ||
| const QStyleOptionGraphicsItem* option, | ||
| QWidget* widget) { | ||
| if (IsTakenUp()) return; | ||
| Entity::paint(painter, option, widget); | ||
| } |
16 changes: 16 additions & 0 deletions
16
GameObjects/BasicObjects/Entities/Towers/TowerSlots/test_tower_slot.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| #pragma once | ||
|
|
||
| #include "tower_slot.h" | ||
|
|
||
| class TestTowerSlot : public TowerSlot { | ||
| public: | ||
| explicit TestTowerSlot(const VectorF& coordinates); | ||
|
|
||
| protected: | ||
| void mousePressEvent(QGraphicsSceneMouseEvent* event) override; | ||
|
|
||
| public: | ||
| void paint(QPainter* painter, | ||
| const QStyleOptionGraphicsItem* option, | ||
| QWidget* widget) override; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.