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
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ set(UTILITIES
Utilities/timer.cpp
Utilities/route.cpp
Utilities/wave.cpp
wave_manager.cpp)
wave_manager.cpp
Utilities/animation.cpp)

add_executable(Game
${RESOURCES}
Expand All @@ -49,6 +50,7 @@ add_executable(Game
main_window.cpp
Controller/controller.cpp
game_view.cpp
constants.cpp game_scene.cpp game_scene.h)
constants.cpp
game_scene.cpp)

target_link_libraries(Game Qt::Core Qt::Gui Qt::Widgets)
19 changes: 13 additions & 6 deletions GameObjects/BasicObjects/Entities/Mobs/Basis/mob.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
#include "mob.h"
#include <utility>

#include <vector>

std::vector<Route>
routes{Route({QPointF(50, 50), QPointF(150, 300),
QPointF(-100, -50)})}; // TODO(parfen01): move in level
std::vector<Route> routes{Route({
QPointF(50, 50),
QPointF(300, 400),
QPointF(-100, -50)})}; // TODO(parfen01): move in level

Mob::Mob(const VectorF& coordinates,
QPixmap* pixmap,
Animation* animation,
int health,
qreal speed)
: Entity(coordinates, pixmap, health),
: Entity(coordinates, animation, health),
speed_(speed) {
route_ = &routes[0];
route_->AddEntity(this);
Entity::setPos(route_->GetStart());
}

Mob::Mob(const VectorF& coordinates,
QPixmap* pixmap,
int health,
qreal speed)
: Mob(coordinates, new Animation(pixmap), health, speed) {}

qreal Mob::GetSpeed() const {
return speed_;
}
Expand Down
6 changes: 6 additions & 0 deletions GameObjects/BasicObjects/Entities/Mobs/Basis/mob.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class Mob : public Entity {
QPixmap* pixmap,
int health,
qreal speed = 0);

Mob(const VectorF& coordinates,
Animation* animation,
int health,
qreal speed = 0);

[[nodiscard]] qreal GetSpeed() const;
void SetSpeed(qreal speed);

Expand Down
59 changes: 45 additions & 14 deletions GameObjects/BasicObjects/Entities/Mobs/test_mob.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
#include "test_mob.h"

#include <QGraphicsScene>

#include "Utilities/Resources/pixmap_loader.h"
#include "constants.h"

void TestMob::Tick(Time delta) {
if (!Mob::route_->isEnd(this)) {
Mob::Tick(delta);
if (is_creating_ && animation_->WasEndedDuringPreviousUpdate()) {
is_creating_ = false;
animation_ = idle_animation_;
}
if (is_destroying_ && animation_->WasEndedDuringPreviousUpdate()) {
animation_->SetIndex(animation_->FrameCount() - 1);
deleteLater();
}

if (!is_creating_ && !Mob::route_->isEnd(this)) {
Mob::route_->Move(this, Mob::speed_ * delta.seconds());
}
}
Expand All @@ -23,26 +31,49 @@ void TestMob::keyPressEvent(QKeyEvent* event) {
setPos(pos() + velocity_vector);
} else if (event->key() == Qt::Key::Key_Down) {
setPos(pos() - velocity_vector);
} else if (event->key() == Qt::Key::Key_Space && !is_destroying_) {
health_ = 0;
is_destroying_ = true;
disappearing_animation_->Reset();
animation_ = disappearing_animation_;
}
}

void TestMob::mousePressEvent(QGraphicsSceneMouseEvent* event) {
scene()->addItem(new TestMob(pos() + VectorF{10, 30}));
scene()->addItem(new TestMob(pos() + VectorF{100, 100}));
}

TestMob::TestMob(const VectorF& coordinates)
: Mob(
coordinates,
PixmapLoader::Pixmaps::kTestMob,
Entities::TestMob::kHealth) {
coordinates,
// TODO(jansenin): Было бы лучше инициализировать анимации отдельно
// от моба
new Animation(PixmapLoader::Pixmaps::kFireTotemAppearing, 50_ms),
Entities::TestMob::kHealth,
100),
is_destroying_(false),
idle_animation_(
new Animation(PixmapLoader::Pixmaps::kFireTotemIdle, 50_ms)),
disappearing_animation_(
new Animation(PixmapLoader::Pixmaps::kFireTotemDisappear, 50_ms)),
appearing_animation_(animation_),
is_creating_(true) {
setFlag(QGraphicsItem::ItemIsFocusable, true);
setScale(1.4);
}
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);

