Skip to content

Commit 7a2b0b4

Browse files
committed
去来江口守空船,绕船月明江水寒
增加一个中间类,将共同代码合并
1 parent 7685bf6 commit 7a2b0b4

8 files changed

Lines changed: 175 additions & 270 deletions

src/BattleSceneAct.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "BattleSceneAct.h"
2+
3+
void BattleSceneAct::setID(int id)
4+
{
5+
battle_id_ = id;
6+
info_ = BattleMap::getInstance()->getBattleInfo(id);
7+
8+
BattleMap::getInstance()->copyLayerData(info_->BattleFieldID, 0, &earth_layer_);
9+
BattleMap::getInstance()->copyLayerData(info_->BattleFieldID, 1, &building_layer_);
10+
}

src/BattleSceneAct.h

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#pragma once
2+
#include <deque>
3+
4+
#include "BattleScene.h"
5+
#include "Font.h"
6+
#include "TextureManager.h"
7+
#include "UIKeyConfig.h"
8+
9+
class BattleSceneAct : public BattleScene
10+
{
11+
public:
12+
struct AttackEffect
13+
{
14+
Pointf Pos;
15+
Pointf Velocity, Acceleration;
16+
Role* Attacker = nullptr; //攻击者
17+
std::map<Role*, int> Defender; //每人只能被一个特效击中一次
18+
Magic* UsingMagic = nullptr;
19+
Item* UsingHiddenWeapon = nullptr;
20+
int Frame = 0; //当前帧数
21+
int TotalFrame = 1; //总帧数,当前帧数超过此值就移除此效果
22+
int TotalEffectFrame = 1; //效果总帧数
23+
int OperationType = -1; //攻击类型
24+
std::string Path; //效果贴图路径
25+
Role* FollowRole = nullptr; //一直保持在角色身上
26+
int Weaken = 0; //弱化程度,减掉
27+
double Strengthen = 1; //强化程度,相乘
28+
int Track = 0; //是否追踪
29+
int Through = 0; //是否贯穿,即击中敌人后可以不消失
30+
int NoHurt = 0; //是否无伤害
31+
32+
void setEft(int num)
33+
{
34+
setPath(fmt1::format("eft/eft{:03}", num));
35+
}
36+
37+
void setPath(const std::string& p)
38+
{
39+
Path = p;
40+
TotalEffectFrame = TextureManager::getInstance()->getTextureGroupCount(Path);
41+
}
42+
};
43+
44+
struct TextEffect
45+
{
46+
Pointf Pos;
47+
std::string Text;
48+
int Size = 15;
49+
int Frame = 0;
50+
BP_Color Color;
51+
int Type = 0; //0-缓缓向上, 1-原地不动
52+
53+
void set(const std::string& text, BP_Color c, Role* r)
54+
{
55+
Text = text;
56+
Color = c;
57+
if (r)
58+
{
59+
Pos = r->Pos;
60+
Pos.x -= 7.5 * Font::getTextDrawSize(Text);
61+
Pos.y -= 50;
62+
}
63+
}
64+
};
65+
66+
Pointf pos_; //坐标为俯视,而非在画面的位置,其中y需除以2画在上面
67+
double gravity_ = -4;
68+
double friction_ = 0.1;
69+
70+
UIKeyConfig::Keys keys_;
71+
72+
std::deque<AttackEffect> attack_effects_;
73+
std::deque<TextEffect> text_effects_;
74+
75+
std::deque<Role*> enemies_;
76+
std::deque<Role> enemies_obj_;
77+
78+
std::vector<std::shared_ptr<Head>> heads_;
79+
std::vector<std::shared_ptr<Head>> head_boss_;
80+
81+
bool is_running_ = false; //主角是否在跑动
82+
Role* role_ = nullptr; //主角
83+
Role* dying_ = nullptr;
84+
int weapon_ = 1;
85+
int frozen_ = 0;
86+
int slow_ = 0;
87+
int shake_ = 0;
88+
int close_up_ = 0;
89+
90+
std::unordered_map<std::string, std::function<void(Role* r)>> special_magic_effect_every_frame_; //每帧
91+
std::unordered_map<std::string, std::function<void(Role* r)>> special_magic_effect_attack_; //发动攻击
92+
std::unordered_map<std::string, std::function<void(AttackEffect&, Role* r)>> special_magic_effect_beat_; //被打中
93+
94+
void setID(int id);
95+
96+
bool canWalk45(int x, int y)
97+
{
98+
if (isOutLine(x, y) || isBuilding(x, y) || isWater(x, y))
99+
{
100+
return false;
101+
}
102+
else
103+
{
104+
return true;
105+
}
106+
}
107+
108+
bool canWalk90(int x, int y)
109+
{
110+
auto p = pos90To45(x, y);
111+
return canWalk45(p.x, p.y);
112+
}
113+
114+
bool canWalk90(Pointf p, Role* r, int dis = -1)
115+
{
116+
if (r->Pos.z > 1) { return true; }
117+
if (dis == -1) { dis = TILE_W * 1.5; }
118+
for (auto r1 : battle_roles_)
119+
{
120+
if (r1 == r || r1->Dead) { continue; }
121+
double dis1 = EuclidDis(p, r1->Pos);
122+
if (dis1 < dis)
123+
{
124+
double dis0 = EuclidDis(r->Pos, r1->Pos);
125+
if (dis0 >= dis1) { return false; }
126+
}
127+
}
128+
auto p45 = pos90To45(p.x, p.y);
129+
return canWalk45(p45.x, p45.y);
130+
}
131+
132+
virtual bool canWalk(int x, int y) override { return canWalk45(x, y); }
133+
134+
static void decreaseToZero(int& i)
135+
{
136+
if (i > 0) { i--; }
137+
}
138+
139+
template <typename T>
140+
static void decreaseToZero(T& i, T v)
141+
{
142+
if (i > 0) { i -= v; }
143+
if (i < 0) { i = 0; }
144+
}
145+
};

