final/src/StickyBomb.java

137 lines
4.3 KiB
Java
Raw Normal View History

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-07 18:12:48 +01:00
public class StickyBomb extends GenericSprite{
public int length;
2022-06-09 18:40:43 +01:00
public static final int spriteLength = 35;
2022-06-07 18:12:48 +01:00
public int xVelocity;
public int yVelocity;
public boolean isMove;
public int realX;
2022-06-09 05:07:44 +01:00
public static BufferedImage sprite ;
public static BufferedImage[] 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-08 18:36:47 +01:00
public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity, BufferedImage sprite, BufferedImage[] explosionSpriteArray){
2022-06-07 18:12:48 +01:00
super(x,y,length,length);
this.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
}
public void update(){
realX = x - GamePanel.camera.x;
}
2022-06-07 19:46:45 +01:00
public void explode(){
2022-06-08 04:59:19 +01:00
double yDis = GamePanel.player.y+Player.PLAYER_HEIGHT/2-(y+(double)length/2);
2022-06-09 05:07:44 +01:00
double xDis = GamePanel.player.x+Player.PLAYER_WIDTH/2-(realX+(double)length/2);
double hypo = Math.sqrt(yDis*yDis+xDis*xDis);
if(hypo<300) {
if (yDis != 0) {
GamePanel.player.yVelocity += 10 * (yDis) / (hypo);
}
if (xDis != 0) {
GamePanel.player.xVelocity += 10 * (xDis) / (hypo);
}
2022-06-07 19:46:45 +01:00
}
GamePanel.player.capSpeed();
2022-06-09 05:07:44 +01:00
alive = false;
2022-06-09 18:05:42 +01:00
for(int i=0; i<GamePanel.enemy.size(); i++){
double disX = GamePanel.enemy.get(i).x+GamePanel.enemy.get(i).npcWidth/2 - (x+length/2);
double disY = GamePanel.enemy.get(i).y+GamePanel.enemy.get(i).npcHeight/2 - (y+length/2);
double eHypo = Math.sqrt(disX*disX+disY*disY);
if(eHypo<200){
GamePanel.enemy.remove(i);
}
}
2022-06-07 19:46:45 +01:00
}
2022-06-09 18:40:43 +01:00
2022-06-07 18:12:48 +01:00
public void move(){
update();
2022-06-07 19:46:45 +01:00
if(fuse>0) {
fuse-=1;
if(fuse == 0) {
explode();
}
}
2022-06-07 18:12:48 +01:00
if(isMove) {
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
x += -Math.signum(xVelocity);
//isMove = false;
}
if(!canUpdate(xVelocity, 0)){
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)){
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;
}
if(canUpdate(xVelocity, yVelocity)) {
y = y + (int) yVelocity;
x = x + (int) xVelocity;
} else {
isMove = false;
}
yVelocity+=3;
}
}
2022-06-09 05:07:44 +01:00
2022-06-07 18:12:48 +01:00
public void draw(Graphics g){
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) {
2022-06-09 18:40:43 +01:00
g.drawImage(sprite, x - GamePanel.camera.x - (spriteLength-length)/2, y - (spriteLength-length)/2, spriteLength, spriteLength, null);
2022-06-08 18:36:47 +01:00
} else if (explosionPixel < explosionSpriteArray.length - 1) {
g.drawImage(explosionSpriteArray[explosionPixel], x - GamePanel.camera.x - 10*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
}
}