Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,7 @@ compile_commands.json

# QtCreator local machine specific files for imported projects
*creator.user*



/.idea/
47 changes: 47 additions & 0 deletions CMakeLists.txt
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)
94 changes: 94 additions & 0 deletions Controller/controller.cpp
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});
Comment thread
Wind-Eagle marked this conversation as resolved.
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);
Comment thread
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));
}
}
}
31 changes: 31 additions & 0 deletions Controller/controller.h
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 {
Comment thread
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_;
};
4 changes: 4 additions & 0 deletions GameObjects/BasicObjects/Entities/Mobs/Basis/mob.cpp
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) {}
12 changes: 12 additions & 0 deletions GameObjects/BasicObjects/Entities/Mobs/Basis/mob.h
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);
};
46 changes: 46 additions & 0 deletions GameObjects/BasicObjects/Entities/Mobs/test_mob.cpp
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);
}
}
Comment thread
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);
Comment thread
Wind-Eagle marked this conversation as resolved.
}
}
21 changes: 21 additions & 0 deletions GameObjects/BasicObjects/Entities/Mobs/test_mob.h
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;
};
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();
}
}
Comment thread
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());
}
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 GameObjects/BasicObjects/Entities/Projectiles/test_projectile.cpp
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 GameObjects/BasicObjects/Entities/Projectiles/test_projectile.h
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;
};
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);
}
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;
};
Loading