package edu.wisc.cs.games10.chrononaut;

public abstract class PatrolEnemy extends Enemy {

	float hspeed = 0.001f; // 1 tile / second
	Level level;
	
	boolean hasChangedDirections;
	
	private java.util.Random randomDirection = new java.util.Random();
	

	@Override
	public void update(float time, Era era) {
		hasChangedDirections = false;
		
		// ai: set velocity when on platform
		int tiley;
		int tilex;
		if (vel.x == 0) {
			tiley = (int) Math.floor(loc.y) + (int) Math.ceil(tileHeight);
			tilex = (int) Math.floor(loc.x);
			if (tiley < level.getHeight() && level.getTile(era, tilex, tiley) == Tile.SOLID) {
				vel.x = randomDirection.nextBoolean() ? hspeed : -hspeed;
			}
		}
		
		// ai: walk back and forth on a platform
		Vector2D projectedLoc = Vector2D.add(loc, Vector2D.mult(vel, time));
		tiley =  + (int) Math.ceil(tileHeight);
		int toptiley = (int) Math.floor(projectedLoc.y);
		tiley = toptiley + (int) Math.ceil(tileHeight);

		tilex = (int) Math.floor(projectedLoc.x);
		if (vel.x > 0) {
			tilex += (int) Math.ceil(tileWidth);
		}
		
		if ((tiley < level.getHeight() && level.getTile(era, tilex, tiley) != Tile.SOLID) || 
			(toptiley >= 0 && level.getTile(era, tilex, toptiley) == Tile.SOLID)) {
			vel.x *= -1;
			hasChangedDirections = true;
		}
	}

}
