From a0a8323032c6a329f457aed91754d7962e77c636 Mon Sep 17 00:00:00 2001 From: keizaal Date: Thu, 27 May 2021 14:53:08 +0200 Subject: [PATCH 1/2] =?UTF-8?q?Impl=C3=A9mentation=20des=20constructeurs?= =?UTF-8?q?=20et=20fonctions=20de=20HitBox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- GamePlay/Hitboxes/HitBox.java | 97 +++++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 5 deletions(-) diff --git a/GamePlay/Hitboxes/HitBox.java b/GamePlay/Hitboxes/HitBox.java index bc7fd89..47c9867 100644 --- a/GamePlay/Hitboxes/HitBox.java +++ b/GamePlay/Hitboxes/HitBox.java @@ -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; + } + +} \ No newline at end of file From e1d1803cd2fb0c2ede1af42999c3761dfd6d2d9f Mon Sep 17 00:00:00 2001 From: keizaal Date: Thu, 27 May 2021 15:18:35 +0200 Subject: [PATCH 2/2] Added some comments --- GamePlay/Hitboxes/HitBox.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/GamePlay/Hitboxes/HitBox.java b/GamePlay/Hitboxes/HitBox.java index 47c9867..3971c62 100644 --- a/GamePlay/Hitboxes/HitBox.java +++ b/GamePlay/Hitboxes/HitBox.java @@ -54,6 +54,10 @@ public class HitBox { } + /* + * @param hb an Hitbox + * @return true if HitBox overlap else return false + */ public Boolean hit(HitBox hb) { Boolean horiz = false; Boolean ver = false;