Search in sources :

Example 1 with Vector3f

use of com.agorria.shootandmove.math.Vector3f 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 Vector3f

use of com.agorria.shootandmove.math.Vector3f in project ShootAndRun by IonAgorria.

the class Robot method update.

/**
 * Handles robot "AI" by shooting near player or moving closer on sight
 *
 * @param delta since update
 */
@Override
public void update(float delta) {
    super.update(delta);
    Vector2f positionXZ = position.getXZ(new Vector2f());
    // Update weapon
    weapon.update(delta);
    // Check if can fire to player
    Player player = world.getPlayer();
    boolean playerSight = false;
    if (player != null) {
        // Check if intersects with world (there is clear sight)
        player.getPosition().getXZ(tmp2f);
        if (!world.isIntersectSegment(positionXZ, tmp2f)) {
            playerSight = true;
            // Set last position
            lastPosition.set(tmp2f);
            // Check distance to player
            float fireDistance = robotType.weaponType.projectileType.distance / 2f;
            if (positionXZ.distance(tmp2f) <= fireDistance) {
                // Calculate weapon data
                Vector3f weaponLocation = new Vector3f();
                float direction = positionXZ.getAngle(lastPosition);
                direction += (world.getRandom().nextFloat() - 0.5f) * FIRE_MISFIRE;
                robotType.weaponLocation.rotateXZ(direction + Constants.HALF_PI, weaponLocation);
                position.add(weaponLocation, weaponLocation);
                // Shot
                Projectile projectile = weapon.shoot(this, weaponLocation, direction);
                // Add extra delay
                if (projectile != null) {
                    float delay = (world.getRandom().nextFloat() - 0.4f) * 2;
                    weapon.addNextShot(FIRE_RANDOM * delay);
                }
            }
        }
    }
    // Set velocity vector to go there or stop
    float direction = positionXZ.getAngle(lastPosition);
    if (positionXZ.distance(lastPosition) > (playerSight ? robotType.followDistance : LAST_SIGHT)) {
        velocity.set(robotType.movementSpeed, 0, 0);
        velocity.rotateXZ(direction);
    } else {
        velocity.set(0);
    }
}
Also used : Vector2f(com.agorria.shootandmove.math.Vector2f) Vector3f(com.agorria.shootandmove.math.Vector3f)

Example 3 with Vector3f

use of com.agorria.shootandmove.math.Vector3f in project ShootAndRun by IonAgorria.

the class Warp method handleCollisionEntity.

/**
 * Causes world level switch upon touch by player
 *
 * @param other entity
 * @return always false
 */
@Override
public boolean handleCollisionEntity(Entity other) {
    world.setRequestedLevel(level);
    // Store relative position of player on this level
    Vector3f relative = other.getPosition().sub(position, new Vector3f());
    relative.y = 0f;
    world.setPlayerPosition(relative);
    // Warp don't move
    return false;
}
Also used : Vector3f(com.agorria.shootandmove.math.Vector3f)

Example 4 with Vector3f

use of com.agorria.shootandmove.math.Vector3f in project ShootAndRun by IonAgorria.

the class Door method updateDrawable.

/**
 * Fix drawable position as {@link Entity#updateDrawable} centers it
 */
@Override
protected void updateDrawable() {
    super.updateDrawable();
    Vector3f drawablePosition = getDrawable().getPosition();
    drawablePosition.x = rectangle.x;
    drawablePosition.z = rectangle.y;
}
Also used : Vector3f(com.agorria.shootandmove.math.Vector3f)

Example 5 with Vector3f

use of com.agorria.shootandmove.math.Vector3f in project ShootAndRun by IonAgorria.

the class WorldLoader method parseObjectLayers.

/**
 * Parses map object layers
 *
 * @param map to use
 */
private void parseObjectLayers(Element map) {
    NodeList objectGroups = map.getElementsByTagName("objectgroup");
    for (int objectGroupsIndex = 0; objectGroupsIndex < objectGroups.getLength(); objectGroupsIndex++) {
        Element objectGroup = (Element) objectGroups.item(objectGroupsIndex);
        NodeList objectsList = objectGroup.getElementsByTagName("object");
        String objectGroupName = objectGroup.getAttribute("name");
        // Check layers
        LayerType layerType;
        try {
            layerType = LayerType.valueOf(objectGroupName);
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Unknown object layer: " + objectGroupName);
        }
        if (layerType != LayerType.Entities)
            continue;
        // Parse objects in group
        for (int objectIndex = 0; objectIndex < objectsList.getLength(); objectIndex++) {
            Element object = (Element) objectsList.item(objectIndex);
            // Get attributes of object
            Vector3f objectPosition = new Vector3f(Float.parseFloat(object.getAttribute("x")) / tileAtlas.regionSize, 0f, Float.parseFloat(object.getAttribute("y")) / tileAtlas.regionSize);
            String objectName = object.getAttribute("name");
            String objectType = object.getAttribute("type");
            Vector3f objectSize = null;
            if (!object.getAttribute("width").isEmpty() && !object.getAttribute("height").isEmpty()) {
                objectSize = new Vector3f(Float.parseFloat(object.getAttribute("width")) / tileAtlas.regionSize, 0f, Float.parseFloat(object.getAttribute("height")) / tileAtlas.regionSize);
            }
            entities.add(new EntitySpawn(objectName, objectType, objectPosition, objectSize));
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Vector3f(com.agorria.shootandmove.math.Vector3f)

Aggregations

Vector3f (com.agorria.shootandmove.math.Vector3f)9 Player (com.agorria.shootandmove.entities.Player)2 SharedPreferences (android.content.SharedPreferences)1 Door (com.agorria.shootandmove.entities.Door)1 Item (com.agorria.shootandmove.entities.Item)1 LockedDoor (com.agorria.shootandmove.entities.LockedDoor)1 Robot (com.agorria.shootandmove.entities.Robot)1 Warp (com.agorria.shootandmove.entities.Warp)1 Weapon (com.agorria.shootandmove.game.Weapon)1 Drawable (com.agorria.shootandmove.graphics.Drawable)1 DrawableBatch (com.agorria.shootandmove.graphics.DrawableBatch)1 Vector2f (com.agorria.shootandmove.math.Vector2f)1 ArrayList (java.util.ArrayList)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1