Mudanças entre as edições de "Android Games: Walkers"

De Aulas
Linha 143: Linha 143:
  
 
     Monster(Texture texture, MyGdxGame game) {
 
     Monster(Texture texture, MyGdxGame game) {
         super(rand.nextInt(game.w - w - 1), rand.nextInt(game.h - h - 1), texture, 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);
 
         direction = game.rand.nextInt(4);
 +
        count = 0;
 
         max = game.rand.nextInt(20) + 1;
 
         max = game.rand.nextInt(20) + 1;
 
     }
 
     }
Linha 151: Linha 157:
 
     public void execute() {
 
     public void execute() {
 
         count++;
 
         count++;
         if (count > max) {
+
         if (count > max) new_goal();
            count = 0;
+
         move(direction);
            max = game.rand.nextInt(20) + 1;
 
            move(game.rand.nextInt(4));
 
         } else {
 
            move(direction);
 
        }
 
 
     }
 
     }
 
}
 
}

Edição das 09h48min de 29 de outubro de 2022

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.w - 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.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();
    }
}