Android Games: Exemplo 02

De Aulas
Revisão de 15h25min de 26 de novembro de 2016 por Admin (discussão | contribs) (Substituição de texto - "<code java n>" por "<syntaxhighlight lang=java line>")
(dif) ← Edição anterior | Revisão atual (dif) | Versão posterior → (dif)

Links relacionados: Jogos Digitais

Recursos

Classe Character

  1package com.mygdx.game;
  2
  3import com.badlogic.gdx.Gdx;
  4import com.badlogic.gdx.Input.Keys;
  5import com.badlogic.gdx.graphics.Texture;
  6import com.badlogic.gdx.graphics.g2d.Sprite;
  7import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  8import com.badlogic.gdx.graphics.g2d.TextureRegion;
  9
 10public class Character {
 11    private MyGdxGame game;
 12    private Texture texture;
 13    private int pw;
 14    private int ph;
 15    private int x;
 16    private int y;
 17    private int step;
 18    private int direction;
 19
 20    public Character(MyGdxGame game, String fileTexture, int x, int y, int tw, int th) {
 21        this.game = game;
 22        setX(x);
 23        setY(y);
 24        pw = (tw / 7);
 25        ph = (th / 4);
 26        texture = new Texture(fileTexture);
 27    }
 28
 29    public void run() {
 30        if (Gdx.input.isKeyPressed(Keys.UP)) {
 31            direction = 2;
 32            if (game.path(x + (pw / 2), y + ph)) {
 33                step++;
 34                y += 2;
 35            }
 36        }
 37        if (Gdx.input.isKeyPressed(Keys.DOWN)) {
 38            direction = 0;
 39            if (game.path(x + (pw / 2), y)) {
 40                step++;
 41                y -= 2;
 42            }
 43        }
 44        if (Gdx.input.isKeyPressed(Keys.LEFT)) {
 45            direction = 1;
 46            if (game.path(x, y + (ph / 2))) {
 47                x -= 2;
 48                step++;
 49            }
 50        }
 51        if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
 52            direction = 3;
 53            if (game.path(x + pw, y + (ph / 2))) {
 54                step++;
 55                x += 2;
 56            }
 57        }
 58        if (step >= 7) {
 59            step = 0;
 60        }
 61    }
 62
 63    public void draw(SpriteBatch batch) {
 64        TextureRegion region = new TextureRegion(texture, step * pw, direction * ph, pw, ph);
 65        Sprite sprite = new Sprite(region);
 66        sprite.setPosition(x - game.getX(), y - game.getY());
 67        sprite.draw(batch);
 68    }
 69
 70    public int getX() {
 71        return x;
 72    }
 73
 74    public void setX(int x) {
 75        this.x = x;
 76    }
 77
 78    public int getY() {
 79        return y;
 80    }
 81
 82    public void setY(int y) {
 83        this.y = y;
 84    }
 85
 86    public void dispose() {
 87        texture.dispose();
 88    }
 89
 90    public int getStep() {
 91        return step;
 92    }
 93
 94    public void setStep(int step) {
 95        this.step = step;
 96    }
 97
 98    public int getDirection() {
 99        return direction;
100    }
101
102    public void setDirection(int direction) {
103        this.direction = direction;
104    }
105
106    public int getWidth() {
107        return pw;
108    }
109
110    public int getHeight() {
111        return ph;
112    }
113}

Classe Principal

 1package com.mygdx.game;
 2
 3import com.badlogic.gdx.Gdx;
 4import com.badlogic.gdx.graphics.Color;
 5import com.badlogic.gdx.graphics.GL20;
 6import com.badlogic.gdx.graphics.Pixmap;
 7import com.badlogic.gdx.graphics.Texture;
 8import com.badlogic.gdx.graphics.g2d.Sprite;
 9import com.badlogic.gdx.graphics.g2d.SpriteBatch;
10import com.badlogic.gdx.graphics.g2d.TextureRegion;
11import com.badlogic.gdx.ApplicationAdapter;
12
13public class MyGdxGame extends ApplicationAdapter {
14    private SpriteBatch batch;
15    private int x, y, w, h;
16    private Texture ground;
17    private Pixmap mask;
18    private Character avatar;
19
20    @Override
21    public void create() {
22        w = Gdx.graphics.getWidth();
23        h = Gdx.graphics.getHeight();
24        batch = new SpriteBatch();
25        ground = new Texture(Gdx.files.internal("bigmap.png"));
26        mask = new Pixmap(Gdx.files.internal("bigmask.png"));
27        avatar = new Character(this, "claudius.png", 710, 64, 224, 256);
28        x = 0;
29        y = 0;
30    }
31
32    public boolean path(int px, int py) {
33        Color color = new Color();
34        int valor = mask.getPixel(px, mask.getHeight() - py);
35        Color.rgb888ToColor(color, valor);
36        int r = (int) (color.r * 255f);
37        int g = (int) (color.g * 255f);
38        int b = (int) (color.b * 255f);
39        if ((r > 20) && (g > 20) && (b > 20)) {
40            return true;
41        }
42        return false;
43    }
44
45    @Override
46    public void dispose() {
47        batch.dispose();
48        ground.dispose();
49        avatar.dispose();
50    }
51
52    @Override
53    public void render() {
54        avatar.run();
55        Gdx.gl.glClearColor(0, 0, 0, 1);
56        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
57        batch.begin();
58        drawBackground();
59        avatar.draw(batch);
60        batch.end();
61    }
62
63    public void drawBackground() {
64        x = avatar.getX() - (w / 2) + (avatar.getWidth() / 2);
65        y = avatar.getY() - (h / 2) + (avatar.getHeight() / 2);
66        TextureRegion reg = new TextureRegion(ground, x, ground.getHeight() - y - h, w, h);
67        Sprite sprite = new Sprite(reg);
68        sprite.setPosition(0, 0);
69        sprite.draw(batch);
70    }
71
72    public int getX() {
73        return x;
74    }
75
76    public int getY() {
77        return y;
78    }
79}