import standard;
import node;
import random;


//constants
var TILE_PIXEL_WIDTH: int const = 16;
var TILE_PIXEL_HEIGHT: int const = 16;

var DIRECTION_DOWN: int const = 0;
var DIRECTION_UP: int const = 1;
var DIRECTION_RIGHT: int const = 2;
var DIRECTION_LEFT: int const = 3;


//variables
var gridPositionX: int = null;
var gridPositionY: int = null;

var direction: int = null; //BUGFIX: animation not looping properly

var stepAI: int = 0;
var health: int = 2;
var requestFree: bool = false;

//animating different cycles on one image
var walkAnimationCounter: int = 0;


//accessors & mutators
fn setGridPosition(node: opaque, x: int, y: int) {
	gridPositionX = x;
	gridPositionY = y;

	node.setNodePositionX(floor( gridPositionX * TILE_PIXEL_WIDTH / node.getNodeScaleX() ));
	node.setNodePositionY(floor( gridPositionY * TILE_PIXEL_HEIGHT / node.getNodeScaleY() ));
}

fn getGridPositionX(node: opaque) {
	return gridPositionX;
}

fn getGridPositionY(node: opaque) {
	return gridPositionY;
}

fn applyDamage(node: opaque, damage: int) {
	health -= damage;
}

fn isRequestingFree(node: opaque) {
	if (requestFree) {
		var parent = node.getParentNode();

		var explosion: opaque = parent.loadChildNode("scripts:/entities/explosion.toy");
		explosion.callNodeFn("setGridPosition", gridPositionX, gridPositionY);
		return true;
	}
	else {
		return false;
	}
}


//lifecycle functions
fn onLoad(node: opaque) {
	node.loadNodeTexture("sprites:/drone.png");
	node.faceDirection(DIRECTION_DOWN);
}

//fn onUpdate(node: opaque, delta: int) {
//	//
//}

fn onStep(node: opaque) {
	if (++walkAnimationCounter >= 12) {
		node.incrementCurrentNodeFrame();
		walkAnimationCounter = 0;
	}

	//move in realspace
	var distX = gridPositionX * TILE_PIXEL_WIDTH - node.getNodePositionX();
	var distY = gridPositionY * TILE_PIXEL_HEIGHT - node.getNodePositionY();

	node.setNodeMotionX( normalize(distX) );
	node.setNodeMotionY( normalize(distY) );
}

fn onFree(node: opaque) {
	node.freeNodeTexture();
}

fn onDraw(node: opaque) {
	var posX: int = node.getNodeWorldPositionX();
	var posY: int = node.getNodeWorldPositionY();

	var scaleX: float = node.getNodeWorldScaleX();
	var scaleY: float = node.getNodeWorldScaleY();

	//this offset is because the sprite cell for lejana is twice as big as the sprite cell for the floor tiles
	var originOffsetX: int = node.getNodeRectW() * int scaleX / 4;
	var originOffsetY: int = node.getNodeRectH() * int scaleY / 2;

	node.drawNode(
		floor(posX * scaleX) - originOffsetX /* + globalCameraX */,
		floor(posY * scaleY) - originOffsetY /* + globalCameraY */,
		floor(node.getNodeRectW() * scaleX),
		floor(node.getNodeRectH() * scaleY)
	);
}

//gameplay functions
fn runAI(node: opaque, rng: opaque) {
	//If I'm out of health, I need to be removed
	if (health <= 0) {
		requestFree = true;
		return;
	}

    //every second call
	if (++stepAI >= 2) {
		stepAI = 0;

		var dir = rng.generateRandomNumber() % 4;
		var moveX = 0;
		var moveY = 0;

		if (dir == DIRECTION_DOWN) {
			moveY += 1;
			node.faceDirection(DIRECTION_DOWN);
		}

		else if (dir == DIRECTION_UP) {
			moveY -= 1;
			node.faceDirection(DIRECTION_UP);
		}

		else if (dir == DIRECTION_RIGHT) {
			moveX += 1;
			node.faceDirection(DIRECTION_RIGHT);
		}

		else if (dir == DIRECTION_LEFT) {
			moveX -= 1;
			node.faceDirection(DIRECTION_LEFT);
		}

		if (node.getParentNode().callNodeFn("getWalkableAt", gridPositionX + moveX, gridPositionY + moveY)) {
			gridPositionX += moveX;
			gridPositionY += moveY;
		}
	}
}


//utils for facing different directions (idling)
fn faceDirection(node: opaque, dir: int) {
	if (direction == dir) return;
	direction = dir;
	node.setNodeRect(32 * 2 * dir, 0, 32, 32);
	node.setNodeFrames(2);
	walkAnimationCounter = 12;
}
