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

Implemented Genetics in blueberry bush. #57

Closed
wants to merge 9 commits into from
Closed
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
15 changes: 15 additions & 0 deletions assets/blocks/SeedMixer.block
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"attachmentAllowed": false,
"rotation": "horizontal",
"tiles" : {
"sides" : "core:ChestSides",
"front" : "core:ChestFront",
"topBottom" : "core:ChestTopBottom"
},
"entity": {
"prefab": "SimpleFarming:SeedMixer"
},
"inventory": {
"stackable": true
}
}
8 changes: 6 additions & 2 deletions assets/prefabs/Bushes/Blueberry/Blueberry.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
},
"Item": {
"icon": "SimpleFarming:Blueberry",
"stackId": "blueberry"
}
"consumedOnUse": true
},
"Food": {
"filling": 10
},
"Genome": {}
}
1 change: 1 addition & 0 deletions assets/prefabs/Bushes/Blueberry/BlueberryBushFull.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"producePrefab": "SimpleFarming:Blueberry"
},
"UnGrowPlantOnHarvest": {},
"Genome": {},
"PlantDefinition": {
"seedPrefab": "BlueberrySeed"
}
Expand Down
8 changes: 6 additions & 2 deletions assets/prefabs/Bushes/Blueberry/BlueberrySeed.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
"name": "Blueberry Seed"
},
"Item": {
"icon": "SimpleFarming:SimpleFarming#berryseed",
"stackId": "blueberrySeed"
"icon": "SimpleFarming:SimpleFarming#berryseed"
},
"Seed": {},
"Genome": {
"genomeId": "blueberry",
"genes": "F"
},
"PlantDefinition": {
"plantName": "Blueberry Bush",
Expand Down
20 changes: 20 additions & 0 deletions assets/prefabs/SeedMixer.prefab
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"SeedMixer": {},
"Inventory": {
"privateToOwner": false,
"itemSlots": [
0,
0,
0,
0
]
},
"RetainBlockInventory": {},
"PlaySoundAction": {
"sounds": "engine:click"
},
"InteractionTarget": {},
"InteractionScreen": {
"screen": "SimpleFarming:SeedMixer"
}
}
Binary file added assets/textures/equals.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/textures/plus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
89 changes: 89 additions & 0 deletions assets/ui/SeedMixer.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"type": "SeedMixerScreen",
"skin": "inventoryDefault",
"contents": {
"type": "relativeLayout",
"contents": [
{
"type": "InventoryGrid",
"id": "inventory",
"maxHorizontalCells" : 10,
"layoutInfo": {
"use-content-width": true,
"use-content-height": true,
"position-bottom": {
"target": "BOTTOM",
"offset": 128
},
"position-horizontal-center": {}
}
},
{
"type": "flowLayout",
"id": "mixerInventory",
"contents": [
{
"type": "InventoryCell",
"id": "S1",
"targetSlot": 0
},
{
"type": "UIImage",
"image": "SimpleFarming:plus",
"layoutInfo": {
"width": 48,
"height": 48,
"position-vertical-center": {}
}
},
{
"type": "InventoryCell",
"id": "S2",
"targetSlot": 1
},
{
"type": "UIImage",
"image": "SimpleFarming:equals",
"layoutInfo": {
"width": 48,
"height": 48,
"position-vertical-center": {}
}
},
{
"type": "InventoryCell",
"id": "O1",
"targetSlot": 2
},
{
"type": "UIImage",
"image": "SimpleFarming:plus",
"layoutInfo": {
"width": 48,
"height": 48,
"position-bottom": {
"target": "BOTTOM",
"widget": "mixerInventory"
}
}
},
{
"type": "InventoryCell",
"id": "O2",
"targetSlot": 3
}
],
"layoutInfo": {
"use-content-width": true,
"use-content-height": true,
"position-horizontal-center": {},
"position-bottom": {
"target": "TOP",
"widget": "inventory",
"offset": 16
}
}
}
]
}
}
8 changes: 8 additions & 0 deletions module.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
{
"id" : "Core",
"minVersion" : "1.0.0"
},
{
"id" : "Genome",
"minVersion" : "0.1.1"
},
{
"id" : "Hunger",
"minVersion" : "1.0.0"
}
],
"isServerSideOnly" : false
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.simpleFarming.components;

import org.terasology.entitySystem.Component;

public class SeedComponent implements Component {
}
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.simpleFarming.components;

import org.terasology.entitySystem.Component;

