Estou fazendo um curso de desenvolvimento de games em java, entendo 0 de programação mas tamo ai na luta né.
Bom, quando o meu player é danificado pelo inimigo, ele pisca no sprite branco dele, até ai ok. Mas, tem a possibilidade do player pegar uma arma e eu queria que ela piscasse no sprite branco dela também ao msm tempo que o player. O professor não iria fazer essa parte no curso pois ele acha que já somos capazes de fazer isso. Dei uma mexida aqui e ali e o meu resultado final foi que o player spawna já com o sprite branco da arma nas costas dele, e quando danificado spawna a arma na mão do player (o que não deveria acontecer) se virado para a direita o sprite da arma para direita e do player piscam em branco e volta com a arma branca nas costas e a arma normal na mão para a direita. O player virado para a esquerda pisca normal, mas a arma que já estava branca continua branca. Por favor, me deem uma luz, tentei o suporte do curso mas tem pergunta que fiz a dois dias, que já resolvi o problema e ainda não responderam.
package com.gcstudios.entities;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import com.gcstudios.graficos.Spritesheet;
import com.gcstudios.main.Game;
import com.gcstudios.world.Camera;
import com.gcstudios.world.World;
public class Player extends Entity{
public boolean right,up,left,down;
public int right_dir = 0,left_dir = 1;
public int dir = right_dir;
public double speed = 1.4;
private int frames = 0, maxFrames = 5, index = 0, maxIndex = 3;
private boolean moved = false;
private BufferedImage[] rightPlayer;
private BufferedImage[] leftPlayer;
private BufferedImage playerDamage;
private BufferedImage gunDamageRight;
private BufferedImage gunDamageLeft;
public int ammo = 0;
public boolean arma = false;
public boolean isDamaged = false;
public boolean gunDamaged = false;
private int damageFrames = 0;
public double life = 100,maxlife = 100;
public Player(int x, int y, int width, int height, BufferedImage sprite) {
super(x, y, width, height, sprite);
rightPlayer = new BufferedImage[4];
leftPlayer = new BufferedImage[4];
playerDamage = Game.spritesheet.getSprite(0, 16, 16, 16);
gunDamageRight = Game.spritesheet.getSprite(128, 32, 16, 16);
gunDamageLeft = Game.spritesheet.getSprite(128+16, 32, 16, 16);
for(int i =0; i < 4; i++) {
rightPlayer[i] = Game.spritesheet.getSprite(32 + (i*16), 0, 16,16);
}
for(int i =0; i < 4; i++) {
leftPlayer[i] = Game.spritesheet.getSprite(32 + (i*16), 16, 16,16);
}
}
public void tick() {
moved = false;
if(right && World.isFree((int)(x+speed),this.getY())) {
moved = true;
dir = right_dir;
x+=speed;
}
else if(left && World.isFree((int)(x-speed),this.getY())) {
moved = true;
dir = left_dir;
x-=speed;
}
if(up && World.isFree(this.getX(),(int)(y-speed))) {
moved = true;
y-=speed;
}
else if(down && World.isFree(this.getX(),(int)(y+speed))) {
moved = true;
y+=speed;
}
if(moved) {
frames++;
if(frames == maxFrames) {
frames = 0;
index++;
if(index > maxIndex)
index = 0;
}
}
this.checkCollisionLifepack();
this.checkCollisionAmmo();
this.checkCollisionGun();
if(isDamaged) {
this.damageFrames++;
if(this.damageFrames == 8){
this.damageFrames = 0;
isDamaged = false;
}
}
/*
if(life<=0) {
Game.entities.clear();
Game.enemies.clear();
Game.entities = new ArrayList<Entity>();
Game.enemies = new ArrayList<Enemy>();
Game.spritesheet = new Spritesheet("/spritesheet.png");
Game.player = new Player(0,0,16,16,Game.spritesheet.getSprite(32, 0, 16, 16));
Game.entities.add(Game.player);
Game.world = new World("/map.png");
return;
}
*/
Camera.x = Camera.clamp(this.getX() - (Game.WIDTH/2),0,World.WIDTH*16 - Game.WIDTH);
Camera.y = Camera.clamp(this.getY() - (Game.HEIGHT/2),0,World.HEIGHT*16 - Game.HEIGHT);
}
public void checkCollisionGun() {
for(int i = 0; i < Game.entities.size(); i++) {
Entity atual = Game.entities.get(i);
if(atual instanceof Weapon) {
if(Entity.isColidding(this, atual)) {
arma = true;
Game.entities.remove(atual);
}
}
}
}
public void checkCollisionAmmo() {
for(int i = 0; i < Game.entities.size(); i++) {
Entity atual = Game.entities.get(i);
if(atual instanceof Bullet) {
if(Entity.isColidding(this, atual)) {
ammo+=10;
//System.out.println("Municao atual;" + ammo);
Game.entities.remove(atual);
}
}
}
}
public void checkCollisionLifepack() {
for(int i = 0; i < Game.entities.size(); i++) {
Entity atual = Game.entities.get(i);
if(atual instanceof Lifepack) {
if(Entity.isColidding(this, atual)) {
life+=8;
if(life >= 100)
life = 100;
Game.entities.remove(atual);
return;
}
}
}
}
public void render(Graphics g) {
if(!isDamaged) {
if(dir == right_dir) {
g.drawImage(rightPlayer[index],this.getX() - Camera.x, this.getY() - Camera.y, null);
if(arma == true) {
//Desenhar arma para direita.
g.drawImage(Entity.GUN_RIGHT, this.getX()+10 - Camera.x,this.getY()-2 - Camera.y, null);
}
}
else if(dir == left_dir) {
g.drawImage(leftPlayer[index],this.getX() - Camera.x, this.getY() - Camera.y, null);
if(arma == true) {
//Desenhar arma para esquerda.
g.drawImage(Entity.GUN_LEFT, this.getX()-10 - Camera.x,this.getY()-2 - Camera.y, null);
}
}
}else {
g.drawImage(playerDamage, this.getX() - Camera.x, this.getY() - Camera.y, null);
}
if(isDamaged && arma == true && dir == right_dir) {
g.drawImage(gunDamageRight, this.getX() + 10 - Camera.x, this.getY() - 2 - Camera.y,null);
}else {
g.drawImage(gunDamageLeft, this.getX() - 10 - Camera.x, this.getY() - 2 - Camera.y,null);
}
}
}
package com.gcstudios.entities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import com.gcstudios.main.Game;
import com.gcstudios.world.Camera;
import com.gcstudios.world.World;
public class Enemy extends Entity{
private double speed = 0.4;
private int maskx = 8, masky = 8, maskw = 10, maskh = 10;
private int frames = 0, maxFrames = 20, index = 0, maxIndex = 1;
private BufferedImage[] sprites;
public Enemy(int x, int y, int width, int height, BufferedImage sprite) {
super(x, y, width, height, null);
sprites = new BufferedImage[2];
sprites[0] = Game.spritesheet.getSprite(112,16,16,16);
sprites[1] = Game.spritesheet.getSprite(112+16,16,16,16);
}
public void tick() {
/*
maskx = 8;
masky = 8;
maskw = 8;
maskh = 8;
*/
if(isColiddingWithPlayer() == false) {
if((int)x < Game.player.getX() && World.isFree((int)(x+speed), this.getY())
&& !isColidding((int)(x+speed), this.getY())) {
x+=speed;
}
else if((int)x > Game.player.getX() && World.isFree((int)(x-speed), this.getY())
&& !isColidding((int)(x-speed), this.getY())) {
x-=speed;
}
if((int)y < Game.player.getY() && World.isFree(this.getX(),(int)(y+speed))
&& !isColidding(this.getX(), (int)(y+speed))) {
y+=speed;
}
else if((int)y > Game.player.getY() && World.isFree(this.getX(),(int)(y-speed))
&& !isColidding(this.getX(), (int)(y-speed))) {
y-=speed;
}
}else {
//Estamos colidindo
if(Game.rand.nextInt(100) < 10) {
Game.player.life-=Game.rand.nextInt(3);
Game.player.isDamaged = true;
if(Game.player.arma = true) {
Game.player.gunDamaged = true;
//System.out.println("Vida: "+ Game.player.life);
}
}
}
frames++;
if(frames == maxFrames) {
frames = 0;
index++;
if(index > maxIndex)
index = 0;
}
}
//}
public boolean isColiddingWithPlayer() {
Rectangle enemyCurrent = new Rectangle(this.getX() + maskx,this.getY() + masky,maskw,maskh);
Rectangle player = new Rectangle(Game.player.getX(),Game.player.getY(), 16,16);
return enemyCurrent.intersects(player);
}
public boolean isColidding(int xnext, int ynext) {
Rectangle enemyCurrent = new Rectangle(xnext + maskx,ynext + masky,maskw,maskh);
for(int i = 0; i < Game.enemies.size(); i++) {
Enemy e = Game.enemies.get(i);
if(e == this)
continue;
Rectangle targetEnemy = new Rectangle(e.getX() + maskx,e.getY() + masky,maskw,maskh);
if(enemyCurrent.intersects(targetEnemy)) {
return true;
}
}
return false;
}
public void render(Graphics g) {
g.drawImage(sprites[index], this.getX() - Camera.x,this.getY() - Camera.y, null);
//g.setColor(Color.blue);
//g.fillRect(this.getX() + maskx - Camera.x, this.getY() + masky - Camera.y, maskw,maskh);
}
}