Bomb Physics

master
bob 2022-06-07 14:46:45 -04:00
parent 0aa3cff3e8
commit 2a194e1f8b
4 changed files with 52 additions and 9 deletions

View File

@ -12,7 +12,9 @@ public class GenericSprite extends Rectangle{
public double yVelocity; public double yVelocity;
public double xVelocity; public double xVelocity;
public final double SPEED = 20; //movement speed of ball public final double SPEED = 20; //movement speed of ball
public final double speedCap = 5; //Speed cap of ball public final double speedCapx = 50;
public final double speedCapy = 20;
public int WIDTH; //size of ball public int WIDTH; //size of ball
public int HEIGHT; //size of ball public int HEIGHT; //size of ball
public boolean rightPressed = false; public boolean rightPressed = false;
@ -54,10 +56,15 @@ public class GenericSprite extends Rectangle{
} }
public void capSpeed(){ public void capSpeed(){
if(xVelocity>speedCap){ if(xVelocity>speedCapx){
xVelocity = speedCap; xVelocity = speedCapx;
} else if(xVelocity<-1*speedCap) { } else if(xVelocity<-1*speedCapx) {
xVelocity = -1*speedCap; xVelocity = -1*speedCapx;
}
if(yVelocity>speedCapy){
yVelocity = speedCapy;
} else if(yVelocity<-1*speedCapy) {
yVelocity = -1*speedCapy;
} }
} }

3
src/GlobalState.java Normal file
View File

@ -0,0 +1,3 @@
public class GlobalState {
public static final int second = 10;
}

View File

@ -17,6 +17,7 @@ public class Player extends GenericSprite {
public int lastXDirection, lastYDirection, lastFrame; public int lastXDirection, lastYDirection, lastFrame;
public int upKey, downKey, rightKey, leftKey; public int upKey, downKey, rightKey, leftKey;
public static final int walkSpeedCap = 5;
public boolean alive; public boolean alive;
private final Sound jump; private final Sound jump;
// sA[0] is -x, -y // sA[0] is -x, -y
@ -117,7 +118,7 @@ public class Player extends GenericSprite {
} }
GamePanel.camera.x+=updateAmount+1; GamePanel.camera.x+=updateAmount+1;
} }
xVelocity = 0; //xVelocity = 0;
} }
if(!canUpdate(0, yVelocity)){ if(!canUpdate(0, yVelocity)){
if(yVelocity>0){ if(yVelocity>0){
@ -135,12 +136,17 @@ public class Player extends GenericSprite {
if(canUpdate(xVelocity, yVelocity)) { if(canUpdate(xVelocity, yVelocity)) {
y = y + (int) yVelocity; y = y + (int) yVelocity;
GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity; GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity;
} else if(canUpdate(0,yVelocity)){
y = y + (int) yVelocity;
} else if(canUpdate(xVelocity,0)){
GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity;
xVelocity*=0.2;
} }
if(rightPressed){ if(rightPressed && xVelocity<5){
xVelocity+=1; xVelocity+=1;
} }
if(leftPressed) { if(leftPressed && xVelocity>-5) {
xVelocity -= 1; xVelocity -= 1;
} }
if(upPressed&isGrounded){ if(upPressed&isGrounded){

View File

@ -11,21 +11,46 @@ public class StickyBomb extends GenericSprite{
public int realX; public int realX;
BufferedImage sprite; BufferedImage sprite;
public int fuse;
public boolean alive;
public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity, BufferedImage sprite){ public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity, BufferedImage sprite){
super(x,y,length,length); super(x,y,length,length);
this.length = length; this.length = length;
this.xVelocity = xVelocity; this.xVelocity = xVelocity;
this.yVelocity = yVelocity; this.yVelocity = yVelocity;
this.sprite = sprite; this.sprite = sprite;
fuse = GlobalState.second*5;
isMove = true; isMove = true;
alive = true;
} }
public void update(){ public void update(){
realX = x - GamePanel.camera.x; realX = x - GamePanel.camera.x;
} }
public void explode(){
double yDis = GamePanel.player.y+Player.PLAYER_HEIGHT/2-(y+length/2);
double xDis = GamePanel.player.x+Player.PLAYER_HEIGHT/2-(realX+length/2);
double hypo = Math.sqrt(yDis*yDis+xDis+xDis);
if(yDis!=0){
GamePanel.player.yVelocity = 10000/(yDis*hypo);
}
if(xDis!=0) {
GamePanel.player.xVelocity = 100000/(xDis*hypo);
}
GamePanel.player.capSpeed();
alive = false;
}
public void move(){ public void move(){
update(); update();
if(fuse>0) {
fuse-=1;
if(fuse == 0) {
explode();
}
}
if(isMove) { if(isMove) {
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){ if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
x += -Math.signum(xVelocity); x += -Math.signum(xVelocity);
@ -78,6 +103,8 @@ public class StickyBomb extends GenericSprite{
} }
public void draw(Graphics g){ public void draw(Graphics g){
g.drawImage(sprite,x-GamePanel.camera.x,y, length, length, null); if(alive) {
g.drawImage(sprite, x - GamePanel.camera.x, y, length, length, null);
}
} }
} }