001package com.fs.starfarer.api.impl.campaign.ghosts;
002
003import org.lwjgl.util.vector.Vector2f;
004
005import com.fs.starfarer.api.Global;
006import com.fs.starfarer.api.campaign.CampaignFleetAPI;
007import com.fs.starfarer.api.campaign.SectorEntityToken;
008import com.fs.starfarer.api.util.Misc;
009
010
011public class GBLeadPlayerTo extends BaseGhostBehavior {
012        
013        protected SectorEntityToken to;
014        protected int maxBurn;
015        protected float maxDistAhead;
016        protected boolean returningToPlayer = false;
017        
018        public GBLeadPlayerTo(float duration, SectorEntityToken to, float maxDistAhead, int maxBurn) {
019                super(duration);
020                this.to = to;
021                this.maxDistAhead = maxDistAhead;
022                this.maxBurn = maxBurn;
023        }
024
025
026
027        @Override
028        public void advance(float amount, SensorGhost ghost) {
029                CampaignFleetAPI pf = Global.getSector().getPlayerFleet();
030                if (pf.getContainingLocation() != ghost.getEntity().getContainingLocation() || !pf.isAlive()) {
031                        end();
032                        return;
033                }
034                super.advance(amount, ghost);
035                
036                
037                float goBackThreshold = maxDistAhead * 2f;
038                if (returningToPlayer) {
039                        goBackThreshold = maxDistAhead * 1.5f;
040                }
041                Vector2f dir = Misc.getUnitVector(pf.getLocation(), to.getLocation());
042                Vector2f diff = Vector2f.sub(ghost.getEntity().getLocation(), pf.getLocation(), new Vector2f());
043                
044                float distAheadOfPlayer = Vector2f.dot(dir, diff);
045                float distFromPlayer = Misc.getDistance(pf, ghost.getEntity());
046                if (distFromPlayer >= goBackThreshold) {
047                        ghost.moveTo(pf.getLocation(), pf.getVelocity(), maxBurn);
048                        returningToPlayer = true;
049                } else if (distAheadOfPlayer > maxDistAhead) {
050                        int burn = (int) (maxBurn * maxDistAhead / distAheadOfPlayer * 0.5f);
051                        ghost.moveTo(to.getLocation(), new Vector2f(), burn);
052                        returningToPlayer = false;
053                } else {
054                        ghost.moveTo(to.getLocation(), new Vector2f(), maxBurn);
055                        returningToPlayer = false;
056                }
057                
058                
059                
060                float dist = Misc.getDistance(ghost.getEntity(), to);
061                if (dist < ghost.getEntity().getRadius() + to.getRadius()) {
062                        end();
063                        return;
064                }
065                
066        }
067        
068        
069        
070}
071
072
073
074
075
076
077
078
079
080
081
082
083