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

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

Introduction

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

Prototype

public Vector2 add(float x, float y) 

Source Link

Document

Adds the given components to this vector

Usage

From source file:com.redthirddivision.astilade.entities.mobs.Enemy.java

License:Apache License

@Override
public void update() {
    xTile = (int) (position.x / 64);
    yTile = (int) (position.y / 64);
    updateBounds();/*from  ww w  .ja  v  a 2  s .c  o  m*/
    stateTime += Gdx.graphics.getDeltaTime() * 8;
    if (stateTime > getCols())
        stateTime = 0;

    Vector2 tempPos = position;

    if (player.getxTile() > xTile)
        velX = speed;
    else if (player.getxTile() < xTile)
        velX = -speed;
    else
        velX = 0;
    if (player.getyTile() > yTile)
        velY = speed;
    else if (player.getyTile() < yTile)
        velY = -speed;
    else
        velY = 0;
    tempPos.add(velX, velY);

    //TODO
    if (path != null && path.contains((int) (tempPos.x / 64), (int) (tempPos.y / 64))) {
        position.add(velX, velY);
    }

    findPlayer();
    animate();
}

From source file:com.saltosion.gladiator.systems.CombatSystem.java

License:Open Source License

@Override
public void update(float deltaTime) {
    for (int i = 0; i < entities.size(); i++) {
        Entity e = entities.get(i);
        CCombat combat = cm.get(e);//ww  w  .  j a  v a  2  s  .c o m
        CPhysics obj = pm.get(e);

        if (combat.swingCdCounter > 0) {
            combat.swingCdCounter -= deltaTime;
            continue;
        }

        // Ready to swing !
        combat.getSwing().setZero();
        if (combat.inputs.get(Direction.LEFT)) {
            combat.getSwing().add(-1, 0);
        }
        if (combat.inputs.get(Direction.RIGHT)) {
            combat.getSwing().add(1, 0);
        }
        if (combat.inputs.get(Direction.UP)) {
            combat.getSwing().add(0, 1);
        }
        if (combat.inputs.get(Direction.DOWN)) {
            combat.getSwing().add(0, -1);
        }

        if (!combat.getSwing().isZero() && combat.swingCdCounter <= 0) {

            // Swinging
            Vector2 pos = obj.getPosition().cpy();

            if (combat.getSwingDirection() == Direction.LEFT) {
                pos.add(-combat.getSwingSize().x / 2, 0);
            } else if (combat.getSwingDirection() == Direction.RIGHT) {
                pos.add(combat.getSwingSize().x / 2, 0);
            } else if (combat.getSwingDirection() == Direction.UP) {
                pos.add(0, combat.getSwingSize().y / 3 * 2);
            } else if (combat.getSwingDirection() == Direction.DOWN) {
                pos.add(0, -combat.getSwingSize().y / 3 * 2);
            }
            createSwingHitbox(e, combat.getSwingDirection(), pos);

            // SFX
            Sound s = AppUtil.jukebox.returnRandomSound(AudioLoader.getSound(Name.SOUND_SWING01),
                    AudioLoader.getSound(Name.SOUND_SWING02), AudioLoader.getSound(Name.SOUND_SWING03));
            s.play(AppUtil.sfxVolume);

            // After-swing
            combat.swingCdCounter = combat.getSwingDuration();
        }
    }
}

From source file:com.stercore.code.net.dermetfan.utils.libgdx.math.GeometryUtils.java

License:Apache License

/** @param vertices the vertices to add the given values to
 *  @param x the x value to add/* w  ww.j  av  a 2  s.  c o  m*/
 *  @param y the y value to add
 *  @return the given vertices for chaining */
public static Vector2[] add(Vector2[] vertices, float x, float y) {
    for (Vector2 vertice : vertices)
        vertice.add(x, y);
    return vertices;
}

From source file:com.tehforce.sofa.logic.Character.java

License:Open Source License

/**
 * //w  w w  .j  a v a2 s  . c  om
 * @param c Target character for operation.
 * @return The direction vector to target from this Character instance.
 */
