Search in sources :

Example 1 with Player

use of com.agorria.shootandmove.entities.Player in project ShootAndRun by IonAgorria.

the class GameThread method run.

/**
 * Called from thread when starts
 */
@Override
public void run() {
    // Initializes stuff
    lastTime = System.currentTimeMillis();
    world = new World(gameView);
    // Main loop
    while (running.get()) {
        long sleep = Constants.THREAD_SLEEP;
        // Check if we are not in pause
        if (!paused) {
            // Calculate delta and store last time
            long time = System.currentTimeMillis();
            float delta = (time - lastTime) / 1000.0f;
            lastTime = time;
            // Checks if UI requests a game state
            GameState requestedState = gameUI.getRequestedGameState();
            if (requestedState != null) {
                setGameState(requestedState);
            }
            // Get player
            Player player = null;
            if (gameState != GameState.Menu && world != null) {
                player = world.getPlayer();
            }
            // Check active state
            float gyroscope = gameUI.getGyroscope();
            if (gameState == GameState.Active) {
                // Check if player is dead
                if (player == null || player.isDestroyed()) {
                    setGameState(GameState.Lost);
                }
                // Check if level is requested
                Integer requestedLevel = world.getRequestedLevel();
                if (requestedLevel != null) {
                    world.setRequestedLevel(null);
                    // Check if is ending or normal level
                    if (requestedLevel > Constants.LEVEL_COUNT) {
                        setGameState(GameState.Menu);
                        gameUI.setCurrentMenu(MenuEnding.class);
                    } else {
                        SharedPreferences preferences = gameView.getPreferences();
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.putInt(Constants.PREFERENCES_KEY_LEVEL, requestedLevel);
                        editor.apply();
                        setLevel(requestedLevel, false);
                    }
                }
                // Check if we are still active
                if (requestedLevel == null && gameState == GameState.Active) {
                    // Update just the player
                    if (player != null) {
                        player.update(Constants.GAME_DELTA, gameUI.getButtons(), gameUI.getJoystick(), gameUI.isGyroscopeEnabled() ? gyroscope : 0f);
                    }
                    // Update and draw world
                    world.update(Constants.GAME_DELTA);
                    // Show any message
                    String message = world.getMessage();
                    if (message != null) {
                        gameUI.showText(message);
                    }
                }
            }
            // Game UI update and draw
            gameUI.update(delta);
            gameUI.draw(gameView, player);
            // Debug draw
            if (Constants.DEBUG_MODE) {
                // Draw FPS
                List<Drawable> drawables = new ArrayList<>();
                String text = String.format(Locale.getDefault(), "U %.1f", 1f / delta);
                if (player != null) {
                    Vector3f pos = player.getPosition();
                    text += String.format(Locale.getDefault(), " X %.1f", pos.x);
                    text += String.format(Locale.getDefault(), " Z %.1f", pos.z);
                    text += String.format(Locale.getDefault(), " D %.1f", player.getDirection());
                    text += String.format(Locale.getDefault(), " G %.1f", gyroscope);
                }
                if (world != null) {
                    text += String.format(Locale.getDefault(), " R %d", world.getRobots());
                }
                gameView.drawText(drawables, 0, 0, 0, 0.05f, text);
                gameView.submitBatch(gameView.createBatchOrthographic(DrawableBatch.SLOT.DebugUI, drawables, Color.WHITE));
            }
            // Check if view should be rendered
            gameView.checkShouldRender();
            // Calculate how much to sleep
            sleep = Constants.GAME_SLEEP;
            sleep -= System.currentTimeMillis() - time;
        }
        // Sleep thread
        if (0 < sleep) {
            try {
                Thread.sleep(sleep);
            } catch (InterruptedException e) {
                Log.d(TAG, "Game loop sleep interrupted");
            }
        }
    }
}
Also used : Player(com.agorria.shootandmove.entities.Player) SharedPreferences(android.content.SharedPreferences) Drawable(com.agorria.shootandmove.graphics.Drawable) ArrayList(java.util.ArrayList) Vector3f(com.agorria.shootandmove.math.Vector3f)

Example 2 with Player

use of com.agorria.shootandmove.entities.Player in project ShootAndRun by IonAgorria.

the class World method load.

/**
 * Loads level as current world
 *
 * @param level to load
 * @param clear state
 */
