Example usage for com.badlogic.gdx.math Vector2 dst

List of usage examples for com.badlogic.gdx.math Vector2 dst

Introduction

In this page you can find the example usage for com.badlogic.gdx.math Vector2 dst.

Prototype

@Override
    public float dst(Vector2 v) 

Source Link

Usage

From source file:com.tnf.ptm.entities.ship.EmWave.java

License:Apache License

@Override
public boolean update(PtmGame game, PtmShip owner, boolean tryToUse) {
    if (!tryToUse) {
        return false;
    }// w ww  .  j a  v a 2s  .com
    Vector2 ownerPos = owner.getPosition();
    for (PtmObject o : game.getObjMan().getObjs()) {
        if (!(o instanceof PtmShip) || o == owner) {
            continue;
        }
        PtmShip oShip = (PtmShip) o;
        if (!game.getFactionMan().areEnemies(oShip, owner)) {
            continue;
        }
        Vector2 oPos = o.getPosition();
        float dst = oPos.dst(ownerPos);
        float perc = KnockBack.getPerc(dst, MAX_RADIUS);
        if (perc <= 0) {
            continue;
        }
        float duration = perc * myConfig.duration;
        oShip.disableControls(duration, game);
    }
    ParticleSrc src = new ParticleSrc(myConfig.cc.effect, MAX_RADIUS, DraLevel.PART_BG_0, new Vector2(), true,
            game, ownerPos, Vector2.Zero, 0);
    game.getPartMan().finish(game, src, ownerPos);
    return true;
}

From source file:com.tnf.ptm.entities.ship.KnockBack.java

License:Apache License

@Override
public boolean update(PtmGame game, PtmShip owner, boolean tryToUse) {
    if (!tryToUse) {
        return false;
    }/*from ww  w. ja  va 2 s  .com*/
    Vector2 ownerPos = owner.getPosition();
    for (PtmObject o : game.getObjMan().getObjs()) {
        if (o == owner || !o.receivesGravity()) {
            continue;
        }
        Vector2 oPos = o.getPosition();
        float dst = oPos.dst(ownerPos);
        if (dst == 0) {
            continue; // O__o
        }
        float perc = getPerc(dst, MAX_RADIUS);
        if (perc <= 0) {
            continue;
        }
        Vector2 toO = PtmMath.distVec(ownerPos, oPos);
        float accLen = myConfig.force * perc;
        toO.scl(accLen / dst);
        o.receiveForce(toO, game, false);
        PtmMath.free(toO);
    }
    ParticleSrc src = new ParticleSrc(myConfig.cc.effect, MAX_RADIUS, DraLevel.PART_BG_0, new Vector2(), true,
            game, ownerPos, Vector2.Zero, 0);
    game.getPartMan().finish(game, src, ownerPos);
    return true;
}

From source file:com.tnf.ptm.entities.ship.UnShield.java

License:Apache License

@Override
public boolean update(PtmGame game, PtmShip owner, boolean tryToUse) {
    if (!tryToUse) {
        return false;
    }//from ww w .j a  v a  2 s  . co  m
    Vector2 ownerPos = owner.getPosition();
    for (PtmObject o : game.getObjMan().getObjs()) {
        if (!(o instanceof PtmShip) || o == owner) {
            continue;
        }
        PtmShip oShip = (PtmShip) o;
        Shield shield = oShip.getShield();
        if (shield == null) {
            continue;
        }
        float shieldLife = shield.getLife();
        if (shieldLife <= 0) {
            continue;
        }
        if (!game.getFactionMan().areEnemies(oShip, owner)) {
            continue;
        }
        Vector2 oPos = o.getPosition();
        float dst = oPos.dst(ownerPos);
        float perc = KnockBack.getPerc(dst, MAX_RADIUS);
        if (perc <= 0) {
            continue;
        }
        float amount = perc * myConfig.amount;
        if (shieldLife < amount) {
            amount = shieldLife;
        }
        oShip.receiveDmg(amount, game, ownerPos, DmgType.ENERGY);
    }
    ParticleSrc src = new ParticleSrc(myConfig.cc.effect, MAX_RADIUS, DraLevel.PART_BG_0, new Vector2(), true,
            game, ownerPos, Vector2.Zero, 0);
    game.getPartMan().finish(game, src, ownerPos);
    return true;
}

From source file:com.tnf.ptm.gfx.farBg.FarBackgroundManagerOld.java

License:Apache License

