I try to make a 2d tiled game in Java.
So I have this Bullet class and Gun class which shoots bullets when I press the left mouse button. How to make the enemy to disappear when a number of bullets collide with it?
I think to have a number of bullets let's say 5 and after 5 collisions of the bullets with the enemy I should stop render the enemy, but how implement that collision with enemy?
The bullets are basically a ball (fillOval).
The bullet class
package BunnyFights.Entities.Creatures;
import BunnyFights.Entities.Entity;
import BunnyFights.Handler;
import BunnyFights.gfx.Assets;
import java.awt.*;
public class Bullet extends Entity {
private Snake snake;
private float dx, dy;
private float xSpeed = 1.2f,ySpeed = 1.2f;
private float speed = 1.5f;
private boolean isDead;
public Bullet(Handler handler, float x, float y, int width, int height,int dx,int dy) {
super(handler, x, y, width, height);
//destination x and y
this.dx = dx;
this.dy = dy;
//distance from destination
int distance = distanceFromDestination();
//set x and y speed
xSpeed = (dx-x)/distance * speed;
ySpeed = (dy-y)/distance * speed;
}
@Override
public void tick() {
if(isDead)
return;
//move bullet
x += xSpeed;
y += ySpeed;
//if distance from destination < 1 then, bullet dies
if(distanceFromDestination() <= 1)
isDead = true;
}
@Override
public void render(Graphics g) {
if(isDead)
return;
g.setColor(Color.RED);
g.fillOval((int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height);
}
public boolean isDead(){
return isDead;
}
private int distanceFromDestination(){
int distance = (int) Math.sqrt(Math.pow(x - dx,2) + Math.pow(y - dy,2));
return distance;
}
}
The gun class which represent the hero who shoots
package BunnyFights.Entities.Creatures;
import BunnyFights.Entities.Entity;
import BunnyFights.Handler;
import java.awt.*;
import java.util.ArrayList;
public class Gun extends Entity {
private ArrayList<Bullet> bullets;
private int bulletWidth = 25,bulletHeight = 25;
private int fireCoolDown = 30;//player can fire after every 80 ticks
private int timer;
private boolean canFire;
public Gun(Handler handler, float x, float y) {
super(handler, x, y, 0,0);
bullets = new ArrayList<Bullet>();
}
@Override
public void tick() {
//counting fire cool down time, if cannot fire
if(!canFire) {
timer++;
if (timer >= fireCoolDown) {
timer = 0;
canFire = true;
}
}
for(Bullet bullet : bullets)
bullet.tick();
//remove dead bullets
for(int i = bullets.size() - 1; i >= 0; i--)
if(bullets.get(i).isDead())
bullets.remove(i);
}
@Override
public void render(Graphics g) {
for(Bullet bullet : bullets)
bullet.render(g);
}
public void fire(float x,float y,int dx,int dy){
if(!canFire)
return;
Bullet b = new Bullet(handler,x,y,bulletWidth,bulletHeight,dx,dy);
bullets.add(b);
canFire = false;
}
}
The enemy class (Snake)
package BunnyFights.Entities.Creatures;
import BunnyFights.Handler;
import BunnyFights.States.GameState;
import BunnyFights.Tiles.Tile;
import BunnyFights.gfx.Assets;
import javax.sql.rowset.spi.XmlReader;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Snake extends Creature {
private BufferedImage image;
public Snake(Handler handler, float x, float y) {
super(handler, x, y, DEFAULT_CREATURE_WIDTH, DEFAULT_CREATURE_HEIGHT);
}
@Override
public void tick() {
move();
// handler.getGameCamera().centerOnEntity(this);
}
@Override
public void render(Graphics g) {
g.drawImage(image, (int) (x - handler.getGameCamera().getxOffset()), (int) (y - handler.getGameCamera().getyOffset()), width, height, null);
}
public void moveToPlayer(Player player) {
float deltaX = player.getX() - this.getX();
float deltaY = player.getY() - this.getY();
float hyp = (float) Math.sqrt(deltaX*deltaX + deltaY*deltaY);
deltaX /= hyp;
deltaY /= hyp;
this.setxMove(deltaX * speed);
this.setyMove(deltaY * speed);
}
public void rotateSnake(Player player) {
if (((player.getX() < (this.getX()+50)) && (player.getX() > (this.getX()-50))) && player.getY() > this.getY()) {
image = Assets.snakeFace;
}
else if (player.getX() > (this.getX()+50)) {
image = Assets.snakeRight;
}
else {
image = Assets.snakeLeft;
}
if (((player.getX() < (this.getX()+50)) && (player.getX() > (this.getX()-50))) && player.getY() < this.getY()) {
image = Assets.snakeBack;
}
}
public void snakePlayerRelation(Player player) {
tick();
moveToPlayer(player);
rotateSnake(player);
}
}