Bomb Physics
parent
0aa3cff3e8
commit
2a194e1f8b
|
@ -12,7 +12,9 @@ public class GenericSprite extends Rectangle{
|
|||
public double yVelocity;
|
||||
public double xVelocity;
|
||||
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 HEIGHT; //size of ball
|
||||
public boolean rightPressed = false;
|
||||
|
@ -54,10 +56,15 @@ public class GenericSprite extends Rectangle{
|
|||
}
|
||||
|
||||
public void capSpeed(){
|
||||
if(xVelocity>speedCap){
|
||||
xVelocity = speedCap;
|
||||
} else if(xVelocity<-1*speedCap) {
|
||||
xVelocity = -1*speedCap;
|
||||
if(xVelocity>speedCapx){
|
||||
xVelocity = speedCapx;
|
||||
} else if(xVelocity<-1*speedCapx) {
|
||||
xVelocity = -1*speedCapx;
|
||||
}
|
||||
if(yVelocity>speedCapy){
|
||||
yVelocity = speedCapy;
|
||||
} else if(yVelocity<-1*speedCapy) {
|
||||
yVelocity = -1*speedCapy;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
public class GlobalState {
|
||||
public static final int second = 10;
|
||||
}
|
|
@ -17,6 +17,7 @@ public class Player extends GenericSprite {
|
|||
public int lastXDirection, lastYDirection, lastFrame;
|
||||
public int upKey, downKey, rightKey, leftKey;
|
||||
|
||||
public static final int walkSpeedCap = 5;
|
||||
public boolean alive;
|
||||
private final Sound jump;
|
||||
// sA[0] is -x, -y
|
||||
|
@ -117,7 +118,7 @@ public class Player extends GenericSprite {
|
|||
}
|
||||
GamePanel.camera.x+=updateAmount+1;
|
||||
}
|
||||
xVelocity = 0;
|
||||
//xVelocity = 0;
|
||||
}
|
||||
if(!canUpdate(0, yVelocity)){
|
||||
if(yVelocity>0){
|
||||
|
@ -135,12 +136,17 @@ public class Player extends GenericSprite {
|
|||
if(canUpdate(xVelocity, yVelocity)) {
|
||||
y = y + (int) yVelocity;
|
||||
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;
|
||||
}
|
||||
if(leftPressed) {
|
||||
if(leftPressed && xVelocity>-5) {
|
||||
xVelocity -= 1;
|
||||
}
|
||||
if(upPressed&isGrounded){
|
||||
|
|
|
@ -11,21 +11,46 @@ public class StickyBomb extends GenericSprite{
|
|||
public int realX;
|
||||
BufferedImage sprite;
|
||||
|
||||
public int fuse;
|
||||
|
||||
public boolean alive;
|
||||
|
||||
public StickyBomb(int x, int y, int length, int xVelocity, int yVelocity, BufferedImage sprite){
|
||||
super(x,y,length,length);
|
||||
this.length = length;
|
||||
this.xVelocity = xVelocity;
|
||||
this.yVelocity = yVelocity;
|
||||
this.sprite = sprite;
|
||||
fuse = GlobalState.second*5;
|
||||
isMove = true;
|
||||
alive = true;
|
||||
}
|
||||
|
||||
public void update(){
|
||||
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(){
|
||||
update();
|
||||
if(fuse>0) {
|
||||
fuse-=1;
|
||||
if(fuse == 0) {
|
||||
explode();
|
||||
}
|
||||
}
|
||||
if(isMove) {
|
||||
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
|
||||
x += -Math.signum(xVelocity);
|
||||
|
@ -78,6 +103,8 @@ public class StickyBomb extends GenericSprite{
|
|||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue