Search in sources :

Example 1 with SpaceshipState

use of co.paralleluniverse.spaceships.SpaceshipState 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);
}
Also used : TransactionalRecord(co.paralleluniverse.db.record.TransactionalRecord) Record(co.paralleluniverse.data.record.Record) StrandedTransactionalRecord(co.paralleluniverse.db.record.StrandedTransactionalRecord) SpaceshipState(co.paralleluniverse.spaceships.SpaceshipState) MutableAABB(co.paralleluniverse.spacebase.MutableAABB) AABB(co.paralleluniverse.spacebase.AABB)

Example 2 with SpaceshipState

use of co.paralleluniverse.spaceships.SpaceshipState in project spaceships-demo by puniverse.

the class GLPort method display.

@Override
public void display(GLAutoDrawable drawable) {
    try {
        final GL3 gl = drawable.getGL().getGL3();
        shaderState.bind(gl);
        vao.bind(gl);
        int spaceshipLoc = gl.glGetUniformLocation(shaderProgram.program(), "spaceshipTex");
        gl.glUniform1i(spaceshipLoc, 0);
        gl.glActiveTexture(GL.GL_TEXTURE0);
        gl.glBindTexture(GL.GL_TEXTURE_2D, spaceshipTex.getTextureObject(gl));
        int explosionLoc = gl.glGetUniformLocation(shaderProgram.program(), "explosionTex");
        gl.glUniform1i(explosionLoc, 1);
        gl.glActiveTexture(GL.GL_TEXTURE0 + 1);
        gl.glBindTexture(GL.GL_TEXTURE_2D, explosionTex.getTextureObject(gl));
        vertices.clear();
        colors.clear();
        final FloatBuffer verticesb = (FloatBuffer) vertices.getBuffer();
        final FloatBuffer colorsb = (FloatBuffer) colors.getBuffer();
        long now = global.now();
        long clock = System.currentTimeMillis();
        fixPort(clock, true);
        MutableAABB currentPort = getCurrentPort(clock);
        portToMvMatrix(currentPort);
        double margins = WIDTH_MARGINS;
        final int n;
        if (clock - lastQueryTime > SB_QUERY_RATE) {
            n = query(now, SpatialQueries.contained(AABB.create(currentPort.min(X) - margins, currentPort.max(X) + margins, currentPort.min(Y) - margins, currentPort.max(Y) + margins)));
            lastQueryTime = clock;
        } else {
            n = indexGen.get();
            lastDispTime = clock;
        }
        int countInPort = 0;
        for (Record<SpaceshipState> s : ships.slice(0, n)) {
            long extrapolationTime = global.extrapolate ? Math.min(now, s.get($lastMoved) + MAX_EXTRAPOLATION_DURATION) : s.get($lastMoved);
            Spaceship.getCurrentLocation(s, extrapolationTime, verticesb);
            if (// 0.01 - start blow animation, 1.0 - end of animation
            s.get($blowTime) > 0)
                colorsb.put(Math.min(1.0f, (now - s.get($blowTime)) / EXPLOSION_DURATION));
            else
                // ship isn't blowing up
                colorsb.put(0);
            colorsb.put((float) Spaceship.getCurrentHeading(s, extrapolationTime));
            // put the shotLength (0 for ship that's not firing)
            colorsb.put(now - s.get($timeFired) < SHOOT_DURATION ? (float) s.get($shotLength) : 0f);
            if (portContains(s.get($x), s.get($y)))
                countInPort++;
        }
        setTitle((glxNode >= 0 ? "Node " + glxNode + ": " : "") + countInPort + " Spaceships " + (int) (port.max(X) - port.min(X)) + "x" + (int) (port.max(Y) - port.min(Y)));
        vertices.flip();
        colors.flip();
        int numElems = verticesb.limit() / 2;
        vertices.write(gl, 0, numElems);
        colors.write(gl, 0, numElems);
        shaderState.setUniform(gl, "in_Matrix", 4, 4, pmv.glGetMvMatrixf());
        gl.glClear(GL3.GL_COLOR_BUFFER_BIT);
        gl.glDrawArrays(GL3.GL_POINTS, 0, numElems);
        vao.unbind(gl);
        shaderState.unbind(gl);
    } catch (Throwable t) {
        System.err.println("XXXXXX");
        t.printStackTrace();
        throw t;
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) MutableAABB(co.paralleluniverse.spacebase.MutableAABB) SpaceshipState(co.paralleluniverse.spaceships.SpaceshipState) GL3(javax.media.opengl.GL3) DebugGL3(javax.media.opengl.DebugGL3)

Aggregations

MutableAABB (co.paralleluniverse.spacebase.MutableAABB)2 SpaceshipState (co.paralleluniverse.spaceships.SpaceshipState)2 Record (co.paralleluniverse.data.record.Record)1 StrandedTransactionalRecord (co.paralleluniverse.db.record.StrandedTransactionalRecord)1 TransactionalRecord (co.paralleluniverse.db.record.TransactionalRecord)1 AABB (co.paralleluniverse.spacebase.AABB)1 FloatBuffer (java.nio.FloatBuffer)1 DebugGL3 (javax.media.opengl.DebugGL3)1 GL3 (javax.media.opengl.GL3)1