2021-06-20 19:01:16 +02:00

105 lines
1.9 KiB
Java

package gameplay.hitboxes;
public class HitBox {
private float position_x;
private float position_y;
private float size_x;
private float size_y;
public HitBox() {
this.position_x = 0.0f;
this.position_y = 0.0f;
this.size_x = 0.0f;
this.size_y = 0.0f;
}
public HitBox(float posX, float posY, float sizeX, float sizeY) {
this.position_x = posX;
this.position_y = posY;
this.size_x = sizeX;
this.size_y = sizeY;
}
public void setPosition_x(float position_x){
this.position_x = position_x;
}
public void setPosition_y(float position_y){
this.position_y = position_y;
}
/*
* @param hb an Hitbox
* @return true if HitBox overlap else return false
*/
public Boolean hit(HitBox hb) {
Boolean horiz = false;
Boolean ver = false;
float horizontal1 = this.position_x + this.size_x;
float vertical1 = this.position_y + this.size_y;
float horizontal2 = hb.position_x + hb.size_x;
float vertical2 = hb.position_y + hb.size_y;
/*
* HitBox overlap horizontally
*/
if(this.position_x < hb.position_x) { //this is at left of hb
if(hb.position_x < horizontal1) {
horiz = true;
}
}else {//this is at left of hb
if(this.position_x < horizontal2) {
horiz = true;
}
}
/*
* HitBox overlap vertically
*/
if(this.position_y < hb.position_y) { //this is at top of hb
if(hb.position_y < vertical1) {
ver = true;
}
}else {//this is at left of hb
if(this.position_x < vertical2) {
ver = true;
}
}
return horiz && ver;
}
public float getPosX() {
return position_x;
}
public float getPosY() {
return position_y;
}
public float getSize_x() {
return size_x;
}
public void setSize_x(float size_x) {
this.size_x = size_x;
}
public float getSize_y() {
return size_y;
}
public void setSize_y(float size_y) {
this.size_y = size_y;
}
public void reverseHorizontally() {
this.position_x = 138*5 - position_x;
this.size_x = -this.size_x;
}
}