package Hexel.things.types;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import Hexel.math.Vector3d;
import Hexel.rendering.Renderable;

public abstract class Humanoid extends Thing implements Movable, Cuboid, Volumetric {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3849125377629870134L;
	protected double x;
	protected double y;
	protected double z;

	public final double xlen = .3;
	public final double ylen = .3;
	public final double zlen = 1.4;

	private double dx = 0;
	private double dy = 0;
	private double dz = 0;
	
	public Humanoid(double x, double y, double z){
		this.x = x;
		this.y = y;
		this.z = z;
	}
	

	@Override
	public void getXYZ(Vector3d v) {
		v.x = x;
		v.y = y;
		v.z = z;
	}

	@Override
	public double getX() {
		return x;
	}

	@Override
	public double getY() {
		return y;
	}

	@Override
	public double getZ() {
		return z;
	}

	@Override
	public double getWidth() {
		return this.xlen;
	}

	@Override
	public double getHeight() {
		return this.ylen;
	}

	@Override
	public double getDepth() {
		return this.zlen;
	}

	@Override
	public void accelerate(double fps, double dx, double dy, double dz) {
		this.dz += dz / fps;
		if (this.thingBridge.inWater(this) && Math.abs(this.dz) > 5)
			this.dz = Math.signum(this.dz) * 5;
	}

	@Override
	public void stopX() {

	}

	@Override
	public void stopY() {

	}

	@Override
	public void stopZ() {
		this.dz = 0;
	}

	@Override
	public Vector3d getReqMoveVector(double fps) {
		Vector3d v = new Vector3d();
		double dragZ = 1;
		double dragXY = 1;
		if (this.thingBridge.inWater(this)) {
			dragZ = .5;
			dragXY = .75;
		}
		v.x = dragXY * this.dx / fps;
		v.y = dragXY * this.dy / fps;
		v.z = dragZ * this.dz / fps;
		return v;
	}
	

	public void jump(double mag) {
		if (this.thingBridge.onGround(this) || this.thingBridge.inWater(this))
			this.dz = mag;
	}
	
	public void setMovement(double dir, double speed){
		this.dx = speed*Math.cos(dir);
		this.dy = speed*Math.sin(dir);
	}

	@Override
	public void applyMoveVector(Vector3d v) {
		this.x += v.x;
		this.y += v.y;
		this.z += v.z;
	}
	
	private void readObject(ObjectInputStream in) throws ClassNotFoundException, IOException {
		x = (Double) in.readObject();
		y = (Double) in.readObject();
		z = (Double) in.readObject();
		
		dx = (Double) in.readObject();
		dy = (Double) in.readObject();
		dz = (Double) in.readObject();
	}

	private void writeObject(ObjectOutputStream out) throws IOException {
		
		out.writeObject(x);
		out.writeObject(y);
		out.writeObject(z);
		
		out.writeObject(dx);
		out.writeObject(dy);
		out.writeObject(dz);
	}


}
