Skip to content

Commit f7d3de0

Browse files
feat: improve 'playerHeight' command (#3841)
Co-Authored-By: Tobias Nett <[email protected]>
1 parent 3da2ae4 commit f7d3de0

File tree

2 files changed

+60
-22
lines changed

2 files changed

+60
-22
lines changed

engine/src/main/java/org/terasology/logic/characters/CharacterMovementComponent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
public final class CharacterMovementComponent implements Component {
3636

3737
// Collision settings
38-
@Range(min = 0, max = 5)
38+
@Range(min = 1, max = 25)
3939
public float height = 1.6f;
4040
@Range(min = 0, max = 5)
4141
public float radius = 0.3f;

engine/src/main/java/org/terasology/logic/debug/MovementDebugCommands.java

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515
*/
1616
package org.terasology.logic.debug;
1717

18+
import org.terasology.config.Config;
1819
import org.terasology.entitySystem.entity.EntityManager;
1920
import org.slf4j.Logger;
2021
import org.slf4j.LoggerFactory;
21-
import org.terasology.logic.characters.CharacterImpulseEvent;
22-
import org.terasology.logic.common.DisplayNameComponent;
22+
import org.terasology.logic.characters.CharacterComponent;
23+
import org.terasology.logic.characters.GazeMountPointComponent;
2324
import org.terasology.logic.characters.CharacterMovementComponent;
2425
import org.terasology.logic.characters.CharacterTeleportEvent;
25-
import org.terasology.logic.characters.GazeMountPointComponent;
26+
import org.terasology.logic.characters.CharacterImpulseEvent;
2627
import org.terasology.logic.characters.MovementMode;
28+
import org.terasology.logic.common.DisplayNameComponent;
2729
import org.terasology.logic.location.Location;
2830
import org.terasology.logic.location.LocationComponent;
2931
import org.terasology.math.geom.Quat4f;
@@ -57,6 +59,9 @@ public class MovementDebugCommands extends BaseComponentSystem {
5759

5860
@In
5961
private EntityManager entityManager;
62+
63+
@In
64+
private Config config;
6065

6166
@Command(shortDescription = "Grants flight and movement through walls", runOnServer = true,
6267
requiredPermission = PermissionManager.CHEAT_PERMISSION)
@@ -233,28 +238,61 @@ public String stepHeight(@Sender EntityRef client, @CommandParam("height") float
233238
return "";
234239
}
235240

241+
private float getJumpSpeed(float ratio, float defaultValue) {
242+
return (float) Math.pow(ratio, 0.74f) * 0.4f * defaultValue + 0.6f * defaultValue;
243+
}
244+
245+
private float getInteractionRange(float ratio, float defaultValue) {
246+
return (float) Math.pow(ratio, 0.62f) * defaultValue;
247+
}
248+
249+
private float getRunFactor(float ratio, float defaultValue) {
250+
return (float) Math.pow(ratio, 0.68f) * defaultValue;
251+
}
252+
236253
@Command(shortDescription = "Sets the height of the player", runOnServer = true,
237254
requiredPermission = PermissionManager.CHEAT_PERMISSION)
238255
public String playerHeight(@Sender EntityRef client, @CommandParam("height") float amount) {
239-
try {
240-
ClientComponent clientComp = client.getComponent(ClientComponent.class);
241-
CharacterMovementComponent move = clientComp.character.getComponent(CharacterMovementComponent.class);
242-
if (move != null) {
243-
float prevHeight = move.height;
244-
move.height = amount;
245-
clientComp.character.saveComponent(move);
246-
LocationComponent loc = client.getComponent(LocationComponent.class);
247-
Vector3f currentPosition = loc.getWorldPosition();
248-
clientComp.character
249-
.send(new CharacterTeleportEvent(new Vector3f(currentPosition.getX(), currentPosition.getY() + (amount - prevHeight) / 2, currentPosition.getZ())));
250-
physics.removeCharacterCollider(clientComp.character);
251-
physics.getCharacterCollider(clientComp.character);
252-
return "Height of player set to " + amount + " (was " + prevHeight + ")";
256+
if (amount >= 1 && amount <= 25) {
257+
try {
258+
ClientComponent clientComp = client.getComponent(ClientComponent.class);
259+
CharacterMovementComponent move = clientComp.character.getComponent(CharacterMovementComponent.class);
260+
CharacterComponent charComp = clientComp.character.getComponent(CharacterComponent.class);
261+
EntityRef player = clientComp.character;
262+
GazeMountPointComponent gazeMountPointComponent = player.getComponent(GazeMountPointComponent.class);
263+
264+
Prefab playerDefaults = Assets.getPrefab("engine:player").get();
265+
CharacterMovementComponent moveDefault = playerDefaults.getComponent(CharacterMovementComponent.class);
266+
CharacterComponent characterDefault = playerDefaults.getComponent(CharacterComponent.class);
267+
268+
if (move != null && gazeMountPointComponent != null) {
269+
float prevHeight = move.height;
270+
float defaultEyeHeight = config.getPlayer().getEyeHeight();
271+
float foreHeadSize = moveDefault.height - defaultEyeHeight;
272+
float ratio = amount / moveDefault.height;
273+
274+
move.height = amount;
275+
move.jumpSpeed = getJumpSpeed(ratio, moveDefault.jumpSpeed);
276+
move.stepHeight = ratio * moveDefault.stepHeight;
277+
move.distanceBetweenFootsteps = ratio * moveDefault.distanceBetweenFootsteps;
278+
move.runFactor = getRunFactor(ratio, moveDefault.runFactor);
279+
charComp.interactionRange = getInteractionRange(ratio, characterDefault.interactionRange);
280+
gazeMountPointComponent.translate.y = amount - foreHeadSize;
281+
Location.removeChild(player, gazeMountPointComponent.gazeEntity);
282+
Location.attachChild(player, gazeMountPointComponent.gazeEntity, gazeMountPointComponent.translate, new Quat4f(Quat4f.IDENTITY));
283+
284+
player.saveComponent(gazeMountPointComponent);
285+
clientComp.character.saveComponent(move);
286+
287+
return "Height of player set to " + amount + " (was " + prevHeight + ")";
288+
}
289+
return "";
290+
} catch (NullPointerException e) {
291+
e.printStackTrace();
292+
return "";
253293
}
254-
return "";
255-
} catch (NullPointerException e) {
256-
e.printStackTrace();
257-
return "";
294+
} else {
295+
return "Invalid input. Accepted values: [1 to 25]";
258296
}
259297
}
260298

0 commit comments

Comments
 (0)