Skip to content

Commit

Permalink
feat: array utils for vec2 (#292)
Browse files Browse the repository at this point in the history
  • Loading branch information
amyspark-ng authored Jul 29, 2024
1 parent 3d92bde commit 3dbb5a0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
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

0 comments on commit 3dbb5a0

Please sign in to comment.