public Vector2 direction(Character c) {
    Vector2 direction = new Vector2(0, 0);

    if (c.getPosition().y > getPosition().y)
        direction.add(0, 1);
    if (c.getPosition().y < getPosition().y)
        direction.add(0, -1);
    if (c.getPosition().x > getPosition().x)
        direction.add(1, 0);
    if (c.getPosition().x < getPosition().x)
        direction.add(-1, 0);

    return direction.nor();
}

From source file:com.tnf.ptm.entities.chunk.ChunkFiller.java

License:Apache License

/**
 * Fill the background of a given chunk with floating junk.
 *
 * @param game    The {@link PtmGame} instance to work with
 * @param chunk   The coordinates of the chunk
 * @param remover//from  ww  w  .  j  a  va  2  s .c  o  m
 * @param farBg   Determines which of the background layers should be filled. <code>true</code> fills the layers furthest away, <code>false</code> fills the closer one.
 */
public void fill(PtmGame game, Vector2 chunk, RemoveController remover, boolean farBg) {
    if (DebugOptions.NO_OBJS) {
        return;
    }

    // Determine the center of the chunk by multiplying the chunk coordinates with the chunk size and adding half a chunk's size
    Vector2 chCenter = new Vector2(chunk);
    chCenter.scl(Const.CHUNK_SIZE);
    chCenter.add(Const.CHUNK_SIZE / 2, Const.CHUNK_SIZE / 2);

    // Define the density multiplier for different layers of junk in the far background
    float[] densityMul = { 1 };

    // Get the environment configuration
    SpaceEnvConfig conf = getConfig(game, chCenter, densityMul, remover, farBg);

    if (farBg) {
        fillFarJunk(game, chCenter, remover, DraLevel.FAR_DECO_3, conf, densityMul[0]);
        fillFarJunk(game, chCenter, remover, DraLevel.FAR_DECO_2, conf, densityMul[0]);
        fillFarJunk(game, chCenter, remover, DraLevel.FAR_DECO_1, conf, densityMul[0]);
    } else {
        fillDust(game, chCenter, remover);
        fillJunk(game, remover, conf, chCenter);
    }
}

From source file:com.ukos.logics.Point.java

public Point add(float x, float y) {
    Vector2 aux = point.cpy();
    return new Point(aux.add(x, y));
}

From source file:com.xemplar.games.android.nerdshooter.items.Launcher.java

License:Open Source License

public boolean onFire() {
    if (e != null) {
        int res = e.inventory.invHasItemType(Ammo.class);
        if (res != -1) {
            ItemStack stack = e.inventory.getItem(res);
            Ammo amm = (Ammo) stack.getItem(0);

            Vector2 pos = e.getPosition().cpy();
            float addX = e.isFacingLeft() ? -(0.1F + amm.pro.getBounds().getWidth())
                    : (0.1F + e.getBounds().getWidth());
            float deg = e.isFacingLeft() ? 180F : 0F;

            pos.add(addX, 0);
            amm.launch(pos, 20F, deg);// ww  w.j  a v a  2 s.co m
        }
    }
    return false;
}

From source file:com.xemplar.games.android.nerdshooter.model.Level.java

License:Open Source License

private Vector2 loadLevel(String pack, int num) {
    Vector2 value = new Vector2(1, 1);

    width = 50;//from   www  .  j a v a2 s. com
    height = 7;

    if (num == -1) {
        File file = new File(Gdx.files.getExternalStoragePath(), "levelExp.txt");

        FileHandle handle = new FileHandle(file);

        value = loadFile(handle);
    } else {
        String fileName = num + "";
        if (num < 100) {
            fileName = "0" + num;
        }
        if (num < 10) {
            fileName = "00" + num;
        }

        FileHandle handle = Gdx.files.internal("levels/" + pack + "/" + fileName + ".nsl");
        value = loadFile(handle);
    }

    value.add(0.25F, 0);

    return value;
}

From source file:de.r2soft.r2physics.instances.OrbitalBody.java

License:Open Source License

