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;
}
}
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);
}
}
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;
}
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;
}
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));
}
}
}
Aggregations