diff --git a/src/core/src/com/mygdx/scngame/entity/component/HurtBox.java b/src/core/src/com/mygdx/scngame/entity/component/HurtBox.java index 999b0ea..47adced 100644 --- a/src/core/src/com/mygdx/scngame/entity/component/HurtBox.java +++ b/src/core/src/com/mygdx/scngame/entity/component/HurtBox.java @@ -2,10 +2,7 @@ import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.utils.Array; -import com.dongbat.jbump.Collision; -import com.dongbat.jbump.Item; -import com.dongbat.jbump.Response; -import com.dongbat.jbump.World; +import com.dongbat.jbump.*; import com.mygdx.scngame.physics.Box; import com.mygdx.scngame.physics.DamageBox; import com.mygdx.scngame.physics.HitBox; @@ -14,7 +11,7 @@ public class HurtBox { public interface HurtListener { - void onHit(); + void onHit(Vector2 knockback); } private HealthComponent health; @@ -92,8 +89,13 @@ public void update(float delta, Vector2 position) { invincible = true; invinceTimer = invinceTime; + Rect rect = col.otherRect; + Vector2 dBoxPos = new Vector2(rect.x + rect.w/2f, col.otherRect.y + rect.h/2f); + + Vector2 knockbackVec = dBoxPos.sub(position.cpy().add(width/2f, height/2f)).nor().scl(dBox.knockback); + for(HurtListener listener : listeners) { - listener.onHit(); + listener.onHit(knockbackVec); } } } diff --git a/src/core/src/com/mygdx/scngame/entity/enemy/Enemy.java b/src/core/src/com/mygdx/scngame/entity/enemy/Enemy.java index 62ec7cd..bdce016 100644 --- a/src/core/src/com/mygdx/scngame/entity/enemy/Enemy.java +++ b/src/core/src/com/mygdx/scngame/entity/enemy/Enemy.java @@ -26,6 +26,8 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.Vector2; import com.dongbat.jbump.CollisionFilter; +import com.dongbat.jbump.Item; +import com.dongbat.jbump.Rect; import com.dongbat.jbump.World; import com.mygdx.scngame.entity.Entity; import com.mygdx.scngame.entity.EntityState; @@ -34,13 +36,18 @@ import com.mygdx.scngame.entity.component.HurtBox; import com.mygdx.scngame.physics.Box; -public class Enemy extends Entity implements HealthComponent.DeathListener { +public class Enemy extends Entity implements HealthComponent.DeathListener, HurtBox.HurtListener { @Override public void onDeath() { this.stateManager.setState(new EnemyDeathState()); } + @Override + public void onHit(Vector2 knockback) { + this.position.sub(knockback); + } + public record EnemyType( Animation walkUpAnimation, Animation walkDownAnimation, @@ -71,6 +78,8 @@ public record EnemyType( HurtBox hurtBox; HealthComponent health; + Item collider; + StateManager stateManager; public Enemy(EnemyType type) { @@ -87,6 +96,21 @@ public Enemy(EnemyType type) { this.hurtBox.setCollisionMask(4, true); health.addDeathListener(this); + hurtBox.addHurtListener(this); + + Box collider = new Box(); + collider.setMask(0, true); + collider.setMask(1, true); + + collider.setLayer(0, true); + collider.setLayer(1, true); + + collider.solid = true; + + collider.internalFilter = Box.SLIDE_FILTER; + + this.collider = new Item<>(collider); + } public Vector2 getCenterPoint() { @@ -100,6 +124,12 @@ public Vector2 getCenterPoint() { public void update(float delta) { stateManager.update(delta); hurtBox.update(delta, this.position); + + this.world.move(collider, position.x, position.y, Box.GLOBAL_FILTER); + + Rect rect = this.world.getRect(collider); + + position.set(rect.x, rect.y); } @Override @@ -111,13 +141,21 @@ public void draw(SpriteBatch batch, ShapeRenderer shape, float alpha) { public void setWorld(World world) { this.stateManager.setWorld(world); this.hurtBox.setWorld(world); + + if(this.world != null) { + if(this.world.hasItem(collider)) this.world.remove(collider); + } this.world = world; + + this.world.add(collider, position.x, position.y, type.width, type.height); } @Override public void removeWorldItems() { this.hurtBox.removeWorldItems(); this.stateManager.removeWorldItems(); + + if(this.world.hasItem(collider)) this.world.remove(collider); } @Override diff --git a/src/core/src/com/mygdx/scngame/entity/player/Player.java b/src/core/src/com/mygdx/scngame/entity/player/Player.java index 2bf144b..0ec646c 100644 --- a/src/core/src/com/mygdx/scngame/entity/player/Player.java +++ b/src/core/src/com/mygdx/scngame/entity/player/Player.java @@ -8,6 +8,7 @@ import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.graphics.glutils.ShapeRenderer; import com.badlogic.gdx.math.MathUtils; +import com.badlogic.gdx.math.Vector2; import com.dongbat.jbump.*; import com.mygdx.scngame.entity.*; import com.mygdx.scngame.entity.component.HealthComponent; @@ -186,7 +187,7 @@ public void dispose() { } @Override - public void onHit() { + public void onHit(Vector2 knockback) { hurtColorTimer = hurtColorTime; } } diff --git a/src/core/src/com/mygdx/scngame/physics/DamageBox.java b/src/core/src/com/mygdx/scngame/physics/DamageBox.java index b49a22a..022ac45 100644 --- a/src/core/src/com/mygdx/scngame/physics/DamageBox.java +++ b/src/core/src/com/mygdx/scngame/physics/DamageBox.java @@ -10,6 +10,7 @@ */ public class DamageBox extends Box { public float damage; + public float knockback = 0f; public DamageType type; public boolean solid = false; @@ -19,8 +20,13 @@ public enum DamageType { } public DamageBox(float damage, DamageType type) { + this(damage, 15f, type); + } + + public DamageBox(float damage, float knockback, DamageType type) { this.damage = damage; this.type = type; + this.knockback = knockback; super.setDebugColor(Color.RED); }