Linked GravityModel to a WorldGenerator and added GM comms transfer
- WorldData no longer acquires a GravityModel automatically - On the server, GravityModel is specified by WorldGenerator - On the client, GravityModel is received from the server via a PacketSetGravityModel
This commit is contained in:
parent
d3c5011063
commit
d438d2aa14
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Progressia
|
||||
* Copyright (C) 2020-2021 Wind Corporation and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package ru.windcorp.progressia.common.world;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
|
||||
public class PacketSetGravityModel extends PacketAffectWorld {
|
||||
|
||||
private String gravityModelId;
|
||||
|
||||
public PacketSetGravityModel() {
|
||||
this("Core:SetGravityModel");
|
||||
}
|
||||
|
||||
protected PacketSetGravityModel(String id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
public void set(GravityModel model) {
|
||||
this.gravityModelId = model.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(DataInput input) throws IOException, DecodingException {
|
||||
gravityModelId = input.readUTF();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(DataOutput output) throws IOException {
|
||||
output.writeUTF(gravityModelId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void apply(WorldData world) {
|
||||
GravityModel model = GravityModelRegistry.getInstance().get(gravityModelId);
|
||||
world.setGravityModel(model);
|
||||
}
|
||||
|
||||
}
|
@ -52,7 +52,7 @@ public class WorldData
|
||||
|
||||
private final Collection<EntityData> entities = Collections.unmodifiableCollection(entitiesById.valueCollection());
|
||||
|
||||
private GravityModel gravityModel = GravityModelRegistry.getInstance().get("Test:TheGravityModel");
|
||||
private GravityModel gravityModel = null;
|
||||
|
||||
private float time = 0;
|
||||
|
||||
@ -214,6 +214,13 @@ public class WorldData
|
||||
* @param gravityModel the gravity model to set
|
||||
*/
|
||||
public void setGravityModel(GravityModel gravityModel) {
|
||||
if (!chunks.isEmpty()) {
|
||||
throw new IllegalStateException(
|
||||
"Attempted to change gravity model to " + gravityModel + " while " + chunks.size()
|
||||
+ " chunks were loaded"
|
||||
);
|
||||
}
|
||||
|
||||
this.gravityModel = gravityModel;
|
||||
}
|
||||
|
||||
|
@ -28,6 +28,7 @@ import gnu.trove.map.TIntObjectMap;
|
||||
import gnu.trove.map.hash.TIntObjectHashMap;
|
||||
import ru.windcorp.progressia.common.comms.CommsChannel.State;
|
||||
import ru.windcorp.progressia.common.comms.packets.Packet;
|
||||
import ru.windcorp.progressia.common.world.PacketSetGravityModel;
|
||||
import ru.windcorp.progressia.common.world.PacketSetLocalPlayer;
|
||||
import ru.windcorp.progressia.common.world.entity.EntityData;
|
||||
import ru.windcorp.progressia.server.Player;
|
||||
@ -74,6 +75,10 @@ public class ClientManager {
|
||||
private void addClientPlayer(ClientPlayer client) {
|
||||
String login = client.getLogin();
|
||||
|
||||
PacketSetGravityModel setGravityModelPacket = new PacketSetGravityModel();
|
||||
setGravityModelPacket.set(getServer().getWorld().getData().getGravityModel());
|
||||
client.sendPacket(setGravityModelPacket);
|
||||
|
||||
EntityData entity = getServer().getPlayerManager().conjurePlayerEntity(login);
|
||||
Player player = new Player(entity, getServer(), client);
|
||||
getServer().getPlayerManager().getPlayers().add(player);
|
||||
|
@ -55,7 +55,9 @@ public class WorldLogic
|
||||
public WorldLogic(WorldData data, Server server, Function<WorldLogic, WorldGenerator> worldGeneratorConstructor) {
|
||||
this.data = data;
|
||||
this.server = server;
|
||||
|
||||
this.generator = worldGeneratorConstructor.apply(this);
|
||||
data.setGravityModel(getGenerator().getGravityModel());
|
||||
|
||||
data.addListener(new WorldDataListener() {
|
||||
@Override
|
||||
|
@ -25,14 +25,23 @@ import java.util.Objects;
|
||||
|
||||
import ru.windcorp.progressia.common.world.ChunkData;
|
||||
import ru.windcorp.progressia.common.world.DecodingException;
|
||||
import ru.windcorp.progressia.common.world.GravityModel;
|
||||
import ru.windcorp.progressia.common.world.GravityModelRegistry;
|
||||
|
||||
public abstract class AbstractWorldGenerator<H> extends WorldGenerator {
|
||||
|
||||
private final Class<H> hintClass;
|
||||
|
||||
public AbstractWorldGenerator(String id, Class<H> hintClass) {
|
||||
private final GravityModel gravityModel;
|
||||
|
||||
public AbstractWorldGenerator(String id, Class<H> hintClass, String gravityModelId) {
|
||||
super(id);
|
||||
this.hintClass = Objects.requireNonNull(hintClass, "hintClass");
|
||||
this.gravityModel = GravityModelRegistry.getInstance().get(Objects.requireNonNull(gravityModelId, "gravityModelId"));
|
||||
|
||||
if (this.gravityModel == null) {
|
||||
throw new IllegalArgumentException("Gravity model with ID \"" + gravityModelId + "\" not found");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -64,4 +73,9 @@ public abstract class AbstractWorldGenerator<H> extends WorldGenerator {
|
||||
chunk.setGenerationHint(hint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GravityModel getGravityModel() {
|
||||
return gravityModel;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ import glm.vec._3.i.Vec3i;
|
||||
import ru.windcorp.progressia.common.util.namespaces.Namespaced;
|
||||
import ru.windcorp.progressia.common.world.ChunkData;
|
||||
import ru.windcorp.progressia.common.world.DecodingException;
|
||||
import ru.windcorp.progressia.common.world.GravityModel;
|
||||
import ru.windcorp.progressia.common.world.WorldData;
|
||||
|
||||
public abstract class WorldGenerator extends Namespaced {
|
||||
@ -43,4 +44,6 @@ public abstract class WorldGenerator extends Namespaced {
|
||||
|
||||
public abstract boolean isChunkReady(Object hint);
|
||||
|
||||
public abstract GravityModel getGravityModel();
|
||||
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ public class TestWorldGenerator extends AbstractWorldGenerator<Boolean> {
|
||||
private final TestTerrainGenerator terrainGen;
|
||||
|
||||
public TestWorldGenerator(WorldLogic world) {
|
||||
super("Test:WorldGenerator", Boolean.class);
|
||||
super("Test:WorldGenerator", Boolean.class, "Test:TheGravityModel");
|
||||
this.terrainGen = new TestTerrainGenerator(this, world);
|
||||
|
||||
world.getData().addListener(new WorldDataListener() {
|
||||
|
Reference in New Issue
Block a user