Skip to content

Commit

Permalink
simplify logic (#16966)
Browse files Browse the repository at this point in the history
* simplify logic
* the same logic in c++
  • Loading branch information
minggo authored May 10, 2024
1 parent 3f17039 commit 82dfc97
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
35 changes: 19 additions & 16 deletions cocos/scene-graph/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2106,27 +2106,30 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable {
public setPosition(x: number, y: number, z?: number): void;

public setPosition (val: Readonly<Vec3> | number, y?: number, z?: number): void {
let x: number;
const localPosition = this._lpos;

if (y === undefined && z === undefined) {
x = (val as Vec3).x;
y = (val as Vec3).y;
z = (val as Vec3).z;
} else if (z === undefined) {
x = val as number;
z = localPosition.z;
} else {
x = val as number;
}
if (y === undefined) {
// The type of val is Readonly<Vec3>
if (localPosition.equals(val as Vec3)) {
return;
}

if (x !== localPosition.x || y !== localPosition.y || z !== localPosition.z) {
this._lpos.set(x, y, z);
this.invalidateChildren(TransformBit.POSITION);
Vec3.copy(this._lpos, val as Vec3);
} else {
if (z === undefined) {
z = localPosition.z;
}

if (this._eventMask & TRANSFORM_ON) {
this.emit(NodeEventType.TRANSFORM_CHANGED, TransformBit.POSITION);
if (val as number === localPosition.x && y === localPosition.y && z === localPosition.z) {
return;
}

Vec3.set(this._lpos, val as number, y, z);
}

this.invalidateChildren(TransformBit.POSITION);
if (this._eventMask & TRANSFORM_ON) {
this.emit(NodeEventType.TRANSFORM_CHANGED, TransformBit.POSITION);
}
}

Expand Down
4 changes: 4 additions & 0 deletions native/cocos/core/scene-graph/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ Node *Node::getChildByPath(const ccstd::string &path) const {

//
void Node::setPositionInternal(float x, float y, float z, bool calledFromJS) {
if (_localPosition.approxEquals({x, y, z})) {
return;
}

_localPosition.set(x, y, z);
invalidateChildren(TransformBit::POSITION);

Expand Down

0 comments on commit 82dfc97

Please sign in to comment.