001package com.fs.starfarer.api.impl.campaign.ghosts; 002 003import org.lwjgl.util.vector.Vector2f; 004 005import com.fs.starfarer.api.campaign.SectorEntityToken; 006import com.fs.starfarer.api.util.IntervalUtil; 007import com.fs.starfarer.api.util.Misc; 008 009 010public class GBFollowClosely extends BaseGhostBehavior { 011 012 protected SectorEntityToken other; 013 protected int maxBurn; 014 protected float desiredRangeMin; 015 protected float desiredRangeMax; 016 protected IntervalUtil interval = new IntervalUtil(5f, 10f); 017 protected float angleOffset = 0f; 018 019 public GBFollowClosely(SectorEntityToken other, float duration, int maxBurn, float desiredRangeMin, float desiredRangeMax) { 020 super(duration); 021 this.other = other; 022 this.maxBurn = maxBurn; 023 this.desiredRangeMin = desiredRangeMin; 024 this.desiredRangeMax = desiredRangeMax; 025 interval.forceIntervalElapsed(); 026 } 027 028 @Override 029 public void advance(float amount, SensorGhost ghost) { 030 if (other.getContainingLocation() != ghost.getEntity().getContainingLocation() || !other.isAlive()) { 031 end(); 032 return; 033 } 034 super.advance(amount, ghost); 035 036 interval.advance(amount * 1f); 037 if (interval.intervalElapsed()) { 038 angleOffset = 60f - Misc.random.nextFloat() * 120f; 039 } 040 041 float dist = Misc.getDistance(ghost.getEntity(), other); 042 dist -= other.getRadius() + ghost.getEntity().getRadius(); 043 if (dist > desiredRangeMax) { 044 ghost.moveTo(other.getLocation(), new Vector2f(), maxBurn); 045 } else if (dist < desiredRangeMin) { 046 float angle = Misc.getAngleInDegrees(other.getLocation(), ghost.getEntity().getLocation()); 047 angle += angleOffset; 048 Vector2f dest = Misc.getUnitVectorAtDegreeAngle(angle); 049 dest.scale(desiredRangeMin + (desiredRangeMax - desiredRangeMin) * 0.5f); 050 Vector2f.add(other.getLocation(), dest, dest); 051 ghost.moveTo(dest, new Vector2f(), maxBurn); 052 } else if (other.getVelocity().length() > 10f) { 053 int burn = (int) Misc.getBurnLevelForSpeed(other.getVelocity().length()); 054 if (burn < 1) burn = 1; 055 Vector2f dest = Misc.getUnitVectorAtDegreeAngle(Misc.getAngleInDegrees(other.getVelocity()) + angleOffset); 056 dest.negate(); 057 dest.scale(desiredRangeMin + (desiredRangeMax - desiredRangeMin) * 0.5f); 058 Vector2f.add(other.getLocation(), dest, dest); 059 ghost.moveTo(dest, new Vector2f(), burn); 060 } 061 } 062} 063 064 065 066 067 068 069 070 071 072 073 074 075