Android Games: Village

De Aulas
Revisão de 20h52min de 27 de outubro de 2022 por Admin (discussão | contribs)
(dif) ← Edição anterior | Revisão atual (dif) | Versão posterior → (dif)

Links relacionados: Jogos Digitais

Recursos

Actor.java

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 java.util.Random;

class Actor {
    static final int DOWN = 0;
    static final int LEFT = 1;
    static final int UP = 2;
    static final int RIGHT = 3;
    private static final int SPEED = 2;
    private static final int animationMax = 4;
    float x, y;
    int w, h;
    private MyGdxGame game;
    private Texture texture;
    private Sprite sprite;
    private int direction = DOWN;
    private int animation;
    private int step = 0;
    private int count;
    private int max;
    private Random rand = new Random();

    Actor(float x, float y, Texture texture, MyGdxGame game) {
        this.game = game;
        this.texture = texture;
        this.x = x;
        this.y = y;
        w = 224 / 7;
        h = 256 / 4;
        max = rand.nextInt(20) + 1;
        sprite = new Sprite(new TextureRegion(texture, 0, 0, w, h));
    }

    void run() {
        count++;
        if (count > max) {
            count = 0;
            max = rand.nextInt(20) + 1;
            move(rand.nextInt(4));
        } else {
            move(direction);
        }
    }

    void move(int dir) {
        direction = dir;
        switch (dir) {
            case DOWN:
                if (game.isFree((int) x + (w / 2), (int) y - SPEED)) {
                    y -= SPEED;
                    updateAnimation();
                }
                break;
            case UP:
                if (game.isFree((int) x + (w / 2), (int) y + 20)) {
                    y += SPEED;
                    updateAnimation();
                }
                break;
            case LEFT:
                if (game.isFree((int) x - SPEED, (int) y)) {
                    x -= SPEED;
                    updateAnimation();
                }
                break;
            case RIGHT:
                if (game.isFree((int) x + w + SPEED, (int) y)) {
                    x += SPEED;
                    updateAnimation();
                }
                break;
        }
    }

    private void updateAnimation() {
        animation++;
        if (animation > animationMax) {
            animation = 0;
            step++;
            if (step >= 7) step = 0;
            if (!outOfScreen()) {
                if ((step == 1) || (step == 4)) {
                    game.step[rand.nextInt(4)].play();
                }
            }
        }
    }

    private boolean outOfScreen() {
        float px = x - game.x;
        float py = y - game.y;
        return (px > game.w) || (px + w < 0) || (py > game.h) || (py + h < 0);
    }

    void draw() {
        if (outOfScreen()) return;
        sprite.setRegion(new TextureRegion(texture, step * w, direction * h, w, h));
        sprite.setPosition(x - game.x, y - game.y);
        sprite.draw(game.batch);
    }
}

Avatar.java

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
    void run() {
        if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
            move(UP);
        } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
            move(LEFT);
        } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
            move(DOWN);
        } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
            move(RIGHT);
        }
    }
}

MyGdxGame.java

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyGdxGame extends ApplicationAdapter {
	SpriteBatch batch;
	Sound[] step = new Sound[4];
	float x, y;
	int w, h;
	private Music music;
	private Pixmap mask;
	private Texture map;
	private Actor avatar;
	private Map<String, Texture> textures = new HashMap<String, Texture>();
	private List<Actor> actors = new ArrayList<Actor>();

	@Override
	public void create() {
		batch = new SpriteBatch();
		w = Gdx.graphics.getWidth();
		h = Gdx.graphics.getHeight();
		mask = new Pixmap(Gdx.files.internal("villagemask.png"));
		map = new Texture("villagemap.png");
		textures.put("kaneda", new Texture("kaneda.png"));
		textures.put("yuki", new Texture("yuki.png"));
		textures.put("misato", new Texture("misato.png"));
		for (int i = 0; i < 4; i++) {
			step[i] = Gdx.audio.newSound(Gdx.files.internal("step" + i + ".wav"));
		}
		avatar = new Avatar(710, 64, textures.get("kaneda"), this);
		actors.add(avatar);
		actors.add(new Actor(380, 410, textures.get("yuki"), this));
		actors.add(new Actor(710, 600, textures.get("misato"), this));
		music = Gdx.audio.newMusic(Gdx.files.internal("s13forest.mp3"));
		music.play();
	}

	private void execute() {
		// Deixamos para tratar os inputs gerais da UI aqui. Dentro do avatar ficam
		// apenas os tratamentos dos inputs referente ao comportamento do player
		if (Gdx.input.isKeyJustPressed(Input.Keys.M)) {
			if (music.isPlaying()) music.stop();
			else music.play();
		}
		if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
			Gdx.app.exit();
		}
		for (Actor a : actors) a.run();
		moveCamera();
	}

	@Override
	public void render() {
		execute();
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		batch.begin();
		batch.draw(map, -x, -y);
		for (Actor a : actors) a.draw();
		batch.end();
	}

	private void moveCamera() {
		x = avatar.x - (w / 2) + (avatar.w / 2);
		y = avatar.y - (h / 2) + (avatar.h / 2);
	}

	boolean isFree(int px, int py) {
		Color color = new Color();
		Color.rgb888ToColor(color, mask.getPixel(px, mask.getHeight() - py));
		return ((color.r > 0.1) && (color.g > 0.1) && (color.b > 0.1));
	}

	@Override
	public void dispose() {
		batch.dispose();
		mask.dispose();
		map.dispose();
		for (Map.Entry<String, Texture> entry : textures.entrySet()) {
			entry.getValue().dispose();
		}
		for (int i = 0; i < 4; i++) step[i].dispose();
		music.dispose();
	}
}