TestMob::~TestMob() {
delete idle_animation_;
delete disappearing_animation_;
delete appearing_animation_;
}

void TestMob::ApplyDamage(Damage damage) {
Damageable::ApplyDamage(damage);

if (health_ <= 0 && !is_destroying_) {
disappearing_animation_->Reset();
is_destroying_ = true;
animation_ = disappearing_animation_;
}
}
11 changes: 8 additions & 3 deletions GameObjects/BasicObjects/Entities/Mobs/test_mob.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,17 @@ class TestMob : public Mob {
explicit TestMob(const VectorF& coordinates = VectorF{0, 0});

void Tick(Time delta) override;
void ApplyDamage(Damage damage) override;

void paint(QPainter* painter,
const QStyleOptionGraphicsItem* option,
QWidget* widget) override;
~TestMob() override;

protected:
void keyPressEvent(QKeyEvent* event) override;
void mousePressEvent(QGraphicsSceneMouseEvent* event) override;

bool is_destroying_;
bool is_creating_;
Animation* idle_animation_;
Animation* disappearing_animation_;
Animation* appearing_animation_;
};
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
#include <QGraphicsScene>

#include "autoguided_projectile.h"
#include "GameObjects/BasicObjects/Entities/Mobs/Basis/mob.h"

AutoguidedProjectile::AutoguidedProjectile(
const VectorF& coordinates,
Animation* animation,
Entity* target,
qreal speed,
Damage damage)
: Projectile(coordinates, animation),
target_(target), speed_(speed), damage_(damage) {
connect(target_, &Entity::destroyed, this,
&AutoguidedProjectile::FindNewTargetOrDie);
}


AutoguidedProjectile::AutoguidedProjectile(
const VectorF& coordinates,
QPixmap* pixmap,
Entity* target,
qreal speed,
Damage damage)
: Projectile(coordinates, pixmap),
target_(target), speed_(speed), damage_(damage) {}
: AutoguidedProjectile(
coordinates, new Animation(pixmap), target, speed, damage) {}

