Skip to content

Commit

Permalink
Add knockback
Browse files Browse the repository at this point in the history
  • Loading branch information
silas-hw committed Sep 11, 2024
1 parent f6160c3 commit 197981b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/core/src/com/mygdx/scngame/entity/component/HurtBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -14,7 +11,7 @@
public class HurtBox {

public interface HurtListener {
void onHit();
void onHit(Vector2 knockback);
}

private HealthComponent health;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
40 changes: 39 additions & 1 deletion src/core/src/com/mygdx/scngame/entity/enemy/Enemy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TextureAtlas.AtlasRegion> walkUpAnimation,
Animation<TextureAtlas.AtlasRegion> walkDownAnimation,
Expand Down Expand Up @@ -71,6 +78,8 @@ public record EnemyType(
HurtBox hurtBox;
HealthComponent health;

Item<Box> collider;

StateManager<Enemy> stateManager;

public Enemy(EnemyType type) {
Expand All @@ -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() {
Expand All @@ -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
Expand All @@ -111,13 +141,21 @@ public void draw(SpriteBatch batch, ShapeRenderer shape, float alpha) {
public void setWorld(World<Box> 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
Expand Down
3 changes: 2 additions & 1 deletion src/core/src/com/mygdx/scngame/entity/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -186,7 +187,7 @@ public void dispose() {
}

@Override
public void onHit() {
public void onHit(Vector2 knockback) {
hurtColorTimer = hurtColorTime;
}
}
6 changes: 6 additions & 0 deletions src/core/src/com/mygdx/scngame/physics/DamageBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
public class DamageBox extends Box {
public float damage;
public float knockback = 0f;
public DamageType type;

public boolean solid = false;
Expand All @@ -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);
}
Expand Down

0 comments on commit 197981b

Please sign in to comment.