public void draw(GameDrawer drawer, PtmCam cam, PtmGame game) {
    Planet np = game.getPlanetMan().getNearestPlanet();
    Vector2 camPos = cam.getPos();
    float nebPerc = (camPos.dst(np.getPos()) - np.getGroundHeight()) / (4 * Const.ATM_HEIGHT);
    nebPerc = PtmMath.clamp(nebPerc, 0, 1);
    myNebTint.a = nebPerc;//ww  w .ja va 2 s .  co m

    float vd = cam.getViewDist();
    drawer.draw(myNebTex, vd * 2, vd * 2, vd, vd, camPos.x, camPos.y, myNebAngle, myNebTint);
    for (int i = 0, myStarsSize = myStars.size(); i < myStarsSize; i++) {
        FarBgStar star = myStars.get(i);
        star.draw(drawer, vd, camPos, cam.getAngle());
    }
}

From source file:com.tnf.ptm.gfx.PtmCam.java

License:Apache License

public void update(PtmGame game) {
    float life = 0;

    PtmShip hero = game.getHero();//w  w w.  j av  a2 s. co m
    float ts = game.getTimeStep();
    if (hero == null) {
        StarPort.Transcendent trans = game.getTranscendentHero();
        if (trans == null) {
            if (DebugOptions.DIRECT_CAM_CONTROL) {
                applyInput(game);
            }
        } else {
            myPos.set(trans.getPosition());
        }
    } else {
        Vector2 heroPos = hero.getHull().getBody().getWorldCenter();
        if (myZoom * VIEWPORT_HEIGHT < heroPos.dst(myPos)) {
            myPos.set(heroPos);
            game.getObjMan().resetDelays();
        } else {
            Vector2 moveDiff = PtmMath.getVec(hero.getSpd());
            moveDiff.scl(ts);
            myPos.add(moveDiff);
            PtmMath.free(moveDiff);
            float moveSpd = MOVE_SPD * ts;
            myPos.x = PtmMath.approach(myPos.x, heroPos.x, moveSpd);
            myPos.y = PtmMath.approach(myPos.y, heroPos.y, moveSpd);
        }
        life = hero.getLife();
    }

    if (life < myPrevHeroLife) {
        float shakeDiff = .1f * MAX_SHAKE * (myPrevHeroLife - life);
        myShake = PtmMath.approach(myShake, MAX_SHAKE, shakeDiff);
    } else {
        myShake = PtmMath.approach(myShake, 0, SHAKE_DAMP * ts);
    }
    myPrevHeroLife = life;

    Vector2 pos = PtmMath.fromAl(PtmMath.rnd(180), myShake);
    pos.add(myPos);
    applyPos(pos.x, pos.y);
    PtmMath.free(pos);

    float desiredAngle = myCamRotStrategy.getRotation(myPos, game);
    float rotSpd = CAM_ROT_SPD * ts;
    myAngle = PtmMath.approachAngle(myAngle, desiredAngle, rotSpd);
    applyAngle();

    updateMap(game);
}

From source file:com.tnf.ptm.handler.dra.DraMan.java

License:Apache License

private boolean isInCam(Vector2 pos, float r, Vector2 camPos, float viewDist) {
    return camPos.dst(pos) - viewDist < r;
}

From source file:com.tnf.ptm.handler.dra.DrasObject.java

License:Apache License

@Override
public void update(PtmGame game) {
    myMoveDiff.set(mySpd);//from ww  w. ja v  a2  s.  c o m
    float ts = game.getTimeStep();
    myMoveDiff.scl(ts);
    myPos.add(myMoveDiff);
    if (myHideOnPlanet) {
        Planet np = game.getPlanetMan().getNearestPlanet();
        Vector2 npPos = np.getPos();
        float npgh = np.getGroundHeight();
        DraMan draMan = game.getDraMan();
        for (int i = 0, myDrasSize = myDras.size(); i < myDrasSize; i++) {
            Dra dra = myDras.get(i);
            if (!(dra instanceof RectSprite)) {
                continue;
            }
            if (!draMan.isInCam(dra)) {
                continue;
            }
            Vector2 draPos = dra.getPos();
            float gradSz = .25f * Const.ATM_HEIGHT;
            float distPerc = (draPos.dst(npPos) - npgh - Const.ATM_HEIGHT) / gradSz;
            distPerc = PtmMath.clamp(distPerc);
            ((RectSprite) dra).tint.a = distPerc;
        }
    } else if (myMaxFadeTime > 0) {
        myFadeTime -= ts;
        float tintPerc = myFadeTime / myMaxFadeTime;
        for (int i = 0, myDrasSize = myDras.size(); i < myDrasSize; i++) {
            Dra dra = myDras.get(i);
            if (!(dra instanceof RectSprite)) {
                continue;
            }
            RectSprite rs = (RectSprite) dra;
            rs.tint.a = PtmMath.clamp(tintPerc * rs.baseAlpha);
        }

    }
}

From source file:com.tnf.ptm.handler.input.AiPilot.java

License:Apache License

