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: array utils for vec2 #292

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ obj.sprite = "bag";
- added `camFlash()` to flash the screen
- added `SpriteComp.getCurAnim()` to get the current animation data
- added `Color.toArray()` to convert a color to an array
- added `Vec2.toArray()` to convert a vec2 to an array (eg: player.pos.toArray())
- added `Vec2.fromArray()` to convert an array to a vec2 (eg: player.pos = Vec2.fromArray(newPosition))
- added `outline()`, `shader()`, and `area()` properties to `debug.inspect` (f1)
- added `kaboomOpt.debugKey` for customizing the key used to toggle debug mode
- added `GameObjRaw.tags` to get a game object's tags
Expand Down
9 changes: 9 additions & 0 deletions src/math/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ export class Vec2 {
return new Vec2(Math.cos(angle), Math.sin(angle));
}

/** Create a new Vec2 from an array */
static fromArray(arr: Array<number>) {
return new Vec2(arr[0], arr[1]);
}

static LEFT = new Vec2(-1, 0);
static RIGHT = new Vec2(1, 0);
static UP = new Vec2(0, -1);
Expand Down Expand Up @@ -287,6 +292,10 @@ export class Vec2 {
toString(): string {
return `vec2(${this.x.toFixed(2)}, ${this.y.toFixed(2)})`;
}

toArray(): Array<number> {
return [this.x, this.y];
}
}

export function vec2(...args: Vec2Args): Vec2 {
Expand Down