Skip to content

Commit

Permalink
feature(ecs-gestalt): Migrate Components to gestalt's Components (#82)
Browse files Browse the repository at this point in the history
Ref: MovingBlocks/Terasology#4753

Co-authored-by: Michael Pollind <mpollind@gmail.com>
  • Loading branch information
skaldarnar and pollend committed Aug 25, 2021
1 parent 6e636f3 commit dfb4934
Show file tree
Hide file tree
Showing 33 changed files with 371 additions and 175 deletions.
15 changes: 15 additions & 0 deletions src/main/java/org/terasology/combatSystem/Faction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.combatSystem;

/**
* Interface used to specify different types of faction lists.
* <p>
* Classes implementing this interface should be <b>immutable</b>. This is to allow for a safe reuse when copying the faction in
* components.
*/
public interface Faction {
//NOTE: if we identify the need to make the faction implementations mutable, or observe any other issues with only doing a shallow copy
// of properties of this type in components, we may add a 'copy()' method to this interface.
}
8 changes: 0 additions & 8 deletions src/main/java/org/terasology/combatSystem/FactionList.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.combatSystem;

/**
* Basic test faction in <b>CombatSystem</b> module for implementation purpose.
*/
public enum PlanetFactionList implements FactionList{
public enum PlanetFactions implements Faction {
MERCURY,
VENUS,
EARTH,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.combatSystem.components;

import org.terasology.combatSystem.FactionList;
import org.terasology.combatSystem.PlanetFactionList;
import org.terasology.engine.entitySystem.Component;
import org.terasology.combatSystem.Faction;
import org.terasology.combatSystem.PlanetFactions;
import org.terasology.engine.network.Replicate;
import org.terasology.gestalt.entitysystem.component.Component;

/**
* Makes the entity belong to a specific faction.
*/
public class FactionComponent implements Component{
public class FactionComponent implements Component<FactionComponent> {
@Replicate
public FactionList faction = PlanetFactionList.EARTH;
public Faction faction = PlanetFactions.EARTH;

@Override
public void copyFrom(FactionComponent other) {
this.faction = other.faction;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.combatSystem.hurting;

import org.terasology.engine.entitySystem.Component;
import org.terasology.engine.network.Replicate;
import org.terasology.gestalt.entitysystem.component.Component;

/**
* Damages an entity critically.
* <p>
* Damaging critically means multiplying the original damage by <b>critFactor</b>.
* The critical damage has <b>critChance</b>/100 probability of occurring.
*/
public class CritDamageComponent implements Component{
public class CritDamageComponent implements Component<CritDamageComponent> {

@Replicate
public int critChance = 10; // in percentage out of 100

@Replicate
public float critFactor = 2; // factor to multiply damage with

@Override
public void copyFrom(CritDamageComponent other) {
this.critChance = other.critChance;
this.critFactor = other.critFactor;
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.combatSystem.hurting;

import org.terasology.engine.entitySystem.Component;
import org.terasology.engine.entitySystem.prefab.Prefab;
import org.terasology.engine.logic.health.EngineDamageTypes;
import org.terasology.engine.network.Replicate;
import org.terasology.gestalt.entitysystem.component.Component;

/**
* Specifies the amount and damage type that an entity will inflict on other entities.
* <p>
* {@link HurtEvent} is used to hurt other entity/entities.
*/
public class HurtingComponent implements Component{
public class HurtingComponent implements Component<HurtingComponent> {
/**
* The amount of damage the entity will inflict.
*/
Expand All @@ -23,4 +26,9 @@ public class HurtingComponent implements Component{
@Replicate
public Prefab damageType = EngineDamageTypes.DIRECT.get();

@Override
public void copyFrom(HurtingComponent other) {
this.amount = other.amount;
this.damageType = other.damageType;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.combatSystem.hurting;

import org.terasology.engine.entitySystem.Component;
import org.terasology.engine.network.Replicate;
import org.terasology.gestalt.entitysystem.component.Component;

/**
* To generate a random damage amount between <b>minDamage</b> and <b>maxDamage</b> (both inclusive).
*/
public class RandomDamageComponent implements Component{
//inclusive
public class RandomDamageComponent implements Component<RandomDamageComponent> {

/** The minimal damage (inclusive). */
@Replicate
public int minDamage = 2;
//inclusive

/** The maximum damage (inclusive). */
@Replicate
public int maxDamage = 4;

@Override
public void copyFrom(RandomDamageComponent other) {
this.minDamage = other.minDamage;
this.maxDamage = other.maxDamage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
package org.terasology.combatSystem.physics.components;

import com.google.common.collect.Lists;
import org.terasology.engine.entitySystem.Component;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.network.Replicate;
import org.terasology.gestalt.entitysystem.component.Component;

import java.util.List;

/**
* specifies the <b>Entities</b> that will not be collided with.
* <p>
* An {@link EntityRef} with this {@link Component} will not collide with <b>exceptions</b>.
* An {@link EntityRef} with this {@link Component} will not collide with <b>exceptions</b>.
*/
public class CollisionExceptionsComponent implements Component{
public class CollisionExceptionsComponent implements Component<CollisionExceptionsComponent> {
@Replicate
public List<EntityRef> exceptions = Lists.<EntityRef>newArrayList();

@Override
public void copyFrom(CollisionExceptionsComponent other) {
this.exceptions = Lists.newArrayList(other.exceptions);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.combatSystem.physics.components;

import org.terasology.engine.entitySystem.Component;
import org.terasology.engine.network.Replicate;
import org.terasology.gestalt.entitysystem.component.Component;

/**
* Adds <b>friction</b>.
* <p>
* <b>this</b> can be used to implement <b>Fluid Viscosity</b> as well.
*
*
* TODO implement this component and its code.
*/
public class FrictionComponent implements Component{
public class FrictionComponent implements Component<FrictionComponent> {
@Replicate
float friction = 20.0f;

@Replicate
float velocity = 20.0f;

@Override
public void copyFrom(FrictionComponent other) {
this.friction = other.friction;
this.velocity = other.velocity;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.combatSystem.physics.components;

import org.joml.Vector3f;
import org.terasology.combatSystem.physics.events.CombatForceEvent;
import org.terasology.engine.entitySystem.Component;
import org.terasology.engine.network.Replicate;
import org.terasology.gestalt.entitysystem.component.Component;

/**
* Adds <b>Gravity</b> to the entity.
Expand All @@ -12,8 +15,12 @@
* That would result in removal of those forces too if <b>Gravity</b> is disabled as a whole.
* Instead use of {@link CombatForceEvent} is encouraged to add forces.
*/
public class GravityComponent implements Component{
public class GravityComponent implements Component<GravityComponent> {
@Replicate
public Vector3f gravityAccel = new Vector3f(0, -10.0f, 0);

@Override
public void copyFrom(GravityComponent other) {
this.gravityAccel.set(other.gravityAccel);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.combatSystem.physics.components;

import org.joml.Vector3f;
import org.terasology.engine.entitySystem.Component;
import org.terasology.engine.network.Replicate;
import org.terasology.engine.physics.components.RigidBodyComponent;
import org.terasology.engine.physics.components.TriggerComponent;
import org.terasology.gestalt.entitysystem.component.Component;

/**
* Adds <b>translational motion</b> feature to an entity.
* <p>
* <b>NOTE</b>: It does not add <b>collider</b> like {@link RigidBodyComponent}.
* {@link TriggerComponent} is used for collisions.
*/
public class MassComponent implements Component{
public class MassComponent implements Component<MassComponent> {
@Replicate(initialOnly = true)
public float mass = 10.0f;

Expand All @@ -23,4 +25,11 @@ public class MassComponent implements Component{
@Replicate
public Vector3f force = new Vector3f();

@Override
public void copyFrom(MassComponent other) {
this.velocity.set(other.velocity);
this.acceleration.set(other.acceleration);
this.force.set(other.force);
this.mass = other.mass;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.combatSystem.systems;

import org.terasology.combatSystem.FactionList;
import org.terasology.combatSystem.Faction;
import org.terasology.combatSystem.components.FactionComponent;
import org.terasology.combatSystem.event.CombatEnteredEvent;
import org.terasology.combatSystem.hurting.HurtEvent;
Expand All @@ -21,8 +24,8 @@ public void combatEntered(HurtEvent event, EntityRef entity){
return;
}

FactionList entityFaction = entity.getComponent(FactionComponent.class).faction;
FactionList otherEntityFaction = entity.getComponent(FactionComponent.class).faction;
Faction entityFaction = entity.getComponent(FactionComponent.class).faction;
Faction otherEntityFaction = entity.getComponent(FactionComponent.class).faction;
if(entityFaction != otherEntityFaction){
entity.send(new CombatEnteredEvent());
otherEntity.send(new CombatEnteredEvent());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.combatSystem.traps.components;

import org.terasology.engine.entitySystem.Component;
import org.terasology.gestalt.entitysystem.component.EmptyComponent;

public class ActivateOnPlaceComponent implements Component{

/**
* Marker component to indicate that the combat effect of an entity should activate when the entity is placed (as a block) in the world.
*/
public class ActivateOnPlaceComponent extends EmptyComponent<ActivateOnPlaceComponent> {
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.combatSystem.traps.components;

import org.terasology.engine.entitySystem.Component;
import org.terasology.engine.entitySystem.prefab.Prefab;
import org.terasology.engine.network.Replicate;
import org.terasology.gestalt.entitysystem.component.Component;

public class BaseLauncherComponent implements Component{
public class BaseLauncherComponent implements Component<BaseLauncherComponent> {
@Replicate
public Prefab entityInBase;

@Replicate
public int amountOfEntityInBase;

@Replicate
public int coolDownTime = 1000;

@Replicate
public float lastLaunchTime = -1.0f;

@Override
public void copyFrom(BaseLauncherComponent other) {
this.entityInBase = other.entityInBase;
this.amountOfEntityInBase = other.amountOfEntityInBase;
this.coolDownTime = other.coolDownTime;
this.lastLaunchTime = other.lastLaunchTime;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// Copyright 2021 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0
package org.terasology.combatSystem.traps.components;

import org.terasology.engine.entitySystem.Component;
import org.terasology.engine.entitySystem.entity.EntityRef;
import org.terasology.engine.network.Replicate;
import org.terasology.gestalt.entitysystem.component.Component;

public class BeaconComponent implements Component{
public class BeaconComponent implements Component<BeaconComponent> {
@Replicate
public EntityRef base = EntityRef.NULL;

@Override
public void copyFrom(BeaconComponent other) {
this.base = other.base;
}
}
Loading

0 comments on commit dfb4934

Please sign in to comment.