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

59 lines
1.6 KiB
JavaScript
Raw Normal View History

2024-06-30 21:12:58 -04:00
/**
* @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
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 item
* @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);
}
/**
* Default starting value of the item
2024-06-30 21:12:58 -04:00
* @return {Int32} gets the value of BaseValue
*/
get BaseValue() {
return this._data.getInt32(24, true);
}
2024-06-30 21:12:58 -04:00
/**
* Default starting value of the item
* @param {Int32} sets the value of BaseValue
*/
set BaseValue(v) {
return this._data.setInt32(24, v, true);
}
/**
2024-06-30 21:12:58 -04:00
* 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;
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];
}
}
}
export default Item