100 lines
1.9 KiB
Java

package Hitboxes;
public class HitBox {
private Double position_x;
private Double position_y;
private Double size_x;
private Double size_y;
public HitBox() {
this.position_x = 0.0;
this.position_y = 0.0;
this.size_x = 0.0;
this.size_y = 0.0;
}
public void setPosition_x(Double position_x){
this.position_x = position_x;
}
public void setPosition_y(Double position_y){
this.position_y = position_y;
}
public HitBox(Double position_x, Double position_y, Double size_x, Double size_y) {
if(position_x < 0.0) {
position_x = 0.0;
}else if(position_x > 1.0) {
position_x = 1.0;
}
this.position_x = position_x;
if(position_y < 0.0) {
position_y = 0.0;
}else if(position_y > 1.0) {
position_y = 1.0;
}
this.position_y = position_y;
if(size_x < 0.0) {
size_x = 0.0;
}else if(size_x > 1.0) {
size_x = 1.0;
}
this.size_x = size_x;
if(size_y < 0.0) {
size_y = 0.0;
}else if(size_y > 1.0) {
size_y = 1.0;
}
this.size_y = size_y;
}
/*
* @param hb an Hitbox
* @return true if HitBox overlap else return false
*/
public Boolean hit(HitBox hb) {
Boolean horiz = false;
Boolean ver = false;
Double horizontal1 = this.position_x + this.size_x;
Double vertical1 = this.position_y + this.size_y;
Double horizontal2 = hb.position_x + hb.size_x;
Double 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;
}
}