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

Update settlements and regions to use sector-scope entities #28

Closed
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import org.terasology.dynamicCities.region.components.UnregisteredRegionComponent;
import org.terasology.dynamicCities.sites.SiteFacet;
import org.terasology.dynamicCities.world.trees.TreeFacet;
import org.terasology.entitySystem.entity.EntityStore;
import org.terasology.entitySystem.entity.EntityBuilder;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.logic.location.LocationComponent;
import org.terasology.math.Region3i;
import org.terasology.math.geom.Vector2i;
Expand All @@ -36,6 +37,8 @@
import org.terasology.world.generation.facets.SurfaceHeightFacet;
import org.terasology.world.generation.facets.base.BaseFieldFacet2D;

import static org.terasology.entitySystem.entity.internal.EntityScope.SECTOR;

/**
* Add an entity for each region to serve as storage for relevant data
* At worldgen create for each region one
Expand All @@ -45,6 +48,11 @@

public class RegionEntityProvider implements EntityProvider {

private EntityManager entityManager;

public RegionEntityProvider(EntityManager entityManager) {
this.entityManager = entityManager;
}

@Override
public void process(Region region, EntityBuffer buffer) {
Expand All @@ -58,27 +66,28 @@ public void process(Region region, EntityBuffer buffer) {
TreeFacet treeFacet = region.getFacet(TreeFacet.class);
SiteFacet siteFacet = region.getFacet(SiteFacet.class);

EntityStore entityStore = new EntityStore();
EntityBuilder builder = entityManager.newBuilder();
builder.setScope(SECTOR);

RoughnessFacetComponent roughnessFacetComponent = new RoughnessFacetComponent(roughnessFacet);
ResourceFacetComponent resourceFacetComponent = new ResourceFacetComponent(resourceFacet);
TreeFacetComponent treeFacetComponent = new TreeFacetComponent(treeFacet);
entityStore.addComponent(roughnessFacetComponent);
entityStore.addComponent(resourceFacetComponent);
entityStore.addComponent(treeFacetComponent);
builder.addComponent(roughnessFacetComponent);
builder.addComponent(resourceFacetComponent);
builder.addComponent(treeFacetComponent);

LocationComponent locationComponent = new LocationComponent(worldRegion.center());
entityStore.addComponent(locationComponent);
builder.addComponent(locationComponent);


if (siteFacet.getSiteComponent() != null) {
entityStore.addComponent(siteFacet.getSiteComponent());
builder.addComponent(siteFacet.getSiteComponent());
}

//Region component is used as identifier for a region entity
entityStore.addComponent(new UnregisteredRegionComponent());
entityStore.addComponent(new NetworkComponent());
buffer.enqueue(entityStore);
builder.addComponent(new UnregisteredRegionComponent());
builder.addComponent(new NetworkComponent());
buffer.enqueue(builder);

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
package org.terasology.dynamicCities.settlements;


import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.terasology.commonworld.Orientation;
Expand Down Expand Up @@ -54,6 +48,9 @@
import org.terasology.economy.events.SubscriberRegistrationEvent;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.event.ReceiveEvent;
import org.terasology.entitySystem.sectors.LoadedSectorUpdateEvent;
import org.terasology.entitySystem.sectors.SectorSimulationEvent;
import org.terasology.entitySystem.systems.BaseComponentSystem;
import org.terasology.entitySystem.systems.RegisterMode;
import org.terasology.entitySystem.systems.RegisterSystem;
Expand All @@ -76,6 +73,13 @@
import org.terasology.utilities.random.Random;
import org.terasology.world.generation.Border3D;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;


@Share(value = SettlementEntityManager.class)
@RegisterSystem(RegisterMode.AUTHORITY)
Expand Down Expand Up @@ -135,31 +139,31 @@ public void update(float delta) {
} else if (settlementCachingSystem.isInitialised() && settlementEntities == null) {
settlementEntities = settlementCachingSystem.getSettlementCacheEntity();
}
}

counter--;
timer++;
if (counter != 0) {
return;
}
Iterable<EntityRef> uncheckedSiteRegions = entityManager.getEntitiesWith(SiteComponent.class);
for (EntityRef siteRegion : uncheckedSiteRegions) {
boolean checkDistance = checkMinDistance(siteRegion);
boolean checkBuildArea = checkBuildArea(siteRegion);
if (checkDistance && regionEntityManager.checkSidesLoadedNear(siteRegion)
&& checkBuildArea) {
EntityRef newSettlement = createSettlement(siteRegion);
newSettlement.send(new SettlementRegisterEvent());
siteRegion.removeComponent(SiteComponent.class);
} else if (!checkDistance || !checkBuildArea) {
siteRegion.removeComponent(SiteComponent.class);
}
@ReceiveEvent(components = SiteComponent.class)
public void createSettlements(SectorSimulationEvent event, EntityRef siteRegion) {
boolean checkDistance = checkMinDistance(siteRegion);
boolean checkBuildArea = checkBuildArea(siteRegion);
if (checkDistance && regionEntityManager.checkSidesLoadedNear(siteRegion)
&& checkBuildArea) {
EntityRef newSettlement = createSettlement(siteRegion);
newSettlement.send(new SettlementRegisterEvent());
siteRegion.removeComponent(SiteComponent.class);
} else if (!checkDistance || !checkBuildArea) {
siteRegion.removeComponent(SiteComponent.class);
}
Iterable<EntityRef> activeSettlements = entityManager.getEntitiesWith(BuildingQueue.class);
for (EntityRef settlement : activeSettlements) {
growSettlement(settlement);
build(settlement);
}
counter = 250;
}

@ReceiveEvent(components = BuildingQueue.class)
public void simulateSettlement(SectorSimulationEvent event, EntityRef settlement) {
//Temp, need to use delta
growSettlement(settlement);
}

@ReceiveEvent(components = BuildingQueue.class)
public void buildSettlement(LoadedSectorUpdateEvent event, EntityRef settlement) {
build(settlement);
}


Expand Down Expand Up @@ -196,7 +200,7 @@ public boolean checkMinDistanceCell(String posString) {
}

private EntityRef createSettlement(EntityRef siteRegion) {
EntityRef settlementEntity = entityManager.create();
EntityRef settlementEntity = entityManager.createSectorEntity(1);

SiteComponent siteComponent = siteRegion.getComponent(SiteComponent.class);
LocationComponent locationComponent = siteRegion.getComponent(LocationComponent.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.terasology.dynamicCities.sites.SiteFacetProvider;
import org.terasology.dynamicCities.world.trees.DefaultTreeProvider;
import org.terasology.engine.SimpleUri;
import org.terasology.entitySystem.entity.EntityManager;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.logic.spawner.FixedSpawner;
import org.terasology.math.geom.ImmutableVector2i;
Expand All @@ -52,6 +53,9 @@ public class PerlinFacetedWorldGenerator extends BaseFacetedWorldGenerator {
@In
private WorldGeneratorPluginLibrary worldGeneratorPluginLibrary;

@In
private EntityManager entityManager;

public PerlinFacetedWorldGenerator(SimpleUri uri) {
super(uri);
}
Expand Down Expand Up @@ -83,7 +87,7 @@ protected WorldBuilder createWorld() {
.addProvider(new DefaultTreeProvider())
//.addProvider(new PlateauProvider(spawnPos, seaLevel + 4, 10, 30))
.addProvider(new ResourceProvider())
.addEntities(new RegionEntityProvider())
.addEntities(new RegionEntityProvider(entityManager))
.addRasterizer(new SolidRasterizer())
.addPlugins()
.addRasterizer(new FloraRasterizer())
Expand Down