#include "scripts/bp_common.txt"
#include "scripts/common.txt"

class ChallengeTotem : ScriptObject {
    kActor @self;
	kActor @keyActor;
	float turnSpeed = Math::Deg2Rad(1);
	int cType;
	kStr keyActorName;
	bool insidePlayer = true;
	//------------------------------------------------------------------------------------------------------------------------
    ChallengeTotem(kActor @actor) {
        @self = actor;
		cType = 1;
		keyActorName = "DummyActor";
    }
	//------------------------------------------------------------------------------------------------------------------------
	bool HasUpgrade() {
		return true;
	}
	//------------------------------------------------------------------------------------------------------------------------
	bool HasWeapon() {
		return true;
	}
	//------------------------------------------------------------------------------------------------------------------------
	kStr NoWeaponText() {
		return "Weapon Required";
	}
	//------------------------------------------------------------------------------------------------------------------------
    void OnTick(void) {
		if (keyActor is null) {
			if (HasChallengeKey(cType)) {
				@keyActor = SpawnSolidActor(keyActorName, self.Origin().x, self.Origin().y, self.Origin().z + 10);
			}
		} else {
			keyActor.Yaw() += turnSpeed;
			if (self.GameTicks() % 4 == 0) {
				keyActor.SpawnFx("fx/generic_120.kfx", kVec3(0, 0, 150));
			}
		}
				
		if (InRadius(self.Origin(), 60.0f)) {
			if (!insidePlayer) {
				if (HasWeapon()) {
					if (!HasUpgrade()) {
						if (HasChallengeKey(cType)) {
							bool isNewGame; //for the restart bug
							GameVariables.GetBool("g_newgame", isNewGame);
							if (!isNewGame) {
								EnterChallengeWarp(cType);
							}
						}
					} else {
						Game.PrintLine("The power is yours", 0);
					}
				} else {
					Game.PrintLine(NoWeaponText(), 0);
				}
			}
		} else {
			insidePlayer = false;
		}
    }
	//------------------------------------------------------------------------------------------------------------------------
    void OnSpawn(void) {
    }
	//------------------------------------------------------------------------------------------------------------------------
};
