refactor(hero): 优化英雄缩放逻辑和位置配置

重构英雄缩放逻辑,使用统一的缩放计算方法替代硬编码值
调整英雄和怪物的初始位置坐标
在Hero和Monster类中添加size变量控制缩放
This commit is contained in:
2025-11-04 10:38:20 +08:00
parent ed1b4f46a4
commit 5c9f299fd7
6 changed files with 55 additions and 38 deletions

View File

@@ -46,19 +46,19 @@ export const getMonList = ()=>{
}
export const HeroPos={
0:{pos:v3(-240,100,0)},
1:{pos:v3(0,100,0)},
2:{pos:v3(0,100,0)},
0:{pos:v3(-240,105,0)},
1:{pos:v3(0,105,0)},
2:{pos:v3(0,105,0)},
}
export const MonSet = {
0:{pos:v3(240,110,0)},
1:{pos:v3(240,90,0)},
2:{pos:v3(320,110,0)},
3:{pos:v3(320,90,0)},
4:{pos:v3(360,110,0)},
5:{pos:v3(360,90,0)},
6:{pos:v3(400,110,0)},
7:{pos:v3(400,90,0)},
0:{pos:v3(240,115,0)},
1:{pos:v3(240,95,0)},
2:{pos:v3(320,115,0)},
3:{pos:v3(320,95,0)},
4:{pos:v3(360,115,0)},
5:{pos:v3(360,95,0)},
6:{pos:v3(400,115,0)},
7:{pos:v3(400,95,0)},
}
export enum HeroConf{

View File

@@ -41,7 +41,7 @@ export class Hero extends ecs.Entity {
// console.log("英雄加载:",uuid,pos,scale,info)
scale = 1
// 查找空闲英雄槽位
let size=1
var path = "game/heros/"+HeroInfo[uuid].path;
var prefab: Prefab = oops.res.get(path, Prefab)!;
var node = instantiate(prefab);
@@ -49,6 +49,7 @@ export class Hero extends ecs.Entity {
node.parent = scene.entityLayer!.node!
const collider = node.getComponent(BoxCollider2D);
if (collider) collider.enabled = false; // 先禁用
node.setScale(size*node.scale.x,size*node.scale.y);
node.setPosition(pos)
// 🔥 设置初始 SiblingIndex - 英雄基础层级 + 位置偏移

View File

@@ -144,12 +144,12 @@ export class HeroMoveSystem extends ecs.ComblockSystem implements ecs.ISystemUpd
// 只有当朝向真正改变时才更新
if (move.currentFacing !== newFacing) {
move.currentFacing = newFacing;
view.node.setScale(newFacing, 1, 1);
view.node.setScale(newFacing*view.node.scale.x, 1*view.node.scale.y);
// 安全获取top节点
const topNode = view.node.getChildByName("top");
if (topNode) {
topNode.setScale(newFacing, 1, 1);
topNode.setScale(newFacing*topNode.scale.x, 1*topNode.scale.y);
}
}
}

View File

@@ -81,8 +81,8 @@ export class HeroViewComp extends CCComp {
this.initUINodes();
/** 方向 */
this.node.setScale(this.scale,1);
this.top_node.setScale(this.scale,1);
this.node.setScale(this.scale*this.node.scale.x,1*this.node.scale.y);
this.top_node.setScale(this.scale*this.top_node.scale.x,1*this.top_node.scale.y);
// if(this.model && this.model.is_boss){
// this.top_node.position=v3(this.node.position.x,this.node.position.y+70,0)
// }

View File

@@ -37,6 +37,7 @@ export class Monster extends ecs.Entity {
/** 加载角色 */
load(pos: Vec3 = Vec3.ZERO,scale:number = 1,uuid:number=1001,lv:number=1,monType:MonType=MonType.NORMAL, buffs: BuffConf[] = [],is_call=false, lane: number = 0, spawnOrder: number = 0) {
scale=-1
let size=1
var scene = smc.map.MapView.scene;
var path = "game/heros/"+HeroInfo[uuid].path;
var prefab: Prefab = oops.res.get(path, Prefab)!;
@@ -45,6 +46,7 @@ export class Monster extends ecs.Entity {
node.parent = scene.entityLayer!.node!
const collider = node.getComponent(BoxCollider2D);
if (collider) collider.enabled = false; // 先禁用 // 延迟一帧启用碰撞体
node.setScale(size*node.scale.x,size*node.scale.y);
node.setPosition(pos)
// 🔥 设置初始 SiblingIndex - 防止溢出

View File

@@ -25,13 +25,13 @@ export class HInfoComp extends CCComp {
// 英雄位置定义
hero_pos:any={
0:v3(420,-57,0), // 不在屏幕内
1:v3(280,-57,0),
2:v3(160,-57,0),
3:v3(0,-67,0),
4:v3(-160,-57,0),
5:v3(-280,-57,0),
6:v3(-420,-57,0), // 不在屏幕内
0:v3(420,-50,0), // 不在屏幕内
1:v3(280,-50,0),
2:v3(160,-50,0),
3:v3(0,-60,0),
4:v3(-160,-50,0),
5:v3(-280,-50,0),
6:v3(-420,-50,0), // 不在屏幕内
}
// 动画锁定标志:防止快速点击导致的动画冲突
@@ -44,6 +44,23 @@ export class HInfoComp extends CCComp {
// 位置索引常量
private static center_pos = 3;
/**
* 根据位置索引获取英雄缩放值
* @param posIndex 位置索引 (0-6)
* @returns Vec3 缩放向量
*/
private getHeroScale(posIndex: number): Vec3 {
switch(posIndex) {
case 2:
case 4:
return v3(-1.6, 1.6, 1); // 2、4位置1.2倍缩放
case 3:
return v3(-1.8, 1.8, 1); // 3位置中心1.5倍缩放
default:
return v3(-1.4, 1.4, 1); // 其他位置1倍缩放
}
}
start() {
this.name_node=this.node.getChildByName("hero").getChildByName("hname").getChildByName("name")
this.type_node=this.node.getChildByName("hero").getChildByName("hname").getChildByName("type")
@@ -105,10 +122,11 @@ export class HInfoComp extends CCComp {
// 载入英雄预制体并设置位置
this.heroNodes[i] = this.load_hui(heroUuid, i);
// 添加初始缩放动画,确保3号位是1.5倍
if (this.heroNodes[i] && i === 3) {
// 添加初始缩放动画,根据位置设置不同的缩放值
if (this.heroNodes[i]) {
let targetScale = this.getHeroScale(i);
tween(this.heroNodes[i])
.to(0.2, { scale: v3(-1.5, 1.5, 1) })
.to(0.2, { scale: targetScale })
.start();
}
}
@@ -124,12 +142,8 @@ export class HInfoComp extends CCComp {
// 设置节点位置
node.setPosition(this.hero_pos[pos_index]);
node.setSiblingIndex(0);
// 设置缩放3号位1.5倍其他位置1倍
if(pos_index==3){
node.setScale(v3(-1.5,1.5,1))
} else {
node.setScale(v3(-1,1,1))
}
// 根据位置设置不同的缩放值
node.setScale(this.getHeroScale(pos_index));
// 加载并播放动画
let anm_path=HeroInfo[uuid].path;
resources.load("game/heros/hero/"+anm_path+"/idle", AnimationClip, (err, clip) => {
@@ -240,7 +254,7 @@ export class HInfoComp extends CCComp {
let targetPos = this.hero_pos[i + 1];
// 使用Tween执行平滑移动和缩放动画
let targetScale = (i + 1) === 3 ? v3(-1.5, 1.5, 1) : v3(-1, 1, 1);
let targetScale = this.getHeroScale(i + 1);
tween(this.heroNodes[i])
.to(0.2, { position: targetPos, scale: targetScale })
@@ -267,9 +281,9 @@ export class HInfoComp extends CCComp {
this.heroNodes[0] = this.load_hui(heros[newIndex], 0);
// 确保新创建的节点初始缩放为1倍因为0号位不是中心位置
// 确保新创建的节点使用正确的缩放值
if (this.heroNodes[0]) {
this.heroNodes[0].setScale(v3(-1, 1, 1));
this.heroNodes[0].setScale(this.getHeroScale(0));
}
// 动画完成,解除锁定
@@ -300,7 +314,7 @@ export class HInfoComp extends CCComp {
let targetPos = this.hero_pos[i - 1];
// 使用Tween执行平滑移动和缩放动画
let targetScale = (i - 1) === 3 ? v3(-1.5, 1.5, 1) : v3(-1, 1, 1);
let targetScale = this.getHeroScale(i - 1);
tween(this.heroNodes[i])
.to(0.2, { position: targetPos, scale: targetScale })
@@ -327,9 +341,9 @@ export class HInfoComp extends CCComp {
this.heroNodes[6] = this.load_hui(heros[newIndex], 6);
// 确保新创建的节点初始缩放为1倍因为6号位不是中心位置
// 确保新创建的节点使用正确的缩放值
if (this.heroNodes[6]) {
this.heroNodes[6].setScale(v3(-1, 1, 1));
this.heroNodes[6].setScale(this.getHeroScale(6));
}
// 动画完成,解除锁定