raylib-wasm-transpiler/server/components/Entity.js

108 lines
3.2 KiB
JavaScript
Raw Normal View History

import Vector3 from "./Vector3"
2024-05-29 20:47:19 -04:00
import Stats from "./Stats"
import DerivedStats from "./DerivedStats"
2024-05-29 20:47:19 -04:00
import Skills from "./Skills"
2024-06-30 21:12:58 -04:00
/**
* @typedef {Object} Entity Something that can exist in 3D space
* @property {string} Name Name of the entity
* @property {Vector3} Vector3 Vector3 of the entity in space
* @property {Stats} Stats Base stats
* @property {DerivedStats} DerivedStats Stats that are derived from skills and base stats
* @property {Skills} Skills Stuff that your character can do
*/
class Entity {
/**
* Name of the entity
2024-06-30 21:12:58 -04:00
* @return {string} gets the value of Name
*/
get Name() {
return this._decoder.decode(new Uint8Array(this._ptr.slice(0, 24)));
}
2024-06-30 21:12:58 -04:00
/**
* Name of the entity
* @param {string} v sets the value of Name
*/
set Name(v) {
2024-06-30 21:12:58 -04:00
if (v.length > 24) {
throw new Error("input is larger than buffer size of 24");
}
const tmp = new Uint8Array(new ArrayBuffer(24));
tmp.set(this._encoder.encode(v))
this._ptr.set(tmp.buffer, 0);
}
/**
* Vector3 of the entity in space
2024-06-30 21:12:58 -04:00
* @return {Vector3} gets the value of Vector3
*/
get Vector3() {
return new Vector3({}, new Uint8Array(this._ptr.slice(24, 36)));
}
2024-06-30 21:12:58 -04:00
/**
* Vector3 of the entity in space
* @param {Vector3} sets the value of Vector3
*/
set Vector3(v) {
2024-06-30 21:12:58 -04:00
this._ptr.set(v._ptr, 24);
}
/**
* Base stats
2024-06-30 21:12:58 -04:00
* @return {Stats} gets the value of Stats
*/
2024-05-29 20:47:19 -04:00
get Stats() {
return new Stats({}, new Uint8Array(this._ptr.slice(36, 41)));
}
2024-06-30 21:12:58 -04:00
/**
* Base stats
* @param {Stats} sets the value of Stats
*/
2024-05-29 20:47:19 -04:00
set Stats(v) {
2024-06-30 21:12:58 -04:00
this._ptr.set(v._ptr, 36);
}
/**
* Stats that are derived from skills and base stats
2024-06-30 21:12:58 -04:00
* @return {DerivedStats} gets the value of DerivedStats
*/
get DerivedStats() {
return new DerivedStats({}, new Uint8Array(this._ptr.slice(41, 49)));
}
2024-06-30 21:12:58 -04:00
/**
* Stats that are derived from skills and base stats
* @param {DerivedStats} sets the value of DerivedStats
*/
set DerivedStats(v) {
2024-06-30 21:12:58 -04:00
this._ptr.set(v._ptr, 41);
}
/**
* Stuff that your character can do
2024-06-30 21:12:58 -04:00
* @return {Skills} gets the value of Skills
*/
2024-05-29 20:47:19 -04:00
get Skills() {
return new Skills({}, new Uint8Array(this._ptr.slice(49, 65)));
}
2024-06-30 21:12:58 -04:00
/**
* Stuff that your character can do
* @param {Skills} sets the value of Skills
*/
2024-06-30 21:12:58 -04:00
set Skills(v) {
this._ptr.set(v._ptr, 49);
}
/**
2024-06-30 21:12:58 -04:00
* Constructs a new Entity
*
* @param {{Name: string, Vector3: Vector3, Stats: Stats, DerivedStats: DerivedStats, Skills: Skills, }} init The arguments to construct the object.
* @param {Uint8Array} ptr The pointer to the C struct.
*/
constructor(init = {}, ptr = undefined) {
this._size = 65;
2024-06-30 21:12:58 -04:00
this._ptr = ptr || new Uint8Array(this._size);
this._data = new DataView(this._ptr.buffer);
this._encoder = new TextEncoder();
this._decoder = new TextDecoder();
for (const key of Object.keys(init)) {
this[key] = init[key];
}
}
2024-05-27 19:26:24 -04:00
}
export default Entity