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

feat(JOML): migrate BlockPlacingWeatherSystem #18

Merged
merged 4 commits into from
Dec 31, 2020
Merged
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
@@ -1,21 +1,12 @@
/*
* Copyright 2018 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.
*/
// Copyright 2020 The Terasology Foundation
// SPDX-License-Identifier: Apache-2.0

package org.terasology.weatherManager.systems;

import org.joml.RoundingMode;
import org.joml.Vector3f;
import org.joml.Vector3i;
import org.joml.Vector3ic;
import org.terasology.context.Context;
import org.terasology.entitySystem.entity.EntityRef;
import org.terasology.entitySystem.event.ReceiveEvent;
Expand All @@ -24,15 +15,14 @@
import org.terasology.entitySystem.systems.RegisterSystem;
import org.terasology.logic.delay.PeriodicActionTriggeredEvent;
import org.terasology.logic.location.LocationComponent;
import org.terasology.math.geom.Vector3i;
import org.terasology.network.Client;
import org.terasology.network.NetworkSystem;
import org.terasology.registry.In;
import org.terasology.utilities.random.FastRandom;
import org.terasology.world.WorldProvider;
import org.terasology.world.block.Block;
import org.terasology.world.block.BlockManager;
import org.terasology.world.chunks.ChunkConstants;
import org.terasology.world.chunks.Chunks;

import static org.terasology.weatherManager.systems.WeatherManagerSystem.EVAPORATE_WATER;
import static org.terasology.weatherManager.systems.WeatherManagerSystem.MELT_SNOW;
Expand Down Expand Up @@ -67,47 +57,51 @@ public void postBegin() {
}

