Android Games: Walkers

De Aulas
Revisão de 18h23min de 27 de outubro de 2022 por Admin (discussão | contribs) (→‎Avatar.class)

Links relacionados: Jogos Digitais

Vídeo Demonstrativo

Recursos

Actor.class

 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;
 6import com.badlogic.gdx.math.Intersector;
 7
 8class Actor {
 9    static final int DOWN = 0;
10    static final int RIGHT = 1;
11    static final int UP = 2;
12    static final int LEFT = 3;
13    private static final int counterMax = 10;
14    private static final int SPEED = 2;
15    int direction = DOWN;
16    MyGdxGame game;
17    Boolean collided = false;
18    private Sprite sprite;
19    public int w = 64;
20    public int h = 64;
21    private int step = 0;
22    private int counter;
23    private boolean moved = false;
24
25    Actor(float x, float y, Texture texture, MyGdxGame game) {
26        this.game = game;
27        this.sprite = new Sprite(new TextureRegion(texture, 0, 0, w, h));
28        sprite.setPosition(x, y);
29    }
30
31    void execute() {
32    }
33
34    void run() {
35        execute();
36        counter++;
37        if (counter > counterMax) {
38            counter = 0;
39            if (moved) step = (step == 1) ? 0 : 1;
40        }
41        moved = false;
42    }
43
44    void move(int dir) {
45        direction = dir;
46        if (direction == UP) {
47            sprite.translateY(SPEED);
48            moved = true;
49        } else if (direction == DOWN) {
50            sprite.translateY(-SPEED);
51            moved = true;
52        } else if (direction == RIGHT) {
53            sprite.translateX(SPEED);
54            moved = true;
55        } else if (direction == LEFT) {
56            sprite.translateX(-SPEED);
57            moved = true;
58        }
59        sprite.setX(sprite.getX(), 0, game.w - w - 1);
60        sprite.setY(sprite.getY(), 0, game.w - h - 1);
61    }
62
63    public float clamp(float val, float min, float max) {
64        if (val < min) return min
65        if (val > max) return max;
66        return val;
67    }
68
69    Boolean collision(Actor other) {
70        return (this != other) &&
71                Intersector.overlaps(
72                        sprite.getBoundingRectangle(),
73                        other.sprite.getBoundingRectangle());
74    }
75
76    void draw() {
77        sprite.setRegion(step * w, direction * h, w, h);
78        sprite.draw(game.batch);
79    }
80}

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(rand.nextInt(game.w - w - 1), rand.nextInt(game.h - h - 1), texture, game);
        direction = game.rand.nextInt(4);
        max = game.rand.nextInt(20) + 1;
    }

    @Override
    public void execute() {
        count++;
        if (count > max) {
            count = 0;
            max = game.rand.nextInt(20) + 1;
            move(game.rand.nextInt(4));
        } else {
            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.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 Texture industrialTexture;
    private BitmapFont font;
    private Avatar avatar;
    private long lastTimeCounted;
    private float sinceChange;
    private float frameRate;

    @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");
        industrialTexture = new Texture("industrial.png");
        for (int i = 0; i < 10; i++) {
            actors.add(new Monster(monsterTexture, this));
        }
        avatar = new Avatar(100, 100, avatarTexture, this);
        actors.add(avatar);
        font = new BitmapFont(
                Gdx.files.internal("verdana.fnt"),
                Gdx.files.internal("verdana.png"), false);
        font.setColor(1, 1, 1, 1);
        lastTimeCounted = TimeUtils.millis();
        sinceChange = 0;
        frameRate = Gdx.graphics.getFramesPerSecond();
    }

    private void execute() {
        if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
            Gdx.app.exit();
        }
        for (Actor a : actors) a.run();
        long delta = TimeUtils.timeSinceMillis(lastTimeCounted);
        lastTimeCounted = TimeUtils.millis();
        sinceChange += delta;
        if (sinceChange >= 1000) {
            sinceChange = 0;
            frameRate = Gdx.graphics.getFramesPerSecond();
        }
    }

    @Override
    public void render() {
        execute();
        batch.begin();
        batch.draw(industrialTexture, 0, 0);
        for (Actor a : actors) a.draw();
        if (avatar.collided) {
            font.draw(batch, "Collided!   " + (int) frameRate + " fps", 1, h + 1);
        } else {
            font.draw(batch, "I'm free... " + (int) frameRate + " fps", 1, h + 1);
        }
        batch.end();
    }

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