Skip to content

Commit

Permalink
Merge PR #2544 by @nihal111 - add proper crouching!
Browse files Browse the repository at this point in the history
  • Loading branch information
Cervator committed Oct 21, 2016
2 parents 0734a92 + 9b7e5e0 commit 3c767f1
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2016 MovingBlocks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.terasology.input.binds.movement;

import org.terasology.input.BindButtonEvent;
import org.terasology.input.DefaultBinding;
import org.terasology.input.InputType;
import org.terasology.input.Keyboard;
import org.terasology.input.RegisterBindButton;

/**
*/
@RegisterBindButton(id = "crouchMode", description = "${engine:menu#binding-crouch-mode}")
@DefaultBinding(type = InputType.KEY, id = Keyboard.KeyId.X)
public class CrouchModeButton extends BindButtonEvent {
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"engine:right",
"engine:toggleSpeedPermanently",
"engine:toggleSpeedTemporarily",
"engine:crouchMode",
"engine:jump",
"engine:crouch"
}) package org.terasology.input.binds.movement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public class KinematicCharacterMover implements CharacterMover {
/**
* The amount of extra distance added to horizontal movement to allow for penetration.
*/
private static final float HORIZONTAL_PENETRATION_LEEWAY = 0.04f;
public static final float HORIZONTAL_PENETRATION_LEEWAY = 0.04f;

/**
* The amount of extra distance added to vertical movement to allow for penetration.
*/
private static final float VERTICAL_PENETRATION_LEEWAY = 0.05f;
public static final float VERTICAL_PENETRATION_LEEWAY = 0.05f;
private static final float CHECK_FORWARD_DIST = 0.05f;

private static final Logger logger = LoggerFactory.getLogger(KinematicCharacterMover.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
public enum MovementMode {
WALKING(1f, 8f, true, true, true, 3f, false),
CROUCHING(1f, 8f, true, true, true, 1.5f, false),
PRONING(1f, 8f, true, true, true, 3f, false),
CLIMBING(0f, 8f, true, true, true, 3f, false),
SWIMMING(0.05f, 1.5f, true, false, true, 2f, true),
DIVING(0f, 2f, true, false, true, 2f, true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.terasology.input.ButtonState;
import org.terasology.input.binds.interaction.FrobButton;
import org.terasology.input.binds.inventory.UseItemButton;
import org.terasology.input.binds.movement.CrouchButton;
import org.terasology.input.binds.movement.ForwardsMovementAxis;
import org.terasology.input.binds.movement.ForwardsRealMovementAxis;
import org.terasology.input.binds.movement.JumpButton;
Expand All @@ -38,28 +39,33 @@
import org.terasology.input.binds.movement.StrafeRealMovementAxis;
import org.terasology.input.binds.movement.ToggleSpeedPermanentlyButton;
import org.terasology.input.binds.movement.ToggleSpeedTemporarilyButton;
import org.terasology.input.binds.movement.CrouchModeButton;
import org.terasology.input.binds.movement.VerticalMovementAxis;
import org.terasology.input.binds.movement.VerticalRealMovementAxis;
import org.terasology.input.events.MouseXAxisEvent;
import org.terasology.input.events.MouseYAxisEvent;
import org.terasology.logic.characters.CharacterComponent;
import org.terasology.logic.characters.CharacterHeldItemComponent;
import org.terasology.logic.characters.CharacterMoveInputEvent;
import org.terasology.logic.characters.CharacterMovementComponent;
import org.terasology.logic.characters.CharacterHeldItemComponent;
import org.terasology.logic.characters.GazeMountPointComponent;
import org.terasology.logic.characters.MovementMode;
import org.terasology.logic.characters.events.OnItemUseEvent;
import org.terasology.logic.characters.events.SetMovementModeEvent;
import org.terasology.logic.characters.interactions.InteractionUtil;
import org.terasology.logic.debug.MovementDebugCommands;
import org.terasology.logic.inventory.ItemComponent;
import org.terasology.logic.location.LocationComponent;
import org.terasology.logic.notifications.NotificationMessageEvent;
import org.terasology.logic.players.event.OnPlayerSpawnedEvent;
import org.terasology.math.AABB;
import org.terasology.math.Direction;
import org.terasology.math.TeraMath;
import org.terasology.math.geom.Quat4f;
import org.terasology.math.geom.Vector3f;
import org.terasology.network.ClientComponent;
import org.terasology.physics.engine.CharacterCollider;
import org.terasology.physics.engine.PhysicsEngine;
import org.terasology.physics.engine.SweepCallback;
import org.terasology.registry.In;
import org.terasology.rendering.AABBRenderer;
import org.terasology.rendering.BlockOverlayRenderer;
Expand All @@ -71,6 +77,8 @@
import org.terasology.world.block.BlockComponent;
import org.terasology.world.block.regions.BlockRegionComponent;

import static org.terasology.logic.characters.KinematicCharacterMover.VERTICAL_PENETRATION_LEEWAY;

/**
*/
// TODO: This needs a really good cleanup
Expand All @@ -87,6 +95,8 @@ public class LocalPlayerSystem extends BaseComponentSystem implements UpdateSubs
private Camera playerCamera;
@In
private MovementDebugCommands movementDebugCommands;
@In
private PhysicsEngine physics;

@In
private Config config;
Expand All @@ -102,6 +112,7 @@ public class LocalPlayerSystem extends BaseComponentSystem implements UpdateSubs
private float lookPitchDelta;
private float lookYaw;
private float lookYawDelta;
private float crouchFraction = 0.5f;

@In
private Time time;
Expand Down Expand Up @@ -160,6 +171,44 @@ private void processInput(EntityRef entity, CharacterMovementComponent character
jump = false;
}

/**
* Reduces height and eyeHeight by crouchFraction and changes MovementMode.
*/
private void crouchPlayer(EntityRef entity) {
ClientComponent clientComp = entity.getComponent(ClientComponent.class);
GazeMountPointComponent gazeMountPointComponent = clientComp.character.getComponent(GazeMountPointComponent.class);
float height = clientComp.character.getComponent(CharacterMovementComponent.class).height;
float eyeHeight = gazeMountPointComponent.translate.getY();
movementDebugCommands.playerHeight(localPlayer.getClientEntity(), height * crouchFraction);
movementDebugCommands.playerEyeHeight(localPlayer.getClientEntity(), eyeHeight * crouchFraction);
clientComp.character.send(new SetMovementModeEvent(MovementMode.CROUCHING));
}

/**
* Checks if there is an impenetrable block above,
* Raises a Notification "Cannot stand up here!" if present
* If not present, increases height and eyeHeight by crouchFraction and changes MovementMode.
*/
private void standPlayer(EntityRef entity) {
ClientComponent clientComp = entity.getComponent(ClientComponent.class);
GazeMountPointComponent gazeMountPointComponent = clientComp.character.getComponent(GazeMountPointComponent.class);
float height = clientComp.character.getComponent(CharacterMovementComponent.class).height;
float eyeHeight = gazeMountPointComponent.translate.getY();
Vector3f pos = entity.getComponent(LocationComponent.class).getWorldPosition();
// Check for collision when rising
CharacterCollider collider = physics.getCharacterCollider(clientComp.character);
// height used below = (1 - crouch_fraction) * standing_height
Vector3f to = new Vector3f(pos.x, pos.y + (1 - crouchFraction) * height / crouchFraction, pos.z);
SweepCallback callback = collider.sweep(pos, to, VERTICAL_PENETRATION_LEEWAY, -1f);
if (callback.hasHit()) {
entity.send(new NotificationMessageEvent("Cannot stand up here!", entity));
return;
}
movementDebugCommands.playerHeight(localPlayer.getClientEntity(), height / crouchFraction);
movementDebugCommands.playerEyeHeight(localPlayer.getClientEntity(), eyeHeight / crouchFraction);
clientComp.character.send(new SetMovementModeEvent(MovementMode.WALKING));
}

@ReceiveEvent
public void onPlayerSpawn(OnPlayerSpawnedEvent event, EntityRef character) {
if (character.equals(localPlayer.getCharacterEntity())) {
Expand Down Expand Up @@ -256,7 +305,33 @@ public void updateVerticalMovement(VerticalRealMovementAxis event, EntityRef ent
public void onToggleSpeedTemporarily(ToggleSpeedTemporarilyButton event, EntityRef entity) {
boolean toggle = event.isDown();
run = runPerDefault ^ toggle;
event.consume();
}

// Crouches if button is pressed. Stands if button is released.
@ReceiveEvent(components = {ClientComponent.class}, priority = EventPriority.PRIORITY_NORMAL)
public void onCrouchTemporarily(CrouchButton event, EntityRef entity) {
ClientComponent clientComp = entity.getComponent(ClientComponent.class);
CharacterMovementComponent move = clientComp.character.getComponent(CharacterMovementComponent.class);
if (event.isDown() && move.mode == MovementMode.WALKING) {
crouchPlayer(entity);
} else if (!event.isDown() && move.mode == MovementMode.CROUCHING) {
standPlayer(entity);
}
event.consume();
}

@ReceiveEvent(components = {ClientComponent.class}, priority = EventPriority.PRIORITY_NORMAL)
public void onCrouchMode(CrouchModeButton event, EntityRef entity) {
ClientComponent clientComp = entity.getComponent(ClientComponent.class);
CharacterMovementComponent move = clientComp.character.getComponent(CharacterMovementComponent.class);
if (event.isDown()) {
if (move.mode == MovementMode.WALKING) {
crouchPlayer(entity);
} else if (move.mode == MovementMode.CROUCHING) {
standPlayer(entity);
}
}
event.consume();
}

Expand Down
1 change: 1 addition & 0 deletions engine/src/main/resources/assets/i18n/menu.lang
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"binding-show-online-players": "binding-show-online-players",
"binding-toggle-speed-permanently": "binding-toggle-speed-permanently",
"binding-toggle-speed-temporarily": "binding-toggle-speed-temporarily",
"binding-toggle-stance": "binding-toggle-stance",
"binding-use-item": "binding-use-item",
"bloom-effect": "bloom-effect",
"bobbing": "bobbing",
Expand Down
1 change: 1 addition & 0 deletions engine/src/main/resources/assets/i18n/menu_en.lang
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"binding-show-online-players": "Show Online Players",
"binding-toggle-speed-permanently": "Toggle Speed Permanently",
"binding-toggle-speed-temporarily": "Toggle Speed Temporarily",
"binding-crouch-mode": "Crouch Mode",
"binding-use-item": "Use Held Item",
"bloom-effect": "Bloom",
"bobbing": "Bobbing",
Expand Down

0 comments on commit 3c767f1

Please sign in to comment.