1 Star 0 Fork 0

pendulum445/AircraftWar-Cpp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
aircraft.h 4.86 KB
一键复制 编辑 原始数据 按行查看 历史
pendulum445 提交于 2024-05-21 15:33 . [feture]新增火力道具
//
// Created by liaoyunjie on 24-5-14.
//
#ifndef AIRCRAFT_H
#define AIRCRAFT_H
#include <QPoint>
#include <list>
#include <mutex>
#include "BaseFlyingObject.h"
#include "Bullet.h"
#include "ImageManager.h"
#include "RandomGenerator.h"
#include "ShootStrategy.h"
#include "prop.h"
class BaseAircraft : public BaseFlyingObject {
protected:
BaseAircraft(FlyingObjectType type, int location_x, int location_y,
int speed_x, int speed_y, int width, int height,
const std::shared_ptr<BaseShootStrategy>& shoot_strategy, int hp,
int direction, int shoot_num, int bullet_power, int shoot_cycle)
: BaseFlyingObject(type, location_x, location_y, speed_x, speed_y, width,
height),
shoot_strategy_(shoot_strategy),
hp_(hp),
direction_(direction),
shoot_num_(shoot_num),
bullet_power_(bullet_power),
shoot_cycle_(shoot_cycle) {}
public:
int factor() const override { return 2; }
int hp() const { return hp_; }
int direction() const { return direction_; }
int shoot_num() const { return shoot_num_; }
int bullet_power() const { return bullet_power_; }
int shoot_cycle() const { return shoot_cycle_; }
auto shoot_strategy() const -> std::shared_ptr<BaseShootStrategy> {
return shoot_strategy_;
}
void decrease_hp(int damage) {
hp_ -= damage;
if (hp_ <= 0) {
vanish();
}
}
void set_shoot_num(int shoot_num) { shoot_num_ = shoot_num; }
void set_shoot_cycle(int shoot_cycle) { shoot_cycle_ = shoot_cycle; }
auto shoot() -> std::list<std::shared_ptr<Bullet>>;
protected:
int hp_;
private:
std::shared_ptr<BaseShootStrategy> shoot_strategy_;
int direction_;
int shoot_num_;
int bullet_power_;
int shoot_cycle_;
int shoot_timer_ = 0;
};
class HeroAircraft final : public BaseAircraft {
public:
static auto instance() -> std::shared_ptr<HeroAircraft>;
HeroAircraft(const HeroAircraft&) = delete;
HeroAircraft& operator=(const HeroAircraft&) = delete;
void forward() override {} // 英雄机靠鼠标控制,forward重写为空
void set_location(const QPoint& pos) {
set_location_x(pos.x());
set_location_y(pos.y());
}
void increase_hp(int hp) {
hp_ += hp;
if (hp_ > HERO_MAX_HP) {
hp_ = HERO_MAX_HP;
}
}
private:
HeroAircraft()
: BaseAircraft(HERO_AIRCRAFT, WINDOW_WIDTH / 2, WINDOW_HEIGHT - 50, 0, 0,
ImageManager::HERO_IMAGE.width(),
ImageManager::HERO_IMAGE.height(),
std::make_shared<DirectShootStrategy>(), HERO_MAX_HP, -1,
1, HERO_BULLET_POWER, HERO_SHOOT_CYCLE) {}
static std::shared_ptr<HeroAircraft> instance_;
static std::once_flag instance_flag_;
};
class EnemyAircraft : public BaseAircraft {
protected:
EnemyAircraft(FlyingObjectType type, int speed_x, int speed_y,
const std::shared_ptr<BaseShootStrategy>& shoot_strategy,
int hp, int direction, int shoot_num, int bullet_power,
int shoot_cycle, int score, int prop_num)
: BaseAircraft(type,
static_cast<int>(RandomGenerator::getRandom0To1() *
(WINDOW_WIDTH -
ImageManager::IMAGE_MAP[type].width())),
WINDOW_HEIGHT * 0.05, speed_x, speed_y,
ImageManager::IMAGE_MAP[type].width(),
ImageManager::IMAGE_MAP[type].height(), shoot_strategy, hp,
direction, shoot_num, bullet_power, shoot_cycle),
score_(score),
prop_num_(prop_num) {}
virtual auto createSingleProp() -> std::shared_ptr<BaseProp> = 0;
public:
auto createProps() -> std::list<std::shared_ptr<BaseProp>>;
void forward() override {
BaseAircraft::forward();
if (location_y() >= WINDOW_HEIGHT) {
vanish();
}
}
int score() const { return score_; }
int prop_num() const { return prop_num_; }
private:
int score_;
int prop_num_;
};
class MobEnemyAircraft final : public EnemyAircraft {
public:
MobEnemyAircraft()
: EnemyAircraft(MOB_ENEMY_AIRCRAFT, 0, MOB_ENEMY_SPEEDY,
std::make_shared<BaseShootStrategy>(), MOB_ENEMY_MAX_HP,
ENEMY_DIRECTION, 1, 0, MOB_ENEMY_SCORE, 100, 0) {}
auto createSingleProp() -> std::shared_ptr<BaseProp> override {
return nullptr;
}
};
class EliteEnemyAircraft final : public EnemyAircraft {
public:
EliteEnemyAircraft()
: EnemyAircraft(
ELITE_ENEMY_AIRCRAFT,
RandomGenerator::getRandom0To1() < 0.5 ? 0 : 1, ELITE_ENEMY_SPPEEDY,
std::make_shared<DirectShootStrategy>(), ELITE_ENEMY_MAX_HP,
ENEMY_DIRECTION, 1, ELITE_ENEMY_BULLET_POWER, ELITE_SHOOT_CYCLE,
ELITE_ENEMY_SCORE, 1) {}
auto createSingleProp() -> std::shared_ptr<BaseProp> override;
};
#endif // AIRCRAFT_H
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yunjie445/aircraft-war-cpp.git
git@gitee.com:yunjie445/aircraft-war-cpp.git
yunjie445
aircraft-war-cpp
AircraftWar-Cpp
main

搜索帮助