Search in sources :

Example 1 with Color

use of com.agorria.shootandmove.graphics.Color in project ShootAndRun by IonAgorria.

the class GameView method createBatch.

/**
 * Creates batch for current drawables in queue, this clears queue.
 *
 * @param slot to set batch
 * @param mode to use
 * @param drawables to draw in the batch
 * @param camera location when drawing
 * @param rotation of camera when drawing
 * @param color to set when drawing
 * @return batch created
 */
public DrawableBatch createBatch(DrawableBatch.SLOT slot, DrawableBatch.MODE mode, List<Drawable> drawables, Vector3f camera, Vector3f rotation, Color color) {
    // Vertices calculation
    int vertexCount = 0;
    for (Drawable drawable : drawables) {
        vertexCount += drawable.getVertexCount();
    }
    // Create buffers
    FloatBuffer positionBuffer = Utilities.getDirectByteBuffer(vertexCount * Constants.STRIDE_POSITION).asFloatBuffer();
    FloatBuffer texCoordBuffer = Utilities.getDirectByteBuffer(vertexCount * Constants.STRIDE_TEXCOORD).asFloatBuffer();
    // Fill buffers with each drawable
    TextureAtlas.Atlas atlas = null;
    int i = 0;
    int indicesCount = 0;
    List<short[]> indicesList = new ArrayList<>();
    for (Drawable drawable : drawables) {
        float[] positions = drawable.getVertexPositions();
        positionBuffer.put(positions);
        texCoordBuffer.put(drawable.getVertexTexCoords());
        short[] indices = drawable.getVertexIndices(i);
        indicesCount += indices.length;
        indicesList.add(indices);
        // Increment indices index
        i += positions.length / Constants.FLOATS_PER_POSITION;
        // Check atlas is the same
        TextureAtlas.Atlas drawableAtlas = drawable.getAtlas();
        if (atlas == null) {
            atlas = drawableAtlas;
        } else if (atlas != drawableAtlas) {
            throw new RuntimeException("This batch uses " + atlas + " but drawable has " + drawableAtlas);
        }
    }
    // Create indices buffer
    ShortBuffer indicesBuffer = Utilities.getDirectByteBuffer(indicesCount * 2).asShortBuffer();
    for (short[] indices : indicesList) {
        indicesBuffer.put(indices);
    }
    // Reset positions for reading
    positionBuffer.position(0);
    texCoordBuffer.position(0);
    indicesBuffer.position(0);
    // Create and return batch
    return new DrawableBatch(slot, mode, camera, rotation, new Color(color), atlas, positionBuffer, texCoordBuffer, indicesBuffer, indicesCount);
}
Also used : Color(com.agorria.shootandmove.graphics.Color) Drawable(com.agorria.shootandmove.graphics.Drawable) ArrayList(java.util.ArrayList) FloatBuffer(java.nio.FloatBuffer) SuppressLint(android.annotation.SuppressLint) TextureAtlas(com.agorria.shootandmove.graphics.TextureAtlas) DrawableBatch(com.agorria.shootandmove.graphics.DrawableBatch) ShortBuffer(java.nio.ShortBuffer)

Example 2 with Color

use of com.agorria.shootandmove.graphics.Color in project ShootAndRun by IonAgorria.

the class GameUI method draw.

/**
 * Draws the UI if required
 *
 * @param view of game
 * @param player instance
 */
void draw(GameView view, Player player) {
    if (player != null) {
        requireDraw |= player.hasChanged();
    }
    if (requireDraw) {
        requireDraw = false;
        List<Drawable> drawables = new ArrayList<>();
        // Draw death overlay
        if (player != null) {
            int health = player.getCurrentHealth();
            if (health <= 50)
                drawables.add(deathDrawables[0]);
            if (health <= 25)
                drawables.add(deathDrawables[1]);
        }
        // Draw UI per state
        Color color = null;
        String text = null;
        if (currentGameState == GameState.Menu) {
            // Draw current menu
            color = Color.WHITE;
            Menu menu = menuMap.get(currentMenu);
            if (menu != null) {
                menu.draw(view, drawables);
            }
        } else if (currentGameState == GameState.Active) {
            color = Color.GRAY_LIGHT_50;
            drawables.addAll(gameDrawables);
            // Movement mode
            if (movementDrawables != null) {
                drawables.add(movementDrawables[movementMode ? 1 : 0]);
            }
            // Add player stuff
            if (player != null) {
                Weapon weapon = player.getWeapon();
                Drawable weaponDrawable = weapon.getDrawable();
                float playerPadding = (1 - PLAYER_PADDING) - PLAYER_SIZE;
                float middle = aspect / 2f;
                // Draw player health
                int health = player.getCurrentHealth();
                view.drawText(drawables, middle, playerPadding, 0, PLAYER_SIZE, false, false, String.valueOf(health));
                // Draw weapon
                weaponDrawable.getPosition().set(middle - PLAYER_SIZE - PLAYER_SPACE, playerPadding - PLAYER_SIZE - PLAYER_SPACE, 0);
                weaponDrawable.getSize().set(PLAYER_SIZE, PLAYER_SIZE, 0);
                drawables.add(weaponDrawable);
                // Draw weapon ammo
                view.drawText(drawables, middle, playerPadding - PLAYER_SIZE - PLAYER_SPACE, 0, PLAYER_SIZE, false, false, String.valueOf(weapon.getShots()));
            }
        } else if (currentGameState == GameState.Lost) {
            color = Color.RED;
            drawables.add(deathDrawables[2]);
            text = view.getText(R.string.lost);
        } else if (currentGameState == GameState.Pause) {
            color = Color.GRAY_LIGHT_50;
            text = view.getText(R.string.paused);
        }
        // Add text
        if (text != null) {
            String[] lines = Utilities.getTextLines(text);
            int textWidth = Utilities.getTextWidth(lines) + 2;
            float charWidth = aspect / textWidth;
            view.drawText(drawables, aspect / 2f, 0.5f, charWidth, 0, true, true, lines);
        }
        // Draw slow text if any
        if (slowTextElement != null) {
            slowTextElement.onDraw(view, drawables);
        }
        // Send drawables to batch if any
        if (drawables.isEmpty()) {
            view.clearBatches(DrawableBatch.SLOT.UI);
        } else {
            DrawableBatch batch = view.createBatchOrthographic(DrawableBatch.SLOT.UI, drawables, color);
            view.submitBatch(batch);
        }
    }
    if (vibrate) {
        vibrate = false;
        view.vibrate(Constants.VIBRATE_UI);
    }
}
Also used : Color(com.agorria.shootandmove.graphics.Color) Drawable(com.agorria.shootandmove.graphics.Drawable) ArrayList(java.util.ArrayList) Menu(com.agorria.shootandmove.menu.Menu) DrawableBatch(com.agorria.shootandmove.graphics.DrawableBatch)

Aggregations

Color (com.agorria.shootandmove.graphics.Color)2 Drawable (com.agorria.shootandmove.graphics.Drawable)2 DrawableBatch (com.agorria.shootandmove.graphics.DrawableBatch)2 ArrayList (java.util.ArrayList)2 SuppressLint (android.annotation.SuppressLint)1 TextureAtlas (com.agorria.shootandmove.graphics.TextureAtlas)1 Menu (com.agorria.shootandmove.menu.Menu)1 FloatBuffer (java.nio.FloatBuffer)1 ShortBuffer (java.nio.ShortBuffer)1