Skip to content

Commit

Permalink
feat: expose settlement events (#31)
Browse files Browse the repository at this point in the history
* expose set and buffer block events
* expose request raster target event
* create new vector references in block event constructors
* don't expose internal vector references
  • Loading branch information
kaen authored Apr 8, 2020
1 parent 450693d commit ba5145b
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@
import org.terasology.dynamicCities.buildings.components.ProductionChestComponent;
import org.terasology.dynamicCities.buildings.components.SettlementRefComponent;
import org.terasology.dynamicCities.buildings.events.OnSpawnDynamicStructureEvent;
import org.terasology.dynamicCities.construction.events.BufferBlockEvent;
import org.terasology.dynamicCities.construction.events.BuildingEntitySpawnedEvent;
import org.terasology.dynamicCities.construction.events.RequestRasterTargetEvent;
import org.terasology.dynamicCities.construction.events.SetBlockEvent;
import org.terasology.dynamicCities.construction.events.SpawnStructureBufferedEvent;
import org.terasology.dynamicCities.decoration.ColumnRasterizer;
import org.terasology.dynamicCities.decoration.DecorationRasterizer;
Expand Down Expand Up @@ -68,6 +71,7 @@
import org.terasology.dynamicCities.rasterizer.window.RectWindowRasterizer;
import org.terasology.dynamicCities.rasterizer.window.SimpleWindowRasterizer;
import org.terasology.dynamicCities.rasterizer.window.WindowRasterizer;
import org.terasology.dynamicCities.settlements.events.CheckBuildingForParcelEvent;
import org.terasology.dynamicCities.roads.RoadSegment;
import org.terasology.economy.components.MultiInvStorageComponent;
import org.terasology.entitySystem.entity.EntityManager;
Expand All @@ -92,6 +96,7 @@
import org.terasology.registry.CoreRegistry;
import org.terasology.registry.In;
import org.terasology.registry.Share;
import org.terasology.structureTemplates.components.CheckBlockRegionConditionComponent;
import org.terasology.structureTemplates.components.SpawnBlockRegionsComponent;
import org.terasology.structureTemplates.interfaces.StructureTemplateProvider;
import org.terasology.structureTemplates.util.BlockRegionTransform;
Expand Down Expand Up @@ -293,13 +298,13 @@ public int flatten(Rect2i area, int defaultHeight, Block filler) {
if (y <= meanHeight) {
for (int i = y; i <= meanHeight; i++) {
setPos.set(pos.x(), i, pos.y());
worldProvider.setBlock(setPos, filler);
worldProvider.getWorldEntity().send(new SetBlockEvent(setPos, filler));
}
}
if (y >= meanHeight) {
for (int i = y; i > meanHeight; i--) {
setPos.set(pos.x(), i, pos.y());
worldProvider.setBlock(setPos, air);
worldProvider.getWorldEntity().send(new SetBlockEvent(setPos, air));
}
}
}
Expand Down Expand Up @@ -340,25 +345,36 @@ public SurfaceHeightFacet sample(Rect2i area, int height) {
return surfaceHeightFacet;
}

/**
* Gets a random building according to {@link BuildingManager#getRandomBuildingOfZoneForCulture(String, Rect2i, CultureComponent)}
*
* @param event
* @param settlement
*/
@ReceiveEvent
public void checkBuildingForParcel(CheckBuildingForParcelEvent event, EntityRef settlement, CultureComponent
cultureComponent) {
String zone = event.dynParcel.getZone();
Rect2i shape = event.dynParcel.getShape();
event.building = buildingManager.getRandomBuildingOfZoneForCulture(zone, shape, cultureComponent);
}

//the standard strategy used in Cities and StaticCities module
public boolean buildParcel(DynParcel dynParcel, EntityRef settlement, CultureComponent cultureComponent) {
public boolean buildParcel(DynParcel dynParcel, EntityRef settlement) {
Region3i region = Region3i.createFromMinMax(new Vector3i(dynParcel.getShape().minX(), 255, dynParcel.getShape().minY()),
new Vector3i(dynParcel.getShape().maxX(), -255, dynParcel.getShape().maxY()));
if (!worldProvider.isRegionRelevant(region)) {
return false;
}
/**
* get the building or shrink parcel size if no fitting building was found.
*/
Optional<GenericBuildingComponent> buildingOptional;
GenericBuildingComponent building;
buildingOptional = buildingManager.getRandomBuildingOfZoneForCulture(dynParcel.getZone(), dynParcel.getShape(), cultureComponent);

if (!buildingOptional.isPresent()) {
CheckBuildingForParcelEvent event = new CheckBuildingForParcelEvent(dynParcel);
settlement.send(event);

if (!event.building.isPresent()) {
return false;
} else {
building = buildingOptional.get();
}

GenericBuildingComponent building = event.building.get();
if (building.isScaledDown) {
Vector2i difference = dynParcel.shape.size().sub(building.minSize).div(2);
dynParcel.shape = Rect2i.createFromMinAndMax(dynParcel.shape.min().add(difference), dynParcel.shape.max().sub(difference));
Expand All @@ -369,7 +385,9 @@ public boolean buildParcel(DynParcel dynParcel, EntityRef settlement, CultureCom
//Flatten the parcel area
dynParcel.height = flatten(dynParcel.shape, dynParcel.height);

RasterTarget rasterTarget = new BufferRasterTarget(blockBufferSystem, cityTheme, dynParcel.shape);
RequestRasterTargetEvent requestRasterTargetEvent = new RequestRasterTargetEvent(cityTheme, dynParcel.shape);
settlement.send(requestRasterTargetEvent);
RasterTarget rasterTarget = requestRasterTargetEvent.rasterTarget;
Rect2i shape = dynParcel.shape;
HeightMap hm = HeightMaps.constant(dynParcel.height);

Expand Down Expand Up @@ -647,15 +665,28 @@ public void onSpawnBlockRegions(SpawnStructureBufferedEvent event, EntityRef ent
block = transformation.transformBlock(block);
if (block.getBlockFamily() == blockManager.getBlockFamily("CoreBlocks:chest")) {
for (Vector3i pos : region) {
worldProvider.setBlock(pos, block);
entity.send(new SetBlockEvent(pos, block));
}
} else {
for (Vector3i pos : region) {
blockBufferSystem.saveBlock(pos, block);
entity.send(new BufferBlockEvent(pos, block));
}
}
}
}

@ReceiveEvent
public void onSetBlockEvent(SetBlockEvent event, EntityRef entity) {
worldProvider.setBlock(event.getPos(), event.block);
}

@ReceiveEvent
public void onBufferBlockEvent(BufferBlockEvent event, EntityRef entity) {
blockBufferSystem.saveBlock(event.getPos(), event.block);
}

@ReceiveEvent
public void onRequestRasterTargetEvent(RequestRasterTargetEvent event, EntityRef entity) {
event.rasterTarget = new BufferRasterTarget(blockBufferSystem, event.theme, event.shape);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.dynamicCities.construction.events;

import org.terasology.entitySystem.event.ConsumableEvent;
import org.terasology.math.geom.Vector3i;
import org.terasology.world.block.Block;

/**
* Sent when a building has been rasterized and a given block should be buffered
*/
public class BufferBlockEvent implements ConsumableEvent {
public final Block block;
private Vector3i pos = Vector3i.zero();
private boolean consumed;

public BufferBlockEvent(Vector3i pos, Block block) {
this.pos = new Vector3i(pos);
this.block = block;
}

@Override
public boolean isConsumed() {
return consumed;
}

@Override
public void consume() {
consumed = true;
}

public Vector3i getPos() {
return new Vector3i(pos);
}

public void setPos(Vector3i pos) {
this.pos.set(pos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.dynamicCities.construction.events;

import org.terasology.cities.BlockTheme;
import org.terasology.cities.raster.RasterTarget;
import org.terasology.entitySystem.event.ConsumableEvent;
import org.terasology.math.geom.Rect2i;

/**
* Emitted when DynamicCities needs a {@link org.terasology.cities.raster.RasterTarget} for rasterizing buildings
*/
public class RequestRasterTargetEvent implements ConsumableEvent {
public RasterTarget rasterTarget;
public BlockTheme theme;
public Rect2i shape;
private boolean consumed;

public RequestRasterTargetEvent(BlockTheme theme, Rect2i shape) {
this.shape = shape;
this.theme = theme;
}

@Override
public boolean isConsumed() {
return consumed;
}

@Override
public void consume() {
consumed = true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.dynamicCities.construction.events;

import org.terasology.entitySystem.event.ConsumableEvent;
import org.terasology.math.geom.Vector3i;
import org.terasology.world.block.Block;

/**
* Sent when a building has been rasterized and a given block should be set directly
*/
public class SetBlockEvent implements ConsumableEvent {
private boolean consumed;
public Block block;
private Vector3i pos;

public SetBlockEvent(Vector3i pos, Block block) {
this.pos = new Vector3i(pos);
this.block = block;
}

@Override
public boolean isConsumed() {
return consumed;
}

@Override
public void consume() {
consumed = true;
}

public Vector3i getPos() {
return new Vector3i(pos);
}

public void setPos(Vector3i pos) {
this.pos.set(pos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public DistrictOverlay(EntityRef entityRef) {
@Override
public void render(Canvas canvas, Rect2f worldRect) {
/**
* Iterate through all known cities in the settlement manager and check if the boundaries of the screenRect are within city reach.
* Iterate through all known cities in the settlement manager and check if the boundaries of the screenRect are
* within city reach.
* Color rectangles of the district grid in a zone specific color.
*
*/
Expand Down
Loading

0 comments on commit ba5145b

Please sign in to comment.