-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: split GenomeAuthoritySystem based on modules (#120)
* feat: split GenomeAuthoritySystem based on modules * feat: add 'requiresOptional' annotation metadata to genome extension systems The integration with Genome and BasicCrafting is optional for SimpleFarming. As a component system can only be loaded properly if all it's dependencies are also active, this means that the combined `GenomeAuthoritySystem` is only loaded if **both** Genome and BasicCrafting are active. By splitting the authority system into `GenomeExtensionAuthoritySystem` and `GenomeCraftingExtensionAuthoritySystem` each extension system can run with the minimal set of required modules. - `GenomeExtensionAuthoritySystem` (works if at least Genome is loaded) - `GenomeCraftingExtensionAuthoritySystem` (works if both Genome and Basic Crafting are loaded) Fixes #119 Co-authored-by: vedant-shroff <[email protected]>
- Loading branch information
1 parent
b3c691b
commit 636c7a2
Showing
3 changed files
with
92 additions
and
53 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...ain/java/org/terasology/simpleFarming/systems/GenomeCraftingExtensionAuthoritySystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2021 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.simpleFarming.systems; | ||
|
||
import org.terasology.crafting.events.OnRecipeCrafted; | ||
import org.terasology.engine.entitySystem.entity.EntityRef; | ||
import org.terasology.engine.entitySystem.event.ReceiveEvent; | ||
import org.terasology.engine.entitySystem.systems.BaseComponentSystem; | ||
import org.terasology.engine.entitySystem.systems.RegisterMode; | ||
import org.terasology.engine.entitySystem.systems.RegisterSystem; | ||
import org.terasology.engine.registry.In; | ||
import org.terasology.genome.GenomeRegistry; | ||
import org.terasology.genome.component.GenomeComponent; | ||
import org.terasology.genome.system.SimpleGenomeManager; | ||
|
||
/** | ||
* Extension system integrating genetics with basic crafting. | ||
* | ||
* This component system is only loaded if both "Genome" and "BasicCrafting" are enabled. | ||
*/ | ||
@RegisterSystem(value = RegisterMode.AUTHORITY, requiresOptional = {"Genome", "BasicCrafting"}) | ||
public class GenomeCraftingExtensionAuthoritySystem extends BaseComponentSystem { | ||
|
||
@In | ||
private GenomeRegistry genomeRegistry; | ||
|
||
/** | ||
* Adds genes to the crafted entity if breeding is possible. | ||
* @param event the OnRecipeCrafted event | ||
* @param entity the crafted entity which is to be modified | ||
*/ | ||
@ReceiveEvent | ||
public void onRecipeCraftedEvent(OnRecipeCrafted event, EntityRef entity) { | ||
EntityRef[] ingredients = event.getIngredients(); | ||
if (ingredients.length != 2) { | ||
return; | ||
} | ||
|
||
if (!(ingredients[0].hasComponent(GenomeComponent.class) || ingredients[1].hasComponent(GenomeComponent.class))) { | ||
return; | ||
} | ||
|
||
SimpleGenomeManager genomeManager = new SimpleGenomeManager(); | ||
boolean result = genomeManager.applyBreeding(ingredients[0], ingredients[1], entity); | ||
if (entity.hasComponent(GenomeComponent.class)) { | ||
GenomeUtil.updateFilling(genomeRegistry, entity); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/org/terasology/simpleFarming/systems/GenomeUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2021 The Terasology Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package org.terasology.simpleFarming.systems; | ||
|
||
import org.terasology.engine.entitySystem.entity.EntityRef; | ||
import org.terasology.genome.GenomeRegistry; | ||
import org.terasology.genome.component.GenomeComponent; | ||
import org.terasology.genome.genomeMap.GenomeMap; | ||
import org.terasology.simpleFarming.events.ModifyFilling; | ||
import org.terasology.simpleFarming.events.ModifyTint; | ||
|
||
public final class GenomeUtil { | ||
|
||
private GenomeUtil() { | ||
} | ||
|
||
public static void updateFilling(GenomeRegistry genomeRegistry, EntityRef entity) { | ||
GenomeComponent genome = entity.getComponent(GenomeComponent.class); | ||
GenomeMap genomeMap = genomeRegistry.getGenomeDefinition(genome.genomeId).getGenomeMap(); | ||
float fillingModifier = genomeMap.getProperty("filling", genome.genes, Float.class); | ||
|
||
float newFilling = entity.send(new ModifyFilling(fillingModifier)).filling.getValue(); | ||
|
||
entity.send(new ModifyTint(newFilling)); | ||
} | ||
} |