Skip to content

Commit

Permalink
feat(JOML): migrate logic.players (MovingBlocks#4442)
Browse files Browse the repository at this point in the history
* feat(JOML): migrate logic.players
* chore: remove unnecessary qualification
  • Loading branch information
jdrueckert authored Jan 30, 2021
1 parent 242beef commit faa9eff
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 239 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2014 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.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.logic.location;

import com.google.common.collect.Lists;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
/*
* Copyright 2015 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.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.logic.players;

import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.terasology.entitySystem.entity.EntityBuilder;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
Expand All @@ -32,8 +21,7 @@
import org.terasology.logic.permission.PermissionManager;
import org.terasology.logic.players.event.OnPlayerSpawnedEvent;
import org.terasology.logic.players.event.ResetCameraEvent;
import org.terasology.math.geom.Quat4f;
import org.terasology.math.geom.Vector3f;
import org.terasology.math.JomlUtil;
import org.terasology.network.ClientComponent;
import org.terasology.registry.In;

Expand Down Expand Up @@ -129,9 +117,10 @@ private void mountCamera() {
LocationComponent cameraLocation = clientComponent.camera.getComponent(LocationComponent.class);
// if the camera already has a location, use that as the relative position of the camera
if (cameraLocation != null) {
Location.attachChild(targetEntityForCamera, clientComponent.camera, cameraLocation.getLocalPosition(), new Quat4f(Quat4f.IDENTITY));

Location.attachChild(targetEntityForCamera, clientComponent.camera, JomlUtil.from(cameraLocation.getLocalPosition()), new Quaternionf());
} else {
Location.attachChild(targetEntityForCamera, clientComponent.camera, Vector3f.zero(), new Quat4f(Quat4f.IDENTITY));
Location.attachChild(targetEntityForCamera, clientComponent.camera, new Vector3f(0, 0, 0), new Quaternionf());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The Terasology Foundation
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.logic.players;

Expand All @@ -17,7 +17,6 @@
import org.terasology.input.events.MouseAxisEvent;
import org.terasology.logic.characters.CharacterComponent;
import org.terasology.logic.debug.DebugProperties;
import org.terasology.logic.time.TimeResynchEvent;
import org.terasology.logic.players.event.WorldtimeResetEvent;
import org.terasology.network.ClientComponent;
import org.terasology.registry.In;
Expand Down
155 changes: 33 additions & 122 deletions engine/src/main/java/org/terasology/logic/players/LocalPlayer.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
/*
* Copyright 2013 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.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.logic.players;

import com.google.common.collect.Sets;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.logic.characters.CharacterComponent;
import org.terasology.logic.characters.CharacterMovementComponent;
Expand All @@ -25,9 +13,7 @@
import org.terasology.logic.characters.events.ActivationRequest;
import org.terasology.logic.location.LocationComponent;
import org.terasology.math.Direction;
import org.terasology.math.JomlUtil;
import org.terasology.math.geom.Quat4f;
import org.terasology.math.geom.Vector3f;
import org.terasology.network.ClientComponent;
import org.terasology.physics.HitResult;
import org.terasology.physics.Physics;
Expand Down Expand Up @@ -100,32 +86,7 @@ public EntityRef getClientInfoEntity() {
public boolean isValid() {
EntityRef characterEntity = getCharacterEntity();
return characterEntity.exists() && characterEntity.hasComponent(LocationComponent.class) && characterEntity.hasComponent(CharacterComponent.class)
&& characterEntity.hasComponent(CharacterMovementComponent.class);
}

/**
* @return
* @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation
* {@link #getPosition(org.joml.Vector3f)}.
*/
@Deprecated
public Vector3f getPosition() {
return getPosition(new Vector3f());
}

/**
* @param out
* @return
* @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation
* {@link #getPosition(org.joml.Vector3f)}.
*/
@Deprecated
public Vector3f getPosition(Vector3f out) {
LocationComponent location = getCharacterEntity().getComponent(LocationComponent.class);
if (location == null || Float.isNaN(location.getWorldPosition().x)) {
return out;
}
return location.getWorldPosition(out);
&& characterEntity.hasComponent(CharacterMovementComponent.class);
}

/**
Expand All @@ -134,10 +95,10 @@ public Vector3f getPosition(Vector3f out) {
* @param dest will hold the result
* @return dest
*/
public org.joml.Vector3f getPosition(org.joml.Vector3f dest) {
public Vector3f getPosition(Vector3f dest) {
LocationComponent location = getCharacterEntity().getComponent(LocationComponent.class);
if (location != null) {
org.joml.Vector3f result = location.getWorldPosition(new org.joml.Vector3f());
Vector3f result = location.getWorldPosition(new Vector3f());
if (result.isFinite()) { //TODO: MP finite check seems to hide a larger underlying problem
dest.set(result);
}
Expand All @@ -148,12 +109,12 @@ public org.joml.Vector3f getPosition(org.joml.Vector3f dest) {
/**
* @return
* @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation
* {@link #getRotation(Quaternionf)}.
* {@link #getRotation(Quaternionf)}.
*/
@Deprecated
public Quat4f getRotation() {
LocationComponent location = getCharacterEntity().getComponent(LocationComponent.class);
if (location == null || Float.isNaN(location.getWorldPosition().x)) {
if (location == null || Float.isNaN(location.getWorldPosition(new Vector3f()).x)) {
return new Quat4f(Quat4f.IDENTITY);
}
return location.getWorldRotation();
Expand All @@ -175,50 +136,21 @@ public Quaternionf getRotation(Quaternionf dest) {
}
return dest;
}
/**
* @return
* @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation
* {@link #getViewPosition(org.joml.Vector3f)}.
*/
@Deprecated
public Vector3f getViewPosition() {
return getViewPosition(new Vector3f());
}

/**
* @param out
* @return
* @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation
* {@link #getViewPosition(org.joml.Vector3f)}.
*/
@Deprecated
public Vector3f getViewPosition(Vector3f out) {
ClientComponent clientComponent = getClientEntity().getComponent(ClientComponent.class);
if (clientComponent == null) {
return out;
}
LocationComponent location = clientComponent.camera.getComponent(LocationComponent.class);
if (location == null || Float.isNaN(location.getWorldPosition().x)) {
return getPosition();
}

return location.getWorldPosition(out);
}

/**
* position of camera if one is present else use {@link #getPosition(org.joml.Vector3f)}
* position of camera if one is present else use {@link #getPosition(Vector3f)}
*
* @param dest will hold the result
* @return dest
*/
public org.joml.Vector3f getViewPosition(org.joml.Vector3f dest) {
public Vector3f getViewPosition(Vector3f dest) {
ClientComponent clientComponent = getClientEntity().getComponent(ClientComponent.class);
if (clientComponent == null) {
return dest;
}
LocationComponent location = clientComponent.camera.getComponent(LocationComponent.class);
if (location != null) {
org.joml.Vector3f result = location.getWorldPosition(new org.joml.Vector3f());
Vector3f result = location.getWorldPosition(new Vector3f());
if (result.isFinite()) { //TODO: MP finite check seems to hide a larger underlying problem
dest.set(result);
return dest;
Expand All @@ -230,7 +162,7 @@ public org.joml.Vector3f getViewPosition(org.joml.Vector3f dest) {
/**
* @return
* @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation
* {@link #getViewRotation(Quaternionf)}.
* {@link #getViewRotation(Quaternionf)}.
*/
@Deprecated
public Quat4f getViewRotation() {
Expand All @@ -239,15 +171,15 @@ public Quat4f getViewRotation() {
return new Quat4f(Quat4f.IDENTITY);
}
LocationComponent location = clientComponent.camera.getComponent(LocationComponent.class);
if (location == null || Float.isNaN(location.getWorldPosition().x)) {
if (location == null || Float.isNaN(location.getWorldPosition(new Vector3f()).x)) {
return getRotation();
}

return location.getWorldRotation();
}

/**
* orientation of camera if one is present else use {@link #getPosition(org.joml.Vector3f)}
* orientation of camera if one is present else use {@link #getPosition(Vector3f)}
*
* @param dest will hold the result
* @return dest
Expand All @@ -274,39 +206,12 @@ public Quaternionf getViewRotation(Quaternionf dest) {
* @param dest will hold the result
* @return dest
*/
public org.joml.Vector3f getViewDirection(org.joml.Vector3f dest) {
public Vector3f getViewDirection(Vector3f dest) {
Quaternionf rot = getViewRotation(new Quaternionf());
return rot.transform(Direction.FORWARD.asVector3f(), dest);
}

/**
* @return
* @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation
* {@link #getViewDirection(org.joml.Vector3f)}
*/
@Deprecated
public Vector3f getViewDirection() {
Quat4f rot = getViewRotation();
// TODO: Put a generator for direction vectors in a util class somewhere
// And just put quaternion -> vector somewhere too
Vector3f dir = Direction.FORWARD.getVector3f();
return rot.rotate(dir, dir);
}
/**
* @return
* @deprecated This is scheduled for removal in an upcoming version method will be replaced with JOML implementation
* {@link #getVelocity(org.joml.Vector3f)}
*/
@Deprecated
public Vector3f getVelocity() {
CharacterMovementComponent movement = getCharacterEntity().getComponent(CharacterMovementComponent.class);
if (movement != null) {
return JomlUtil.from(movement.getVelocity());
}
return new Vector3f();
}

public org.joml.Vector3f getVelocity(org.joml.Vector3f dest) {
public Vector3f getVelocity(Vector3f dest) {
CharacterMovementComponent movement = getCharacterEntity().getComponent(CharacterMovementComponent.class);
if (movement != null) {
return dest.set(movement.getVelocity());
Expand Down Expand Up @@ -349,32 +254,36 @@ boolean activateTargetAsClient() {
private boolean activateTargetOrOwnedEntity(EntityRef usedOwnedEntity) {
EntityRef character = getCharacterEntity();
CharacterComponent characterComponent = character.getComponent(CharacterComponent.class);
org.joml.Vector3f direction = getViewDirection(new org.joml.Vector3f());
org.joml.Vector3f originPos = getViewPosition(new org.joml.Vector3f());
Vector3f direction = getViewDirection(new Vector3f());
Vector3f originPos = getViewPosition(new Vector3f());
if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.RECORDING) {
this.directionAndOriginPosRecorderList.getTargetOrOwnedEntityDirectionAndOriginPosRecorder().add(direction, originPos);
} else if (recordAndReplayCurrentStatus.getStatus() == RecordAndReplayStatus.REPLAYING) {
org.joml.Vector3f[] data = this.directionAndOriginPosRecorderList.getTargetOrOwnedEntityDirectionAndOriginPosRecorder().poll();
Vector3f[] data =
this.directionAndOriginPosRecorderList.getTargetOrOwnedEntityDirectionAndOriginPosRecorder().poll();
direction = data[0];
originPos = data[1];
}
boolean ownedEntityUsage = usedOwnedEntity.exists();
int activationId = nextActivationId++;
Physics physics = CoreRegistry.get(Physics.class);
HitResult result = physics.rayTrace(originPos, direction, characterComponent.interactionRange, Sets.newHashSet(character), CharacterSystem.DEFAULTPHYSICSFILTER);
HitResult result = physics.rayTrace(originPos, direction, characterComponent.interactionRange,
Sets.newHashSet(character), CharacterSystem.DEFAULTPHYSICSFILTER);
boolean eventWithTarget = result.isHit();
if (eventWithTarget) {
EntityRef activatedObject = usedOwnedEntity.exists() ? usedOwnedEntity : result.getEntity();
activatedObject.send(new ActivationPredicted(character, result.getEntity(), originPos, direction,
result.getHitPoint(), result.getHitNormal(), activationId));
character.send(new ActivationRequest(character, ownedEntityUsage, usedOwnedEntity, eventWithTarget, result.getEntity(),
originPos, direction, result.getHitPoint(), result.getHitNormal(), activationId));
result.getHitPoint(), result.getHitNormal(), activationId));
character.send(new ActivationRequest(character, ownedEntityUsage, usedOwnedEntity, eventWithTarget,
result.getEntity(),
originPos, direction, result.getHitPoint(), result.getHitNormal(), activationId));
return true;
} else if (ownedEntityUsage) {
usedOwnedEntity.send(new ActivationPredicted(character, EntityRef.NULL, originPos, direction,
originPos, new org.joml.Vector3f(), activationId));
character.send(new ActivationRequest(character, ownedEntityUsage, usedOwnedEntity, eventWithTarget, EntityRef.NULL,
originPos, direction, originPos, new org.joml.Vector3f(), activationId));
originPos, new Vector3f(), activationId));
character.send(new ActivationRequest(character, ownedEntityUsage, usedOwnedEntity, eventWithTarget,
EntityRef.NULL,
originPos, direction, originPos, new Vector3f(), activationId));
return true;
}
return false;
Expand All @@ -383,7 +292,9 @@ private boolean activateTargetOrOwnedEntity(EntityRef usedOwnedEntity) {

@Override
public String toString() {
Vector3f pos = getPosition(new Vector3f());
return String.format("player (x: %.2f, y: %.2f, z: %.2f | x: %.2f, y: %.2f, z: %.2f)",
getPosition().x, getPosition().y, getPosition().z, getViewDirection().x, getViewDirection().y, getViewDirection().z);
pos.x, pos.y, pos.z, getViewDirection(new Vector3f()).x,
getViewDirection(new Vector3f()).y, getViewDirection(new Vector3f()).z);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
/*
* Copyright 2020 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.
*/
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.logic.players;

import org.joml.Math;
Expand Down Expand Up @@ -52,7 +39,6 @@
import org.terasology.input.events.MouseAxisEvent;
import org.terasology.input.events.MouseAxisEvent.MouseAxis;
import org.terasology.joml.geom.AABBf;
import org.terasology.joml.geom.AABBfc;
import org.terasology.logic.characters.CharacterComponent;
import org.terasology.logic.characters.CharacterHeldItemComponent;
import org.terasology.logic.characters.CharacterMoveInputEvent;
Expand All @@ -65,8 +51,6 @@
import org.terasology.logic.location.LocationComponent;
import org.terasology.logic.players.event.LocalPlayerInitializedEvent;
import org.terasology.logic.players.event.OnPlayerSpawnedEvent;
import org.terasology.math.AABB;
import org.terasology.math.JomlUtil;
import org.terasology.network.ClientComponent;
import org.terasology.network.NetworkMode;
import org.terasology.network.NetworkSystem;
Expand Down
Loading

0 comments on commit faa9eff

Please sign in to comment.