use of com.agorria.shootandmove.math.Vector2f 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.Vector2f in project ShootAndRun by IonAgorria.
the class GameUI method getJoystick.
/**
* @return joystick position
*/
Vector2f getJoystick() {
lock.lock();
Vector2f vector;
try {
vector = new Vector2f(joystickVector);
} finally {
lock.unlock();
}
return vector;
}
use of com.agorria.shootandmove.math.Vector2f in project ShootAndRun by IonAgorria.
the class GameUI method processJoystick.
/**
* Processes the joystick input
*
* @param x of pointer
* @param y of pointer
*/
private void processJoystick(float x, float y) {
float size = CONTROLS_SIZE / 2;
// This sets the center of joystick as 0, left/bottom as negative and right/top as positive
Vector2f center = joystickRectangle.getCenter();
x -= center.x;
y -= center.y;
// Limit and normalize values
x = Utilities.limitValue(x, size) / size;
y = Utilities.limitValue(y, size) / size;
// Check if in dead zone
x = Utilities.zoneValue(x, JOYSTICK_DEAD_ZONE);
y = Utilities.zoneValue(y, JOYSTICK_DEAD_ZONE);
// Set the vector
joystickVector.set(x, y);
// Log.d(TAG, "Joystick " + joystickVector);
}
use of com.agorria.shootandmove.math.Vector2f in project ShootAndRun by IonAgorria.
the class World method update.
/**
* Updates the world
*
* @param delta time since last update
*/
void update(float delta) {
// Update entities except player
List<Entity> entitiesCollision = new ArrayList<>();
for (Entity entity : entities) {
// This removes entity as is not added to next list
if (entity.isDestroyed()) {
removedEntity(entity);
continue;
}
// Update entity
entity.update(delta);
// Add to next
entitiesNext.add(entity);
if (entity.getCollision() != null) {
entitiesCollision.add(entity);
}
}
// Check if collides with world
for (Entity entity : entitiesCollision) {
for (Rectangle collision : collisions) {
if (!entity.ignoreCollisionWorld(collision) && entity.checkCollision(collision)) {
boolean unhandled = entity.handleCollisionWorld(collision);
if (unhandled) {
Vector2f vector = entity.resolveCollision(collision);
if (vector != null) {
entity.getPosition().add(vector.x, 0, vector.y);
entity.updateAfterPosition();
}
}
}
}
}
// Check if entities collide
for (Iterator<Entity> iterator = entitiesCollision.iterator(); iterator.hasNext(); ) {
Entity entity = iterator.next();
// Check other entities
for (Entity other : entitiesCollision) {
CollisionInterface otherCollision = other.getCollision();
if (entity != other && !(entity.ignoreCollisionEntity(other) || other.ignoreCollisionEntity(entity)) && entity.checkCollision(otherCollision)) {
boolean entityUnhandled = entity.handleCollisionEntity(other) && !other.ignoreCollisionResolution(entity);
boolean otherUnhandled = other.handleCollisionEntity(entity) && !entity.ignoreCollisionResolution(other);
if (entityUnhandled) {
Vector2f vector = entity.resolveCollision(otherCollision);
if (vector != null) {
if (otherUnhandled) {
// Handle both by distributing equally at opposing direction
vector.mul(0.5f);
other.getPosition().add(-vector.x, 0, -vector.y);
other.updateAfterPosition();
}
// Handle entity
entity.getPosition().add(vector.x, 0, vector.y);
entity.updateAfterPosition();
}
} else if (otherUnhandled) {
// Only handle other
Vector2f vector = other.resolveCollision(entity.getCollision());
if (vector != null) {
other.getPosition().add(vector.x, 0, vector.y);
other.updateAfterPosition();
}
}
}
}
// Remove entity as we have checked it
iterator.remove();
}
// Remove entities that are destroyed and get drawables
List<Drawable> entitiesDrawable = new ArrayList<>();
for (Iterator<Entity> iterator = entitiesNext.iterator(); iterator.hasNext(); ) {
Entity entity = iterator.next();
if (entity.isDestroyed()) {
// Remove entity
removedEntity(entity);
iterator.remove();
} else {
// Add to draw
Drawable drawable = entity.getDrawable();
if (drawable != null) {
entitiesDrawable.add(drawable);
}
}
}
// Set the current entities from next
entities.clear();
entities = entitiesNext;
entitiesNext = new ArrayList<>();
// Update camera
if (player != null && !player.isDestroyed()) {
updateCamera();
}
// Submit batch to draw entities
DrawableBatch batch = view.createBatch(DrawableBatch.SLOT.Entities, DrawableBatch.MODE.PerspectiveDepthBlend, entitiesDrawable, cameraPosition, cameraRotation, Color.WHITE);
view.submitBatch(batch);
}
use of com.agorria.shootandmove.math.Vector2f in project ShootAndRun by IonAgorria.
the class ButtonElement method onDraw.
/**
* Draws button and text if any
*
* @param view to use
* @param drawables list that is going to be draw
*/
@Override
public void onDraw(GameView view, List<Drawable> drawables) {
super.onDraw(view, drawables);
// Draw drawable
drawables.add(drawable);
// Draw text
if (text != null) {
Vector2f center = rectangle.getCenter();
view.drawText(drawables, center.x, center.y, 0, rectangle.height * TEXT_BUTTON, true, true, text);
}
}
Aggregations