public class SeedMixerComponent implements Component {
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import org.terasology.entitySystem.systems.BaseComponentSystem;
import org.terasology.entitySystem.systems.RegisterMode;
import org.terasology.entitySystem.systems.RegisterSystem;
import org.terasology.genome.GenomeDefinition;
import org.terasology.genome.GenomeRegistry;
import org.terasology.genome.component.GenomeComponent;
import org.terasology.hunger.component.FoodComponent;
import org.terasology.logic.common.ActivateEvent;
import org.terasology.logic.common.DisplayNameComponent;
import org.terasology.logic.delay.DelayManager;
Expand Down Expand Up @@ -89,19 +93,23 @@ public class FarmingAuthoritySystem extends BaseComponentSystem {
BlockEntityRegistry blockEntityRegistry;
@In
PrefabManager prefabManager;
@In
private GenomeRegistry genomeRegistry;

GenomeComponent newGenomeComponent = new GenomeComponent();

Random random = new FastRandom();

@ReceiveEvent
/**
* Handles the seed drop on plant destroyed event.
* Handles the seed planting event.
*
* @param event The corresponding event.
* @param seedItem Reference to the seed entity.
* @param plantDefinitionComponent The definition of the plant.
* @param itemComponent The item component corresponding to the event
*/
public void onPlantSeed(ActivateEvent event, EntityRef seedItem, PlantDefinitionComponent plantDefinitionComponent, ItemComponent itemComponent) {
public void onPlantSeed(ActivateEvent event, EntityRef seedItem, PlantDefinitionComponent plantDefinitionComponent, GenomeComponent genomeComponent, ItemComponent itemComponent) {
if (!event.getTarget().exists() || event.getTargetLocation() == null) {
return;
}
Expand Down Expand Up @@ -143,6 +151,7 @@ public void onPlantSeed(ActivateEvent event, EntityRef seedItem, PlantDefinition
worldProvider.setBlock(plantPosition, plantBlock);
EntityRef plantEntity = blockEntityRegistry.getBlockEntityAt(plantPosition);
plantEntity.addComponent(newPlantDefinitionComponent);
plantEntity.addOrSaveComponent(genomeComponent);

TreeDefinitionComponent treeDefinitionComponent = seedItem.getComponent(TreeDefinitionComponent.class);
if (treeDefinitionComponent != null) {
Expand Down Expand Up @@ -175,9 +184,10 @@ private Map.Entry<String, TimeRange> getGrowthStage(PlantDefinitionComponent pla
* @param plantDefinitionComponent The definition of the plant.
* @param blockComponent The block component corresponding to the event
*/
public void onPlantDestroyed(CreateBlockDropsEvent event, EntityRef entity, PlantDefinitionComponent plantDefinitionComponent, BlockComponent blockComponent) {
public void onPlantDestroyed(CreateBlockDropsEvent event, EntityRef entity, PlantDefinitionComponent plantDefinitionComponent, GenomeComponent genomeComponent, BlockComponent blockComponent) {
event.consume();
EntityRef seedItem = entityManager.create(plantDefinitionComponent.seedPrefab);
seedItem.addComponent(genomeComponent);
Vector3f position = blockComponent.getPosition().toVector3f().add(0, 0.5f, 0);
seedItem.send(new DropItemEvent(position));
seedItem.send(new ImpulseEvent(random.nextVector3f(30.0f)));
Expand Down Expand Up @@ -213,15 +223,19 @@ public void scheduledPlantGrowth(DelayedActionTriggeredEvent event, EntityRef en
* Handles plant growth event.
*
* @param event The event corresponding to the plant growth.
* @param entity The entity which is going to grown
* @param entity The entity which is going to grow
* @param plantDefinitionComponent The definition of the plant.
* @param blockComponent The block component corresponding to the event
*/
public void onPlantGrowth(OnPlantGrowth event, EntityRef entity, PlantDefinitionComponent plantDefinitionComponent, BlockComponent blockComponent) {
public void onPlantGrowth(OnPlantGrowth event, EntityRef entity, PlantDefinitionComponent plantDefinitionComponent, GenomeComponent genomeComponent, BlockComponent blockComponent) {
if (delayManager.hasDelayedAction(entity, GROWTH_ACTION)) {
delayManager.cancelDelayedAction(entity, GROWTH_ACTION);
}

GenomeComponent newGenomeComponent = new GenomeComponent();
newGenomeComponent.genomeId = genomeComponent.genomeId;
newGenomeComponent.genes = genomeComponent.genes;

TimeRange nextGrowthStage = null;
String nextGrowthStageBlockName = "";

Expand Down Expand Up @@ -258,26 +272,23 @@ public void onPlantGrowth(OnPlantGrowth event, EntityRef entity, PlantDefinition

TreeDefinitionComponent treeDefinitionComponent = entity.getComponent(TreeDefinitionComponent.class);

entity.destroy();

// Grow the plant into the next growth stage.
worldProvider.setBlock(blockComponent.getPosition(), newPlantBlock);

// Creates new entity.
EntityRef newEntity = blockEntityRegistry.getBlockEntityAt(blockComponent.getPosition());

// Check the new entity for PlantDefinitionComponent.
if (newEntity.hasComponent(PlantDefinitionComponent.class)) {
newEntity.saveComponent(plantDefinitionComponent);
} else {
newEntity.addComponent(plantDefinitionComponent);
}
newEntity.addOrSaveComponent(plantDefinitionComponent);

// Check the new entity for GenomeComponent.
newEntity.addOrSaveComponent(newGenomeComponent);

// Check for TreeDefinitionComponent
if (treeDefinitionComponent != null) {
if (newEntity.hasComponent(TreeDefinitionComponent.class)) {
newEntity.saveComponent(treeDefinitionComponent);
} else {
newEntity.addComponent(treeDefinitionComponent);
}
newEntity.addOrSaveComponent(treeDefinitionComponent);
}

schedulePlantGrowth(newEntity, nextGrowthStage);
Expand All @@ -292,7 +303,7 @@ public void onPlantGrowth(OnPlantGrowth event, EntityRef entity, PlantDefinition
* @param plantDefinitionComponent The definition of the plant.
* @param blockComponent The block component corresponding to the event.
*/
public void onPlantUnGrowth(OnPlantUnGrowth event, EntityRef entity, PlantDefinitionComponent plantDefinitionComponent, BlockComponent blockComponent) {
public void onPlantUnGrowth(OnPlantUnGrowth event, EntityRef entity, PlantDefinitionComponent plantDefinitionComponent, GenomeComponent genomeComponent, BlockComponent blockComponent) {
List<Map.Entry<String, TimeRange>> growthStages = getGrowthStages(plantDefinitionComponent);

if (growthStages.size() == 0) {
Expand Down Expand Up @@ -332,11 +343,10 @@ public void onPlantUnGrowth(OnPlantUnGrowth event, EntityRef entity, PlantDefini
// Creates a new entity
EntityRef newEntity = blockEntityRegistry.getBlockEntityAt(blockComponent.getPosition());
// Check the new entity for PlantDefinitionComponent.
if (newEntity.hasComponent(PlantDefinitionComponent.class)) {
newEntity.saveComponent(plantDefinitionComponent);
} else {
newEntity.addComponent(plantDefinitionComponent);
}
newEntity.addOrSaveComponent(plantDefinitionComponent);

// Check the new entity for GenomeComponent.
newEntity.addOrSaveComponent(genomeComponent);

// Schedules a plant growth.
schedulePlantGrowth(newEntity, previousGrowthStage);
Expand All @@ -350,7 +360,7 @@ public void onPlantUnGrowth(OnPlantUnGrowth event, EntityRef entity, PlantDefini
* @param event The event corresponding to the plant harvest
* @param entity The entity which is going to be harvested
*/
public void onHarvest(ActivateEvent event, EntityRef entity) {
public void onHarvest(ActivateEvent event, EntityRef entity, GenomeComponent genomeComponent) {
EntityRef target = event.getTarget();
EntityRef instigator = event.getInstigator();
EntityRef harvestingEntity = entity;
Expand All @@ -362,6 +372,10 @@ public void onHarvest(ActivateEvent event, EntityRef entity) {
PlantProduceComponent plantProduceComponent = target.getComponent(PlantProduceComponent.class);
if (plantProduceComponent != null) {
EntityRef produceItem = plantProduceComponent.produceItem;
FoodComponent foodComponent = new FoodComponent();
GenomeDefinition genomeDefinition = genomeRegistry.getGenomeDefinition(genomeComponent.genomeId);
foodComponent.filling = genomeDefinition.getGenomeMap().getProperty("filling", genomeComponent.genes, Integer.class);
produceItem.addComponent(foodComponent);
plantProduceComponent.produceItem = EntityRef.NULL;
target.saveComponent(plantProduceComponent);
if (!inventoryManager.giveItem(harvestingEntity, target, produceItem) && target.hasComponent(BlockComponent.class)) {
Expand All @@ -386,11 +400,7 @@ public void onHarvest(ActivateEvent event, EntityRef entity) {
public void onPlantProduceCreation(OnAddedComponent event, EntityRef entityRef, PlantProduceCreationComponent plantProduceCreationComponent) {
EntityRef newItem = entityManager.create(plantProduceCreationComponent.producePrefab);
PlantProduceComponent plantProduceComponent = new PlantProduceComponent(newItem);
if (entityRef.hasComponent(PlantProduceComponent.class)) {
entityRef.saveComponent(plantProduceComponent);
} else {
entityRef.addComponent(plantProduceComponent);
}
entityRef.addOrSaveComponent(plantProduceComponent);
}

@ReceiveEvent
Expand Down
Loading