/**
* Places snow blocks on the ground when it is snowing.
* The event with the id PLACE_SNOW will only be created when it is snowing
* and the other events also correspond with the appropriate weather condition.
* Places snow blocks on the ground when it is snowing. The event with the id PLACE_SNOW will only be created when
* it is snowing and the other events also correspond with the appropriate weather condition.
*
* @param event The event that means it is time to place snow
* @param worldEntity The entity that sent the event (assumed to be the player)
*/
@ReceiveEvent
public void onPlaceEvent(PeriodicActionTriggeredEvent event, EntityRef worldEntity) {
if (event.getActionId().equals(PLACE_SNOW)) {
for(Client currentPlayer : networkSystem.getPlayers()) {
LocationComponent locComp = currentPlayer.getEntity().getComponent(LocationComponent.class);
Vector3i playerPos = new Vector3i(locComp.getWorldPosition());

placeSnow(playerPos);
}
} else if (event.getActionId().equals(MELT_SNOW)) {
for(Client currentPlayer : networkSystem.getPlayers()) {
LocationComponent locComp = currentPlayer.getEntity().getComponent(LocationComponent.class);
Vector3i playerPos = new Vector3i(locComp.getWorldPosition());

meltSnow(playerPos);
}
} else if (event.getActionId().equals(PLACE_WATER)) {
for(Client currentPlayer : networkSystem.getPlayers()) {
LocationComponent locComp = currentPlayer.getEntity().getComponent(LocationComponent.class);
Vector3i playerPos = new Vector3i(locComp.getWorldPosition());

placeWater(playerPos);
}
} else if (event.getActionId().equals(EVAPORATE_WATER)) {
for(Client currentPlayer : networkSystem.getPlayers()) {
LocationComponent locComp = currentPlayer.getEntity().getComponent(LocationComponent.class);
Vector3i playerPos = new Vector3i(locComp.getWorldPosition());

evaporateWater(playerPos);
}
final Vector3f position = new Vector3f();
final Vector3i playerPos = new Vector3i();
switch (event.getActionId()) {
case PLACE_SNOW:
for (Client currentPlayer : networkSystem.getPlayers()) {
LocationComponent locComp = currentPlayer.getEntity().getComponent(LocationComponent.class);
playerPos.set(locComp.getWorldPosition(position), RoundingMode.FLOOR);
placeSnow(playerPos);
}
break;
case MELT_SNOW:
for (Client currentPlayer : networkSystem.getPlayers()) {
LocationComponent locComp = currentPlayer.getEntity().getComponent(LocationComponent.class);
playerPos.set(locComp.getWorldPosition(position), RoundingMode.FLOOR);
meltSnow(playerPos);
}
break;
case PLACE_WATER:
for (Client currentPlayer : networkSystem.getPlayers()) {
LocationComponent locComp = currentPlayer.getEntity().getComponent(LocationComponent.class);
playerPos.set(locComp.getWorldPosition(position), RoundingMode.FLOOR);
placeWater(playerPos);
}
break;
case EVAPORATE_WATER:
for (Client currentPlayer : networkSystem.getPlayers()) {
LocationComponent locComp = currentPlayer.getEntity().getComponent(LocationComponent.class);
playerPos.set(locComp.getWorldPosition(position), RoundingMode.FLOOR);
evaporateWater(playerPos);
}
break;
}
}

/**
* Finds a spot to place a block.
*
* @param toCheck the block type that we should be looking for.
* @param x the x position that the blocks
* @return a vector with the height where the block should be placed, null if no block should be placed.
Expand All @@ -127,51 +121,51 @@ private Vector3i findSpot(Block toCheck, int x, int z, int initialY) {
return null;
}

if (worldProvider.getSunlight(x, currentY, z) != ChunkConstants.MAX_SUNLIGHT) {
if (worldProvider.getSunlight(x, currentY, z) != Chunks.MAX_SUNLIGHT) {
// The block isn't actually exposed to the weather.
return null;
}
Block ground = worldProvider.getBlock(x, currentY-1, z);
Block ground = worldProvider.getBlock(x, currentY - 1, z);
if (ground.equals(toCheck)) {
return new Vector3i(x, currentY-1, z);
return new Vector3i(x, currentY - 1, z);
} else if (toCheck.equals(air) && !ground.isPenetrable() && ground.isAttachmentAllowed()) {
return new Vector3i(x, currentY, z);
} else {
return null;
}
}

private void placeSnow(Vector3i playerPos) {
int x = getValueToPlaceBlock(playerPos.x);
int z = getValueToPlaceBlock(playerPos.z);
Vector3i spotToPlace = findSpot(air, x, z, playerPos.y);
private void placeSnow(Vector3ic playerPos) {
int x = getValueToPlaceBlock(playerPos.x());
int z = getValueToPlaceBlock(playerPos.z());
Vector3i spotToPlace = findSpot(air, x, z, playerPos.y());
if (spotToPlace != null) {
worldProvider.setBlock(spotToPlace, snow);
}
}

private void meltSnow(Vector3i playerPos) {
int x = getValueToPlaceBlock(playerPos.x);
int z = getValueToPlaceBlock(playerPos.z);
Vector3i spotToPlace = findSpot(snow, x, z, playerPos.y);
private void meltSnow(Vector3ic playerPos) {
int x = getValueToPlaceBlock(playerPos.x());
int z = getValueToPlaceBlock(playerPos.z());
Vector3i spotToPlace = findSpot(snow, x, z, playerPos.y());
if (spotToPlace != null) {
worldProvider.setBlock(spotToPlace, water);
}
}

private void placeWater(Vector3i playerPos) {
int x = getValueToPlaceBlock(playerPos.x);
int z = getValueToPlaceBlock(playerPos.z);
Vector3i spotToPlace = findSpot(air, x, z, playerPos.y);
private void placeWater(Vector3ic playerPos) {
int x = getValueToPlaceBlock(playerPos.x());
int z = getValueToPlaceBlock(playerPos.z());
Vector3i spotToPlace = findSpot(air, x, z, playerPos.y());
if (spotToPlace != null) {
worldProvider.setBlock(spotToPlace, water);
}
}

private void evaporateWater(Vector3i playerPos) {
int x = getValueToPlaceBlock(playerPos.x);
int z = getValueToPlaceBlock(playerPos.z);
Vector3i spotToPlace = findSpot(water, x, z, playerPos.y);
private void evaporateWater(Vector3ic playerPos) {
int x = getValueToPlaceBlock(playerPos.x());
int z = getValueToPlaceBlock(playerPos.z());
Vector3i spotToPlace = findSpot(water, x, z, playerPos.y());
if (spotToPlace != null) {
worldProvider.setBlock(spotToPlace, air);
}
Expand Down