package structures;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import _texture.loader.Textures;
import engine.Game;
import engine.Handler;
import hud.Clock;
import interfaces.GameObject;
import interfaces.Structure;
import lighting.RadialLight;
import utils.Animation;
import utils.Point2f;
import utils.Utils;
import world.DroppedItem;

/*  ------------- Nihilne ------------
    * Created by Mitch Weaver - 2016 *
      ~~~~ All Rights Reserved ~~~~ */

public final class Fire extends Structure implements GameObject {
	
	public static final int WIDTH = 22, HEIGHT = 22;
	
	private final Animation animation = new Animation(17, Textures.structureSheet[0], Textures.structureSheet[1],Textures.structureSheet[2],Textures.structureSheet[3],Textures.structureSheet[4],Textures.structureSheet[5]);
	private RadialLight light;
	
	public Fire(Point2f center) {
		super(8, center, WIDTH, HEIGHT);
		light = new RadialLight(center, 100, 0.775f);
		RadialLight.lights.add(light);
		health = Clock.NIGHT_DURATION * 1.3f;
	}
	
	public final synchronized void destroy() {
		new DroppedItem(center, 9);
		
		// REMOVE THE FIRES LIGHT
		RadialLight.lights.remove(light);
		
		Handler.structures.remove(this);
	}
	
	public final void tick() {
		animation.runAnimation();
		
		if(--health <= 0) destroy();
	}

	private Graphics2D copy;
	private AffineTransform transform = new AffineTransform();
	public final void render(Graphics2D g2d) {
		copy = (Graphics2D) g2d.create(); transform.setToRotation(-Utils._45 * (Game.rotation - 1), center.x, center.y); copy.transform(transform);
		
		animation.drawAnimation(copy, pos.x, pos.y);
		
		
		if(Game.drawCollisionBounds){
			g2d.setColor(Color.red);
			g2d.drawOval((int)(center.x - radius), (int)(center.y - radius), (int)radius*2, (int)radius*2);
		}
	}
}
