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

59 lines
1.6 KiB
JavaScript

/**
* @typedef {Object} Item Item
* @property {string} Name Name of the item
* @property {Int32} i32 Default starting value of the item
*/
class Item {
/**
* Name of the item
* @return {string} gets the value of Name
*/
get Name() {
return this._decoder.decode(new Uint8Array(this._ptr.slice(0, 24)));
}
/**
* Name of the item
* @param {string} v sets the value of Name
*/
set Name(v) {
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);
}
/**
* Default starting value of the item
* @return {Int32} gets the value of BaseValue
*/
get BaseValue() {
return this._data.getInt32(24, true);
}
/**
* Default starting value of the item
* @param {Int32} sets the value of BaseValue
*/
set BaseValue(v) {
return this._data.setInt32(24, v, true);
}
/**
* Constructs a new Item
*
* @param {{Name: string, BaseValue: Int32, }} init The arguments to construct the object.
* @param {Uint8Array} ptr The pointer to the C struct.
*/
constructor(init = {}, ptr = undefined) {
this._size = 28;
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];
}
}
}
export default Item