Skip to content

Commit

Permalink
Animacao player
Browse files Browse the repository at this point in the history
  • Loading branch information
elisandro-tnb committed Dec 4, 2021
1 parent b2e454f commit 4eb6e89
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 41 deletions.
54 changes: 44 additions & 10 deletions src/com/ks2002br/entities/Player.java
Expand Up @@ -20,11 +20,25 @@ public class Player extends GameObject {
private Texturas tex = Game.getInstance();

private GameController gc;

private Animation animIdle, animEsq, animDir;
private int dir = 1; //1 = direita -1 = esquerda
private boolean move = false;

public Player(float x, float y, int tipo, ObjectId id, GameController gc) {
super(x, y, id);
this.gc = gc;
this.tipo = tipo;

startPlayer();
}

private void startPlayer() {

animIdle = new Animation(10,tex.player_idle);
animEsq = new Animation(5, tex.playerLeft);
animDir = new Animation(5, tex.playerRight);

}

public void tick(LinkedList<GameObject> obj) {
Expand All @@ -42,12 +56,27 @@ public void tick(LinkedList<GameObject> obj) {
spdY += gravity;
if(spdY > MAX_SPD) spdY = MAX_SPD;
}

verificarColisao(obj);

if (spdX > 0 ) dir = 1;
if (spdX < 0 ) dir = -1;

startAnim();
verificarColisao(obj);

}


private void startAnim() {

animEsq.runAnimation();
animDir.runAnimation();
animIdle.runAnimation();

if (spdX != 0 ) move = true;
else move = false;

}

private void verificarColisao(LinkedList<GameObject> obj) {
for (int i = 0; i < gc.obj.size(); i++) {
GameObject tempObj = gc.obj.get(i);
Expand Down Expand Up @@ -94,15 +123,20 @@ else if (getBoundsDir() .intersects(tempObj.getBounds()))
}

public void render(Graphics g) {
//g.setColor(Color.white);
// g.fillRect((int) x, (int) y, width, height);

// if(tipo== 0) g.drawImage(tex.player[0],(int) x,(int) y,null);
// if(tipo== 1) g.drawImage(tex.player[1],(int) x,(int) y,null);
// if(tipo== 2) g.drawImage(tex.player[2],(int) x,(int) y,null);
// if(tipo== 3) g.drawImage(tex.player[3],(int) x,(int) y,null);

Graphics2D g2d = (Graphics2D) g;

if(move) {

if(dir == 1) animDir.renderAnimation(g2d, (int) x, (int) y);
else if(dir == -1) animEsq.renderAnimation(g2d, (int) x, (int) y);


}else {
animIdle.renderAnimation(g2d, (int) x, (int) y);
}




// REDERIZACAO DAS CAIXAS DE COLISOES
if (gc.isDebug()) {
Expand Down
8 changes: 4 additions & 4 deletions src/com/ks2002br/game/Game.java
Expand Up @@ -37,7 +37,7 @@ public class Game extends Canvas implements Runnable {
private static Texturas tex;


private TestAnim testAnim;
//private TestAnim testAnim;


// CONTRUTOR DA CLASSE
Expand All @@ -56,7 +56,7 @@ private void startGame() {
CarregarImagem mapa = new CarregarImagem();
tex = new Texturas();

testAnim = new TestAnim(40, 60, null);
//testAnim = new TestAnim(40, 60, null);

level = mapa.pegarImagem("/mapa-01.png");
world = new World(level, gc);
Expand Down Expand Up @@ -133,7 +133,7 @@ private void tick() {
}


testAnim.tick(null);
// testAnim.tick(null);

}

Expand Down Expand Up @@ -161,7 +161,7 @@ private void render() {
g2d.translate(-cam.getCamX(),-cam.getCamY()); // CAMERA FIM


testAnim.render(g2d);
// testAnim.render(g2d);

// FINAL DO OBJETOS A SEREM DESENHADOS
bs.show(); // MOSTRAR TUDO QUE O PINTOR DESENHOU
Expand Down
42 changes: 15 additions & 27 deletions src/com/ks2002br/graficos/Texturas.java
Expand Up @@ -51,43 +51,31 @@ private void getTextures() {

//PLAYER

playerUp[0] = playerSheet.pegarSpriteColRow(1, 1, 32, 64);
playerUp[1] = playerSheet.pegarSpriteColRow(2, 1, 32, 64);
playerUp[2] = playerSheet.pegarSpriteColRow(3, 1, 32, 64);
playerUp[3] = playerSheet.pegarSpriteColRow(4, 1, 32, 64);
playerUp[4] = playerSheet.pegarSpriteColRow(5, 1, 32, 64);
for (int i = 0; i < playerUp.length; i++) {
playerUp[i] = playerSheet.pegarSpriteColRow(i+1, 1, 32, 64);
}

// PLAYER DOWN
playerDown[0] = playerSheet.pegarSpriteColRow(1, 2, 32, 64);
playerDown[1] = playerSheet.pegarSpriteColRow(2, 2, 32, 64);
playerDown[2] = playerSheet.pegarSpriteColRow(3, 2, 32, 64);
playerDown[3] = playerSheet.pegarSpriteColRow(4, 2, 32, 64);
playerDown[4] = playerSheet.pegarSpriteColRow(5, 2, 32, 64);
for (int i = 0; i <playerDown.length; i++) {
playerDown[i] = playerSheet.pegarSpriteColRow(i+1, 2, 32, 64);

}

// PLAYER LEFT
playerLeft[0] = playerSheet.pegarSpriteColRow(1, 3, 32, 64);
playerLeft[1] = playerSheet.pegarSpriteColRow(2, 3, 32, 64);
playerLeft[2] = playerSheet.pegarSpriteColRow(3, 3, 32, 64);
playerLeft[3] = playerSheet.pegarSpriteColRow(4, 3, 32, 64);
playerLeft[4] = playerSheet.pegarSpriteColRow(5, 3, 32, 64);
playerLeft[5] = playerSheet.pegarSpriteColRow(6, 3, 32, 64);
playerLeft[6] = playerSheet.pegarSpriteColRow(7, 3, 32, 64);
playerLeft[7] = playerSheet.pegarSpriteColRow(8, 3, 32, 64);
for (int i = 0; i < playerLeft.length; i++) {
playerLeft[i] = playerSheet.pegarSpriteColRow(i+1, 4, 32, 64);
}

// PLAYER RIGHT
playerRight[0] = playerSheet.pegarSpriteColRow(1, 4, 32, 64);
playerRight[1] = playerSheet.pegarSpriteColRow(2, 4, 32, 64);
playerRight[2] = playerSheet.pegarSpriteColRow(3, 4, 32, 64);
playerRight[3] = playerSheet.pegarSpriteColRow(4, 4, 32, 64);
playerRight[4] = playerSheet.pegarSpriteColRow(5, 4, 32, 64);
playerRight[5] = playerSheet.pegarSpriteColRow(6, 4, 32, 64);
playerRight[6] = playerSheet.pegarSpriteColRow(7, 4, 32, 64);
playerRight[7] = playerSheet.pegarSpriteColRow(8, 4, 32, 64);
for (int i = 0; i < playerRight.length; i++) {
playerRight[i] = playerSheet.pegarSpriteColRow(i+1, 3, 32, 64);
}

player_idle[0] = playerSheet.pegarSpriteColRow(7, 2, 32,64); //FRAME 01
player_idle[1] = playerSheet.pegarSpriteColRow(8, 2, 32,64); //FRAME 02

}


//94
}

0 comments on commit 4eb6e89

Please sign in to comment.