src/BattleSceneHades.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,6 @@ BattleSceneHades::~BattleSceneHades()
7676
{
7777
}
7878

79-
void BattleSceneHades::setID(int id)
80-
{
81-
battle_id_ = id;
82-
info_ = BattleMap::getInstance()->getBattleInfo(id);
83-
84-
BattleMap::getInstance()->copyLayerData(info_->BattleFieldID, 0, &earth_layer_);
85-
BattleMap::getInstance()->copyLayerData(info_->BattleFieldID, 1, &building_layer_);
86-
}
87-
8879
void BattleSceneHades::draw()
8980
{
9081
//在这个模式下,使用的是直角坐标

src/BattleSceneHades.h

Lines changed: 5 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#pragma once
2-
#include "BattleScene.h"
3-
#include "Font.h"
2+
#include "BattleSceneAct.h"
43
#include "Head.h"
5-
#include "TextureManager.h"
64
#include "UIKeyConfig.h"
75
#include <deque>
86
#include <unordered_map>
@@ -11,147 +9,33 @@
119
//每场战斗可以选择从4种武学中选择轻重
1210
//硬直,判定范围,威力,消耗体力,在不攻击的时候可以回复体力
1311

14-
class BattleSceneHades : public BattleScene
12+
class BattleSceneHades : public BattleSceneAct
1513
{
16-
struct AttackEffect
17-
{
18-
Pointf Pos;
19-
Pointf Velocity, Acceleration;
20-
Role* Attacker = nullptr; //攻击者
21-
std::map<Role*, int> Defender; //每人只能被一个特效击中一次
22-
Magic* UsingMagic = nullptr;
23-
Item* UsingHiddenWeapon = nullptr;
24-
int Frame = 0; //当前帧数
25-
int TotalFrame = 1; //总帧数,当前帧数超过此值就移除此效果
26-
int TotalEffectFrame = 1; //效果总帧数
27-
int OperationType = -1; //攻击类型
28-
std::string Path; //效果贴图路径
29-
Role* FollowRole = nullptr; //一直保持在角色身上
30-
int Weaken = 0; //弱化程度,减掉
31-
double Strengthen = 1; //强化程度,相乘
32-
int Track = 0; //是否追踪
33-
int Through = 0; //是否贯穿,即击中敌人后可以不消失
34-
int NoHurt = 0; //是否无伤害
35-
void setEft(int num)
36-
{
37-
setPath(fmt1::format("eft/eft{:03}", num));
38-
}
39-
void setPath(const std::string& p)
40-
{
41-
Path = p;
42-
TotalEffectFrame = TextureManager::getInstance()->getTextureGroupCount(Path);
43-
}
44-
};
45-
46-
struct TextEffect
47-
{
48-
Pointf Pos;
49-
std::string Text;
50-
int Size = 15;
51-
int Frame = 0;
52-
BP_Color Color;
53-
int Type = 0; //0-缓缓向上, 1-原地不动
54-
void set(const std::string& text, BP_Color c, Role* r)
55-
{
56-
Text = text;
57-
Color = c;
58-
if (r)
59-
{
60-
Pos = r->Pos;
61-
Pos.x -= 7.5 * Font::getTextDrawSize(Text);
62-
Pos.y -= 50;
63-
}
64-
}
65-
};
66-
6714
public:
6815
BattleSceneHades();
6916
BattleSceneHades(int id);
7017
virtual ~BattleSceneHades();
71-
void setID(int id);
7218

7319
//继承自基类的函数
7420
virtual void draw() override;
7521
virtual void dealEvent(BP_Event& e) override; //战场主循环
7622
virtual void dealEvent2(BP_Event& e) override; //用于停止自动
7723
virtual void onEntrance() override;
7824
virtual void onExit() override;
25+
7926
virtual void backRun() override {}
27+
8028
virtual void backRun1();
8129
void Action(Role* r);
8230
void AI(Role* r);
8331
virtual void onPressedCancel() override;
8432

8533
protected:
86-
Pointf pos_; //坐标为俯视,而非在画面的位置,其中y需除以2画在上面
87-
double gravity_ = -4;
88-
double friction_ = 0.1;
89-
90-
UIKeyConfig::Keys keys_;
91-
92-
std::deque<AttackEffect> attack_effects_;
93-
std::deque<TextEffect> text_effects_;
94-
95-
std::deque<Role*> enemies_;
96-
std::deque<Role> enemies_obj_;
97-
98-
std::vector<std::shared_ptr<Head>> heads_;
99-
std::vector<std::shared_ptr<Head>> head_boss_;
100-
101-
bool is_running_ = false; //主角是否在跑动
102-
Role* role_ = nullptr; //主角
103-
Role* dying_ = nullptr;
104-
int weapon_ = 1;
105-
int frozen_ = 0;
106-
int slow_ = 0;
107-
int shake_ = 0;
108-
int close_up_ = 0;
109-
11034
std::shared_ptr<Menu> menu_;
11135
std::vector<std::shared_ptr<Button>> button_magics_;
11236
std::shared_ptr<Button> button_item_;
11337
std::shared_ptr<TextBox> show_auto_;
11438

115-
std::unordered_map<std::string, std::function<void(Role* r)>> special_magic_effect_every_frame_; //每帧
116-
std::unordered_map<std::string, std::function<void(Role* r)>> special_magic_effect_attack_; //发动攻击
117-
std::unordered_map<std::string, std::function<void(AttackEffect&, Role* r)>> special_magic_effect_beat_; //被打中
118-
119-
bool canWalk45(int x, int y)
120-
{
121-
if (isOutLine(x, y) || isBuilding(x, y) || isWater(x, y))
122-
{
123-
return false;
124-
}
125-
else
126-
{
127-
return true;
128-
}
129-
}
130-
131-
bool canWalk90(int x, int y)
132-
{
133-
auto p = pos90To45(x, y);
134-
return canWalk45(p.x, p.y);
135-
}
136-
bool canWalk90(Pointf p, Role* r, int dis = -1)
137-
{
138-
if (r->Pos.z > 1) { return true; }
139-
if (dis == -1) { dis = TILE_W; }
140-
for (auto r1 : battle_roles_)
141-
{
142-
if (r1 == r || r1->Dead) { continue; }
143-
double dis1 = EuclidDis(p, r1->Pos);
144-
if (dis1 < dis)
145-
{
146-
double dis0 = EuclidDis(r->Pos, r1->Pos);
147-
if (dis0 >= dis1) { return false; }
148-
}
149-
}
150-
auto p45 = pos90To45(p.x, p.y);
151-
return canWalk45(p45.x, p45.y);
152-
}
153-
virtual bool canWalk(int x, int y) override { return canWalk45(x, y); }
154-
15539
void renderExtraRoleInfo(Role* r, double x, double y);
15640
//int calHurt(Role* r0, Role* r1);
15741
virtual int checkResult() override;
@@ -160,10 +44,7 @@ class BattleSceneHades : public BattleScene
16044
Role* findFarthestEnemy(int team, Pointf p);
16145
int calCast(int act_type, int operation_type, Role* r);
16246
int calCoolDown(int act_type, int operation_type, Role* r);
163-
void decreaseToZero(int& i)
164-
{
165-
if (i > 0) { i--; }
166-
}
47+
16748
void defaultMagicEffect(AttackEffect& ae, Role* r);
16849
virtual int calRolePic(Role* r, int style, int frame) override;
16950

src/BattleSceneSekiro.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,6 @@ BattleSceneSekiro::BattleSceneSekiro()
4040
easy_block_ = GameUtil::getInstance()->getInt("game", "easy_block", 0);
4141
}
4242

43-
void BattleSceneSekiro::setID(int id)
44-
{
45-
battle_id_ = id;
46-
info_ = BattleMap::getInstance()->getBattleInfo(id);
47-
48-
BattleMap::getInstance()->copyLayerData(info_->BattleFieldID, 0, &earth_layer_);
49-
BattleMap::getInstance()->copyLayerData(info_->BattleFieldID, 1, &building_layer_);
50-
}
51-
5243
void BattleSceneSekiro::draw()
5344
{
5445
//在这个模式下,使用的是直角坐标
@@ -99,6 +90,7 @@ void BattleSceneSekiro::draw()
9990
int breathless = 0;
10091
int draw_turn = 1; //主要为了被击倒的画到后面
10192
};
93+
10294
std::vector<DrawInfo> draw_infos;
10395
draw_infos.reserve(10000);
10496

0 commit comments

Comments
 (0)