From 8c5493f78e973fd3dc6227abc123d767c7baa953 Mon Sep 17 00:00:00 2001 From: OLEGSHA Date: Mon, 25 Jan 2021 21:35:46 +0300 Subject: [PATCH] Added GravityModels, removed gravity switch - Added GravityModel - can specify gravity varying by location and time - Added GravityModelRegistry - Stored in WorldData - Removed Minecraft gravity mode --- .../client/graphics/world/LayerWorld.java | 13 +++--- .../progressia/common/world/GravityModel.java | 41 +++++++++++++++++++ .../common/world/GravityModelRegistry.java | 30 ++++++++++++++ .../progressia/common/world/WorldData.java | 16 ++++++++ .../progressia/test/LayerTestGUI.java | 11 ----- .../windcorp/progressia/test/TestContent.java | 3 ++ .../progressia/test/TestPlayerControls.java | 18 +------- .../progressia/test/gen/TestGravityModel.java | 34 +++++++++++++++ .../resources/assets/languages/en-US.lang | 1 - .../resources/assets/languages/ru-RU.lang | 1 - 10 files changed, 133 insertions(+), 35 deletions(-) create mode 100644 src/main/java/ru/windcorp/progressia/common/world/GravityModel.java create mode 100644 src/main/java/ru/windcorp/progressia/common/world/GravityModelRegistry.java create mode 100644 src/main/java/ru/windcorp/progressia/test/gen/TestGravityModel.java diff --git a/src/main/java/ru/windcorp/progressia/client/graphics/world/LayerWorld.java b/src/main/java/ru/windcorp/progressia/client/graphics/world/LayerWorld.java index 317dfa2..d8ac99a 100644 --- a/src/main/java/ru/windcorp/progressia/client/graphics/world/LayerWorld.java +++ b/src/main/java/ru/windcorp/progressia/client/graphics/world/LayerWorld.java @@ -41,6 +41,7 @@ import ru.windcorp.progressia.common.Units; import ru.windcorp.progressia.common.collision.Collideable; import ru.windcorp.progressia.common.collision.colliders.Collider; import ru.windcorp.progressia.common.util.FloatMathUtil; +import ru.windcorp.progressia.common.util.Vectors; import ru.windcorp.progressia.common.world.entity.EntityData; import ru.windcorp.progressia.test.CollisionModelRenderer; import ru.windcorp.progressia.test.TestPlayerControls; @@ -197,16 +198,18 @@ public class LayerWorld extends Layer { entity.getVelocity().mul((float) Math.exp(-FRICTION_COEFF / entity.getCollisionMass() * tickLength)); } - private static final float MC_g = Units.get("32 m/s^2"); - private static final float IRL_g = Units.get("9.8 m/s^2"); - private void tmp_applyGravity(EntityData entity, float tickLength) { if (ClientState.getInstance().getLocalPlayer().getEntity() == entity && tmp_testControls.isFlying()) { return; } - final float gravitationalAcceleration = tmp_testControls.useMinecraftGravity() ? MC_g : IRL_g; - entity.getVelocity().add(0, 0, -gravitationalAcceleration * tickLength); + Vec3 gravitationalAcceleration = Vectors.grab3(); + ClientState.getInstance().getWorld().getData().getGravityModel().getGravity(gravitationalAcceleration); + + gravitationalAcceleration.mul(tickLength); + entity.getVelocity().add(gravitationalAcceleration); + + Vectors.release(gravitationalAcceleration); } @Override diff --git a/src/main/java/ru/windcorp/progressia/common/world/GravityModel.java b/src/main/java/ru/windcorp/progressia/common/world/GravityModel.java new file mode 100644 index 0000000..ce21649 --- /dev/null +++ b/src/main/java/ru/windcorp/progressia/common/world/GravityModel.java @@ -0,0 +1,41 @@ +/* + * 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 . + */ +package ru.windcorp.progressia.common.world; + +import glm.vec._3.Vec3; +import ru.windcorp.progressia.common.util.namespaces.Namespaced; + +public abstract class GravityModel extends Namespaced { + + public GravityModel(String id) { + super(id); + } + + public Vec3 getGravity(Vec3 output) { + if (output == null) { + output = new Vec3(); + } + + doGetGravity(output); + + return output; + } + + protected abstract void doGetGravity(Vec3 output); + +} diff --git a/src/main/java/ru/windcorp/progressia/common/world/GravityModelRegistry.java b/src/main/java/ru/windcorp/progressia/common/world/GravityModelRegistry.java new file mode 100644 index 0000000..b48cd0c --- /dev/null +++ b/src/main/java/ru/windcorp/progressia/common/world/GravityModelRegistry.java @@ -0,0 +1,30 @@ +/* + * 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 . + */ +package ru.windcorp.progressia.common.world; + +import ru.windcorp.progressia.common.util.namespaces.NamespacedInstanceRegistry; + +public class GravityModelRegistry extends NamespacedInstanceRegistry { + + public static final GravityModelRegistry INSTANCE = new GravityModelRegistry(); + + public static GravityModelRegistry getInstance() { + return INSTANCE; + } + +} diff --git a/src/main/java/ru/windcorp/progressia/common/world/WorldData.java b/src/main/java/ru/windcorp/progressia/common/world/WorldData.java index 85c6188..6806c1b 100644 --- a/src/main/java/ru/windcorp/progressia/common/world/WorldData.java +++ b/src/main/java/ru/windcorp/progressia/common/world/WorldData.java @@ -51,6 +51,8 @@ public class WorldData private final TLongObjectMap entitiesById = TCollections.synchronizedMap(new TLongObjectHashMap<>()); private final Collection entities = Collections.unmodifiableCollection(entitiesById.valueCollection()); + + private GravityModel gravityModel = GravityModelRegistry.getInstance().get("Test:TheGravityModel"); private float time = 0; @@ -200,6 +202,20 @@ public class WorldData return null; return block.getCollisionModel(); } + + /** + * @return the gravity model + */ + public GravityModel getGravityModel() { + return gravityModel; + } + + /** + * @param gravityModel the gravity model to set + */ + public void setGravityModel(GravityModel gravityModel) { + this.gravityModel = gravityModel; + } public Collection getListeners() { return listeners; diff --git a/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java b/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java index 30cca35..fa50625 100755 --- a/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java +++ b/src/main/java/ru/windcorp/progressia/test/LayerTestGUI.java @@ -91,17 +91,6 @@ public class LayerTestGUI extends GUILayer { ) ); - panel.addChild( - new Label( - "GravityModeDisplay", - font, - tmp_dynFormat( - "LayerTestGUI.GravityModeDisplay", - () -> tpc.useMinecraftGravity() ? "Minecraft" : "Realistic" - ) - ) - ); - panel.addChild( new Label( "LanguageDisplay", diff --git a/src/main/java/ru/windcorp/progressia/test/TestContent.java b/src/main/java/ru/windcorp/progressia/test/TestContent.java index 9fabd96..fe647f2 100644 --- a/src/main/java/ru/windcorp/progressia/test/TestContent.java +++ b/src/main/java/ru/windcorp/progressia/test/TestContent.java @@ -47,6 +47,7 @@ import ru.windcorp.progressia.common.collision.AABB; import ru.windcorp.progressia.common.collision.CollisionModel; import ru.windcorp.progressia.common.comms.controls.*; import ru.windcorp.progressia.common.state.StatefulObjectRegistry.Factory; +import ru.windcorp.progressia.common.world.GravityModelRegistry; import ru.windcorp.progressia.common.world.block.*; import ru.windcorp.progressia.common.world.entity.*; import ru.windcorp.progressia.common.world.io.ChunkIO; @@ -56,6 +57,7 @@ import ru.windcorp.progressia.server.comms.controls.*; import ru.windcorp.progressia.server.world.block.*; import ru.windcorp.progressia.server.world.entity.*; import ru.windcorp.progressia.server.world.tile.*; +import ru.windcorp.progressia.test.gen.TestGravityModel; public class TestContent { @@ -423,6 +425,7 @@ public class TestContent { private static void registerMisc() { ChunkIO.registerCodec(new TestChunkCodec()); ChunkRenderOptimizerRegistry.getInstance().register("Core:SurfaceOptimizer", ChunkRenderOptimizerSurface::new); + GravityModelRegistry.getInstance().register(new TestGravityModel()); } } diff --git a/src/main/java/ru/windcorp/progressia/test/TestPlayerControls.java b/src/main/java/ru/windcorp/progressia/test/TestPlayerControls.java index 710cbc1..0a0d3d3 100644 --- a/src/main/java/ru/windcorp/progressia/test/TestPlayerControls.java +++ b/src/main/java/ru/windcorp/progressia/test/TestPlayerControls.java @@ -83,7 +83,6 @@ public class TestPlayerControls { private double lastSprintPress = Double.NEGATIVE_INFINITY; private boolean captureMouse = true; - private boolean useMinecraftGravity = false; private int selectedBlock = 0; private int selectedTile = 0; @@ -199,12 +198,6 @@ public class TestPlayerControls { handleCameraMode(); break; - case GLFW.GLFW_KEY_G: - if (!event.isPress()) - return false; - handleGravitySwitch(); - break; - case GLFW.GLFW_KEY_L: if (!event.isPress()) return false; @@ -269,7 +262,7 @@ public class TestPlayerControls { return; } - getEntity().getVelocity().add(0, 0, JUMP_VELOCITY * (useMinecraftGravity ? 2 : 1)); + getEntity().getVelocity().add(0, 0, JUMP_VELOCITY); } private void handleShift(int multiplier) { @@ -313,11 +306,6 @@ public class TestPlayerControls { } } - private void handleGravitySwitch() { - useMinecraftGravity = !useMinecraftGravity; - updateGUI(); - } - private void handleLanguageSwitch() { Localizer localizer = Localizer.getInstance(); if (localizer.getLanguage().equals("ru-RU")) { @@ -427,10 +415,6 @@ public class TestPlayerControls { return captureMouse; } - public boolean useMinecraftGravity() { - return useMinecraftGravity; - } - public BlockData getSelectedBlock() { return TestContent.PLACEABLE_BLOCKS.get(selectedBlock); } diff --git a/src/main/java/ru/windcorp/progressia/test/gen/TestGravityModel.java b/src/main/java/ru/windcorp/progressia/test/gen/TestGravityModel.java new file mode 100644 index 0000000..0b666ce --- /dev/null +++ b/src/main/java/ru/windcorp/progressia/test/gen/TestGravityModel.java @@ -0,0 +1,34 @@ +/* + * 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 . + */ +package ru.windcorp.progressia.test.gen; + +import glm.vec._3.Vec3; +import ru.windcorp.progressia.common.world.GravityModel; + +public class TestGravityModel extends GravityModel { + + public TestGravityModel() { + super("Test:TheGravityModel"); + } + + @Override + protected void doGetGravity(Vec3 output) { + output.set(0, 0, -9.8); + } + +} diff --git a/src/main/resources/assets/languages/en-US.lang b/src/main/resources/assets/languages/en-US.lang index a47e216..62e2e6e 100644 --- a/src/main/resources/assets/languages/en-US.lang +++ b/src/main/resources/assets/languages/en-US.lang @@ -8,7 +8,6 @@ LayerTestGUI.IsFlyingDisplay = Flying: %5s (Space bar x2) LayerTestGUI.IsSprintingDisplay = Sprinting: %5s (W x2) LayerTestGUI.IsMouseCapturedDisplay = Mouse captured: %5s (Esc) LayerTestGUI.CameraModeDisplay = Camera mode: %5d (F5) -LayerTestGUI.GravityModeDisplay = Gravity: %9s (G) LayerTestGUI.LanguageDisplay = Language: %5s (L) LayerTestGUI.FPSDisplay = FPS: LayerTestGUI.TPSDisplay = TPS: diff --git a/src/main/resources/assets/languages/ru-RU.lang b/src/main/resources/assets/languages/ru-RU.lang index 180b25a..081a630 100644 --- a/src/main/resources/assets/languages/ru-RU.lang +++ b/src/main/resources/assets/languages/ru-RU.lang @@ -8,7 +8,6 @@ LayerTestGUI.IsFlyingDisplay = Полёт: %5s (Пробел x2) LayerTestGUI.IsSprintingDisplay = Бег: %5s (W x2) LayerTestGUI.IsMouseCapturedDisplay = Захват мыши: %5s (Esc) LayerTestGUI.CameraModeDisplay = Камера: %5d (F5) -LayerTestGUI.GravityModeDisplay = Гравитация: %9s (G) LayerTestGUI.LanguageDisplay = Язык: %5s (L) LayerTestGUI.FPSDisplay = FPS: LayerTestGUI.TPSDisplay = TPS: