Skip to content

Commit

Permalink
More fine-grained friction control for Entities; minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Danjb1 committed Oct 20, 2019
1 parent 01fbd8a commit 1a3f0f3
Show file tree
Hide file tree
Showing 16 changed files with 124 additions and 100 deletions.
10 changes: 8 additions & 2 deletions demo/demo/game/entities/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class Player extends DemoEntity {
public Player(float x, float y) {
super(x, y, WIDTH, HEIGHT, DemoEntity.TYPE_PLAYER);

hitbox.setAirFrictionXCoefficient(10f);
hitbox.setAirFrictionCoefficient(10f);
hitbox.setMaxSpeedX(MAX_SPEED_X);
}

Expand Down Expand Up @@ -79,7 +79,13 @@ private void applyAcceleration(int delta) {
}

@Override
public boolean isAffectedByFriction() {
public boolean isAffectedByGroundFriction() {
// Only when no direction is pressed
return movementDirection == DirectionX.NONE;
}

@Override
public boolean isAffectedByAirFrictionX() {
// Only when no direction is pressed
return movementDirection == DirectionX.NONE;
}
Expand Down
4 changes: 2 additions & 2 deletions src/engine/game/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public Component(String key) {
* <p>This is a generic message-passing mechanism that can be used to signal
* to components when certain events take place.
*
* @param event
* @param eventBeforeCast
*/
public void notify(ComponentEvent event) {
public void notify(ComponentEvent eventBeforeCast) {
// Do nothing by default
}

Expand Down
12 changes: 1 addition & 11 deletions src/engine/game/ComponentEvent.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
package engine.game;

public abstract class ComponentEvent {

private String key;

public ComponentEvent(String key) {
this.key = key;
}

public String getKey() {
return key;
}

// Reserved for future use
}
9 changes: 9 additions & 0 deletions src/engine/game/ComponentStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ public List<T> asList() {
return new ArrayList<>(components);
}

/**
* Determines if the store contains no Components.
*
* @return
*/
public boolean isEmpty() {
return components.isEmpty();
}

/**
* Removes all Components from this ComponentStore.
*/
Expand Down
16 changes: 14 additions & 2 deletions src/engine/game/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,28 @@ protected void applyPhysics(Entity entity, int delta) {

Hitbox hitbox = entity.getHitbox();

// Gravity
if (entity.isAffectedByGravity()) {
hitbox.applyGravity(delta);
}

// Movement
if (entity.canMove()) {
hitbox.moveWithCollision(this, delta);
}

if (entity.isAffectedByFriction()) {
hitbox.applyFriction(delta);
// Friction
if (hitbox.isGrounded()) {
if (entity.isAffectedByGroundFriction()) {
hitbox.applyGroundFriction(delta);
}
} else {
if (entity.isAffectedByAirFrictionX()) {
hitbox.applyAirFrictionX(delta);
}
if (entity.isAffectedByAirFrictionY()) {
hitbox.applyAirFrictionY(delta);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/engine/game/entities/CameraSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void entityTeleported() {

@Override
public void notify(ComponentEvent event) {
if (event.getKey() == TeleportEntityComponentEvent.KEY) {
if (event instanceof EntityTeleported) {
entityTeleported();
}
}
Expand Down
31 changes: 26 additions & 5 deletions src/engine/game/entities/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public int getEntityId(){
public void teleport(float x, float y) {
hitbox.setPos(x, y);

components.notifyAll(CameraSettings.KEY,
new TeleportEntityComponentEvent());
components.notifyAll(CameraSettings.KEY, new EntityTeleported());
}

/**
Expand Down Expand Up @@ -208,15 +207,37 @@ public boolean canMove() {
}

/**
* Determines whether friction should be applied to this Entity.
* Determines whether ground friction should be applied to this Entity.
*
* @see Hitbox#applyFriction
* @see Hitbox#applyGroundFriction
* @return
*/
public boolean isAffectedByFriction() {
public boolean isAffectedByGroundFriction() {
return true;
}

/**
* Determines whether air friction should be applied to this Entity.
*
* @see Hitbox#applyAirFrictionX
* @return
*/
public boolean isAffectedByAirFrictionX() {
return true;
}

/**
* Determines whether air friction should be applied to this Entity.
*
* <p>By default this is false because it interferes with gravity.
*
* @see Hitbox#applyAirFrictionY
* @return
*/
public boolean isAffectedByAirFrictionY() {
return false;
}

////////////////////////////////////////////////////////////////////////////
// Components
////////////////////////////////////////////////////////////////////////////
Expand Down
4 changes: 3 additions & 1 deletion src/engine/game/entities/EntityComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

public abstract class EntityComponent extends Component {

protected Entity entity;

public EntityComponent(String key) {
super(key);
}
Expand All @@ -18,7 +20,7 @@ public EntityComponent(String key) {
* @param parent
*/
public void onAttach(Entity parent) {
// Do nothing by default
entity = parent;
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/engine/game/entities/EntityTeleported.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package engine.game.entities;

import engine.game.ComponentEvent;

public class EntityTeleported extends ComponentEvent {
// No content
}
13 changes: 0 additions & 13 deletions src/engine/game/entities/TeleportEntityComponentEvent.java

This file was deleted.

22 changes: 11 additions & 11 deletions src/engine/game/physics/CollisionResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,62 +86,62 @@ public Hitbox getHitbox() {
*
* @return
*/
public float getLeft() {
public float left() {
return newX;
}

/**
* Getter for the new Hitbox right.
*
* See {@link #getLeft}.
* See {@link #left}.
*
* @return
*/
public float getRight() {
public float right() {
return newX + hitbox.width - Physics.SMALLEST_DISTANCE;
}

/**
* Getter for the new Hitbox centre.
*
* See {@link #getLeft}.
* See {@link #left}.
*
* @return
*/
public float getCentreX() {
public float centreX() {
return newX + hitbox.width / 2;
}

/**
* Getter for the new Hitbox centre.
*
* See {@link #getLeft}.
* See {@link #left}.
*
* @return
*/
public float getCentreY() {
public float centreY() {
return newY + hitbox.height / 2;
}

/**
* Getter for the new Hitbox top.
*
* See {@link #getLeft}.
* See {@link #left}.
*
* @return
*/
public float getTop() {
public float top() {
return newY;
}

/**
* Getter for the new Hitbox bottom.
*
* See {@link #getLeft}.
* See {@link #left}.
*
* @return
*/
public float getBottom() {
public float bottom() {
return newY + hitbox.height - Physics.SMALLEST_DISTANCE;
}

Expand Down
76 changes: 33 additions & 43 deletions src/engine/game/physics/Hitbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,9 @@ public class Hitbox {

/**
* Multiplier that determines how strongly this Hitbox is affected by air
* friction in the x-axis.
*/
private float airFrictionXCoefficient = 1;

/**
* Multiplier that determines how strongly this Hitbox is affected by air
* friction in the y-axis.
*
* This is zero by default, because it interferes with gravity. It is most
* useful for Entities which are not affected by gravity.
* friction.
*/
private float airFrictionYCoefficient = 0;
private float airFrictionCoefficient = 1;

/**
* Whether this Hitbox is affected by collisions.
Expand Down Expand Up @@ -235,7 +226,7 @@ public void moveWithCollision(Logic logic, int delta) {
// Move to the nearest collision
CollisionResult result =
Physics.getCollisionResult(logic, this, dx, dy);
setPos(result.getLeft(), result.getTop());
setPos(result.left(), result.top());

// Collide with slopes if this Hitbox doesn't support them
if (!getCollisionFlag(SUPPORT_SLOPES)
Expand Down Expand Up @@ -391,27 +382,36 @@ public void setGrounded(boolean nowOnGround) {
////////////////////////////////////////////////////////////////////////////

/**
* Adjusts this Hitbox's speed according to friction.
*
* <p>There are 2 different values for friction; one is used when this
* Hitbox is on the ground, the other is used when this Hitbox is in the
* air.
* Adjusts this Hitbox's speed according to ground friction.
*
* @see Hitbox#setAirFrictionXCoefficient
* @see Hitbox#setAirFrictionYCoefficient
* @see Hitbox#setGroundFrictionCoefficient
* @param delta
*/
public void applyFriction(int delta) {
if (onGround){
setSpeedX(Physics.applyGroundFriction(
speedX, delta, groundFrictionCoefficient));
} else {
setSpeedX(Physics.applyAirFriction(
speedX, delta, airFrictionXCoefficient));
setSpeedY(Physics.applyAirFriction(
speedY, delta, airFrictionYCoefficient));
}
public void applyGroundFriction(int delta) {
setSpeedX(Physics.applyGroundFriction(
speedX, delta, groundFrictionCoefficient));
}

/**
* Adjusts this Hitbox's x-speed according to air friction.
*
* @see Hitbox#setAirFrictionCoefficient
* @param delta
*/
public void applyAirFrictionX(int delta) {
setSpeedX(Physics.applyAirFriction(
speedX, delta, airFrictionCoefficient));
}

/**
* Adjusts this Hitbox's y-speed according to air friction.
*
* @see Hitbox#setAirFrictionCoefficient
* @param delta
*/
public void applyAirFrictionY(int delta) {
setSpeedY(Physics.applyAirFriction(
speedY, delta, airFrictionCoefficient));
}

/**
Expand Down Expand Up @@ -446,22 +446,12 @@ public void setGroundFrictionCoefficient(float groundFrictionCoefficient) {

/**
* Sets the multiplier used to determine the strength of air friction, as
* applied to this Hitbox, in the x-axis.
*
* @param airFrictionXCoefficient
*/
public void setAirFrictionXCoefficient(float airFrictionXCoefficient) {
this.airFrictionXCoefficient = airFrictionXCoefficient;
}

/**
* Sets the multiplier used to determine the strength of air friction, as
* applied to this Hitbox, in the y-axis.
* applied to this Hitbox.
*
* @param airFrictionYCoefficient
* @param airFrictionCoefficient
*/
public void setAirFrictionYCoefficient(float airFrictionYCoefficient) {
this.airFrictionYCoefficient = airFrictionYCoefficient;
public void setAirFrictionCoefficient(float airFrictionCoefficient) {
this.airFrictionCoefficient = airFrictionCoefficient;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/engine/game/physics/Physics.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ private static void detectCollisionsY(CollisionResult result, Logic logic,
for (float node : collisionNodesX){

// Use the already-calculated x-collision result
float x = result.getLeft() + node;
float x = result.left() + node;
float yBefore = result.getCollisionEdgeY();
float yAfter = yBefore + result.getAttemptedDy();

Expand Down
Loading

0 comments on commit 1a3f0f3

Please sign in to comment.