Android Games: Village

De Aulas
Revisão de 14h44min de 26 de abril de 2018 por Admin (discussão | contribs) (→‎Avatar.java)
(dif) ← Edição anterior | Revisão atual (dif) | Versão posterior → (dif)

Links relacionados: Jogos Digitais

Recursos

Actor.java

  1package com.mygdx.game;
  2
  3import com.badlogic.gdx.graphics.Texture;
  4import com.badlogic.gdx.graphics.g2d.Sprite;
  5import com.badlogic.gdx.graphics.g2d.TextureRegion;
  6
  7import java.util.Random;
  8
  9class Actor {
 10    static final int DOWN = 0;
 11    static final int LEFT = 1;
 12    static final int UP = 2;
 13    static final int RIGHT = 3;
 14    private static final int SPEED = 2;
 15    private static final int animationMax = 4;
 16    float x, y;
 17    int w, h;
 18    private MyGdxGame game;
 19    private Texture texture;
 20    private Sprite sprite;
 21    private int direction = DOWN;
 22    private int animation;
 23    private int step = 0;
 24    private int count;
 25    private int max;
 26    private Random rand = new Random();
 27
 28    Actor(float x, float y, Texture texture, MyGdxGame game) {
 29        this.game = game;
 30        this.texture = texture;
 31        this.x = x;
 32        this.y = y;
 33        w = 224 / 7;
 34        h = 256 / 4;
 35        max = rand.nextInt(20) + 1;
 36        sprite = new Sprite(new TextureRegion(texture, 0, 0, w, h));
 37    }
 38
 39    void run() {
 40        count++;
 41        if (count > max) {
 42            count = 0;
 43            max = rand.nextInt(20) + 1;
 44            move(rand.nextInt(4));
 45        } else {
 46            move(direction);
 47        }
 48    }
 49
 50    void move(int dir) {
 51        direction = dir;
 52        switch (dir) {
 53            case DOWN:
 54                if (game.isFree((int) x + (w / 2), (int) y - SPEED)) {
 55                    y -= SPEED;
 56                    updateAnimation();
 57                }
 58                break;
 59            case UP:
 60                if (game.isFree((int) x + (w / 2), (int) y + 20)) {
 61                    y += SPEED;
 62                    updateAnimation();
 63                }
 64                break;
 65            case LEFT:
 66                if (game.isFree((int) x - SPEED, (int) y)) {
 67                    x -= SPEED;
 68                    updateAnimation();
 69                }
 70                break;
 71            case RIGHT:
 72                if (game.isFree((int) x + w + SPEED, (int) y)) {
 73                    x += SPEED;
 74                    updateAnimation();
 75                }
 76                break;
 77        }
 78    }
 79
 80    private void updateAnimation() {
 81        animation++;
 82        if (animation > animationMax) {
 83            animation = 0;
 84            step++;
 85            if (step >= 7) step = 0;
 86            if (!outOfScreen()) {
 87                if ((step == 1) || (step == 4)) {
 88                    game.step[rand.nextInt(4)].play();
 89                }
 90            }
 91        }
 92    }
 93
 94    private boolean outOfScreen() {
 95        float px = x - game.x;
 96        float py = y - game.y;
 97        return (px > game.w) || (px + w < 0) || (py > game.h) || (py + h < 0);
 98    }
 99
100    void draw() {
101        if (outOfScreen()) return;
102        sprite.setRegion(new TextureRegion(texture, step * w, direction * h, w, h));
103        sprite.setPosition(x - game.x, y - game.y);
104        sprite.draw(game.batch);
105    }
106}

Avatar.java

 1package com.mygdx.game;
 2
 3import com.badlogic.gdx.Gdx;
 4import com.badlogic.gdx.Input;
 5import com.badlogic.gdx.graphics.Texture;
 6
 7class Avatar extends Actor {
 8    Avatar(float x, float y, Texture texture, MyGdxGame game) {
 9        super(x, y, texture, game);
10    }
11
12    @Override
13    void run() {
14        if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
15            move(UP);
16        } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
17            move(LEFT);
18        } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
19            move(DOWN);
20        } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
21            move(RIGHT);
22        }
23    }
24}

