Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a new item that contains only the necessary visual components … #3137

Merged
merged 3 commits into from
Nov 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.terasology.logic.players;

import org.terasology.engine.Time;
import org.terasology.entitySystem.Component;
import org.terasology.entitySystem.entity.EntityBuilder;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
Expand All @@ -36,11 +37,8 @@
import org.terasology.math.geom.Quat4f;
import org.terasology.math.geom.Vector3f;
import org.terasology.network.ClientComponent;
import org.terasology.network.NetworkComponent;
import org.terasology.physics.components.RigidBodyComponent;
import org.terasology.registry.In;
import org.terasology.rendering.logic.LightComponent;
import org.terasology.rendering.logic.MeshComponent;
import org.terasology.rendering.logic.VisualComponent;
import org.terasology.rendering.world.WorldRenderer;

@RegisterSystem(RegisterMode.CLIENT)
Expand All @@ -62,9 +60,6 @@ public class FirstPersonClientSystem extends BaseComponentSystem implements Upda
// the item from the inventory synchronized with the server
private EntityRef currentHeldItem = EntityRef.NULL;

// the client side entity representing the hold item
private EntityRef clientHeldItem = EntityRef.NULL;

private EntityRef getHandEntity() {
if (handEntity == null) {
// create the hand entity
Expand Down Expand Up @@ -102,8 +97,7 @@ public void ensureHeldItemIsMountedOnLoad(OnChangedComponent event, EntityRef en
if (localPlayer.getClientEntity().equals(entityRef) && localPlayer.getCharacterEntity().exists() && localPlayer.getCameraEntity().exists()) {
CharacterHeldItemComponent characterHeldItemComponent = localPlayer.getCharacterEntity().getComponent(CharacterHeldItemComponent.class);
if (characterHeldItemComponent != null) {
// special case of sending in null so that the initial load works
linkHeldItemLocationForLocalPlayer(characterHeldItemComponent.selectedItem, null);
linkHeldItemLocationForLocalPlayer(characterHeldItemComponent.selectedItem);
}
}
}
Expand All @@ -129,18 +123,16 @@ public void setFirstPersonheldItemMountPointRotation(@CommandParam("x") float x,
@ReceiveEvent
public void onHeldItemActivated(OnActivatedComponent event, EntityRef character, CharacterHeldItemComponent heldItemComponent, CharacterComponent characterComponents) {
if (localPlayer.getCharacterEntity().equals(character)) {
EntityRef oldHeldItem = currentHeldItem;
currentHeldItem = heldItemComponent.selectedItem;
linkHeldItemLocationForLocalPlayer(currentHeldItem, oldHeldItem);
EntityRef newItem = heldItemComponent.selectedItem;
linkHeldItemLocationForLocalPlayer(newItem);
}
}

@ReceiveEvent
public void onHeldItemChanged(OnChangedComponent event, EntityRef character, CharacterHeldItemComponent heldItemComponent, CharacterComponent characterComponents) {
if (localPlayer.getCharacterEntity().equals(character)) {
EntityRef oldHeldItem = currentHeldItem;
currentHeldItem = heldItemComponent.selectedItem;
linkHeldItemLocationForLocalPlayer(currentHeldItem, oldHeldItem);
EntityRef newItem = heldItemComponent.selectedItem;
linkHeldItemLocationForLocalPlayer(newItem);
}
}

Expand All @@ -150,60 +142,52 @@ public void onHeldItemChanged(OnChangedComponent event, EntityRef character, Cha
* <p>Detaches old held item and removes it's components. Adds components to new held item and
* attaches it to the mount point entity.</p>
*/
private void linkHeldItemLocationForLocalPlayer(EntityRef newItem, EntityRef oldItem) {
if (!newItem.equals(oldItem)) {
private void linkHeldItemLocationForLocalPlayer(EntityRef newItem) {
if (!newItem.equals(currentHeldItem)) {
EntityRef camera = localPlayer.getCameraEntity();
FirstPersonHeldItemMountPointComponent mountPointComponent = camera.getComponent(FirstPersonHeldItemMountPointComponent.class);
if (mountPointComponent != null) {

if (clientHeldItem.exists()) {
// TODO: Review if more components need to be removed (or more likely: overhaul more substantially)
clientHeldItem.removeComponent(MeshComponent.class);
clientHeldItem.removeComponent(LightComponent.class);
}

// remove the location from the old item
if (oldItem != null && oldItem.exists()) {
oldItem.removeComponent(ItemIsHeldComponent.class);
} else {
getHandEntity().removeComponent(ItemIsHeldComponent.class);
//currentHeldItem is at this point the old item
if (currentHeldItem != EntityRef.NULL) {
currentHeldItem.destroy();
}

// use the hand if there is no new item
EntityRef heldItem;
if (!newItem.exists()) {
heldItem = getHandEntity();
EntityRef newHeldItem;
if (newItem == EntityRef.NULL) {
newHeldItem = getHandEntity();
} else {
heldItem = newItem;
newHeldItem = newItem;
}

// create client side held item entity
clientHeldItem = heldItem.copy();
currentHeldItem = entityManager.create();

// remove network-component to prohibit replicating the client-side held entity to other clients
// also remove rigid-body-component to prohibit the held item falling to the ground
clientHeldItem.removeComponent(NetworkComponent.class);
clientHeldItem.removeComponent(RigidBodyComponent.class);

clientHeldItem.addOrSaveComponent(new LocationComponent());
// add the visually relevant components
for (Component component : newHeldItem.iterateComponents()) {
if (component instanceof VisualComponent) {
currentHeldItem.addComponent(component);
}
}

heldItem.addOrSaveComponent(new ItemIsHeldComponent());
// ensure world location is set
currentHeldItem.addComponent(new LocationComponent());
currentHeldItem.addComponent(new ItemIsHeldComponent());

FirstPersonHeldItemTransformComponent heldItemTransformComponent = clientHeldItem.getComponent(FirstPersonHeldItemTransformComponent.class);
FirstPersonHeldItemTransformComponent heldItemTransformComponent = currentHeldItem.getComponent(FirstPersonHeldItemTransformComponent.class);
if (heldItemTransformComponent == null) {
heldItemTransformComponent = new FirstPersonHeldItemTransformComponent();
clientHeldItem.addComponent(heldItemTransformComponent);
currentHeldItem.addComponent(heldItemTransformComponent);
}

Location.attachChild(mountPointComponent.mountPointEntity, clientHeldItem,
Location.attachChild(mountPointComponent.mountPointEntity, currentHeldItem,
heldItemTransformComponent.translate,
new Quat4f(
TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.y,
TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.x,
TeraMath.DEG_TO_RAD * heldItemTransformComponent.rotateDegrees.z),
heldItemTransformComponent.scale);

currentHeldItem = clientHeldItem;
}
}
}
Expand All @@ -215,14 +199,14 @@ private void linkHeldItemLocationForLocalPlayer(EntityRef newItem, EntityRef old
public void update(float delta) {

// ensure empty hand is shown if no item is hold at the moment
if (!currentHeldItem.exists() && clientHeldItem != getHandEntity()) {
linkHeldItemLocationForLocalPlayer(currentHeldItem, null);
if (!currentHeldItem.exists() && currentHeldItem != getHandEntity()) {
linkHeldItemLocationForLocalPlayer(getHandEntity());
}

// ensure that there are no lingering items that are marked as still held. This situation happens with client side predicted items
for (EntityRef entityRef : entityManager.getEntitiesWith(ItemIsHeldComponent.class)) {
if (!entityRef.equals(currentHeldItem) && !entityRef.equals(handEntity)) {
entityRef.removeComponent(ItemIsHeldComponent.class);
entityRef.destroy();
}
}

Expand Down Expand Up @@ -260,8 +244,8 @@ public void update(float delta) {

@Override
public void preSave() {
if (clientHeldItem != EntityRef.NULL) {
clientHeldItem.destroy();
if (currentHeldItem != EntityRef.NULL) {
currentHeldItem.destroy();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package org.terasology.logic.players;

import org.terasology.entitySystem.Component;
import org.terasology.math.geom.Vector3f;
import org.terasology.rendering.logic.VisualComponent;

public class FirstPersonHeldItemTransformComponent implements Component {
public class FirstPersonHeldItemTransformComponent implements VisualComponent {
public Vector3f rotateDegrees = Vector3f.zero();
public Vector3f translate = Vector3f.zero();
public float scale = 1f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
*/
package org.terasology.rendering.logic;

import org.terasology.entitySystem.Component;
import org.terasology.rendering.nui.Color;

/**
* Makes the game render the specified text at the current location of the enitity.
*/
public class FloatingTextComponent implements Component {
public class FloatingTextComponent implements VisualComponent {
public String text;
public Color textColor = Color.WHITE;
public Color textShadowColor = Color.BLACK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.terasology.rendering.logic;

import org.terasology.entitySystem.Component;
import org.terasology.math.geom.Vector3f;
import org.terasology.network.Replicate;
import org.terasology.network.ReplicationCheck;
Expand All @@ -25,7 +24,7 @@
* Add this component to an entity for it to transmit light from its location. By default the component is configured to act similarly to a placed torch block.
*/
// TODO: Split into multiple components? Point, Directional?
public final class LightComponent implements Component, ReplicationCheck {
public final class LightComponent implements VisualComponent, ReplicationCheck {

public enum LightType {
POINT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
*/
package org.terasology.rendering.logic;

import org.terasology.entitySystem.Component;
import org.terasology.network.Replicate;

/**
*/
public final class LightFadeComponent implements Component {
public final class LightFadeComponent implements VisualComponent {

@Replicate
public float targetDiffuseIntensity = 1.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.terasology.rendering.logic;

import org.terasology.entitySystem.Component;
import org.terasology.network.Replicate;
import org.terasology.rendering.assets.material.Material;
import org.terasology.rendering.assets.mesh.Mesh;
Expand All @@ -25,7 +24,7 @@
/**
*/
@ForceBlockActive
public final class MeshComponent implements Component {
public final class MeshComponent implements VisualComponent {

@Replicate
public Mesh mesh;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
*/
package org.terasology.rendering.logic;

import org.terasology.entitySystem.Component;
import org.terasology.math.geom.Vector3i;
import org.terasology.rendering.nui.Color;

/**
* Entities with this component will cause a outline be drawn about the specified region in block coordinates.
*/
public class RegionOutlineComponent implements Component {
public class RegionOutlineComponent implements VisualComponent {
public Vector3i corner1;
public Vector3i corner2;
public Color color = Color.WHITE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.terasology.rendering.logic;

import com.google.common.collect.Lists;
import org.terasology.entitySystem.Component;
import org.terasology.entitySystem.Owns;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.math.geom.Vector3f;
Expand All @@ -34,7 +33,7 @@
/**
*/
@ForceBlockActive
public class SkeletalMeshComponent implements Component {
public class SkeletalMeshComponent implements VisualComponent {
public SkeletalMesh mesh;
public Material material;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2017 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.rendering.logic;

import org.terasology.entitySystem.Component;

public interface VisualComponent extends Component {
}