private void applyForce(OrthographicCamera camera) {
    R2Float AccDT = new R2Float(acceleration.x, acceleration.y);
    AccDT.scl(Gdx.graphics.getDeltaTime()); // todo get delta t
    velocity.add(AccDT);/*from   w ww . j  av a  2  s .c om*/
    R2Float VelDT = new R2Float(velocity.x, velocity.y);
    VelDT.scl(Gdx.graphics.getDeltaTime()); // todo get delta t
    position.add(VelDT);

    /** Draws debug vector. Remove this from build version */
    R2Float tmp = parent.getPosition().cpy();
    renderer.setProjectionMatrix(camera.combined);
    renderer.begin(ShapeType.Line);
    renderer.setColor(1, 1, 1, 1);
    Vector2 tmp2 = new Vector2(acceleration.x, acceleration.y);
    tmp2.scl(10f);
    tmp2.add(tmp.x, tmp.y);
    renderer.line(new Vector2(tmp.x, tmp.y), tmp2);
    renderer.end();

    parent.updatePosition(position);
}

From source file:jordanlw.gdxGame.Game.java

License:Open Source License

public void render() {
    Vector2 bulletVector = new Vector2();
    Vector2 relativeMousePosition = new Vector2();
    Vector2 distanceToMouse = new Vector2();
    Boolean gunFiredThisFrame = false;
    float delta = Gdx.graphics.getDeltaTime();
    movementThisFrame = false;//from  www .  j  a  v  a  2 s  .c  om

    Gdx.gl.glClearColor(0, 0, 0, 0);
    Gdx.gl.glClear(0);
    handleEnemyWaves();

    //Handle player wanting to pause
    if (Gdx.input.isKeyJustPressed(Input.Keys.P)) {
        if (gamePaused) {
            if (!isGameCreated) {
                setupGame();
            }
            gamePaused = false;
            aMusicLibrary.backgroundMusic.play();
        } else {
            gamePaused = true;
            aMusicLibrary.backgroundMusic.pause();
        }
    }

    //Does the user want multiplayer?
    if (Gdx.input.isKeyJustPressed(Input.Keys.M)) {
        NetworkSetup.getTextInput("Multiplayer Network Address");
    }

    if (Gdx.input.isKeyJustPressed(Input.Keys.H)) {
        NetworkSetup.getAnswer("Host Multiplayer?");
    }

    if (!gamePaused) {
        totalTime += delta;
        for (Player player : players) {
            player.secondsDamaged -= delta;
        }
        //Update player rotation wrt mouse position
        getLocalPlayer().rotation = (float) Mouse
                .angleBetween(getLocalPlayer().position.getPosition(new Vector2()));

        Zombie.zombeGroanSoundTimer += delta;
        if (Zombie.zombeGroanSoundTimer > 6f) {
            int index = (int) (Math.random() * (aMusicLibrary.zombieSounds.length - 1));
            aMusicLibrary.zombieSounds[index].setVolume(aMusicLibrary.zombieSounds[index].play(),
                    0.5f * volume);
            Zombie.zombeGroanSoundTimer = 0;
        }

        handleInput(relativeMousePosition, mousePressedPosition, distanceToMouse, bulletVector);

        //Anything serverside eg. enemy movement, medkit respawning.
        if (isServer) {
            if (!aMusicLibrary.backgroundMusic.isPlaying()) {
                for (Player player : players) {
                    player.health = 0;
                }
            }
            for (Player player : players) {
                medkit.time += delta;
                if (medkit.time > Medkit.SECS_TILL_DISAPPEAR && medkit.health <= 0) {
                    medkit.health = Medkit.healthGiven;
                    medkit.position.setPosition((float) (camera.viewportWidth * Math.random()),
                            (float) (camera.viewportHeight * Math.random()));
                } else if (medkit.time >= Medkit.SECS_TILL_DISAPPEAR
                        && player.position.overlaps(medkit.position)) {
                    player.health += medkit.health;
                    medkit.health = 0;
                    medkit.time = 0;
                    aMusicLibrary.medkitSound.play(0.3f * volume);
                    if (player.health > 100) {
                        player.health = 100;
                    }
                }

                for (Zombie enemy : enemies) {
                    if (enemy.health <= 0) {
                        continue;
                    }
                    enemy.secondsDamaged -= delta;

                    Vector2 vecPlayer = new Vector2();
                    Vector2 vecEnemy = new Vector2();
                    enemy.position.getPosition(vecEnemy);
                    player.position.getPosition(vecPlayer);

                    Vector2 tmpEnemy = new Vector2(
                            vecPlayer.sub(vecEnemy).nor().scl(delta * enemy.walkingSpeed));
                    if (player.position.getPosition(new Vector2())
                            .dst(enemy.position.getPosition(new Vector2())) < 300) {
                        tmpEnemy.rotate(enemy.swarmAngle);
                    }

                    enemy.rotation = tmpEnemy.angle();
                    tmpEnemy.add(enemy.position.x, enemy.position.y);
                    enemy.position.setPosition(tmpEnemy);
                }
            }
        }
        //Respond to player pressing mouse button
        if (mousePressedPosition.x != -1 && mousePressedPosition.y != -1 && getLocalPlayer().health > 0) {
            //Gun sound for player
            if (totalTime > timeGunSound) {
                timeGunSound = totalTime + 0.5f;
                aMusicLibrary.gunSound.play(0.25f * volume);
                //aMusicLibrary.gunSound.setPitch(soundId, 1 + (long) (0.3f * Math.random()));
                gunFiredThisFrame = true;
                shootingTime = torsoAnimLength;
                Collections.sort(enemies, new ZombieDistance());
                for (Zombie enemy : enemies) {
                    if (enemy.health <= 0) {
                        continue;
                    }

                    Vector2 vecPlayer = new Vector2();
                    getLocalPlayer().position.getCenter(vecPlayer);

                    float angle = (float) Mouse.angleBetween(vecPlayer);
                    Vector2 vecAngle = new Vector2(vecPlayer);
                    Vector2 tmp = new Vector2(1, 1);
                    tmp.setAngle(angle).nor().scl(98765);
                    vecAngle.add(tmp);

                    if (Intersector.intersectSegmentCircle(vecPlayer, vecAngle,
                            enemy.position.getCenter(new Vector2()),
                            (enemy.position.width / 2) * (enemy.position.width / 2))) {
                        enemy.secondsDamaged = 0.5f;
                        enemy.health -= 35;
                        if (enemy.health <= 0) {
                            gold.saveEnemy(currentWave, enemies.indexOf(enemy));
                        }
                        break;
                    }
                }
            }
        }
    }
    camera.update();
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.setColor(Color.WHITE);
    for (int width = 0; width < windowSize.x; width += backgroundTexture.getWidth()) {
        for (int height = 0; height < windowSize.y; height += backgroundTexture.getHeight()) {
            batch.draw(backgroundTexture, width, height);
        }
    }
    medkit.draw(batch);
    gold.draw(batch);

    //Draw enemies
    for (Zombie enemy : enemies) {
        enemy.draw(batch, totalTime);
    }
    batch.setColor(Color.WHITE);

    for (Player player : players) {
        player.draw(batch, totalTime, delta);
    }

    if (getLocalPlayer().health <= 0) {
        batch.draw(gameOverTexture, camera.viewportWidth / 2 - gameOverTexture.getWidth() / 2,
                camera.viewportHeight / 2 - gameOverTexture.getHeight() / 2);
    } else if (gamePaused) {
        batch.draw(gameStartTexture, camera.viewportWidth / 2 - gameStartTexture.getWidth() / 2,
                camera.viewportHeight / 2 - gameStartTexture.getHeight() / 2);
    }

    batch.setColor(Color.YELLOW);
    if (gunFiredThisFrame) {
        //noinspection SuspiciousNameCombination
        batch.draw(singlePixel, getLocalPlayer().position.x, getLocalPlayer().position.y, 0, 0, 1, 1, 1,
                distanceToMouse.x, 180 + (float) Math.toDegrees(
                        Math.atan2((double) relativeMousePosition.x, (double) relativeMousePosition.y)));

    }
    batch.end();

    isLeftMousePressedThisFrame = false;
    mousePressedPosition.set(-1, -1);
}