refactor(scene): 重构影子节点结构并简化Shadow2D组件
- 移除动态创建影子节点的逻辑,改为在场景中预置Shadow节点 - 简化Shadow2D组件,移除target绑定和动态更新逻辑 - 统一所有影子节点的命名和结构,统一使用Shadow名称 - 调整影子节点位置,统一放在角色节点下作为子节点 - 移除所有动态影子参数配置,使用固定椭圆参数绘制影子
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,60 +1,12 @@
|
||||
import { _decorator, Component, Node, Graphics, Color, UITransform } from 'cc';
|
||||
const { ccclass, property } = _decorator;
|
||||
import { _decorator, Component, Graphics, Color } from 'cc';
|
||||
const { ccclass } = _decorator;
|
||||
|
||||
@ccclass('Shadow2D')
|
||||
export class Shadow2D extends Component {
|
||||
|
||||
@property(Node)
|
||||
target: Node | null = null; // 角色节点
|
||||
|
||||
@property
|
||||
shadowWidth: number = 60;
|
||||
|
||||
@property
|
||||
shadowHeight: number = 20;
|
||||
|
||||
@property
|
||||
shadowAlpha: number = 80; // 0-255
|
||||
|
||||
private shadowNode: Node | null = null;
|
||||
|
||||
start() {
|
||||
if (!this.target) {
|
||||
console.warn("Shadow2D: 未绑定角色节点 target");
|
||||
return;
|
||||
}
|
||||
|
||||
// 创建影子节点
|
||||
this.shadowNode = new Node("Shadow");
|
||||
let g = this.shadowNode.addComponent(Graphics);
|
||||
this.target.addChild(this.shadowNode);
|
||||
|
||||
// 调整大小
|
||||
let ui = this.shadowNode.addComponent(UITransform);
|
||||
ui.setContentSize(this.shadowWidth, this.shadowHeight);
|
||||
|
||||
// 画椭圆
|
||||
g.fillColor = new Color(0, 0, 0, this.shadowAlpha);
|
||||
g.ellipse(0, 0, this.shadowWidth / 2, this.shadowHeight / 2);
|
||||
let g = this.node.addComponent(Graphics);
|
||||
g.fillColor = new Color(0, 0, 0, 100); // 半透明黑色
|
||||
g.ellipse(0, 0, 40, 15); // 椭圆(radiusX=40, radiusY=15)
|
||||
g.fill();
|
||||
|
||||
// 放到角色脚下
|
||||
this.shadowNode.setPosition(0, -this.target.getComponent(UITransform).height / 2);
|
||||
}
|
||||
|
||||
update(dt: number) {
|
||||
if (!this.shadowNode || !this.target) return;
|
||||
|
||||
if (!this.target.active) {
|
||||
this.shadowNode.active = false;
|
||||
return;
|
||||
}
|
||||
|
||||
// 始终保持在角色底部(比如角色动画高度变化时也能跟随)
|
||||
let ui = this.target.getComponent(UITransform);
|
||||
|
||||
if (!ui) return;
|
||||
|
||||
this.shadowNode.setPosition(0, -ui.height / 2 + 10);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user