@Override
public void updateFar(PtmGame game, FarShip farShip) {
    Vector2 shipPos = farShip.getPos();
    HullConfig hullConfig = farShip.getHullConfig();
    float maxIdleDist = getMaxIdleDist(hullConfig);
    myDestProvider.update(game, shipPos, maxIdleDist, hullConfig, null);
    Vector2 dest = myDestProvider.getDest();

    Vector2 spd = farShip.getSpd();
    float angle = farShip.getAngle();
    Engine engine = farShip.getEngine();
    float ts = game.getTimeStep();
    if (dest == null || engine == null) {
        if (myPlanetBind == null) {
            if (myBindAwait > 0) {
                myBindAwait -= ts;/*from w w  w. ja va  2 s .c  om*/
            } else {
                myPlanetBind = PlanetBind.tryBind(game, shipPos, angle);
                myBindAwait = MAX_BIND_AWAIT;
            }
        }
        if (myPlanetBind != null) {
            myPlanetBind.setDiff(spd, shipPos, false);
            spd.scl(1 / ts);
            angle = myPlanetBind.getDesiredAngle();
        }
    } else {
        float toDestLen = shipPos.dst(dest);
        float desiredAngle;
        float maxIdleDistHack = .05f; // to avoid StillGuards from getting stuck inside ground
        if (myDestProvider.shouldStopNearDest() && toDestLen < maxIdleDistHack) {
            spd.set(myDestProvider.getDestSpd());
            desiredAngle = angle; // can be improved
        } else {
            desiredAngle = PtmMath.angle(shipPos, dest);
            if (myDestProvider.shouldAvoidBigObjs()) {
                desiredAngle = myMover.getBigObjAvoider().avoid(game, shipPos, dest, desiredAngle);
            }
            float desiredSpdLen = myDestProvider.getDesiredSpdLen();
            float spdLenDiff = engine.getAcc() * ts;
            float spdLen = PtmMath.approach(spd.len(), desiredSpdLen, spdLenDiff);
            if (toDestLen < spdLen) {
                spdLen = toDestLen;
            }
            PtmMath.fromAl(spd, desiredAngle, spdLen);
        }
        angle = PtmMath.approachAngle(angle, desiredAngle, engine.getMaxRotSpd() * ts);
    }

    farShip.setSpd(spd);
    farShip.setAngle(angle);

    Vector2 newPos = PtmMath.getVec(spd);
    newPos.scl(ts);
    newPos.add(shipPos);
    farShip.setPos(newPos);
    PtmMath.free(newPos);
}

From source file:com.tnf.ptm.handler.input.BeaconDestProvider.java

License:Apache License

@Override
public void update(PtmGame game, Vector2 shipPos, float maxIdleDist, HullConfig hullConfig,
        PtmShip nearestEnemy) {//  www  . j  a v  a 2  s.co  m
    BeaconHandler bh = game.getBeaconHandler();
    myDest.set(bh.getPos());
    myShouldManeuver = null;
    BeaconHandler.Action a = bh.getCurrAction();
    if (nearestEnemy != null && a == BeaconHandler.Action.ATTACK) {
        if (shipPos.dst(myDest) < shipPos.dst(nearestEnemy.getPosition()) + .1f) {
            myShouldManeuver = true;
        }
    }
    myShouldStopNearDest = STOP_AWAIT < game.getTime() - bh.getClickTime();
    myDestSpd.set(bh.getSpd());
}

From source file:com.tnf.ptm.handler.input.BigObjAvoider.java

License:Apache License

public float avoid(PtmGame game, Vector2 from, Vector2 dest, float toDestAngle) {
    float toDestLen = from.dst(dest);
    if (toDestLen > MAX_DIST_LEN) {
        toDestLen = MAX_DIST_LEN;//from   w  ww .  ja va 2 s  . com
    }
    float res = toDestAngle;
    Planet p = game.getPlanetMan().getNearestPlanet(from);
    Vector2 pPos = p.getPos();
    float pRad = p.getFullHeight();
    if (dest.dst(pPos) < pRad) {
        pRad = p.getGroundHeight();
    }
    myProj.set(pPos);
    myProj.sub(from);
    PtmMath.rotate(myProj, -toDestAngle);
    if (0 < myProj.x && myProj.x < toDestLen) {
        if (PtmMath.abs(myProj.y) < pRad) {
            toDestLen = myProj.x;
            res = toDestAngle + 45 * PtmMath.toInt(myProj.y < 0);
        }
    }
    Vector2 sunPos = p.getSys().getPos();
    float sunRad = Const.SUN_RADIUS;
    myProj.set(sunPos);
    myProj.sub(from);
    PtmMath.rotate(myProj, -toDestAngle);
    if (0 < myProj.x && myProj.x < toDestLen) {
        if (PtmMath.abs(myProj.y) < sunRad) {
            res = toDestAngle + 45 * PtmMath.toInt(myProj.y < 0);
        }
    }
    return res;
}