-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed connection issues with new block families
- Loading branch information
Showing
2 changed files
with
203 additions
and
75 deletions.
There are no files selected for viewing
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
179 changes: 179 additions & 0 deletions
179
src/main/java/org/terasology/minecarts/blocks/RailsBlockFamilyUpdateSystem.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,179 @@ | ||
/* | ||
* Copyright 2015 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.minecarts.blocks; | ||
|
||
import com.google.common.collect.Sets; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.terasology.entitySystem.entity.EntityRef; | ||
import org.terasology.entitySystem.event.EventPriority; | ||
import org.terasology.entitySystem.event.ReceiveEvent; | ||
import org.terasology.entitySystem.systems.BaseComponentSystem; | ||
import org.terasology.entitySystem.systems.RegisterMode; | ||
import org.terasology.entitySystem.systems.RegisterSystem; | ||
import org.terasology.entitySystem.systems.UpdateSubscriberSystem; | ||
import org.terasology.logic.common.ActivateEvent; | ||
import org.terasology.logic.health.DoDamageEvent; | ||
import org.terasology.logic.health.DoDestroyEvent; | ||
import org.terasology.logic.health.EngineDamageTypes; | ||
import org.terasology.logic.inventory.ItemComponent; | ||
import org.terasology.math.Side; | ||
import org.terasology.math.SideBitFlag; | ||
import org.terasology.math.geom.Vector3i; | ||
import org.terasology.registry.In; | ||
import org.terasology.world.BlockEntityRegistry; | ||
import org.terasology.world.OnChangedBlock; | ||
import org.terasology.world.WorldProvider; | ||
import org.terasology.world.block.Block; | ||
import org.terasology.world.block.BlockComponent; | ||
import org.terasology.world.block.BlockManager; | ||
import org.terasology.world.block.entity.neighbourUpdate.LargeBlockUpdateFinished; | ||
import org.terasology.world.block.entity.neighbourUpdate.LargeBlockUpdateStarting; | ||
import org.terasology.world.block.items.BlockItemComponent; | ||
import org.terasology.world.block.items.OnBlockItemPlaced; | ||
|
||
import java.util.Set; | ||
|
||
@RegisterSystem(RegisterMode.AUTHORITY) | ||
public class RailsBlockFamilyUpdateSystem extends BaseComponentSystem implements UpdateSubscriberSystem { | ||
private static final Logger logger = LoggerFactory.getLogger(RailsBlockFamilyUpdateSystem.class); | ||
|
||
@In | ||
private WorldProvider worldProvider; | ||
@In | ||
private BlockEntityRegistry blockEntityRegistry; | ||
@In | ||
private BlockManager blockManager; | ||
|
||
private int largeBlockUpdateCount; | ||
private Set<Vector3i> blocksUpdatedInLargeBlockUpdate = Sets.newHashSet(); | ||
private int[] checkOnHeight = {-1, 0, 1}; | ||
|
||
@ReceiveEvent | ||
public void largeBlockUpdateStarting(LargeBlockUpdateStarting event, EntityRef entity) { | ||
largeBlockUpdateCount++; | ||
} | ||
|
||
@ReceiveEvent | ||
public void largeBlockUpdateFinished(LargeBlockUpdateFinished event, EntityRef entity) { | ||
largeBlockUpdateCount--; | ||
if (largeBlockUpdateCount < 0) { | ||
largeBlockUpdateCount = 0; | ||
throw new IllegalStateException("LargeBlockUpdateFinished invoked too many times"); | ||
} | ||
|
||
if (largeBlockUpdateCount == 0) { | ||
notifyNeighboursOfChangedBlocks(); | ||
} | ||
} | ||
|
||
@ReceiveEvent() | ||
public void doDestroy(DoDestroyEvent event, EntityRef entity, BlockComponent blockComponent) { | ||
Vector3i upBlock = new Vector3i(blockComponent.getPosition()); | ||
upBlock.y += 1; | ||
Block block = worldProvider.getBlock(upBlock); | ||
|
||
if (block.getBlockFamily() instanceof RailBlockFamily) { | ||
blockEntityRegistry.getEntityAt(upBlock).send(new DoDamageEvent(1000, EngineDamageTypes.DIRECT.get())); | ||
} | ||
} | ||
|
||
//prevents rails from being stacked on top of each other. | ||
@ReceiveEvent(components = {BlockItemComponent.class, ItemComponent.class}, priority = EventPriority.PRIORITY_HIGH) | ||
public void onBlockActivated(ActivateEvent event, EntityRef item) { | ||
BlockComponent blockComponent = event.getTarget().getComponent(BlockComponent.class); | ||
if (blockComponent == null) | ||
return; | ||
|
||
Vector3i targetBlock = blockComponent.getPosition(); | ||
Block centerBlock = worldProvider.getBlock(targetBlock.x, targetBlock.y, targetBlock.z); | ||
|
||
if (centerBlock.getBlockFamily() instanceof RailBlockFamily) { | ||
event.consume(); | ||
} | ||
} | ||
|
||
@ReceiveEvent(components = {BlockItemComponent.class, ItemComponent.class}) | ||
public void onPlaceBlock(OnBlockItemPlaced event, EntityRef entity) { | ||
BlockComponent blockComponent = event.getPlacedBlock().getComponent(BlockComponent.class); | ||
if (blockComponent == null) | ||
return; | ||
|
||
Vector3i targetBlock = blockComponent.getPosition(); | ||
Block centerBlock = worldProvider.getBlock(targetBlock.x, targetBlock.y, targetBlock.z); | ||
|
||
if (centerBlock.getBlockFamily() instanceof RailBlockFamily) { | ||
processUpdateForBlockLocation(targetBlock); | ||
} | ||
|
||
} | ||
|
||
private void notifyNeighboursOfChangedBlocks() { | ||
// Invoke the updates in another large block change for this class only | ||
largeBlockUpdateCount++; | ||
while (!blocksUpdatedInLargeBlockUpdate.isEmpty()) { | ||
Set<Vector3i> blocksToUpdate = blocksUpdatedInLargeBlockUpdate; | ||
|
||
// Setup new collection for blocks changed in this pass | ||
blocksUpdatedInLargeBlockUpdate = Sets.newHashSet(); | ||
|
||
for (Vector3i blockLocation : blocksToUpdate) { | ||
processUpdateForBlockLocation(blockLocation); | ||
} | ||
} | ||
largeBlockUpdateCount--; | ||
} | ||
|
||
@ReceiveEvent(components = {BlockComponent.class}) | ||
public void blockUpdate(OnChangedBlock event, EntityRef blockEntity) { | ||
if (largeBlockUpdateCount > 0) { | ||
blocksUpdatedInLargeBlockUpdate.add(event.getBlockPosition()); | ||
} else { | ||
Vector3i blockLocation = event.getBlockPosition(); | ||
processUpdateForBlockLocation(blockLocation); | ||
} | ||
} | ||
|
||
private void processUpdateForBlockLocation(Vector3i blockLocation) { | ||
for (int height : checkOnHeight) { | ||
for (Side side : Side.horizontalSides()) { | ||
Vector3i neighborLocation = new Vector3i(blockLocation); | ||
neighborLocation.add(side.getVector3i()); | ||
neighborLocation.y += height; | ||
Block neighborBlock = worldProvider.getBlock(neighborLocation); | ||
EntityRef blockEntity = blockEntityRegistry.getBlockEntityAt(neighborLocation); | ||
if (blockEntity.hasComponent(RailComponent.class)) { | ||
RailBlockFamily railsFamily = (RailBlockFamily) neighborBlock.getBlockFamily(); | ||
Block neighborBlockAfterUpdate = railsFamily.getBlockForPlacement(neighborLocation, null,null); | ||
if (neighborBlock != neighborBlockAfterUpdate && neighborBlockAfterUpdate != null) { | ||
byte connections = Byte.parseByte(neighborBlock.getURI().getIdentifier().toString()); | ||
//only add segment with two connections | ||
if (SideBitFlag.getSides(connections).size() <= 1) | ||
worldProvider.setBlock(neighborLocation, neighborBlockAfterUpdate); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void update(float delta) { | ||
if (largeBlockUpdateCount > 0) { | ||
logger.error("Unmatched LargeBlockUpdateStarted - LargeBlockUpdateFinished not invoked enough times"); | ||
} | ||
largeBlockUpdateCount = 0; | ||
} | ||
} |