void AutoguidedProjectile::Tick(Time delta) {
Projectile::Tick(delta);
if (target_ == nullptr) {
return;
}
if (target_->GetHealth() <= 0) {
FindNewTargetOrDie();
}
if (target_ == nullptr) {
return;
}

Move(delta);

if (target_->collidesWithItem(this)) {
Expand All @@ -21,9 +46,49 @@ void AutoguidedProjectile::Tick(Time delta) {
}

void AutoguidedProjectile::Move(Time delta) {
if (target_ == nullptr) {
return;
}
VectorF target_point = target_->scenePos();
VectorF delta_pos = target_point - scenePos();
VectorF velocity = delta_pos.normalized() * speed_;

MoveBy(velocity * delta.seconds());
}

void AutoguidedProjectile::FindNewTargetOrDie() {
if (scene()->Mobs().empty()) {
SetTarget(nullptr);
deleteLater();
} else {
auto old_target = target_;
for (auto new_target : scene()->Mobs()) {
if (new_target != target_ &&
VectorF(new_target->scenePos() - scenePos()).length() < 100) {
SetTarget(new_target);
break;
}
}
if (old_target == target_) {
SetTarget(nullptr);
deleteLater();
}
}
}

void AutoguidedProjectile::SetTarget(Entity* target) {
// TODO(jansenin): what if target_ is destroyed?
disconnect(
target_,
&Entity::destroyed,
this,
&AutoguidedProjectile::FindNewTargetOrDie);
target_ = target;
if (target_ != nullptr) {
connect(
target_,
&Entity::destroyed,
this,
&AutoguidedProjectile::FindNewTargetOrDie);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,16 @@ class AutoguidedProjectile : public Projectile {
QPixmap* pixmap,
Entity* target, qreal speed, Damage damage);

AutoguidedProjectile(const VectorF& coordinates,
Animation* animation,
Entity* target, qreal speed, Damage damage);
void SetTarget(Entity* target);

void Tick(Time delta) override;

protected:
void Move(Time delta);
void FindNewTargetOrDie();

Entity* target_;
qreal speed_;
Expand Down
5 changes: 4 additions & 1 deletion GameObjects/BasicObjects/Entities/Projectiles/projectile.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "projectile.h"

Projectile::Projectile(const VectorF& coordinates, QPixmap* pixmap)
: Entity(coordinates, pixmap) {}
: Projectile(coordinates, new Animation(pixmap)) {}

Projectile::Projectile(const VectorF& coordinates, Animation* animation)
: Entity(coordinates, animation) {}
1 change: 1 addition & 0 deletions GameObjects/BasicObjects/Entities/Projectiles/projectile.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Projectile : public Entity {
public:
Projectile(const VectorF& coordinates, QPixmap* pixmap);
Projectile(const VectorF& coordinates, Animation* animation);
};


Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "test_tower_slot.h"

#include <QGraphicsScene>

#include "GameObjects/BasicObjects/Entities/Towers/test_tower.h"
#include <Utilities/Resources/pixmap_loader.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ void TowerSlot::ClearArea() {
}

TowerSlot::TowerSlot(const VectorF& coordinates, QPixmap* pixmap)
: Entity(coordinates, pixmap), tower_(nullptr) {}
: TowerSlot(coordinates, new Animation(pixmap)) {}

TowerSlot::TowerSlot(const VectorF& coordinates, Animation* animation)
: Entity(coordinates, animation), tower_(nullptr) {}

void TowerSlot::Tick(Time time) {
Entity::Tick(time);

if (tower_ != nullptr) {
tower_->Tick(time);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
class TowerSlot : public Entity {
public:
TowerSlot(const VectorF& coordinates, QPixmap* pixmap);
TowerSlot(const VectorF& coordinates, Animation* animation);

[[nodiscard]] bool IsTakenUp() const;
void TakeUpArea(Tower* tower);
void ClearArea();
Expand Down
6 changes: 5 additions & 1 deletion GameObjects/BasicObjects/Entities/Towers/test_tower.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "test_tower.h"

#include <QGraphicsScene>
#include <QtMath>

#include "GameObjects/BasicObjects/Entities/Projectiles/test_projectile.h"
Expand Down Expand Up @@ -30,13 +29,18 @@ TestTower::TestTower(const VectorF& coordinates)
}

void TestTower::Tick(Time delta) {
Tower::Tick(delta);

attack_timer_.Tick(delta);

if (attack_timer_.IsExpired()) {
QList<QGraphicsItem*> items_in_attack_area =
scene()->items(scene_attack_area_);
for (QGraphicsItem* item : items_in_attack_area) {
if (Mob* mob = dynamic_cast<Mob*>(item)) {
if (mob->GetHealth() <= 0) {
continue;
}
scene()->addItem(new TestProjectile(scenePos(), mob));
attack_timer_.Start(Entities::TestTower::kAttackCooldown);
break;
Expand Down
5 changes: 4 additions & 1 deletion GameObjects/BasicObjects/Entities/Towers/tower.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#include "tower.h"

Tower::Tower(const VectorF& coordinates, QPixmap* pixmap, int health)
: Entity(coordinates, pixmap, health) {}
: Tower(coordinates, new Animation(pixmap), health) {}

Tower::Tower(const VectorF& coordinates, Animation* animation, int health)
: Entity(coordinates, animation, health) {}
1 change: 1 addition & 0 deletions GameObjects/BasicObjects/Entities/Towers/tower.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
class Tower : public Entity {
public:
Tower(const VectorF& coordinates, QPixmap* pixmap, int health = 0);
Tower(const VectorF& coordinates, Animation* animation, int health = 0);
};
4 changes: 4 additions & 0 deletions GameObjects/BasicObjects/Interface/damageable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ void Damageable::ApplyDamage(Damage damage) {
void Damageable::SetHealth(int health) {
health_ = health;
}

int Damageable::GetHealth() const {
return health_;
}
1 change: 1 addition & 0 deletions GameObjects/BasicObjects/Interface/damageable.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Damageable {
public:
explicit Damageable(int health);
virtual void ApplyDamage(Damage damage);
[[nodiscard]] int GetHealth() const;

protected:
int health_;
Expand Down
Loading