void load(Integer level, boolean clear) {
    // Store player relative data if any
    float playerDirection = Constants.HALF_PI;
    Vector3f playerPreviousPosition = null;
    if (clear) {
        playerPosition = null;
        walked = 0;
    }
    if (!clear && player != null) {
        playerDirection = player.getDirection();
        playerPreviousPosition = player.getPreviousPosition().sub(player.getPosition(), new Vector3f());
    }
    // Reset stuff
    cameraPosition = new Vector3f(0, 0, 0);
    cameraRotation = new Vector3f(0, 0, 0);
    batches.clear();
    entities.clear();
    entitiesNext.clear();
    collisions.clear();
    doors.clear();
    player = null;
    requestedLevel = null;
    robots = 0;
    // Use loader if none or different name
    if (level == null) {
        throw new RuntimeException("No level to load");
    }
    if (loader == null || !level.equals(loader.level)) {
        loader = new WorldLoader(level, view);
    }
    // Load the world collisions
    collisions.addAll(loader.collisions);
    // Load world entities from spawns
    entities.clear();
    for (WorldLoader.EntitySpawn spawn : loader.entities) {
        Vector3f position = new Vector3f(spawn.position);
        Vector3f size = spawn.size == null ? null : new Vector3f(spawn.size);
        switch(spawn.name) {
            case "Player":
                // Restore old player position
                if (playerPosition != null) {
                    position.add(playerPosition);
                }
                player = new Player(this, position, playerDirection);
                // Restore old previous position
                if (playerPreviousPosition != null) {
                    player.getPosition().add(playerPreviousPosition, player.getPreviousPosition());
                }
                entities.add(player);
                break;
            case "Robot":
                entities.add(new Robot(this, position, spawn.type));
                robots++;
                break;
            case "Pistol":
                entities.add(new Item(this, position, Item.ItemType.Pistol));
                break;
            case "Plasma":
                entities.add(new Item(this, position, Item.ItemType.Plasma));
                break;
            case "Missile":
                entities.add(new Item(this, position, Item.ItemType.Missile));
                break;
            case "Health":
                entities.add(new Item(this, position, Item.ItemType.Health));
                break;
            case "Door":
                Door door = new Door(this, position, size, spawn.type);
                entities.add(door);
                doors.add(door);
                break;
            case "LockedDoor":
                LockedDoor lockedDoor = new LockedDoor(this, position, size, spawn.type);
                entities.add(lockedDoor);
                doors.add(lockedDoor);
                break;
            case "Warp":
                entities.add(new Warp(this, position, spawn.type));
                break;
            default:
                throw new IllegalArgumentException("Unknown object name");
        }
    }
    // Update camera before batch
    updateCamera();
    // Create the batch, use depth as world has no transparency
    DrawableBatch batch = view.createBatch(DrawableBatch.SLOT.World, DrawableBatch.MODE.PerspectiveDepth, loader.planes, cameraPosition, cameraRotation, Color.WHITE);
    view.submitBatch(batch);
    batches.add(batch);
}
Also used : Warp(com.agorria.shootandmove.entities.Warp) Player(com.agorria.shootandmove.entities.Player) LockedDoor(com.agorria.shootandmove.entities.LockedDoor) Door(com.agorria.shootandmove.entities.Door) Item(com.agorria.shootandmove.entities.Item) Vector3f(com.agorria.shootandmove.math.Vector3f) LockedDoor(com.agorria.shootandmove.entities.LockedDoor) DrawableBatch(com.agorria.shootandmove.graphics.DrawableBatch) Robot(com.agorria.shootandmove.entities.Robot)

Aggregations

Player (com.agorria.shootandmove.entities.Player)2 Vector3f (com.agorria.shootandmove.math.Vector3f)2 SharedPreferences (android.content.SharedPreferences)1 Door (com.agorria.shootandmove.entities.Door)1 Item (com.agorria.shootandmove.entities.Item)1 LockedDoor (com.agorria.shootandmove.entities.LockedDoor)1 Robot (com.agorria.shootandmove.entities.Robot)1 Warp (com.agorria.shootandmove.entities.Warp)1 Drawable (com.agorria.shootandmove.graphics.Drawable)1 DrawableBatch (com.agorria.shootandmove.graphics.DrawableBatch)1 ArrayList (java.util.ArrayList)1