Implémentation des constructeurs et fonctions de HitBox
This commit is contained in:
parent
cc811c93a2
commit
a0a8323032
@ -2,8 +2,95 @@ package Hitboxes;
|
||||
|
||||
public class HitBox {
|
||||
|
||||
public Double position_x;
|
||||
public Double position_y;
|
||||
public Double size_x;
|
||||
public Double size_y;
|
||||
}
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user