-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add FallingPlugin to simulate gravity
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
1 parent
a9e5773
commit 3f1d33f
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/main/java/org/terasology/flexiblepathfinding/plugins/basic/FallingPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |