使用ecs系统进行重构
This commit is contained in:
9
assets/script/game/hero/Aheros.meta
Normal file
9
assets/script/game/hero/Aheros.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "29b69aa0-d30f-47bf-adb0-1c761bf4bec8",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
14
assets/script/game/hero/Aheros/BaseComp.ts
Normal file
14
assets/script/game/hero/Aheros/BaseComp.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { _decorator, Component, Node } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@ccclass('BaseComp')
|
||||
export class BaseComp extends Component {
|
||||
start() {
|
||||
|
||||
}
|
||||
|
||||
update(deltaTime: number) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
9
assets/script/game/hero/Aheros/BaseComp.ts.meta
Normal file
9
assets/script/game/hero/Aheros/BaseComp.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "0a844c8e-978a-4c93-99ed-f4e6a7d53286",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
34
assets/script/game/hero/Aheros/Module.ts
Normal file
34
assets/script/game/hero/Aheros/Module.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { ecs } from "../../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
|
||||
/** Module 模块 */
|
||||
@ecs.register(`Module`)
|
||||
export class Module extends ecs.Entity {
|
||||
/** ---------- 数据层 ---------- */
|
||||
// ModuleModel!: ModuleModelComp;
|
||||
|
||||
/** ---------- 业务层 ---------- */
|
||||
// ModuleBll!: ModuleBllComp;
|
||||
|
||||
/** ---------- 视图层 ---------- */
|
||||
// ModuleView!: ModuleViewComp;
|
||||
|
||||
/** 实始添加的数据层组件 */
|
||||
protected init() {
|
||||
// this.addComponents<ecs.Comp>();
|
||||
}
|
||||
|
||||
/** 模块资源释放 */
|
||||
destroy() {
|
||||
// 注: 自定义释放逻辑,视图层实现 ecs.IComp 接口的 ecs 组件需要手动释放
|
||||
super.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
/** Module 模块业务逻辑系统组件,如无业务逻辑处理可删除此对象 */
|
||||
export class EcsModuleSystem extends ecs.System {
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
// this.add(new ecs.ComblockSystem());
|
||||
}
|
||||
}
|
||||
9
assets/script/game/hero/Aheros/Module.ts.meta
Normal file
9
assets/script/game/hero/Aheros/Module.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "013c01e2-3700-446a-824e-074584d7787b",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
@@ -21,19 +21,14 @@ export class Hero extends ecs.Entity {
|
||||
HeroModel!: HeroModelComp;
|
||||
// 视图层
|
||||
HeroView!: HeroViewComp;
|
||||
|
||||
|
||||
protected init() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
this.remove(HeroViewComp);
|
||||
this.remove(MoveToComp);
|
||||
super.destroy();
|
||||
}
|
||||
|
||||
/** 加载角色 */
|
||||
load(pos: Vec3 = Vec3.ZERO,scale:number = 1,uuid:number=1001,is_call:boolean=false,lv:number=1) {
|
||||
scale = 1
|
||||
@@ -50,7 +45,6 @@ export class Hero extends ecs.Entity {
|
||||
this.hero_init(uuid,node,scale,box_group,is_call,lv)
|
||||
oops.message.dispatchEvent("hero_load",this)
|
||||
}
|
||||
|
||||
hero_init(uuid:number=1001,node:Node,scale:number=1,box_group=BoxSet.HERO,is_call:boolean=false,lv:number=1){
|
||||
var hv = node.getComponent(HeroViewComp)!;
|
||||
// console.log("hero_init",buff)
|
||||
@@ -81,50 +75,38 @@ export class Hero extends ecs.Entity {
|
||||
let sklv=slv
|
||||
if(sklv >= 5) sklv=5;
|
||||
|
||||
hv.sk1 = hero.sk1[sklv];
|
||||
hv.sk2 = hero.sk2[sklv];
|
||||
hv.sk3 = hero.sk3[sklv];
|
||||
hv.akc = hero.akc[sklv];
|
||||
hv.uac = hero.uac[sklv];
|
||||
hv.crc = hero.crc[sklv];
|
||||
hv.dgc = hero.dgc[sklv];
|
||||
hv.akr = hero.akr[sklv];
|
||||
hv.uar = hero.uar[sklv];
|
||||
hv.crr = hero.crr[sklv];
|
||||
hv.dgr = hero.dgr[sklv];
|
||||
hv.rhp_max=hv.hp= hv.hp_max =(hero.hp+hero.hp_up*hv.lv)*(1+hero.shp_up/100*slv) ;
|
||||
hv.sk1 = hero.sk1[sklv]
|
||||
hv.sk2 = hero.sk2[sklv]
|
||||
hv.sk3 = hero.sk3[sklv]
|
||||
hv.akc = hero.akc[sklv]
|
||||
hv.uac = hero.uac[sklv]
|
||||
hv.crc = hero.crc[sklv]
|
||||
hv.dgc = hero.dgc[sklv]
|
||||
hv.akr = hero.akr[sklv]
|
||||
hv.uar = hero.uar[sklv]
|
||||
hv.crr = hero.crr[sklv]
|
||||
hv.dgr = hero.dgr[sklv]
|
||||
hv.rhp_max=hv.hp= hv.hp_max =(hero.hp+hero.hp_up*hv.lv)*(1+hero.shp_up/100*slv)
|
||||
hv.ap = (hero.ap+hero.ap_up*hv.lv) *(1+hero.sap_up/100*slv);
|
||||
hv.def= (hero.def+hero.def_up*hv.lv)*(1+hero.sdef_up/100*slv);
|
||||
hv.cd = hero.a_cd
|
||||
hv.crit = hero.crit; //暴击率
|
||||
hv.crit = hero.crit //暴击率
|
||||
hv.crit_add = hero.crit_add;//暴击伤害加成
|
||||
hv.dodge = hero.dodge; //闪避率
|
||||
hv.aexp=hero.aexp;
|
||||
hv.uaexp=hero.uaexp;
|
||||
hv.dodge = hero.dodge //闪避率
|
||||
hv.aexp=hero.aexp
|
||||
hv.uaexp=hero.uaexp
|
||||
hv.cexp=hero.cexp
|
||||
hv.doexp=hero.doexp
|
||||
hv.dexp=hero.dexp;
|
||||
|
||||
hv.dexp=hero.dexp
|
||||
this.add(hv);
|
||||
}
|
||||
set_ratio(uuid:number){
|
||||
let ratio=1;
|
||||
switch (HeroInfo[uuid].level) {
|
||||
case 2:
|
||||
ratio=1.05
|
||||
break;
|
||||
case 3:
|
||||
ratio=1.1
|
||||
break;
|
||||
case 4:
|
||||
ratio=1.15
|
||||
break;
|
||||
case 5:
|
||||
ratio=1.2
|
||||
break;
|
||||
default:
|
||||
ratio=1
|
||||
}
|
||||
return ratio;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/** Module 模块业务逻辑系统组件,如无业务逻辑处理可删除此对象 */
|
||||
export class EcsModuleSystem extends ecs.System {
|
||||
constructor() {
|
||||
super();
|
||||
// this.add(new ecs.ComblockSystem());
|
||||
}
|
||||
}
|
||||
9
assets/script/game/heros.meta
Normal file
9
assets/script/game/heros.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.2.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "7419c29e-7ead-48a1-bef4-06c52b520491",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
50
assets/script/game/heros/HeroComponent.ts
Normal file
50
assets/script/game/heros/HeroComponent.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
|
||||
import { _decorator, Node } from 'cc';
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
// 继承框架的ECComponent
|
||||
|
||||
// 基础属性组件
|
||||
@ecs.register('HeroBase')
|
||||
export class HeroBase extends ecs.Comp {
|
||||
// 定义需要序列化的字段
|
||||
static serializeFields = ['hp', 'attack', 'node'];
|
||||
|
||||
hp: number = 100;
|
||||
attack: number = 10;
|
||||
node: Node = null;
|
||||
|
||||
reset() {
|
||||
this.hp = 100;
|
||||
this.attack = 10;
|
||||
this.node = null;
|
||||
}
|
||||
}
|
||||
|
||||
// 技能组件
|
||||
@ecs.register('HeroSkill')
|
||||
export class HeroSkill extends ecs.Comp {
|
||||
static serializeFields = ['skillId'];
|
||||
|
||||
skillId: string = "";
|
||||
cooldown: number = 0;
|
||||
|
||||
reset() {
|
||||
this.skillId = "";
|
||||
this.cooldown = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 状态组件
|
||||
@ecs.register('HeroState')
|
||||
export class HeroState extends ecs.Comp {
|
||||
current: 'idle' | 'attack' | 'die' = 'idle';
|
||||
previous: 'idle' | 'attack' | 'die' = 'idle';
|
||||
|
||||
reset() {
|
||||
this.current = 'idle';
|
||||
this.previous = 'idle';
|
||||
}
|
||||
}
|
||||
|
||||
9
assets/script/game/heros/HeroComponent.ts.meta
Normal file
9
assets/script/game/heros/HeroComponent.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "dbeb1c15-5c41-49bd-b633-728335443a38",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
22
assets/script/game/heros/HeroSystem.ts
Normal file
22
assets/script/game/heros/HeroSystem.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
@ecs.registerSystem()
|
||||
export class HeroMoveSystem extends ecs.System {
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(HeroModelComp, HeroViewComp, MoveToComp);
|
||||
}
|
||||
|
||||
update(entities: Hero[]) {
|
||||
const deltaTime = oops.timer.delta;
|
||||
entities.forEach(hero => {
|
||||
// 每个英雄独立计算移动
|
||||
const move = hero.get(MoveToComp);
|
||||
const view = hero.get(HeroViewComp);
|
||||
|
||||
// 计算移动向量(单个英雄逻辑)
|
||||
const dir = move.target.subtract(view.node.position).normalize();
|
||||
const speed = view.speed * deltaTime;
|
||||
|
||||
// 更新位置(独立操作)
|
||||
view.node.position = view.node.position.add(dir.multiplyScalar(speed));
|
||||
});
|
||||
}
|
||||
}
|
||||
28
assets/script/game/heros/HeroTypeSystem.ts
Normal file
28
assets/script/game/heros/HeroTypeSystem.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
@ecs.registerSystem()
|
||||
export class HeroTypeSystem extends ecs.System {
|
||||
filter() {
|
||||
return ecs.allOf(HeroTypeComp);
|
||||
}
|
||||
|
||||
update(entities: Hero[]) {
|
||||
entities.forEach(hero => {
|
||||
const type = hero.get(HeroTypeComp).type;
|
||||
switch(type) {
|
||||
case HeroType.MELEE:
|
||||
this.processMelee(hero);
|
||||
break;
|
||||
case HeroType.RANGED:
|
||||
this.processRanged(hero);
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private processMelee(hero: Hero) {
|
||||
// 近战英雄特有逻辑
|
||||
}
|
||||
|
||||
private processRanged(hero: Hero) {
|
||||
// 远程英雄特有逻辑
|
||||
}
|
||||
}
|
||||
0
assets/script/game/heros/Heros.ts
Normal file
0
assets/script/game/heros/Heros.ts
Normal file
9
assets/script/game/heros/Heros.ts.meta
Normal file
9
assets/script/game/heros/Heros.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "3d62e7a3-a90b-47f3-bf88-85745af0b8b4",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
27
assets/script/game/skill/DOTSystem.ts
Normal file
27
assets/script/game/skill/DOTSystem.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
@ecs.registerSystem()
|
||||
export class DOTSystem extends ecs.System {
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(DOTComp);
|
||||
}
|
||||
|
||||
update(entities: ecs.Entity[]) {
|
||||
const delta = oops.timer.delta;
|
||||
|
||||
entities.forEach(hero => {
|
||||
const dot = hero.get(DOTComp);
|
||||
dot.duration -= delta;
|
||||
|
||||
// 每秒伤害
|
||||
if (dot.accumulator >= 1) {
|
||||
hero.get(HeroModelComp).hp -= dot.damagePerSec;
|
||||
dot.accumulator -= 1;
|
||||
}
|
||||
dot.accumulator += delta;
|
||||
|
||||
// 效果结束
|
||||
if (dot.duration <= 0) {
|
||||
hero.remove(DOTComp);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
19
assets/script/game/skill/SkillCollisionSystem.ts
Normal file
19
assets/script/game/skill/SkillCollisionSystem.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
@ecs.registerSystem()
|
||||
export class SkillCollisionSystem extends ecs.System {
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(SkillEffectComp, ColliderComp);
|
||||
}
|
||||
|
||||
update(skills: ecs.Entity[]) {
|
||||
skills.forEach(skill => {
|
||||
const collider = skill.get(ColliderComp);
|
||||
const collisionResult = skill.get(CollisionResultComp) || skill.add(CollisionResultComp);
|
||||
|
||||
// 检测碰撞(伪代码,实际使用物理引擎检测)
|
||||
const hitHeroes = PhysicsSystem.overlap(collider.bounds, 'Hero');
|
||||
|
||||
// 转换为ECS实体
|
||||
collisionResult.targets = hitHeroes.map(h => h.entity);
|
||||
});
|
||||
}
|
||||
}
|
||||
20
assets/script/game/skill/SkillComponent.ts
Normal file
20
assets/script/game/skill/SkillComponent.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
// 技能效果组件
|
||||
@ecs.register('SkillEffect')
|
||||
export class SkillEffectComp extends ecs.Comp {
|
||||
damage: number = 0; // 基础伤害
|
||||
effectType: 'instant' | 'dot' = 'instant'; // 效果类型
|
||||
duration: number = 0; // 持续时间(DOT用)
|
||||
|
||||
reset() {
|
||||
this.damage = 0;
|
||||
this.effectType = 'instant';
|
||||
this.duration = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// 碰撞结果组件
|
||||
@ecs.register('CollisionResult')
|
||||
export class CollisionResultComp extends ecs.Comp {
|
||||
targets: ecs.Entity[] = []; // 碰撞到的英雄实体
|
||||
reset() { this.targets = []; }
|
||||
}
|
||||
47
assets/script/game/skill/SkillEffectSystem.ts
Normal file
47
assets/script/game/skill/SkillEffectSystem.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
@ecs.registerSystem()
|
||||
export class SkillEffectSystem extends ecs.System {
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(SkillEffectComp, CollisionResultComp);
|
||||
}
|
||||
|
||||
update(skills: ecs.Entity[]) {
|
||||
skills.forEach(skill => {
|
||||
const effect = skill.get(SkillEffectComp);
|
||||
const targets = skill.get(CollisionResultComp).targets;
|
||||
|
||||
targets.forEach(hero => {
|
||||
// 应用即时伤害
|
||||
if (effect.effectType === 'instant') {
|
||||
this.applyInstantDamage(hero, effect.damage);
|
||||
}
|
||||
// 应用持续效果
|
||||
else if (effect.effectType === 'dot') {
|
||||
this.applyDOT(hero, effect);
|
||||
}
|
||||
});
|
||||
|
||||
// 清除碰撞结果
|
||||
skill.remove(CollisionResultComp);
|
||||
});
|
||||
}
|
||||
|
||||
private applyInstantDamage(hero: ecs.Entity, damage: number) {
|
||||
const model = hero.get(HeroModelComp);
|
||||
model.hp -= damage;
|
||||
|
||||
// 触发受击事件
|
||||
oops.message.dispatch('HeroDamaged', {
|
||||
hero,
|
||||
damage,
|
||||
currentHp: model.hp
|
||||
});
|
||||
}
|
||||
|
||||
private applyDOT(hero: ecs.Entity, effect: SkillEffectComp) {
|
||||
// 添加持续伤害组件
|
||||
hero.add(DOTComp, {
|
||||
damagePerSec: effect.damage,
|
||||
duration: effect.duration
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user