final/src/StickyBomb.java

226 lines
8.0 KiB
Java
Raw Normal View History

2022-06-19 23:10:09 +01:00
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
2022-06-07 18:12:48 +01:00
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
2022-06-07 18:19:54 +01:00
import java.awt.image.BufferedImage;
2022-06-19 23:10:09 +01:00
import java.io.IOException;
import java.io.Serializable;
2022-06-07 18:12:48 +01:00
public class StickyBomb extends GenericSprite implements Serializable {
2022-06-09 18:40:43 +01:00
public static final int spriteLength = 35;
2022-06-12 01:02:41 +01:00
public static final int length = 25;
2022-06-07 18:12:48 +01:00
public int xVelocity;
public int yVelocity;
public boolean isMove;
public int realX;
public BufferedImageWrapper sprite ;
public BufferedImageWrapper[] explosionSpriteArray;
2022-06-07 18:12:48 +01:00
2022-06-07 19:46:45 +01:00
public int fuse;
2022-06-08 18:36:47 +01:00
public int explosionPixel = 0;
public int explosionCounter = 0;
2022-06-07 19:46:45 +01:00
public boolean alive;
2022-06-09 05:07:44 +01:00
public boolean erase;
2022-06-12 01:02:41 +01:00
public StickyBomb(int x, int y, int xVelocity, int yVelocity, BufferedImageWrapper sprite, BufferedImageWrapper[] explosionSpriteArray){
2022-06-07 18:12:48 +01:00
super(x,y,length,length);
this.xVelocity = xVelocity;
this.yVelocity = yVelocity;
2022-06-07 18:19:54 +01:00
this.sprite = sprite;
2022-06-08 18:36:47 +01:00
this.explosionSpriteArray = explosionSpriteArray;
2022-06-07 19:46:45 +01:00
fuse = GlobalState.second*5;
2022-06-07 18:12:48 +01:00
isMove = true;
2022-06-07 19:46:45 +01:00
alive = true;
2022-06-09 05:07:44 +01:00
erase = false;
2022-06-07 18:12:48 +01:00
}
2022-06-07 18:12:48 +01:00
public void update(){
realX = x - GameFrame.game.camera.x;
2022-06-07 18:12:48 +01:00
}
2022-06-19 23:10:09 +01:00
public void explode() throws UnsupportedAudioFileException, LineUnavailableException, IOException {
// Sound explode = new Sound("sound/explode.wav");
// if (explode == null) {
// try {
// explode = new Sound("sound/explode.wav");
// } catch (UnsupportedAudioFileException | LineUnavailableException e) {
// throw new RuntimeException(e);
// }
// }
// explode.start();
SoundWrapper.playSound("sound/explode.wav");
double yDis = GameFrame.game.player.y+Player.PLAYER_HEIGHT/2-(y+(double)length/2);
double xDis = GameFrame.game.player.x+Player.PLAYER_WIDTH/2-(realX+(double)length/2);
2022-06-09 05:07:44 +01:00
double hypo = Math.sqrt(yDis*yDis+xDis*xDis);
if(hypo<300) {
if (yDis != 0) {
GameFrame.game.player.yVelocity += 10 * (yDis) / (hypo);
2022-06-09 05:07:44 +01:00
}
if (xDis != 0) {
GameFrame.game.player.xVelocity += 10 * (xDis) / (hypo);
2022-06-09 05:07:44 +01:00
}
2022-06-07 19:46:45 +01:00
}
GameFrame.game.player.capSpeed();
2022-06-09 05:07:44 +01:00
alive = false;
for(int i=0; i<GameFrame.game.enemy.size(); i++){
double disX = GameFrame.game.enemy.get(i).x+GameFrame.game.enemy.get(i).npcWidth/2 - (x+length/2);
double disY = GameFrame.game.enemy.get(i).y+GameFrame.game.enemy.get(i).npcHeight/2 - (y+length/2);
2022-06-09 18:05:42 +01:00
double eHypo = Math.sqrt(disX*disX+disY*disY);
if(eHypo<200){
GameFrame.game.enemy.get(i).isDead = true;
2022-06-09 19:19:23 +01:00
}
2022-06-12 04:48:57 +01:00
2022-06-09 18:05:42 +01:00
}
2022-06-15 16:25:38 +01:00
int lowX = Math.max(0, ((this.x+GamePanel.GAME_WIDTH/2)/Tile.length)-4);
int highX = Math.min(lowX + 8, GameFrame.game.map.length);
2022-06-12 19:11:09 +01:00
int lowY = Math.max(0,(this.y/Tile.length)-6);
int highY = Math.min(lowY + 12, GameFrame.game.map[0].length);
2022-06-15 16:25:38 +01:00
for(int i=lowX; i<highX; i++) {
2022-06-12 19:11:09 +01:00
for (int j = lowY; j < highY; j++) {
if (GameFrame.game.map[i][j] != null && GameFrame.game.map[i][j].breakable) {
double disX = (x + WIDTH / 2) - (GameFrame.game.map[i][j].x + Tile.length / 2);
double disY = (y + HEIGHT / 2) - (GameFrame.game.map[i][j].y + Tile.length / 2);
2022-06-15 16:25:38 +01:00
if (Math.sqrt(disX * disX + disY * disY) < Tile.length * 3) {
GameFrame.game.map[i][j] = null;
2022-06-12 19:11:09 +01:00
}
}
}
}
2022-06-07 19:46:45 +01:00
}
2022-06-09 18:40:43 +01:00
public boolean collide(Tile tile, double x, double y){
if(!tile.collision||tile.nonBombCollide){
return false;
}
if(x+WIDTH>tile.x&&x<tile.x+Tile.length&&y-tile.y<Tile.length&&tile.y-y<HEIGHT){
return true;
}
return false;
}
2022-06-19 23:10:09 +01:00
public void move() throws UnsupportedAudioFileException, LineUnavailableException, IOException {
2022-06-12 01:02:41 +01:00
boolean checked = false;
2022-06-09 19:19:23 +01:00
if(yVelocity>50){yVelocity=50;}
2022-06-07 18:12:48 +01:00
update();
2022-06-07 19:46:45 +01:00
if(fuse>0) {
fuse-=1;
if(fuse == 0) {
explode();
}
}
2022-06-12 22:28:27 +01:00
if(canUpdate(1,1)&&canUpdate(-1,1)&&canUpdate(0,-1)&&isMove == false&&fuse>0){
isMove = true;
xVelocity = 0;
yVelocity = 0;
}
2022-06-07 18:12:48 +01:00
if(isMove) {
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
2022-06-12 01:02:41 +01:00
checked = true;
2022-06-07 18:12:48 +01:00
x += -Math.signum(xVelocity);
2022-06-12 01:02:41 +01:00
isMove = false;
int updateAmount = 0;
if(xVelocity>0&&yVelocity>0){
while(canUpdate(updateAmount, updateAmount)){
updateAmount++;
}
x+=updateAmount;
y+=updateAmount;
} else if(xVelocity<0&&yVelocity>0){
while(canUpdate(-updateAmount, updateAmount)){
updateAmount++;
}
x-=updateAmount;
y+=updateAmount;
} else if(xVelocity>0&&yVelocity<0){
while(canUpdate(updateAmount, -updateAmount)){
updateAmount++;
}
x+=updateAmount;
y-=updateAmount;
} else if(xVelocity<0&&yVelocity<0){
while(canUpdate(-updateAmount, -updateAmount)){
updateAmount++;
}
x-=updateAmount;
y-=updateAmount;
}
xVelocity = 0;
yVelocity = 0;
2022-06-07 18:12:48 +01:00
}
if(!canUpdate(xVelocity, 0)){
2022-06-12 01:02:41 +01:00
checked = true;
2022-06-07 18:12:48 +01:00
isMove = false;
int updateAmount = 0;
if(xVelocity>0){
while(canUpdate(updateAmount, 0)){
updateAmount++;
}
x+=updateAmount-1;
} else if(xVelocity<0){
while(canUpdate(updateAmount, 0)){
updateAmount--;
}
x+=updateAmount+1;
}
xVelocity = 0;
}
if(!canUpdate(0, yVelocity)){
2022-06-12 01:02:41 +01:00
checked = true;
2022-06-07 18:12:48 +01:00
isMove = false;
if(yVelocity>0){
while(canUpdate(0,1)){
y+=1;
}
isGrounded = true;
} else if(yVelocity<0){
while(canUpdate(0,-1)){
y-=1;
}
}
yVelocity = 0;
}
2022-06-12 01:02:41 +01:00
if(canUpdate(xVelocity, yVelocity)&&!checked) {
2022-06-07 18:12:48 +01:00
y = y + (int) yVelocity;
x = x + (int) xVelocity;
} else {
isMove = false;
}
yVelocity+=3;
}
2022-06-09 19:19:23 +01:00
2022-06-07 18:12:48 +01:00
}
2022-06-09 05:07:44 +01:00
2022-06-07 18:12:48 +01:00
2022-06-12 01:02:41 +01:00
2022-06-19 23:10:09 +01:00
public void draw(Graphics g) throws UnsupportedAudioFileException, LineUnavailableException, IOException {
2022-06-08 18:36:47 +01:00
if (explosionCounter >= 2) {
explosionPixel += 1;
explosionCounter -= 2;
}
2022-06-07 19:46:45 +01:00
if(alive) {
g.drawImage(sprite.image, x - GameFrame.game.camera.x - (spriteLength-length)/2, y - (spriteLength-length)/2, spriteLength, spriteLength, null);
//g.drawRect(x-GameFrame.game.camera.x,y,length,length);
2022-06-08 18:36:47 +01:00
} else if (explosionPixel < explosionSpriteArray.length - 1) {
// please note that the explosion is completely centered on the x plane ("5*") but tends upwards on the y plane ("10*")
g.drawImage(explosionSpriteArray[explosionPixel].image, x - GameFrame.game.camera.x - 5*explosionPixel,
2022-06-09 18:40:43 +01:00
y-10*explosionPixel, spriteLength+10*explosionPixel, spriteLength+10*explosionPixel, null);
2022-06-08 18:36:47 +01:00
explosionCounter += 1;
2022-06-09 05:07:44 +01:00
} else {
erase = true;
2022-06-07 19:46:45 +01:00
}
2022-06-07 18:12:48 +01:00
}
}