Skip to content

Commit

Permalink
add InstanceAPI for items, destructables and units
Browse files Browse the repository at this point in the history
  • Loading branch information
DevilVsLive2 committed Jan 8, 2024
1 parent a61287d commit bf311a2
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/handles/Destructable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { Widget, WidgetEventMap } from "./Widget.js";

export interface DestructableEventMap extends WidgetEventMap {}

export interface Destructable {
get handle(): HandleHolder<"destructable">;
}

export class Destructable<T extends DestructableEventMap = DestructableEventMap> extends Widget<T> {
constructor(destructableHandle: HandleHolder<"item">);
constructor(destructableHandle: Destructable);
Expand Down
4 changes: 4 additions & 0 deletions src/handles/Item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { CreateItemNe } from "../utils/common.js";

export interface ItemEventMap extends WidgetEventMap {}

export interface Item {
get handle(): HandleHolder<"item">;
}

export class Item<T extends ItemEventMap = ItemEventMap> extends Widget<T> {
constructor(itemHandle: HandleHolder<"item">);
constructor(itemHandle: Item);
Expand Down
53 changes: 53 additions & 0 deletions src/handles/instanceApi/DestructableData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-disable matching-export-value-to-file-name */
import { Destructable } from "../Destructable.js";

export interface DestructableLevelAccessorNatives<T> {
get: (destructable: HandleHolder<"destructable">, field: HandleHolder<"_enum">) => T;
set: (destructable: HandleHolder<"destructable">, field: HandleHolder<"_enum">, value: T) => boolean;

baseGet: (destructableId: number, field: HandleHolder<"_enum">) => T;
baseSet: (destructableId: number, field: HandleHolder<"_enum">, value: T) => boolean;
}

export interface DestructableData {
instance: number | Destructable;
}

export class DestructableLevelAccessor<T> {
#destructableData: DestructableData;
#field: HandleHolder<"_enum">;

#accessorNatives: DestructableLevelAccessorNatives<T>;

constructor(destructableData: DestructableData, field: HandleHolder<"_enum">, accessNatives: DestructableLevelAccessorNatives<T>) {
this.#destructableData = destructableData;
this.#field = field;

this.#accessorNatives = accessNatives;
}

public getValue() {
return typeof this.#destructableData.instance === "number"
? this.#accessorNatives.baseGet(this.#destructableData.instance, this.#field)
: this.#accessorNatives.get(this.#destructableData.instance.handle, this.#field);
}

public setValue(value: T) {
return typeof this.#destructableData.instance === "number"
? this.#accessorNatives.baseSet(this.#destructableData.instance, this.#field, value)
: this.#accessorNatives.set(this.#destructableData.instance.handle, this.#field, value);
}
}

export class DestructableData {
#_instance: number | Destructable;

constructor(instance: number | Destructable) {
this.#_instance = instance;

Object.defineProperty(this, "instance", {
value: instance,
writable: false,
});
}
}
53 changes: 53 additions & 0 deletions src/handles/instanceApi/ItemData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* eslint-disable matching-export-value-to-file-name */
import { Item } from "../Item.js";

export interface ItemLevelAccessorNatives<T> {
get: (item: HandleHolder<"item">, field: HandleHolder<"_enum">) => T;
set: (item: HandleHolder<"item">, field: HandleHolder<"_enum">, value: T) => boolean;

baseGet: (itemId: number, field: HandleHolder<"_enum">) => T;
baseSet: (itemId: number, field: HandleHolder<"_enum">, value: T) => boolean;
}

export interface ItemData {
instance: number | Item;
}

export class ItemLevelAccessor<T> {
#itemData: ItemData;
#field: HandleHolder<"_enum">;

#accessorNatives: ItemLevelAccessorNatives<T>;

constructor(itemData: ItemData, field: HandleHolder<"_enum">, accessNatives: ItemLevelAccessorNatives<T>) {
this.#itemData = itemData;
this.#field = field;

this.#accessorNatives = accessNatives;
}

public getValue() {
return typeof this.#itemData.instance === "number"
? this.#accessorNatives.baseGet(this.#itemData.instance, this.#field)
: this.#accessorNatives.get(this.#itemData.instance.handle, this.#field);
}

public setValue(value: T) {
return typeof this.#itemData.instance === "number"
? this.#accessorNatives.baseSet(this.#itemData.instance, this.#field, value)
: this.#accessorNatives.set(this.#itemData.instance.handle, this.#field, value);
}
}

export class ItemData {
#_instance: number | Item;

constructor(instance: number | Item) {
this.#_instance = instance;

Object.defineProperty(this, "instance", {
value: instance,
writable: false,
});
}
}
81 changes: 81 additions & 0 deletions src/handles/instanceApi/UnitData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-disable matching-export-value-to-file-name */
import { Unit } from "../Unit.js";

export interface UnitLevelAccessorNatives<T> {
get: (unit: HandleHolder<"unit">, field: HandleHolder<"_enum">) => T;
set: (unit: HandleHolder<"unit">, field: HandleHolder<"_enum">, value: T) => boolean;

baseGet: (unitId: number, field: HandleHolder<"_enum">) => T;
baseSet: (unitId: number, field: HandleHolder<"_enum">, value: T) => boolean;

weaponGet: (unit: HandleHolder<"unit">, field: HandleHolder<"_enum">, index: number) => T;
weaponSet: (unit: HandleHolder<"unit">, field: HandleHolder<"_enum">, index: number, value: T) => boolean;

weaponBaseGet: (unitId: number, field: HandleHolder<"_enum">, index: number) => T;
weaponBaseSet: (unitId: number, field: HandleHolder<"_enum">, index: number, value: T) => boolean;
}

export interface UnitData {
instance: number | Unit;
}

export class UnitLevelAccessor<T> implements Iterable<T> {
#unitData: UnitData;
#field: HandleHolder<"_enum">;

#accessorNatives: UnitLevelAccessorNatives<T>;

constructor(unitData: UnitData, field: HandleHolder<"_enum">, accessNatives: UnitLevelAccessorNatives<T>) {
this.#unitData = unitData;
this.#field = field;

this.#accessorNatives = accessNatives;
}

*[Symbol.iterator](): Iterator<T> {
for (let i = 0; i < this.getLength(); ++i) {
yield this.getIndexValue(i);
}
}

public getLength() {
return 2;
}

public getValue() {
return typeof this.#unitData.instance === "number"
? this.#accessorNatives.baseGet(this.#unitData.instance, this.#field)
: this.#accessorNatives.get(this.#unitData.instance.handle, this.#field);
}

public setValue(value: T) {
return typeof this.#unitData.instance === "number"
? this.#accessorNatives.baseSet(this.#unitData.instance, this.#field, value)
: this.#accessorNatives.set(this.#unitData.instance.handle, this.#field, value);
}

public getIndexValue(index: number) {
return typeof this.#unitData.instance === "number"
? this.#accessorNatives.weaponBaseGet(this.#unitData.instance, this.#field, index)
: this.#accessorNatives.weaponGet(this.#unitData.instance.handle, this.#field, index);
}

public setIndexValue(index: number, value: T) {
return typeof this.#unitData.instance === "number"
? this.#accessorNatives.weaponBaseSet(this.#unitData.instance, this.#field, index, value)
: this.#accessorNatives.weaponSet(this.#unitData.instance.handle, this.#field, index, value);
}
}

export class unitData {
#_instance: number | Unit;

constructor(instance: number | Unit) {
this.#_instance = instance;

Object.defineProperty(this, "instance", {
value: instance,
writable: false,
});
}
}

0 comments on commit bf311a2

Please sign in to comment.