//actor radius is respawn time
class BPPickupSpawner : ScriptObject
{
    kActor @self;
    float m_respawnTime;
	//------------------------------------------------------------------------------------------------------------------------
    BPPickupSpawner(kActor @actor) {
        @self = actor;
        m_respawnTime = 0;
		self.Radius() = 30.0f;
    }
	//------------------------------------------------------------------------------------------------------------------------
    bool SpawnItem() {
		array<kStr> itemList;
		if (Player.Actor().Health() < 100) {
			itemList.insertLast("BP_PickupHealth");
        }
		if (Player.HasWeapon(TW_WEAPON_BOW) && Player.GetAmmo(TW_WEAPON_BOW) < 15) {
			itemList.insertLast("BP_PickupArrows");
		}
		if (Player.HasWeapon(TW_WEAPON_MISSILE) && Player.GetAmmo(TW_WEAPON_MISSILE) < 12) {
			itemList.insertLast("BP_PickupRockets");
		}
		if (Player.HasWeapon(TW_WEAPON_GRENADE) && Player.GetAmmo(TW_WEAPON_GRENADE) < 20) {
			itemList.insertLast("BP_PickupGrenades");
		}
		if ((Player.HasWeapon(TW_WEAPON_PULSERIFLE) || Player.HasWeapon(TW_WEAPON_LASERRIFLE) || Player.HasWeapon(TW_WEAPON_ACCELERATOR)) && Player.GetAmmo(TW_WEAPON_PULSERIFLE) < 100) {
			itemList.insertLast("BP_PickupCell");
		}
        if ((Player.HasWeapon(TW_WEAPON_PISTOL) || Player.HasWeapon(TW_WEAPON_RIFLE)) && Player.GetAmmo(TW_WEAPON_PISTOL) < 200) {
			itemList.insertLast("BP_PickupClip");
        }
        if ((Player.HasWeapon(TW_WEAPON_SHOTGUN) || Player.HasWeapon(TW_WEAPON_ASHOTGUN)) && Player.GetAmmo(TW_WEAPON_SHOTGUN) < 40) {
			itemList.insertLast("BP_PickupShells");
        }
        if (Player.HasWeapon(TW_WEAPON_MINIGUN) && Player.GetAmmo(TW_WEAPON_MINIGUN) < 500) {
			itemList.insertLast("BP_PickupMinigunAmmo");
        }
		if (itemList.length() > 0) {
			int index = Math::RandMax(itemList.length());
			kActor @item = ActorFactory.Spawn(itemList[index], self.Origin().x, self.Origin().y, self.FloorHeight(), 0, self.SectorIndex());
			self.SetTarget(item);
			return true;
		}
		return false;
    }
	//------------------------------------------------------------------------------------------------------------------------
    void OnTick(void) {
        if (!(self.GetTarget() is null)) {
            if (!self.GetTarget().IsStale()) {
                self.GetTarget().SpawnFx("fx/generic_230.kfx", kVec3(0, 0, GAME_SCALE));
                return;
            }
            self.SetTarget(null);
            m_respawnTime = 0;
            return;
        }
        
        m_respawnTime += GAME_DELTA_TIME;
        if(m_respawnTime >= self.Radius()) {
            m_respawnTime = 0;
            if (SpawnItem()) {
                kVec3 org = self.Origin();
                org.z = self.FloorHeight() + (3*GAME_SCALE);
                
                self.PlaySound("sounds/shaders/generic_3_energy_pickup.ksnd");
                Game.SpawnFx("fx/generic_260.kfx", org, self.SectorIndex());
            }
        }
    }
	//------------------------------------------------------------------------------------------------------------------------
    void OnSpawn(void) {
    }
	//------------------------------------------------------------------------------------------------------------------------
}
