Mudanças entre as edições de "Android Games: Walkers"
De Aulas
Linha 141: | Linha 141: | ||
private int max; | private int max; | ||
− | Monster( | + | Monster(Texture texture, MyGdxGame game) { |
− | super( | + | super(rand.nextInt(game.w - w - 1), rand.nextInt(game.h - h - 1), texture, game); |
direction = game.rand.nextInt(4); | direction = game.rand.nextInt(4); | ||
max = game.rand.nextInt(20) + 1; | max = game.rand.nextInt(20) + 1; |
Edição das 18h22min de 27 de outubro de 2022
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
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 public void execute() {
14 if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
15 move(UP);
16 } else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
17 move(DOWN);
18 } else if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
19 move(RIGHT);
20 } else if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
21 move(LEFT);
22 }
23 collided = false;
24 for (Actor a : game.actors) {
25 if (collision(a)) {
26 collided = true;
27 break;
28 }
29 }
30 }
31}
Monster.class
1package com.mygdx.game;
2
3import com.badlogic.gdx.graphics.Texture;
4
5class Monster extends Actor {
6 private int count = 0;
7 private int max;
8
9 Monster(Texture texture, MyGdxGame game) {
10 super(rand.nextInt(game.w - w - 1), rand.nextInt(game.h - h - 1), texture, game);
11 direction = game.rand.nextInt(4);
12 max = game.rand.nextInt(20) + 1;
13 }
14
15 @Override
16 public void execute() {
17 count++;
18 if (count > max) {
19 count = 0;
20 max = game.rand.nextInt(20) + 1;
21 move(game.rand.nextInt(4));
22 } else {
23 move(direction);
24 }
25 }
26}
MyGdxGame.class
1package com.mygdx.game;
2
3import com.badlogic.gdx.ApplicationAdapter;
4import com.badlogic.gdx.Gdx;
5import com.badlogic.gdx.Input;
6import com.badlogic.gdx.graphics.Texture;
7import com.badlogic.gdx.graphics.g2d.BitmapFont;
8import com.badlogic.gdx.graphics.g2d.SpriteBatch;
9import com.badlogic.gdx.utils.TimeUtils;
10
11import java.util.ArrayList;
12import java.util.List;
13import java.util.Random;
14
15public class MyGdxGame extends ApplicationAdapter {
16 int w, h;
17 SpriteBatch batch;
18 Random rand = new Random();
19 List<Actor> actors = new ArrayList<Actor>();
20 private Texture avatarTexture;
21 private Texture monsterTexture;
22 private Texture industrialTexture;
23 private BitmapFont font;
24 private Avatar avatar;
25 private long lastTimeCounted;
26 private float sinceChange;
27 private float frameRate;
28
29 @Override
30 public void create() {
31 w = Gdx.graphics.getWidth();
32 h = Gdx.graphics.getHeight();
33 batch = new SpriteBatch();
34 avatarTexture = new Texture("avatar.png");
35 monsterTexture = new Texture("monster.png");
36 industrialTexture = new Texture("industrial.png");
37 for (int i = 0; i < 10; i++) {
38 actors.add(new Monster(rand.nextInt(700), rand.nextInt(500), monsterTexture, this));
39 }
40 avatar = new Avatar(100, 100, avatarTexture, this);
41 actors.add(avatar);
42 font = new BitmapFont(
43 Gdx.files.internal("verdana.fnt"),
44 Gdx.files.internal("verdana.png"), false);
45 font.setColor(1, 1, 1, 1);
46 lastTimeCounted = TimeUtils.millis();
47 sinceChange = 0;
48 frameRate = Gdx.graphics.getFramesPerSecond();
49 }
50
51 private void execute() {
52 if (Gdx.input.isKeyPressed(Input.Keys.ESCAPE)) {
53 Gdx.app.exit();
54 }
55 for (Actor a : actors) a.run();
56 long delta = TimeUtils.timeSinceMillis(lastTimeCounted);
57 lastTimeCounted = TimeUtils.millis();
58 sinceChange += delta;
59 if (sinceChange >= 1000) {
60 sinceChange = 0;
61 frameRate = Gdx.graphics.getFramesPerSecond();
62 }
63 }
64
65 @Override
66 public void render() {
67 execute();
68 batch.begin();
69 batch.draw(industrialTexture, 0, 0);
70 for (Actor a : actors) a.draw();
71 if (avatar.collided) {
72 font.draw(batch, "Collided! " + (int) frameRate + " fps", 1, h + 1);
73 } else {
74 font.draw(batch, "I'm free... " + (int) frameRate + " fps", 1, h + 1);
75 }
76 batch.end();
77 }
78
79 @Override
80 public void dispose() {
81 batch.dispose();
82 avatarTexture.dispose();
83 monsterTexture.dispose();
84 industrialTexture.dispose();
85 }
86}