-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add InstanceAPI for items, destructables and units
- Loading branch information
1 parent
a61287d
commit bf311a2
Showing
5 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
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, | ||
}); | ||
} | ||
} |
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
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, | ||
}); | ||
} | ||
} |
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
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, | ||
}); | ||
} | ||
} |