generated from well-known-components/base-ts-project
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fixed documentation and param names on Vector3 function. * Improved documentation and param names * Barebones implementation of Vector2 * Updated api.md * lint-fix pass
- Loading branch information
1 parent
1076173
commit 2739e89
Showing
6 changed files
with
165 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* @public | ||
* Vector2 is a type and a namespace. | ||
* - The namespace contains all types and functions to operates with Vector2 | ||
* - The type Vector2 is an alias to Vector2.ReadonlyVector2 | ||
* ``` | ||
* | ||
* // Namespace usage example | ||
* const next = Vector2.add(pointA, velocityA) // add function not implemented yet | ||
* | ||
* // Type usage example | ||
* const readonlyPosition: Vector2 = Vector2.Zero() | ||
* readonlyPosition.x = 0.1 // this FAILS | ||
* | ||
* // For mutable usage, use `Vector2.Mutable` | ||
* const position: Vector2.Mutable = Vector2.One() | ||
* position.x = 3.0 // this WORKS | ||
* ``` | ||
*/ | ||
export type Vector2 = Vector2.ReadonlyVector2 | ||
|
||
/** | ||
* @public | ||
* Vector2 is a type and a namespace. | ||
* ``` | ||
* // The namespace contains all types and functions to operates with Vector2 | ||
* const next = Vector2.add(pointA, velocityA) // add function not implemented yet | ||
* // The type Vector2 is an alias to Vector2.ReadonlyVector2 | ||
* const readonlyPosition: Vector2 = Vector2.Zero() | ||
* readonlyPosition.x = 0.1 // this FAILS | ||
* | ||
* // For mutable usage, use `Vector2.Mutable` | ||
* const position: Vector2.Mutable = Vector2.One() | ||
* position.x = 3.0 // this WORKS | ||
* ``` | ||
*/ | ||
export namespace Vector2 { | ||
/** | ||
* @public | ||
* For external use, type with `Vector2`, e.g. `const zeroPosition: Vector2 = Vector2.Zero()`. | ||
* For mutable typing, use `Vector2.Mutable`, e.g. `const oneVector: Vector2.Mutable = Vector2.One()`. | ||
*/ | ||
export type ReadonlyVector2 = { | ||
readonly x: number | ||
readonly y: number | ||
} | ||
|
||
/** | ||
* @public | ||
* For external usage, type with `Vector2`, e.g. `const zeroPosition: Vector2 = Vector2.Zero()`. | ||
* For mutable typing, use `Vector2.Mutable`, e.g. `const oneVector: Vector2.Mutable = Vector2.One()`. | ||
*/ | ||
export type MutableVector2 = { | ||
x: number | ||
y: number | ||
} | ||
|
||
/** | ||
* @public | ||
* Type with `Vector2` for readonly usage, e.g. `const zeroPosition: Vector2 = Vector2.Zero()`. | ||
* For mutable, use `Vector2.Mutable`, e.g. `const oneVector: Vector2.Mutable = Vector2.One()`. | ||
*/ | ||
export type Mutable = MutableVector2 | ||
|
||
/** | ||
* Creates a new Vector2 object from the given x, y (floats) coordinates. | ||
* @param x - defines the first coordinates (on X axis) | ||
* @param y - defines the second coordinates (on Y axis) | ||
*/ | ||
export function create( | ||
/** | ||
* Defines the first coordinates (on X axis) | ||
*/ | ||
x: number = 0, | ||
/** | ||
* Defines the second coordinates (on Y axis) | ||
*/ | ||
y: number = 0 | ||
): MutableVector2 { | ||
return { x, y } | ||
} | ||
|
||
/** | ||
* Returns a new Vector2 set to (0.0, 0.0) | ||
* @returns a new empty Vector2 | ||
*/ | ||
export function Zero(): MutableVector2 { | ||
return create(0.0, 0.0) | ||
} | ||
|
||
/** | ||
* Returns a new Vector2 set to (1.0, 1.0) | ||
* @returns a new unit Vector2 | ||
*/ | ||
export function One(): MutableVector2 { | ||
return create(1.0, 1.0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
export * from './Quaternion' | ||
export * from './Vector2' | ||
export * from './Vector3' | ||
export * from './Color3' | ||
export * from './Color4' | ||
export * from './Vector3' | ||
export * from './Scalar' | ||
export * from './types' | ||
export * from './index' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Vector2 } from '../src/Vector2' | ||
|
||
const results = { | ||
zeros: '(0.0, 0.0)', | ||
ones: '(1.0, 1.0)' | ||
} | ||
|
||
const normalize = (v: string) => (v === '-0.0' ? '0.0' : v) | ||
|
||
function vector2ToString(vec: Vector2.MutableVector2) { | ||
const x = normalize(vec.x.toFixed(1).slice(0, 6)) | ||
const y = normalize(vec.y.toFixed(1).slice(0, 6)) | ||
|
||
return `(${x}, ${y})` | ||
} | ||
|
||
describe('ECS Vector2 - Next tests', () => { | ||
it('Vector2.create One', () => { | ||
expect(vector2ToString(Vector2.One())).toEqual(results.ones) | ||
}) | ||
|
||
it('Vector3.create zeros', () => { | ||
expect(vector2ToString(Vector2.Zero())).toEqual(results.zeros) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters