Created
June 1, 2019 06:14
-
-
Save timetocode/cf28b7e9b28b706ebc024c462aa78ac5 to your computer and use it in GitHub Desktop.
babylon.js + nengi.js entity example. Note: one can write the same thing in more of an ECS style (instead of regular oop) if desired by moving all of the methods off of PlayerCharacter and into systems
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
import nengi from 'nengi' | |
import * as BABYLON from 'babylonjs' | |
class PlayerCharacter { | |
constructor(scene) { | |
this.name = 'anon' | |
this.mesh = new BABYLON.Mesh('dummy', scene) | |
this.mesh.entity = this // for collisions within babylon | |
this.hitpoints = 100 | |
// velocity from own impulses | |
this.velocity = new BABYLON.Vector3(0, 0, 0) | |
// velocity from external impulses (explosions, pushes, etc) | |
this.evelocity = new BABYLON.Vector3(0, 0, 0) | |
this.jumpCooldown = new Cooldown(0.5) | |
this.sprintCooloff = new Cooloff(0.33) | |
this.scene = scene | |
} | |
get x() { return this.mesh.position.x } | |
set x(value) { this.mesh.position.x = value } | |
get y() { return this.mesh.position.y } | |
set y(value) { this.mesh.position.y = value } | |
get z() { return this.mesh.position.z } | |
set z(value) { this.mesh.position.z = value } | |
/* skipped rotationX, etc */ | |
dispose() { | |
this.mesh.dispose() | |
} | |
processInput(command) { | |
// movement logic goes here | |
} | |
takeDamage(sourceClient, amount, fromPosition, fromId) { | |
// ... | |
} | |
ressurect() { | |
this.hitpoints = 100 | |
//... respawn | |
} | |
getForwardRay() { | |
this.mesh.computeWorldMatrix(true) | |
const f = BABYLON.Vector3.TransformCoordinates(BABYLON.Vector3.Forward(), this.mesh.getWorldMatrix()) | |
const d = f.subtract(this.mesh.position) | |
const v = d.normalize() | |
return new BABYLON.Ray(this.mesh.position, v, 999) | |
} | |
} | |
PlayerCharacter.protocol = { | |
x: { type: nengi.Float64, interp: true }, | |
y: { type: nengi.Float64, interp: true }, | |
z: { type: nengi.Float64, interp: true }, | |
rotationX: { type: nengi.RotationFloat32, interp: true }, | |
rotationY: { type: nengi.RotationFloat32, interp: true }, | |
rotationZ: { type: nengi.RotationFloat32, interp: true }, | |
name: nengi.String, | |
hitpoints: { type: nengi.UInt8, interp: true }, | |
} | |
/* because there are getters/setters for x, y, z, rotationX, etc | |
* recreating this entity on the client is as simply as: | |
* const player = new PlayerCharacter(scene) | |
* Object.assign(player, networkDataForPlayer) | |
* */ | |
export default PlayerCharacter |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment