Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: worldPos and screenPos can be used as setter now #136

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions src/components/transform/pos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,14 @@ export function pos(...args: Vec2Args): PosComp {
},

// Get the position of the object relative to the root
worldPos(this: GameObj<PosComp>): Vec2 {
return this.parent
? this.parent.transform.multVec2(this.pos)
: this.pos;
worldPos(this: GameObj<PosComp>, pos: Vec2 | null = null): Vec2 {
if (pos) {
this.pos = this.pos.add(this.fromWorld(pos));
} else {
return this.parent
? this.parent.transform.multVec2(this.pos)
: this.pos;
}
},

// Transform a local point to a world point
Expand All @@ -67,11 +71,18 @@ export function pos(...args: Vec2Args): PosComp {
},

// Transform a screen point (relative to the camera) to a local point (relative to this)
screenPos(this: GameObj<PosComp | FixedComp>): Vec2 {
const pos = this.worldPos();
return isFixed(this)
? pos
: k.toScreen(pos);
screenPos(
this: GameObj<PosComp | FixedComp>,
pos: Vec2 | null = null,
): Vec2 {
if (pos) {
this.pos = this.pos.add(this.fromScreen(pos));
} else {
const pos = this.worldPos();
return isFixed(this)
? pos
: k.toScreen(pos);
}
},

// Transform a local point (relative to this) to a screen point (relative to the camera)
Expand Down