Search in sources :

Example 1 with Weapon

use of com.agorria.shootandmove.game.Weapon in project ShootAndRun by IonAgorria.

the class Player method update.

/**
 * Player specific update
 *
 * @param delta since last update
 * @param buttons that user pressed since last update
 * @param joystick value
 * @param gyroscope value
 */
public void update(float delta, Set<GameUI.BUTTON> buttons, Vector2f joystick, float gyroscope) {
    // Set velocity from joystick
    float movementAmount = joystick.y * PLAYER_MOVEMENT_SPEED;
    velocity.set(movementAmount, 0, 0);
    // Set rotation or lateral movement
    float lateral;
    float rotation;
    if (buttons.contains(GameUI.BUTTON.MOVEMENT)) {
        lateral = joystick.x;
        rotation = gyroscope;
    } else {
        lateral = gyroscope;
        rotation = joystick.x;
    }
    // Apply lateral movement
    velocity.z = lateral * PLAYER_MOVEMENT_SPEED;
    // Change direction from joystick, apply delta
    float directionAmount = rotation * PLAYER_ROTATION_SPEED * delta;
    direction = Utilities.warpValue(direction + directionAmount, (float) Math.PI);
    // Rotate velocity according to direction
    velocity.rotateXZ(direction);
    // Update entity manually
    super.update(delta);
    // Update weapon
    Weapon weapon = weapons.get(weaponIndex);
    weapon.update(delta);
    // Handle buttons
    if (buttons.contains(GameUI.BUTTON.SHOOT)) {
        Vector3f weaponLocation = new Vector3f();
        WEAPON_LOCATION.rotateXZ(direction + Constants.HALF_PI, weaponLocation);
        position.add(weaponLocation, weaponLocation);
        Projectile projectile = weapon.shoot(this, weaponLocation, direction);
        if (projectile != null) {
            changed = true;
            world.getView().vibrate(Constants.VIBRATE_SHOOT);
        }
    } else if (buttons.contains(GameUI.BUTTON.PREV)) {
        int oldWeaponIndex = weaponIndex;
        if (weaponIndex == 0) {
            weaponIndex = weapons.size() - 1;
        } else {
            weaponIndex -= 1;
        }
        changed = oldWeaponIndex != weaponIndex;
    } else if (buttons.contains(GameUI.BUTTON.NEXT)) {
        int oldWeaponIndex = weaponIndex;
        if (weaponIndex + 1 < weapons.size()) {
            weaponIndex += 1;
        } else {
            weaponIndex = 0;
        }
        changed = oldWeaponIndex != weaponIndex;
    }
}
Also used : Vector3f(com.agorria.shootandmove.math.Vector3f) Weapon(com.agorria.shootandmove.game.Weapon)

Example 2 with Weapon

use of com.agorria.shootandmove.game.Weapon in project ShootAndRun by IonAgorria.

the class Player method pickItem.

/**
 * Called when player picks item
 *
 * @param item picked
 */
public void pickItem(Item item) {
    world.getView().vibrate(Constants.VIBRATE_PICKUP);
    Item.ItemType itemType = item.getItemType();
    if (itemType == Item.ItemType.Health) {
        changeHealth(50);
    } else {
        Weapon.WeaponType weaponType;
        switch(itemType) {
            case Pistol:
                weaponType = Weapon.WeaponType.PlayerPistol;
                break;
            case Plasma:
                weaponType = Weapon.WeaponType.PlayerPlasma;
                break;
            case Missile:
                weaponType = Weapon.WeaponType.PlayerMissile;
                break;
            default:
                throw new RuntimeException("Unknown item type");
        }
        // Find the weapon
        Weapon weapon = null;
        for (Weapon current : weapons) {
            if (weaponType == current.getType()) {
                weapon = current;
                break;
            }
        }
        // Create if doesn't have
        if (weapon == null) {
            weapon = new Weapon(world, weaponType, false);
            weapons.add(weapon);
            // Reorder
            Collections.sort(weapons, Weapon.COMPARATOR);
            // This way the player is aware of acquired weapon
            weaponIndex = weapons.indexOf(weapon);
        }
        // Add cartridge
        weapon.addCartridge();
        changed = true;
    }
}
Also used : Weapon(com.agorria.shootandmove.game.Weapon)

Aggregations

Weapon (com.agorria.shootandmove.game.Weapon)2 Vector3f (com.agorria.shootandmove.math.Vector3f)1