package edu.wisc.cs.games10.chrononaut;

import processing.core.*;


/** Gives a new body part to player if collides with creature */
public class BodyPartExtractor extends SimpleMovable {
	
	private float lastTimeAnimation;
	private PImage imageToDraw;
	private PImage images[];
	private int currIndex;
	
	boolean facingRight;

	public BodyPartExtractor(Player p, PApplet canvas) {
		
		lastTimeAnimation = 0;
		currIndex = 0;
		
		facingRight = true;
		
		images = new PImage[2];
		
		images[0] = canvas.loadImage("player/bodyPartExtractor.png");
		images[1] = canvas.loadImage("player/bodyPartExtractor2.png");
		
		imageToDraw = images[0];
		
		tileHeight = imageToDraw.height / 32.0f;
		tileWidth = imageToDraw.width / 32.0f;
		
		move(p);
		vel = new Vector2D(0, 0);
	}
	
	public void move(Player p) {
		// create extractor relative to player 'p' position
		loc = p.loc.copy();
		facingRight = p.faceRight;
		if (facingRight)
			loc.x += p.getTileWidth();
		else
			loc.x -= tileWidth;
		
		loc.y += (p.tileHeight - 2.5f);
	}

	public void kill() {
		// TODO Auto-generated method stub
	}
	
	public void render(PApplet canvas, float time) {
		
		if((time - lastTimeAnimation) > 500)
		{
			lastTimeAnimation = time;
			imageToDraw = images[++currIndex % 2];
		}
		
		canvas.pushMatrix();
			
			int firstVertexTexCoord;
			int secondVertexTexCoord;
			int thirdVertexTexCoord;
			int fourthVertexTexCoord;

			if(facingRight) {
				firstVertexTexCoord = 0;
				secondVertexTexCoord = imageToDraw.width;
				thirdVertexTexCoord = imageToDraw.width;
				fourthVertexTexCoord = 0;
			} else {
				firstVertexTexCoord = imageToDraw.width;
				secondVertexTexCoord = 0;
				thirdVertexTexCoord = 0;
				fourthVertexTexCoord = imageToDraw.width;
			}
			
			canvas.translate(loc.x, loc.y);
			
			canvas.beginShape(PConstants.QUADS);
				canvas.texture(imageToDraw);
				canvas.vertex(0, 0, firstVertexTexCoord, 0);
				canvas.vertex(tileWidth, 0, secondVertexTexCoord,0);
				canvas.vertex(tileWidth, tileHeight, thirdVertexTexCoord, 64);
				canvas.vertex(0, tileHeight, fourthVertexTexCoord, 64);
			canvas.endShape();
		
		canvas.popMatrix();
	}
	
	
}
