Skip to content

Commit 4054ff7

Browse files
committed
ESM Bindings part 1
1 parent 86db0bd commit 4054ff7

7 files changed

Lines changed: 215 additions & 53 deletions

File tree

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"license": "AGPL-3.0-or-later WITH GPL-3.0-interface-exception",
55
"main": "index.js",
66
"scripts": {
7-
"build-client": "asc --config asconfig.client.json",
8-
"build-common": "asc --config asconfig.common.json",
9-
"build-server": "asc --config asconfig.server.json",
10-
"build-singleplayer": "asc --config asconfig.singleplayer.json --target debug",
11-
"dev-build-client": "asc --config asconfig.client.json --target debug",
12-
"dev-build-common": "asc --config asconfig.common.json --target debug",
13-
"dev-build-server": "asc --config asconfig.server.json --target debug",
14-
"dev-build-singleplayer": "asc --config asconfig.singleplayer.json --target debug",
7+
"build-client": "asc --config asconfig.client.json --bindings esm",
8+
"build-common": "asc --config asconfig.common.json --bindings esm",
9+
"build-server": "asc --config asconfig.server.json --bindings esm",
10+
"build-singleplayer": "asc --config asconfig.singleplayer.json --target debug --bindings esm",
11+
"dev-build-client": "asc --config asconfig.client.json --target debug --bindings esm",
12+
"dev-build-common": "asc --config asconfig.common.json --target debug --bindings esm",
13+
"dev-build-server": "asc --config asconfig.server.json --target debug --bindings esm",
14+
"dev-build-singleplayer": "asc --config asconfig.singleplayer.json --target debug --bindings esm",
1515
"update": "npm-check-updates -u"
1616
},
1717
"repository": {

src/assembly/common/character.ts

Lines changed: 135 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
1818

1919
import {CharacterInputController} from "./characterInputController";
2020
import {World} from "./world";
21-
import {Collidable, CollidableType} from "./collidable";
21+
import {Collidable, CollidableCategory} from "./collidable";
2222
import {CollidableTypes} from "../levelpack/levelpack";
2323
import {Skill} from "./skill";
2424
import {Item} from "./item";
2525
import {InventorySlot} from "./inventory";
26-
import {Quaternion, Vector3} from "./math";
26+
import {Quaternion, Ray, RayPickingInfo, Vector3} from "./math";
2727

2828
/**
2929
* A character in the game world. Can be an Ally, an Enemy, or both. Can be a Player or an NPC.
3030
*/
3131
export class Character {
32-
protected _characterWorld!: World;
33-
protected _characterInputController!: CharacterInputController;
32+
protected _characterWorld: World;
33+
protected _characterInputController: CharacterInputController;
3434

3535
// Character Location and Rotation
3636
protected _characterPosition!: Vector3;
@@ -54,16 +54,16 @@ export class Character {
5454
protected _characterNormalGravititationalAcceleration: number = -1.0;
5555

5656
// Character State
57-
protected _healthPoints!: number;
58-
protected _manaPoints!: number;
57+
protected _healthPoints: number;
58+
protected _manaPoints: number;
5959

6060
protected _canWallJumpNow: boolean = true;
61-
protected _lastWallWallJumpedFrom: Collidable = null;
61+
protected _lastWallWallJumpedFrom: Collidable | null = null;
6262
protected _jumpState: boolean = false;
6363
/**
6464
* A map from a world surface type to a boolean
6565
*/
66-
public readonly isCharacterOnWorldSurface: Map<CollidableType, boolean> = new Map([]);
66+
public readonly isCharacterOnWorldSurface: Map<CollidableCategory, boolean> = new Map();
6767

6868
/**
6969
* An array of skills this character is using right now.
@@ -181,7 +181,7 @@ export class Character {
181181
return this._canWallJumpNow;
182182
}
183183

184-
public get lastWallWallJumpedFrom(): Collidable {
184+
public get lastWallWallJumpedFrom(): Collidable | null {
185185
return this._lastWallWallJumpedFrom;
186186
}
187187

@@ -207,7 +207,7 @@ export class Character {
207207
}
208208

209209
protected checkCollisions(): void {
210-
this.isCharacterOnWorldSurface.forEach((_value: boolean, collidableType: CollidableType) => {
210+
this.isCharacterOnWorldSurface.forEach((_value: boolean, collidableType: CollidableCategory) => {
211211
this.isCharacterOnWorldSurface.set(collidableType, false);
212212
})
213213
this._characterWorld.collidables.forEach((collidable) => {
@@ -368,33 +368,25 @@ export class Character {
368368
this.applyGravity(getDeltaTime);
369369
this.capYVelocity();
370370

371-
let deltaPos = this._characterVelocity.scale(getDeltaTime() / 1000.0);
371+
let deltaPos: Vector3 = (getDeltaTime() / 1000.0) * this._characterVelocity;
372372

373373
if (deltaPos.length() > 0) {
374-
let ray: Ray = new Ray(this._characterPosition, deltaPos.normalizeToNew(), this._characterHeight / 2);
375-
this.movementRayHelper = new RayHelper(ray);
376-
this.movementRayHelper.show(
377-
this._characterWorld.babylonScene,
378-
Color3.Purple()
379-
);
380-
let hit: Nullable<PickingInfo> = this._characterWorld.babylonScene.pickWithRay(ray,
381-
(mesh: AbstractMesh) => {
382-
return this._characterWorld.collidables
383-
.map((collidable) => collidable.babylonMesh).includes(mesh);
374+
let ray: Ray = new Ray(this._characterPosition, deltaPos.normalize(), this._characterHeight / 2);
375+
let hit: RayPickingInfo = this._characterWorld.pickWithRay(ray,
376+
(collidable: Collidable): boolean => {
377+
return this._characterWorld.collidables.indexOf(collidable) > 0;
384378
});
385-
if (hit && hit.pickedPoint && hit.getNormal(true)) {
386-
console.debug(hit, hit.getNormal(true))
387-
this._babylonMesh.position = this._characterPosition =
388-
hit.pickedPoint.add(hit.getNormal(true)!.scale(this._characterHeight / 2));
389-
this._characterVelocity = this._characterVelocity
390-
.subtract(hit.getNormal(true)!.scale(this._characterVelocity.dot(hit.getNormal(true)!)));
391-
379+
if (hit && hit.getHitPosition() && hit.getNormalVector()) {
380+
console.debug(hit, hit.getNormalVector())
381+
this._characterPosition =
382+
hit.getHitPosition() + (this._characterHeight / 2) * hit.getNormalVector()!;
383+
this._characterVelocity = this._characterVelocity -
384+
Vector3.dot(this._characterVelocity, hit.getNormalVector()!) * hit.getNormalVector()!;
392385
} else {
393-
this._babylonMesh.position = this._characterPosition = this._characterPosition.add(deltaPos);
386+
this._characterPosition = this._characterPosition + deltaPos;
394387
}
395388
}
396-
console.assert(!!this._babylonMesh.rotationQuaternion, "Rotation quaternion cannot be undefined");
397-
this._characterOrientation = this._babylonMesh.rotationQuaternion as Quaternion;
389+
console.assert(!!this._characterOrientation, "Rotation quaternion cannot be undefined");
398390
this.checkCollisions();
399391
this.applyGravity(getDeltaTime);
400392
this.capYVelocity();
@@ -409,4 +401,116 @@ export class Character {
409401
protected get horizontalMovementScaleFactor(): number {
410402
return this.isCharacterOnWorldSurface.get(CollidableTypes.GROUND) ? 5.0 : 1.0;
411403
}
404+
}
405+
406+
export function newCharacter(characterHeight: number, characterWorld: World,
407+
characterInputController: CharacterInputController): Character {
408+
return new Character(characterHeight, characterWorld, characterInputController);
409+
}
410+
411+
/**
412+
* The world the character is currently in.
413+
*/
414+
export function getCharacterWorld(target: Character): World {
415+
return target.characterWorld;
416+
}
417+
418+
export function getCharacterInputController(target: Character): CharacterInputController {
419+
return target.characterInputController;
420+
}
421+
422+
423+
// Character Location and Rotation
424+
/**
425+
* Character position in 3D Space.
426+
*/
427+
export function getCharacterPosition(target: Character): Vector3 {
428+
return target.characterPosition;
429+
}
430+
431+
/**
432+
* Character Velocity in 3D Space.
433+
*/
434+
export function getCharacterVelocity(target: Character): Vector3 {
435+
return target.characterVelocity;
436+
}
437+
438+
/**
439+
* Character Orientation in 3D Space (as a Quaternion)
440+
*/
441+
export function getCharacterOrientation(target: Character): Quaternion {
442+
return target.characterOrientation;
443+
}
444+
445+
/**
446+
* Character Ray of View in 3D Space
447+
*/
448+
export function getCharacterRayOfView(target: Character): Vector3 {
449+
return target.characterRayOfView;
450+
}
451+
452+
// Character Properties
453+
export function getCharacterHeight(target: Character): number {
454+
return target.characterHeight;
455+
}
456+
457+
export function getCharacterMaximumHealthPoints(target: Character): number {
458+
return target.characterMaximumHealthPoints;
459+
}
460+
461+
export function getCharacterMaximumManaPoints(target: Character): number {
462+
return target.characterMaximumManaPoints;
463+
}
464+
465+
export function getCharacterMaximumSkillPoints(target: Character): number {
466+
return target.characterMaximumSkillPoints;
467+
}
468+
469+
export function getCharacterMaximumHorizontalSpeedUponJoystickNeutral(target: Character): number {
470+
return target.characterMaximumHorizontalSpeedUponJoystickNeutral;
471+
}
472+
473+
export function getCharacterMaximumHorizontalSpeedUponJoystickFullyActive(target: Character): number {
474+
return target.characterMaximumHorizontalSpeedUponJoystickFullyActive;
475+
}
476+
477+
export function getCharacterMaximumVerticalSpeed(target: Character): number {
478+
return target.characterMaximumVerticalSpeed;
479+
}
480+
481+
export function getCharacterVerticalJumpVelocity(target: Character): number {
482+
return target.characterVerticalJumpVelocity;
483+
}
484+
485+
export function getCharacterGroundFriction(target: Character): number {
486+
return target.characterGroundFriction;
487+
}
488+
489+
// Character State
490+
export function getCharacterHealthPoints(target: Character): number {
491+
return target.healthPoints;
492+
}
493+
494+
export function getCharacterManaPoints(target: Character): number {
495+
return target.manaPoints;
496+
}
497+
498+
export function getCharacterCanWallJumpNow(target: Character): boolean {
499+
return target.canWallJumpNow;
500+
}
501+
502+
export function getCharacterLastWallWallJumpedFrom(target: Character): Collidable | null {
503+
return target.lastWallWallJumpedFrom;
504+
}
505+
506+
export function getCharacterJumpState(target: Character): boolean {
507+
return target.jumpState;
508+
}
509+
510+
export function setCharacterPositionAndRotation(
511+
target: Character,
512+
pos: Vector3,
513+
rot: Quaternion
514+
): Character {
515+
return target.setPositionAndRotation(pos, rot);
412516
}

src/assembly/common/characterInputController.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
* along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
*/
1818

19+
import {Vector2} from "./math";
20+
1921
/**
2022
* Controls a character.
2123
*/

src/assembly/common/collidable.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,38 @@ import {NamespacedKey} from "./namespacedKey";
2121
/**
2222
* A type of {@link Collidable}
2323
*/
24-
export class CollidableType {
24+
export class CollidableCategory {
2525
public readonly key: NamespacedKey;
2626

2727
public constructor(key: NamespacedKey) {
2828
this.key = key;
2929
}
3030
}
3131

32+
export function newCollidableType(key: NamespacedKey): CollidableCategory {
33+
return new CollidableCategory(key);
34+
}
35+
3236
/**
3337
* An object that can collide with characters.
3438
*/
3539
export class Collidable {
36-
protected _collidableType: CollidableType;
40+
protected _collidableCategory: CollidableCategory;
3741

38-
public get collidableType(): CollidableType {
39-
return this._collidableType;
42+
public get collidableCategory(): CollidableCategory {
43+
return this._collidableCategory;
4044
}
4145

42-
constructor(collidableCategory: CollidableType) {
43-
this._collidableType = collidableCategory;
46+
constructor(collidableCategory: CollidableCategory) {
47+
this._collidableCategory = collidableCategory;
4448
}
4549

50+
}
51+
52+
export function newCollidable(collidableCategory: CollidableCategory): Collidable {
53+
return new Collidable(collidableCategory);
54+
}
55+
56+
export function getCollidableCategory(collidable: Collidable): CollidableCategory {
57+
return collidable.collidableCategory;
4658
}

src/assembly/common/math.ts

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
*/
1818

1919
export class Vector3 {
20-
private readonly _x: number = 0;
21-
private readonly _y: number = 0;
22-
private readonly _z: number = 0;
20+
private _x: number = 0;
21+
private _y: number = 0;
22+
private _z: number = 0;
2323

2424
constructor(x: number, y: number, z: number) {
2525
this._x = x;
@@ -42,9 +42,21 @@ export class Vector3 {
4242
}
4343

4444
@operator("*")
45-
public static scale(scalar: number, vector: Vector3): Vector3 {
45+
public static scale(vector: Vector3, scalar: f64): Vector3 {
4646
return new Vector3(scalar * vector.x, scalar * vector.y, scalar * vector.z);
4747
}
48+
49+
public static dot(vector: Vector3, vector2: Vector3): f64 {
50+
return vector.x * vector2.x + vector.y * vector2.y + vector.z * vector2.z;
51+
}
52+
53+
public length(): f64 {
54+
return Math.sqrt(Vector3.dot(this, this));
55+
}
56+
57+
public normalize(): Vector3 {
58+
return this * (1 / this.length());
59+
}
4860
}
4961

5062
export class Vector2 {
@@ -70,9 +82,21 @@ export class Vector2 {
7082
}
7183

7284
@operator("*")
73-
public static scale(scalar: number, vector: Vector2): Vector2 {
85+
public static scale(vector: Vector2, scalar: f64): Vector2 {
7486
return new Vector2(scalar * vector.x, scalar * vector.y);
7587
}
88+
89+
public static dot(vector: Vector2, vector2: Vector2): f64 {
90+
return vector.x * vector2.x + vector.y * vector2.y;
91+
}
92+
93+
public length(): f64 {
94+
return Math.sqrt(Vector2.dot(this, this));
95+
}
96+
97+
public normalize(): Vector2 {
98+
return this * (1 / this.length());
99+
}
76100
}
77101

78102
export class Quaternion {
@@ -92,4 +116,19 @@ export class Quaternion {
92116
public get b(): number { return this._b; }
93117
public get c(): number { return this._c; }
94118
public get d(): number { return this._d; }
119+
}
120+
121+
export class Ray {
122+
constructor(a: Vector3, b: Vector3, c: number) {
123+
}
124+
}
125+
126+
export class RayPickingInfo {
127+
public getHitPosition(): Vector3 | null {
128+
return null;
129+
}
130+
131+
public getNormalVector(): Vector3 | null {
132+
return null;
133+
}
95134
}

0 commit comments

Comments
 (0)