use of co.paralleluniverse.spacebase.AABB in project spaceships-demo by puniverse.
the class Spaceship method chaseAndShoot.
private void chaseAndShoot() throws SuspendExecution, InterruptedException {
record(1, "Spaceship", "chaseAndShoot", "%s: locked", this);
// check lock range, chase, shoot
boolean foundLockedOn = false;
try (Element<Record<SpaceshipState>> target = global.sb.readElement(lockedOn)) {
final Record<SpaceshipState> lockedSpaceship;
if (target != null && (lockedSpaceship = target.get()) != null) {
foundLockedOn = true;
final AABB aabb = getAABB(lockedSpaceship);
// double angularDiversion = abs(atan2(lockedSpaceship.vx, lockedSpaceship.vy) - getCurrentHeading(shootTime));
if (inShotRange(aabb) & global.random.nextGaussian() < SHOOT_PROBABLITY) {
record(1, "Spaceship", "chaseAndShoot", "%s: shootrange", this);
final double range = mag(lockedSpaceship.get($x) - state.get($x), lockedSpaceship.get($y) - state.get($y));
final long now = global.now();
shoot(range, now);
lockedSpaceship.get($spaceship).send(new Shot(state.get($x), state.get($y)));
}
if (inLockRange(aabb)) {
record(1, "Spaceship", "chaseAndShoot", "%s: lockrange", this);
chase(lockedSpaceship);
} else {
record(1, "Spaceship", "chaseAndShoot", "%s: release lock", this);
// not in range, release lock
lockOnTarget(null);
}
}
}
if (!foundLockedOn)
lockOnTarget(null);
}
use of co.paralleluniverse.spacebase.AABB in project spaceships-demo by puniverse.
the class Spaceship method applyNeighborRejectionAndMove.
private void applyNeighborRejectionAndMove(final long now) throws InterruptedException, SuspendExecution {
record(1, "Spaceship", "applyNeighborRejectionAndMove", "%s", this);
AABB myAABB = getAABB();
try (ResultSet<Record<SpaceshipState>> rs = global.sb.queryForUpdate(SpatialQueries.range(myAABB, global.range), SpatialQueries.equals(state, myAABB), false)) {
// Debug.exit(1);
assert rs.getResultForUpdate().size() == 1;
ElementUpdater<Record<SpaceshipState>> updater = rs.getResultForUpdate().iterator().next();
// this is me
assert updater.elem().equals(state);
applyNeighborRejection(rs.getResultReadOnly(), now);
move(now);
state.set($status, status);
state.set($timeFired, timeFired);
state.set($shotLength, shotLength);
state.set($exVelocityUpdated, exVelocityUpdated);
updater.update(getAABB());
}
reduceExternalVelocity(now);
}
use of co.paralleluniverse.spacebase.AABB in project spaceships-demo by puniverse.
the class Spaceship method move.
/**
* Update ship position
*/
private void move(long now) {
assert status == Status.ALIVE;
state.set($exVx, exVx);
state.set($exVy, exVy);
final long lastMoved = state.get($lastMoved);
if (lastMoved > 0 & now > lastMoved) {
double x = state.get($x);
double y = state.get($y);
double vx = state.get($vx);
double vy = state.get($vy);
double ax = state.get($ax);
double ay = state.get($ay);
final AABB bounds = global.bounds;
final double duration = (double) (now - lastMoved) / TimeUnit.SECONDS.toMillis(1);
// * Math.signum(duration);
final double duration2 = duration * duration;
x = x + (vx + exVx) * duration + ax * duration2 / 2.0;
y = y + (vy + exVy) * duration + ay * duration2 / 2.0;
vx = vx + ax * duration;
vy = vy + ay * duration;
// before limitSpeed
state.set($vx, vx);
state.set($vy, vy);
limitSpeed();
vx = state.get($vx);
vy = state.get($vy);
assert !Double.isNaN(vx + vy);
if (x > bounds.max(X) || x < bounds.min(X)) {
x = min(x, bounds.max(X));
x = max(x, bounds.min(X));
vx = -vx * SPEED_BOUNCE_DAMPING;
ax = 0;
}
if (y > bounds.max(Y) || y < bounds.min(Y)) {
y = min(y, bounds.max(Y));
y = max(y, bounds.min(Y));
vy = -vy * SPEED_BOUNCE_DAMPING;
ay = 0;
}
assert !Double.isNaN(x + y);
state.set($x, x);
state.set($y, y);
state.set($vx, vx);
state.set($vy, vy);
state.set($ax, ax);
state.set($ay, ay);
}
state.set($lastMoved, now);
}
Aggregations