Skip to content

Commit

Permalink
Update collisionbox after unmarshalling a CollisionEntity
Browse files Browse the repository at this point in the history
This fixes an issue with values from the MapObject not being properly reflected by the entity's collisionbox right after it has been loaded from the map.
  • Loading branch information
steffen-wilke committed Nov 14, 2020
1 parent 2a0e41d commit eddb149
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions src/de/gurkenlabs/litiengine/entities/CollisionEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import de.gurkenlabs.litiengine.Align;
import de.gurkenlabs.litiengine.Game;
import de.gurkenlabs.litiengine.Valign;
import de.gurkenlabs.litiengine.environment.tilemap.IMapObject;
import de.gurkenlabs.litiengine.environment.tilemap.MapObjectProperty;
import de.gurkenlabs.litiengine.environment.tilemap.TmxProperty;
import de.gurkenlabs.litiengine.physics.Collision;
Expand Down Expand Up @@ -55,7 +56,7 @@ protected CollisionEntity() {
this.valign = info.valign();
this.align = info.align();
this.collisionType = info.collisionType();
this.collisionBox = this.getCollisionBox(this.getLocation());
this.refreshCollisionBox();
}

public static Rectangle2D getCollisionBox(final Point2D location, final double entityWidth, final double entityHeight, final double collisionBoxWidth, final double collisionBoxHeight, final Align align, final Valign valign) {
Expand Down Expand Up @@ -87,8 +88,7 @@ public Rectangle2D getCollisionBox() {
/**
* Gets the collision box.
*
* @param location
* the location
* @param location the location
* @return the collision box
*/
@Override
Expand Down Expand Up @@ -127,32 +127,32 @@ public Collision getCollisionType() {
@Override
public float[] getTweenValues(TweenType tweenType) {
switch (tweenType) {
case COLLISION_WIDTH:
return new float[] { (float) this.getCollisionBoxWidth() };
case COLLISION_HEIGHT:
return new float[] { (float) this.getCollisionBoxHeight() };
case COLLISION_BOTH:
return new float[] { (float) this.getCollisionBoxWidth(), (float) this.getCollisionBoxHeight() };
default:
return super.getTweenValues(tweenType);
case COLLISION_WIDTH:
return new float[]{(float) this.getCollisionBoxWidth()};
case COLLISION_HEIGHT:
return new float[]{(float) this.getCollisionBoxHeight()};
case COLLISION_BOTH:
return new float[]{(float) this.getCollisionBoxWidth(), (float) this.getCollisionBoxHeight()};
default:
return super.getTweenValues(tweenType);
}
}

@Override
public void setTweenValues(TweenType tweenType, float[] newValues) {
switch (tweenType) {
case COLLISION_WIDTH:
this.setCollisionBoxWidth(newValues[0]);
break;
case COLLISION_HEIGHT:
this.setCollisionBoxHeight(newValues[0]);
break;
case COLLISION_BOTH:
this.setCollisionBoxWidth(newValues[0]);
this.setCollisionBoxHeight(newValues[1]);
break;
default:
super.setTweenValues(tweenType, newValues);
case COLLISION_WIDTH:
this.setCollisionBoxWidth(newValues[0]);
break;
case COLLISION_HEIGHT:
this.setCollisionBoxHeight(newValues[0]);
break;
case COLLISION_BOTH:
this.setCollisionBoxWidth(newValues[0]);
this.setCollisionBoxHeight(newValues[1]);
break;
default:
super.setTweenValues(tweenType, newValues);
}
}

Expand All @@ -169,8 +169,7 @@ public boolean hasCollision() {
/**
* Sets the collision.
*
* @param collision
* the new collision
* @param collision the new collision
*/
@Override
public void setCollision(final boolean collision) {
Expand All @@ -180,49 +179,49 @@ public void setCollision(final boolean collision) {
@Override
public void setCollisionBoxAlign(final Align align) {
this.align = align;
this.collisionBox = this.getCollisionBox(this.getLocation());
this.refreshCollisionBox();
}

@Override
public void setCollisionBoxHeight(final double collisionBoxHeight) {
this.collisionBoxHeight = collisionBoxHeight;
this.collisionBox = this.getCollisionBox(this.getLocation());
this.refreshCollisionBox();
}

@Override
public void setCollisionBoxValign(final Valign valign) {
this.valign = valign;
this.collisionBox = this.getCollisionBox(this.getLocation());
this.refreshCollisionBox();
}

@Override
public void setCollisionBoxWidth(final double collisionBoxWidth) {
this.collisionBoxWidth = collisionBoxWidth;
this.collisionBox = this.getCollisionBox(this.getLocation());
this.refreshCollisionBox();
}

@Override
public void setLocation(final Point2D location) {
super.setLocation(location);
this.collisionBox = this.getCollisionBox(this.getLocation());
this.refreshCollisionBox();
}

@Override
public void setSize(final double width, final double height) {
super.setSize(width, height);
this.collisionBox = this.getCollisionBox(this.getLocation());
this.refreshCollisionBox();
}

@Override
public void setHeight(final double height) {
super.setHeight(height);
this.collisionBox = this.getCollisionBox(this.getLocation());
this.refreshCollisionBox();
}

@Override
public void setWidth(final double width) {
super.setWidth(width);
this.collisionBox = this.getCollisionBox(this.getLocation());
this.refreshCollisionBox();
}

@Override
Expand Down Expand Up @@ -258,4 +257,13 @@ public void fireCollisionEvent(CollisionEvent event) {
listener.collisionResolved(event);
}
}

protected void refreshCollisionBox() {
this.collisionBox = this.getCollisionBox(this.getLocation());
}

@SuppressWarnings("unused")
private void afterTmxUnmarshal(IMapObject mapObject) {
this.refreshCollisionBox();
}
}

0 comments on commit eddb149

Please sign in to comment.