final/src/StickyBomb.java

86 lines
2.4 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;
public class StickyBomb extends GenericSprite{
public int length;
public int xVelocity;
public int yVelocity;
public boolean isMove;
public int realX;
public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity){
super(x,y,length,length);
this.length = length;
this.xVelocity = xVelocity;
this.yVelocity = yVelocity;
isMove = true;
}
public void update(){
realX = x - GamePanel.camera.x;
}
public void move(){
update();
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;
}
}
public void mousePressed(MouseEvent e){
int xx = e.getX();
int yy = e.getY();
GamePanel.b = new StickyBomb(GamePanel.player.x+GamePanel.camera.x,GamePanel.player.y,35, (xx-GamePanel.player.x-GamePanel.camera.x)/20, (yy-GamePanel.player.y)/10);
}
public void draw(Graphics g){
g.drawRect(x-GamePanel.camera.x,y,length,length);
}
}