@registerAbility()
class item_treasure_chest_2 extends BaseItem {
position!: Vector;
angles!: Vector;
kv!: Record<string, string>;
channelingPlaceholder!: CBaseAnimating;
Spawn() {
if (IsServer()) {
this.kv = this.GetAbilityKeyValues() as Record<string, string>;
this.HandleSkinAndScale();
}
}
private HandleSkinAndScale(){
const kvSkin = this.GetSkin();
const kvScale = this.GetScale();
if(kvSkin == 0 && kvScale == 1){
return;
}
Timers.CreateTimer(0.01, () => {
const container = this.GetContainer();
if(container){
container.SetSkin(kvSkin);
container.SetModelScale(kvScale);
}
});
}
GetAbilityTextureName() {
return GetAbilityTextureNameForAbility(this.GetName());
}
OnSpellStart() {
if (IsClient()) {
return;
}
if(!this.position){
this.position = this.GetContainer()!.GetOrigin();
this.angles = this.GetContainer()!.GetAnglesAsVector();
}
if (!this.channelingPlaceholder) {
this.channelingPlaceholder = this.SpawnReplacementChests(this.GetChestModel());
}
}
OnChannelFinish(interrupted: boolean) {
if (IsClient()) {
return;
}
if (interrupted) {
this.RedropChest();
this.DeleteChest();
return;
}
this.OnChestOpen();
}
OnChestOpen() {
print("Opened chest");
this.CreateLoot();
this.SpawnReplacementChests(this.GetChestOpenModel());
this.DeleteChest();
}
private SpawnReplacementChests(model: string): CBaseAnimating{
const item = SpawnEntityFromTableSynchronous("prop_dynamic", {
model: model,
scale: this.GetScale(),
origin: this.position,
angles: this.angles,
skin: this.GetSkin()
}) as CBaseAnimating;
item.ResetSequence("chest_treasure_idle");
return item as CBaseAnimating;
}
private RedropChest() {
const chestReplace = CreateItem(this.GetName(), undefined, undefined);
const item = CreateItemOnPositionSync(this.position, chestReplace);
item.SetAngles(this.angles.x, this.angles.y, this.angles.z);
item.SetModelScale(this.GetScale());
item.ResetSequence("chest_treasure_idle");
}
private DeletePlaceholder(){
this.channelingPlaceholder.RemoveSelf();
}
private DeleteChest() {
this.GetCaster().RemoveItem(this);
this.DeletePlaceholder();
}
private CreateLoot(){
const caster = this.GetCaster();
const item = CreateItem("item_desolator", caster.GetPlayerOwner(), undefined);
const worldItem = CreateItemOnPositionSync(this.position, item);
item?.LaunchLoot(false, 124, RandomFloat(0.5, 1.2), caster.GetOrigin().__add(RandomVector(RandomInt(50, 150))));
}
private GetChestOpenModel() {
return this.kv["ReplaceOnOpen"];
}
private GetChestModel(){
return this.kv["Model"];
}
private GetSkin(){
return tonumber(this.kv["Skin"]) || 0;
}
protected GetScale(){
return tonumber(this.kv["Scale"]) || 1;
}
}