Skip to content

Commit

Permalink
Adding a canBePushed property to let players push breakables around
Browse files Browse the repository at this point in the history
  • Loading branch information
Interrupt committed Oct 7, 2020
1 parent 2890289 commit a7ba335
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
53 changes: 46 additions & 7 deletions Dungeoneer/src/com/interrupt/dungeoneer/entities/Breakable.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ public class Breakable extends Model {
@EditorProperty
public float shakeAmount = 4f;

@EditorProperty
public boolean canBePushed = false;

@EditorProperty
public boolean canBreak = true;

public Vector3 pushVel = new Vector3();
public float pushTime = 0;

public transient float shakeTimer = 0f;

public Array<Entity> spawns = new Array<Entity>();
Expand All @@ -72,6 +81,22 @@ public Breakable(String meshFile, String textureFile) {
isSolid = true;
stepHeight = 0f;
}

// touching player
@Override
public void encroached(Player player)
{
if(canBePushed) {
pushVel.x = player.xa;
pushVel.y = player.ya;
pushVel.nor().scl(0.025f / mass);

// Pushing should last longer than the initial contact
pushTime = 25;

physicsSleeping = false;
}
}

@Override
public void hit(float projx, float projy, int damage, float knockback, DamageType damageType, Entity instigator) {
Expand All @@ -80,8 +105,13 @@ public void hit(float projx, float projy, int damage, float knockback, DamageTyp
}

super.hit(projx, projy, damage, knockback, damageType, instigator);
if(pushable) this.applyPhysicsImpulse(new Vector3(projx,projy,0).scl(knockback));


if(canBePushed)
this.applyPhysicsImpulse(new Vector3(projx,projy,0).scl(knockback));

if(!canBreak)
return;

hp -= damage;

shakeTimer = 50f;
Expand Down Expand Up @@ -169,7 +199,13 @@ public void tick(Level level, float delta) {
// if it's not pushable, this can't move so don't tick physics
super.tick(level, delta);

physicsSleeping = isOnFloor && !pushable;
pushTime -= delta;
if(pushTime <= 0) {
pushVel.set(0, 0, 0);
} else {
xa = pushVel.x;
ya = pushVel.y;
}

if(shakeTimer > 0 && Game.instance != null) {
shakeTimer -= delta;
Expand All @@ -178,6 +214,9 @@ public void tick(Level level, float delta) {

if(shakeTimer <= 0) shakeTimer = 0;
}

// Make sure non pushable breakables go to sleep right away
physicsSleeping = isOnFloor && !canBePushed;
}

public void gib(Level level, Vector3 gibVel) {
Expand Down Expand Up @@ -211,10 +250,10 @@ public void gib(Level level, Vector3 gibVel) {

@Override
public void applyPhysicsImpulse(Vector3 impulse) {
float magnitude = impulse.len();
if (magnitude >= 1f) {
this.hit(impulse.x, impulse.y, (int)magnitude, magnitude, DamageType.PHYSICAL, null);
}
xa += impulse.x / mass;
ya += impulse.y / mass;
za += impulse.z / mass;
this.physicsSleeping = false;
}

// never draw as a static mesh
Expand Down
12 changes: 1 addition & 11 deletions Dungeoneer/src/com/interrupt/dungeoneer/entities/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public enum HaloMode { NONE, BOTH, STENCIL_ONLY, CORONA_ONLY }
public float stepHeight = 0.5f;
public float calcStepHeight = stepHeight;

/** Can Entity be pushed. */
@Deprecated
public boolean pushable = false;

/** Non-directional sprite. */
Expand Down Expand Up @@ -311,9 +311,6 @@ public void tick(Level level, float delta)

encroaching.encroached(this);
this.encroached(encroaching);

if(encroaching.pushable)
encroaching.xa = xa * 2;
}
}
}
Expand Down Expand Up @@ -381,9 +378,6 @@ public void tick(Level level, float delta)

encroaching.encroached(this);
this.encroached(encroaching);

if(encroaching.pushable)
encroaching.ya = ya * 2;
}
}
}
Expand Down Expand Up @@ -747,10 +741,6 @@ public void encroached(float hitx, float hity)
public void encroached(Player player)
{
// overload this
if(pushable) {
xa += player.xa * 0.1f;
ya += player.ya * 0.1f;
}
}

public void steppedOn(Entity e) {
Expand Down
8 changes: 4 additions & 4 deletions Dungeoneer/src/com/interrupt/dungeoneer/entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -401,21 +401,21 @@ public void tick(Level level, float delta) {
encroaching.steppedOn(this);
}
else {
xa = 0;
if(!inEditor) {
encroaching.encroached(this);
}
xa = 0;
}
}
else {
x += xa * delta;
}
}
else {
xa = 0;
if(!inEditor) {
encroaching.encroached(this);
}
xa = 0;
}
}
else {
Expand Down Expand Up @@ -444,21 +444,21 @@ public void tick(Level level, float delta) {
encroaching.steppedOn(this);
}
else {
ya = 0;
if(!inEditor) {
encroaching.encroached(this);
}
ya = 0;
}
}
else {
y += ya * delta;
}
}
else {
ya = 0;
if(!inEditor) {
encroaching.encroached(this);
}
ya = 0;
}
}
else {
Expand Down

0 comments on commit a7ba335

Please sign in to comment.