/*************************************************************************
 *  _bubbleenemy.zes Entity Script
 * 
 * Author: GD
 * Date:   2 July 2002
 *
 * Desc:   A script for a bubble enemy, found in dungeons, they look like
 *		   skulls with fire circling them.
 *
 *         NOTE: This Script REQUIRES the enemy library entity: _enemylib
 *         to be included in the project.
 *
 * Usage:  
 *
 * Sprites: _EnemySheet1.spt
 *         
 ************************************************************************/
#include <entity>
#include <general>
#include <float>
#include <core>
#include <counter>

new MainImage[20]   = "_ebubble1";
new float: moveCount = 0.00;
new float: TakeMagicCount = 0.00;
new BubbleImage[20];
new adj = 2;
new glimit = 10;
new param;
new TakeMagic = true;

//----------------------------------------
// Name: main()
//----------------------------------------
main()
{
	if (FirstRun())
	{
		// Set some general parameters
		SetActiveDist("this", 220);
		SetType("this", enemyType);
		SetSpeed( "this", 100 );   
		SetDamage("this", 50);
		SetMoveAngle("this", 45);
		
		param = GetParam("this");
		
		if (param == 'r')
		{
			BubbleImage = "_ebubble2";
		}
		if (param == 'b')
		{
			BubbleImage = "_ebubble3";
			SetSpeed( "this", 110 ); 
		}
	}
	
	if (!isActive("this"))
	return;
	
	// Check for a collision with the player
	if ( CallFunction("_enemylib", true, "CheckForPlayer", "NULL"))
	{
		// if this is the blue bubble then take some magic as well
		if (param == 'b' && TakeMagic)
		{
			if (GetCounterValue("magicMeter") > 0)
			{
				IncCounterTarget("magicMeter", -5);
				PlaySound("_magicincrease.wav", 240);	
				TakeMagic = false;
				TakeMagicCount = 0.00;
			}
		}
	}
	
	// A system so that the bubble doesnt take too much magic from the player
	if ( !TakeMagic )
	{
		TakeMagicCount += 10 * GetTimeDelta();
		if (TakeMagicCount > 30)
			TakeMagic = true;
	}
	
	Move();
}

//----------------------------------------
// Name: Move()
//----------------------------------------
Move()
{
	new a;
	new xf[4][4] = { {6, 0, -6, 0},  {6, -2, -6, 2},  {4, -4, -4, 4}, {2, -6, -2, 6} };
	new yf[4][4] = { {0, 6,  0, -6}, {2, 6,  -2, -6}, {4, 4,  -4, -4},{6, 2,  -6, -2} };
	new width  = GetWidth(MainImage);
	new height = GetHeight(MainImage);
	new x = GetX("this");
	new y = GetY("this");
	
	new CurrentAngle = GetMoveAngle("this");
	new extend = 5;

	// Move the enemy if the game is unpaused
	if (GetPauseLevel() == 0)
	{
		// Perform some collision detection to make the enemy bounce off the walls
		// Also make sure the enemy doesnt go outside the current group
		// Theres probably a much better way of doing this.
		if (CurrentAngle < 90)
		{
			if ( (CheckMask(x + 4, y - extend, true) < 240) || y - extend <= GetGroupMinY() + glimit ) 
				SetMoveAngle("this", 315);
			else if ( CheckMask(x - extend, y + 4, true) < 240 || x - extend <= GetGroupMinX() + glimit ) 
				SetMoveAngle("this", 135);	
		}
		
		else if (CurrentAngle >= 90 && CurrentAngle < 180)
		{
			if ( CheckMask(x + 4, y - extend, true) < 240 || y - extend <= GetGroupMinY() + glimit) 
				SetMoveAngle("this", 225);
			else if ( CheckMask(x + 8 + extend, y + 4, true) < 240 || x + 8 + extend >= GetGroupMaxX() - glimit) 
				SetMoveAngle("this", 45);	
		}
		else if (CurrentAngle >= 180 && CurrentAngle < 270)
		{
			if ( CheckMask(x + 8 + extend, y + 4, true) < 240 || x + 8 + extend >= GetGroupMaxX() - glimit) 
				SetMoveAngle("this", 315);
			else if ( CheckMask(x + 4, y + 8 + extend, true) < 240 || y + 8 + extend >= GetGroupMaxY() - glimit) 
				SetMoveAngle("this", 135);	
		}
		else if (CurrentAngle >= 270 )
		{
			if ( CheckMask(x + 4, y + 8 + extend, true) < 240 || y + 8 + extend >= GetGroupMaxY() - glimit) 
				SetMoveAngle("this", 45);
			else if ( CheckMask(x - extend, y + 4, true) < 240 || x - extend <= GetGroupMinX() + glimit) 
				SetMoveAngle("this", 225);	
		}
				
		// Move the enemy
		AngleMove("this");
		
		moveCount += 14.00 * GetTimeDelta();
		if (moveCount > 3.00)
			moveCount = 0.00;
			
		a = floatround(moveCount)
	}
	
	x = GetX("this");
	y = GetY("this");
		
	// Draw the enemy 
	if (isVisible("this"))
	{
		PutSprite(MainImage, x, y, y + 24);
		PutSprite(BubbleImage, x + xf[a][0], y + yf[a][0], y + 24);
		PutSprite(BubbleImage, x + xf[a][1], y + yf[a][1], y + 24);
		PutSprite(BubbleImage, x + xf[a][2], y + yf[a][2], y + 24);
		PutSprite(BubbleImage, x + xf[a][3], y + yf[a][3], y + 24);
	}
		
	// Set a collision rectangle around the Enemy
	SetCollisionRect("this", 0, false, x + adj, y + adj, x + width - adj, y + height - adj);
}

//----------------------------------------
// Name: HitByWeapon(wtype[], damage, x, y)
//----------------------------------------
public HitByWeapon(wtype[], damage, x, y)
{
	// I think if these were hit with magic powder they died...
}
