package detectiongame.proto;
import static engine.GfxEngine.loadModel;
import engine.Entity;
import engine.Vec3f;
import static org.lwjgl.opengl.GL11.*;
public class Seeker {
private Entity entity;
private float viewRange;
private double viewAngle,phase,turnAngle,yVariance;
private boolean canSeeTarget;
public Seeker(){
entity = new Entity();
entity.setModel(loadModel("chamfer_box.obj"));
canSeeTarget = false;
viewRange = 10f;
viewAngle = Math.toRadians(45f/2f);
phase = Math.random()*2;
turnAngle = Math.random()+0.2;
yVariance = Math.random()*0.1-0.05;
}
public void spawn(Vec3f pos, float range, float angle){
this.entity.setPos(pos);
this.entity.setRot(0, (float)Math.random()*360, 0);
this.viewRange = range;
this.viewAngle = Math.toRadians(angle/2f);
}
public boolean updateLogic(float t_x, float t_z){
if(this.canSeeTarget == false){
this.entity.rotate(0, 0.5f*(float)Math.sin(phase), 0);
phase += 0.03*turnAngle;
}
double diff_x,diff_z,diff_a;
diff_x = t_x - this.entity.getPos().x;
diff_z = t_z - this.entity.getPos().z;
if(Math.sqrt(Math.pow(diff_x, 2.0) + Math.pow(diff_z, 2.0)) > this.viewRange){
this.canSeeTarget = false;
return false;
}
double box_a,fin;
box_a = Math.toRadians(this.entity.getRot().y);
diff_a = Math.atan2(diff_x, diff_z);
fin = Math.abs(diff_a - box_a);
fin = fin/Math.toRadians(360);
fin = fin - (int)fin;
fin = fin * Math.toRadians(360);
fin -= (fin > Math.toRadians(180))?Math.toRadians(360):0;
//System.out.println(fin);
if(fin < this.viewAngle && fin > -this.viewAngle){
this.canSeeTarget = true;
return true;
}
this.canSeeTarget = false;
return false;
}
private void drawFov(float yOffset){
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND);
glPushMatrix();
double rad = Math.toRadians(this.entity.getRot().y);
if(this.canSeeTarget == true){
glColor4f(0.0f, 255.0f, 0.0f, 1f);
glBegin(GL_LINE_LOOP);
for(double r = -this.viewAngle + rad; r<this.viewAngle + rad; r=r+0.025){
glVertex3d(this.entity.getPos().x+this.viewRange*Math.sin(r),-1+yOffset ,this.entity.getPos().z+this.viewRange*Math.cos(r));
}
glVertex3d(this.entity.getPos().x + this.viewRange * Math.sin(this.viewAngle+rad),-1+yOffset ,this.entity.getPos().z + this.viewRange * Math.cos(this.viewAngle+rad));
glVertex3d(this.entity.getPos().x,-1+yOffset,this.entity.getPos().z);
glEnd();
glColor4f(0.0f, 255.0f, 0.0f, 0.25f);
glBegin(GL_POLYGON);
}
else{
glColor4f(255.0f, 0.0f, 0.0f, 1f);
glBegin(GL_LINE_LOOP);
}
for(double r = -this.viewAngle + rad; r<this.viewAngle + rad; r=r+0.025){
glVertex3d(this.entity.getPos().x+this.viewRange*Math.sin(r),-1+yOffset ,this.entity.getPos().z+this.viewRange*Math.cos(r));
}
glVertex3d(this.entity.getPos().x + this.viewRange * Math.sin(this.viewAngle+rad),-1+yOffset ,this.entity.getPos().z + this.viewRange * Math.cos(this.viewAngle+rad));
glVertex3d(this.entity.getPos().x,-1+yOffset,this.entity.getPos().z);
glEnd();
glPopMatrix();
glDisable(GL_BLEND);
glEnable(GL_CULL_FACE);
}
public void exist(int sort, boolean drawFOV){
glEnable(GL_BLEND);
this.entity.exist();
glDisable(GL_BLEND);
if(drawFOV == true) {
this.drawFov(0.01f*(float)sort);
}
}
}