Android Games: Walkers

De Aulas

Links relacionados: Jogos Digitais

Vídeo Demonstrativo

Recursos

Actor.class

package com.mygdx.game;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Intersector;

class Actor {
    static final int DOWN = 0;
    static final int RIGHT = 1;
    static final int UP = 2;
    static final int LEFT = 3;
    private static final int SPEED = 2;
    int direction = DOWN;
    MyGdxGame game;
    Boolean collided = false;
    private final Sprite sprite;
    public int w = 64;
    public int h = 64;
    private int step = 0;
    private int counter;
    private static final int counterMax = 10;
    private boolean moved = false;

    Actor(float x, float y, Texture texture, MyGdxGame game) {
        this.game = game;
        this.sprite = new Sprite(new TextureRegion(texture, 0, 0, w, h));
        sprite.setPosition(x, y);
    }

    void execute() {
    }

    void run() {
        execute();
        counter++;
        if (counter > counterMax) {
            counter = 0;
            if (moved) step = (step == 1) ? 0 : 1;
        }
        moved = false;
    }

    void move(int dir) {
        direction = dir;
        if (direction == UP) {
            sprite.translateY(SPEED);
            moved = true;
        } else if (direction == DOWN) {
            sprite.translateY(-SPEED);
            moved = true;
        } else if (direction == RIGHT) {
            sprite.translateX(SPEED);
            moved = true;
        } else if (direction == LEFT) {
            sprite.translateX(-SPEED);
            moved = true;
        }
        sprite.setPosition(
                clamp(sprite.getX(), 0, game.w - w - 1),
                clamp(sprite.getY(), 0, game.h - h - 1));
    }

    public float clamp(float val, float min, float max) {
        if (val < min) return min;
        if (val > max) return max;
        return val;
    }

    Boolean collision(Actor other) {
        return this != other &&
                Intersector.overlaps(
                        sprite.getBoundingRectangle(),
                        other.sprite.getBoundingRectangle());
    }

    void draw() {
        sprite.setRegion(step * w, direction * h, w, h);
        sprite.draw(game.batch);
    }
}

Avatar.class

package com.mygdx.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;

class Avatar extends Actor {
    Avatar(float x, float y, Texture texture, MyGdxGame game) {
        super(x, y, texture, game);
    }

    @Override
    public void execute() {
        if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
            move(UP);
        } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
            move(DOWN);
        } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
            move(RIGHT);
        } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
            move(LEFT);
        }
        collided = false;
        for (Actor a : game.actors) {
            if (collision(a)) {
                collided = true;
                break;
            }
        }
    }
}

Monster.class

package com.mygdx.game;

import com.badlogic.gdx.graphics.Texture;

class Monster extends Actor {
    private int count = 0;
    private int max;

    Monster(Texture texture, MyGdxGame game) {
        super(game.rand.nextInt(game.w - 64),
                game.rand.nextInt(game.h - 64), texture, game);
        new_goal();
    }

    private void new_goal() {
        direction = game.rand.nextInt(4);
        count = 0;
        max = game.rand.nextInt(20) + 1;
    }

    @Override
    public void execute() {
        count++;
        if (count > max) new_goal();
        move(direction);
    }
}

MyGdxGame.class

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MyGdxGame extends ApplicationAdapter {
	int w, h;
	SpriteBatch batch;
	Random rand = new Random();
	List<Actor> actors = new ArrayList<Actor>();
	private Texture avatarTexture;
	private Texture monsterTexture;
	private BitmapFont font;
	private Avatar avatar;

	@Override
	public void create() {
		w = Gdx.graphics.getWidth();
		h = Gdx.graphics.getHeight();
		batch = new SpriteBatch();
		avatarTexture = new Texture("avatar.png");
		monsterTexture = new Texture("monster.png");
		for (int i = 0; i < 10; i++) {
			actors.add(new Monster(monsterTexture, this));
		}
		actors.add(avatar = new Avatar(100, 100, avatarTexture, this));
		font = new BitmapFont(
				Gdx.files.internal("verdana.fnt"),
				Gdx.files.internal("verdana.png"), false);
		font.getData().setScale(0.75f, 0.75f);
		font.setColor(1, 1, 0, 1);
	}

	private void execute() {
		if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
			Gdx.app.exit();
		}
		for (Actor a : actors) a.run();
	}

	@Override
	public void render() {
		execute();
		ScreenUtils.clear(0, 0.5f, 0, 1);
		float delta = Gdx.graphics.getDeltaTime();
		float frameRate = Gdx.graphics.getFramesPerSecond();
		batch.begin();
		for (Actor a : actors) a.draw();
		String text = avatar.collided ? "Collided!   " : "I'm free... ";
		font.draw(batch, text + (int) frameRate + " fps" + " delta: " + delta, 1, h + 1);
		batch.end();
	}

	@Override
	public void dispose() {
		batch.dispose();
		avatarTexture.dispose();
		monsterTexture.dispose();
	}
}