MyGdxGame.java

 1package com.mygdx.game;
 2
 3import com.badlogic.gdx.ApplicationAdapter;
 4import com.badlogic.gdx.Gdx;
 5import com.badlogic.gdx.Input;
 6import com.badlogic.gdx.audio.Music;
 7import com.badlogic.gdx.audio.Sound;
 8import com.badlogic.gdx.graphics.Color;
 9import com.badlogic.gdx.graphics.GL20;
10import com.badlogic.gdx.graphics.Pixmap;
11import com.badlogic.gdx.graphics.Texture;
12import com.badlogic.gdx.graphics.g2d.SpriteBatch;
13
14import java.util.ArrayList;
15import java.util.HashMap;
16import java.util.List;
17import java.util.Map;
18
19public class MyGdxGame extends ApplicationAdapter {
20	SpriteBatch batch;
21	Sound[] step = new Sound[4];
22	float x, y;
23	int w, h;
24	private Music music;
25	private Pixmap mask;
26	private Texture map;
27	private Actor avatar;
28	private Map<String, Texture> textures = new HashMap<String, Texture>();
29	private List<Actor> actors = new ArrayList<Actor>();
30
31	@Override
32	public void create() {
33		batch = new SpriteBatch();
34		w = Gdx.graphics.getWidth();
35		h = Gdx.graphics.getHeight();
36		mask = new Pixmap(Gdx.files.internal("villagemask.png"));
37		map = new Texture("villagemap.png");
38		textures.put("kaneda", new Texture("kaneda.png"));
39		textures.put("yuki", new Texture("yuki.png"));
40		textures.put("misato", new Texture("misato.png"));
41		for (int i = 0; i < 4; i++) {
42			step[i] = Gdx.audio.newSound(Gdx.files.internal("step" + i + ".wav"));
43		}
44		avatar = new Avatar(710, 64, textures.get("kaneda"), this);
45		actors.add(avatar);
46		actors.add(new Actor(380, 410, textures.get("yuki"), this));
47		actors.add(new Actor(710, 600, textures.get("misato"), this));
48		music = Gdx.audio.newMusic(Gdx.files.internal("s13forest.mp3"));
49		music.play();
50	}
51
52	private void execute() {
53		// Deixamos para tratar os inputs gerais da UI aqui. Dentro do avatar ficam
54		// apenas os tratamentos dos inputs referente ao comportamento do player
55		if (Gdx.input.isKeyJustPressed(Input.Keys.M)) {
56			if (music.isPlaying()) music.stop();
57			else music.play();
58		}
59		if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
60			Gdx.app.exit();
61		}
62		for (Actor a : actors) a.run();
63		moveCamera();
64	}
65
66	@Override
67	public void render() {
68		execute();
69		Gdx.gl.glClearColor(0, 0, 0, 1);
70		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
71		batch.begin();
72		batch.draw(map, -x, -y);
73		for (Actor a : actors) a.draw();
74		batch.end();
75	}
76
77	private void moveCamera() {
78		x = avatar.x - (w / 2) + (avatar.w / 2);
79		y = avatar.y - (h / 2) + (avatar.h / 2);
80	}
81
82	boolean isFree(int px, int py) {
83		Color color = new Color();
84		Color.rgb888ToColor(color, mask.getPixel(px, mask.getHeight() - py));
85		return ((color.r > 0.1) && (color.g > 0.1) && (color.b > 0.1));
86	}
87
88	@Override
89	public void dispose() {
90		batch.dispose();
91		mask.dispose();
92		map.dispose();
93		for (Map.Entry<String, Texture> entry : textures.entrySet()) {
94			entry.getValue().dispose();
95		}
96		for (int i = 0; i < 4; i++) step[i].dispose();
97		music.dispose();
98	}
99}