Galera to tentando apontar (rotacionar) o tiro do personagem para o ponto clicado com o mouse, tentei essa maçaroca mas no consegui alguém me ajuda a completar esse BO?
public class Game extends Canvas implements Runnable, KeyListener, MouseListener {
private static final long serialVersionUID = 1L;
public static JFrame frame;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public final int scale = 1;
private int FPS;
private Thread thread;
private boolean isRunning;
private BufferedImage image;
public static Player player;
public static List<Entity>entities;
public static SpreteSheet spriteSheet;
public static World world;
public static Random aliatorio;
public static List<Inimigos>inimigosLista;
public static List<Tiros>tiros;
public UI ui;
public Game() {
aliatorio = new Random();
addKeyListener(this);
addMouseListener(this);
initFrame();
ui = new UI();
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
entities = new ArrayList<Entity>();
inimigosLista = new ArrayList<Inimigos>();
tiros = new ArrayList<Tiros>();
spriteSheet = new SpreteSheet("/sprites.png");
player = new Player(0,0,64,64,spriteSheet.getSprite(0, 0, 64, 64));
entities.add(player);
world = new World("/MiniMapa.png");
}
public void initFrame() {
frame = new JFrame("Meu Game");
frame.setPreferredSize(new Dimension(WIDTH * scale, HEIGHT * scale));
frame.add(this);
frame.setResizable(false);
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/icone.png")));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public synchronized void start() {
thread = new Thread(this);
isRunning = true;
thread.start();
}
public synchronized void stop() {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Game game = new Game();
game.start();
}
public void tick() {
for (int i =0; i<entities.size();i++) {
Entity e =entities.get(i);
e.tick();
}
for(int i=0; i<tiros.size();i++) {
tiros.get(i).tick();
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = image.getGraphics();
g.setColor(new Color(1,1,1));
g.fillRect(0,0,WIDTH, HEIGHT);
// objetos a serem renderizados
world.render(g);
for (int i =0; i<entities.size();i++) {
Entity e =entities.get(i);
e.render(g);
}
// Imagens a serem renderizadas
for(int i=0; i<tiros.size();i++) {
tiros.get(i).render(g);
}
ui.render(g);
g=bs.getDrawGraphics();
g.drawImage(image,0,0,WIDTH*scale, HEIGHT*scale,null);
g.setFont(new Font("Arial Black",Font.BOLD,30));
g.setColor(Color.BLACK);
g.drawString(""+player.municao, 717*scale, 105*scale);
g.setColor(new Color(0,255,255));
g.drawString(""+player.municao, 713*scale, 100*scale);
g.setFont(new Font("Arial Black",Font.BOLD,14));
g.setColor(Color.BLACK);
g.drawString("Pitombos",player.getX()-Camera.x+5, player.getY()-Camera.y-10);
g.setFont(new Font("Congenial UltraLight",Font.BOLD,14));
g.setColor(Color.WHITE);
g.drawString("FPS: "+FPS, 725*scale, 550*scale);
bs.show();
}
@Override
public void run() {
long lestTime = System.nanoTime();
double amountOfTicks = 60;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
int frames = 0;
double time = System.currentTimeMillis();
requestFocus();
while (isRunning) {
long now = System.nanoTime();
delta += (now - lestTime) / ns;
lestTime = now;
if (delta >= 1) {
tick();
render();
frames++;
delta--;
}
if (System.currentTimeMillis() - time >= 1000) {
System.out.println("FPS: " + FPS);
FPS = frames;
frames = 0;
time += 1000;
}
}
stop();
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
player.right= true;
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {
player.left= true;
}
if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
player.up= true;
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
player.down= true;
}
if(e.getKeyCode() == KeyEvent.VK_X) {
player.atirando=true;
}
}
@Override
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_RIGHT || e.getKeyCode() == KeyEvent.VK_D) {
player.right= false;
}
else if(e.getKeyCode() == KeyEvent.VK_LEFT || e.getKeyCode() == KeyEvent.VK_A) {
player.left= false;
}
if(e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_W) {
player.up= false;
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN || e.getKeyCode() == KeyEvent.VK_S) {
player.down= false;
}
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
player.atirandoComMouse=true;
player.posicaoMouseX=(e.getX()/scale);
player.posicaoMouseY=(e.getY()/scale);
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
/////
package Pack.Entities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import Pack.World.Camera;
import Pack.World.World;
import Pack.game.Game;
public class Tiros extends Entity {
private double direcaoX; // Mudei para double!
private double direcaoY; // Mudei para double!
private double speedT=4;
public boolean right_Tiros, up_Tiros, left_Tiros,down_Tiros;
public int right_dir_Tiros=0, up_dir_Tiros=1, left_dir_Tiros=3,down_dir_Tiros=4;
public int dir_Tiros = down_dir_Tiros;
private int frames_Tiros =0, maxFrame_Tiros=4, index_Tiros =1,maxIndex_Tiros=2;
public boolean muved_Tiros = false;
private int mascaraXTiros=16 ,mascaraYTiros=16,mascaraWTiros=30, mascaraHTiros=30;
private BufferedImage[] right_imTiros;
private BufferedImage[] left_imTiros;
private BufferedImage[] up_imTiros;
private BufferedImage[] down_imTiros;
public Tiros(int x, int y, int width, int height, BufferedImage sprite, double dx, double dy) {
super(x, y, width, height, null);
this.direcaoX =dx;
this.direcaoY =dy;
this.right_imTiros = new BufferedImage[4];
this.left_imTiros = new BufferedImage[4];
this.up_imTiros = new BufferedImage[4];
this.down_imTiros= new BufferedImage[4];
for(int i=0; i<4;i++) {
this.right_imTiros [i]=Game.spriteSheet.getSprite(64*3+(i*64), 64*6, 64, 64);
this.left_imTiros [i]=Game.spriteSheet.getSprite(64*3+(i*64), 64*5, 64, 64);
this.up_imTiros [i]=Game.spriteSheet.getSprite(64*3+(i*64), 64*7, 64, 64);
this.down_imTiros [i]=Game.spriteSheet.getSprite(64*3+(i*64), 64*4, 64, 64);
}
}
// testandoa rotaçao dos tiros com mouse
private BufferedImage rotateSprite(BufferedImage sprite, double angle) {
int spriteWidth =sprite.getWidth();
int spriteHeight =sprite.getHeight();
BufferedImage rotateSprite= new BufferedImage(spriteWidth,spriteHeight,BufferedImage.TYPE_INT_RGB);
Graphics2D g= rotateSprite.createGraphics();
AffineTransform transform = new AffineTransform();
transform.rotate(angle,spriteWidth/2,spriteHeight/2);
g.setTransform(transform);
g.drawImage(sprite, 0, 0, null);
g.dispose();
return rotateSprite;
}
public void tick(){
x+=this.direcaoX*speedT;
y+=this.direcaoY*speedT;
if (Game.player.atirando==true && muved_Tiros==true) {
frames_Tiros++;
if(frames_Tiros == maxFrame_Tiros) {
frames_Tiros=0;
index_Tiros ++;
if(index_Tiros >maxIndex_Tiros) {
index_Tiros=0;
}
}
}
// teste de rotaçao dos tiros
double angle=Math.atan2(Game.player.posicaoMouseY-y, Game.player.posicaoMouseX-x);
sprite = rotateSprite(sprite,angle);
x+=this.direcaoX*speedT;
y+=this.direcaoY*speedT;
//
if (muved_Tiros) {
frames_Tiros++;
if(frames_Tiros == maxFrame_Tiros) {
frames_Tiros=0;
index_Tiros ++;
if(index_Tiros >maxIndex_Tiros) {
index_Tiros=0;
}
}
}
}
public void render(Graphics g) {
if(dir_Tiros ==right_dir_Tiros) {
g.drawImage(right_imTiros[index_Tiros], this.getX()-Camera.x, this.getY()-Camera.y,null);
}
else if(dir_Tiros ==left_dir_Tiros) {
g.drawImage(left_imTiros[index_Tiros], this.getX()-Camera.x, this.getY()-Camera.y,null);
}
else if(dir_Tiros ==up_dir_Tiros) {
g.drawImage(up_imTiros[index_Tiros], this.getX()-Camera.x, this.getY()-Camera.y,null);
}
else if(dir_Tiros ==down_dir_Tiros) {
g.drawImage(down_imTiros[index_Tiros], this.getX()-Camera.x, this.getY()-Camera.y,null);
}
// g.setColor(Color.BLACK);
// g.fillOval(this.getX()-Camera.x, this.getY()-Camera.y, width, height);
}
}