Skip to content

Commit

Permalink
feat: add FallingPlugin to simulate gravity
Browse files Browse the repository at this point in the history
Plugins like Walking or Leaping assume that entities are not affected by gravity.

To keep this assumption we introduce a new plugin to explicitly enable gravity for entities. This also needs to be reflected in the respective Behaviors movement plugin.
  • Loading branch information
skaldarnar authored and jdrueckert committed Feb 11, 2022
1 parent a9e5773 commit 3f1d33f
Showing 1 changed file with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright 2022 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.flexiblepathfinding.plugins.basic;

import org.joml.Vector3i;
import org.joml.Vector3ic;
import org.terasology.engine.world.WorldProvider;
import org.terasology.engine.world.block.BlockRegion;

/**
* A pathfinding plugin to model entities falling down based on gravity.
* <br>
* All blocks between {@code from} and {@code to} position must be penetrable.
* The target block must be {@link #isWalkable(Vector3ic) walkable}.
* <br>
* <pre>
* o
* +---+ v
* | o
* +---+
* </pre>
*/
// TODO: do we need this, or can we mimic the same behavior in WalkingMovementPlugin?
public class FallingPlugin extends WalkingPlugin {

//TODO: definition of walkable is a bit questionable - how can you fall into liquid blocks?

public FallingPlugin(WorldProvider world, float width, float height) {
super(world, width, height);
}

@Override
public boolean isReachable(Vector3ic to, Vector3ic from) {
// only allowed to move 1 unit in negative y axis
if (to.x() - from.x() != 0 || to.z() - from.z() != 0 || to.y() - from.y() != -1) {
return false;
}

// check that all blocks passed through by this movement are penetrable
for (Vector3ic occupiedBlock : getOccupiedRegionRelative()) {
// the start/stop for this block in the occupied region
Vector3i occupiedBlockTo = new Vector3i(to).add(occupiedBlock);
Vector3i occupiedBlockFrom = new Vector3i(from).add(occupiedBlock);

BlockRegion movementBounds = new BlockRegion(occupiedBlockTo).union(occupiedBlockFrom);
for (Vector3ic block : movementBounds) {
if (!world.getBlock(block).isPenetrable()) {
return false;
}
}
}

return isWalkable(to);
}
}

0 comments on commit 3f1d33f

Please sign in to comment.