

Class DoomOrb : Inventory {

	int age;
	int maxAge;
	
	Default {
		Inventory.MaxAmount 500;
		Inventory.Amount 1;
		Inventory.PickupMessage "";
		Inventory.PickupSound "orbs/inventory/up";
		+BRIGHT;
	}
	
	states {
		Spawn:
			AORB A 5;
			AORB B 5 checkAge;
			Loop;
		Decaying:
			AORB A 2;
			AORB B 2;
			TNT1 A 2;
			loop;
	}
	
	override void PostBeginPlay() {
		self.maxAge = 350 + random(0, 50);
	}
	
	action void checkAge() {
		if (invoker.age > invoker.maxAge -100) {
			invoker.SetState(ResolveState("Decaying"));
		}
	}
	
	override void Tick() {
		super.Tick();
		if (self.owner) { return; }
		age++;
		if (age > 350) {
			self.Destroy();
		}
	}
	
	void makeBig() {
		self.Scale = (2,2);
		self.Amount = 10;
		self.maxAge = 500 + random(0, 50);
	}
}

Class MonsterDeathEventHandler : EventHandler {

	override void WorldThingDied(WorldEvent e) {
		vector3 monsterPosition = e.Thing.pos;
		double monsterHeight = e.Thing.height;
		int remainingHealth = e.Thing.GetMaxHealth();
		
		while (remainingHealth >= 10) {
			DoomOrb orb = DoomOrb(Actor.Spawn(
				"DoomOrb", 
				(
					monsterPosition.x, 
					monsterPosition.y, 
					monsterPosition.z + monsterHeight/2
				)
			));
			if (remainingHealth >= 100) {
				orb.makeBig();
				remainingHealth -= 100;
			}
			else {
				remainingHealth -= 10;
			}
			vector3 orbVelocity = (
				frandom(-3, 3),
				frandom(-3, 3),
				frandom(5, 10)
			);
			orb.vel = orbVelocity;
		}
	}
}

Class OrbUIHandler : EventHandler {

    override void RenderOverlay(RenderEvent e) {
        if (AutoMapActive) { return; }
        
        PlayerPawn p = players[consoleplayer].mo;
        int orbs = p.CountInv("DoomOrb");
        
        Statusbar.BeginHUD();
        HUDFont myfont = HUDFont.Create(smallfont);
        
        Statusbar.DrawImage("HUDORB", (4, 4),
            Statusbar.DI_SCREEN_LEFT_TOP | Statusbar.DI_ITEM_TOP |
            Statusbar.DI_ITEM_LEFT, scale: (2, 2));
            
        Statusbar.DrawString(myfont, orbs .. "", (100, 10), 
            StatusBar.DI_SCREEN_LEFT_TOP | Statusbar.DI_TEXT_ALIGN_RIGHT,
            Font.CR_RED, scale: (2, 2)
        );
    }
    
    override void NetworkProcess(ConsoleEvent e) {
        if (e.Name == "orbRecharge") {
            PlayerPawn p = players[e.player].mo;
            int orbs = p.CountInv("DoomOrb");

            if (orbs >= 20 && p.health < 100) {
                p.TakeInventory("DoomOrb", 20);
                p.GiveInventory("HealthBonus", 10);
                p.A_StartSound("misc/p_pkup");

                // Cap the health at 100
                if (p.health > 100) {
                    p.health = 100;
                }
            }
        }
    }
	
	override void WorldTick() {
		PlayerPawn p = players[consoleplayer].mo;
		int buttons = p.GetPlayerInput(INPUT_BUTTONS); 
		if (buttons & BT_USER1) {
			// Activate orbs functionality
			ActivateOrbs(p);
		}
	}
	
	void ActivateOrbs(PlayerPawn p) {
		int orbs = p.CountInv("DoomOrb");

		if (orbs >= 20 && p.health < 100) {
			p.TakeInventory("DoomOrb", 20);
			p.GiveInventory("HealthBonus", 10);
			p.A_StartSound("misc/p_pkup");

			// Cap the health at 100
			if (p.health > 100) {
				p.health = 100;
			}
		}
	}
}
