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

Add water and terrain as child game object #265

Merged
merged 9 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
36 changes: 20 additions & 16 deletions commons/src/main/com/mbrlabs/mundus/commons/scene3d/GameObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,24 +144,12 @@ public void addTag(String tag) {
* output array
* @param type
* component type
* @param includeChilds
* search in node children of this game object as well?
* @param recursive
* recursive search?
* @return components found
*/
public <T extends Component> Array<T> findComponentsByType(Array<T> out, Component.Type type, boolean includeChilds) {
if (includeChilds) {
for (GameObject go : this) {
for (Component c : go.components) {
if (c.getType() == type) out.add((T) c);
}
}
} else {
for (Component c : components) {
if (c.getType() == type) out.add((T)c);
}
}

return out;
public <T extends Component> Array<T> findComponentsByType(Array<T> out, Component.Type type, boolean recursive) {
return findComponentsByType(out, this, type, recursive);
}

/**
Expand Down Expand Up @@ -391,4 +379,20 @@ private void updateChildrenScaleChanged(GameObject go) {
}
}

private <T extends Component> Array<T> findComponentsByType(Array<T> out, GameObject go, Component.Type type, boolean recursive) {
for (int i = 0; i < go.components.size; ++i) {
Component c = go.components.get(i);
if (c.getType() == type) out.add((T)c);
}

if (recursive && go.children != null) {
for (int i = 0; i < go.children.size; ++i) {
GameObject child = go.children.get(i);
findComponentsByType(out, child, type, true);
}
}

return out;
}

}
1 change: 1 addition & 0 deletions editor/CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- Optimize AssetUsage system by using DTO objects instead of loading scene
- Use BufferedInputStream for faster loading of terrains
- Fix activated/deactivated terrain at helper lines
- Fix add water and terrain as child

[0.5.1] ~ 08/08/2023
- Added FPS launcher argument, always call setForegroundFPS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.kotcrab.vis.ui.widget.VisTable
import com.kotcrab.vis.ui.widget.VisTextButton
import com.kotcrab.vis.ui.widget.VisTextField
import com.mbrlabs.mundus.commons.assets.TerrainAsset
import com.mbrlabs.mundus.commons.scene3d.GameObject
import com.mbrlabs.mundus.commons.scene3d.components.Component
import com.mbrlabs.mundus.commons.scene3d.components.TerrainComponent
import com.mbrlabs.mundus.commons.terrain.SplatMapResolution
Expand Down Expand Up @@ -66,6 +67,8 @@ class AddTerrainDialog : BaseDialog("Add Terrain") {
private var projectManager : ProjectManager
private var ioManager : IOManager

private var selectedGO : GameObject? = null

init {
isResizable = true
projectManager = Mundus.inject()
Expand All @@ -75,6 +78,11 @@ class AddTerrainDialog : BaseDialog("Add Terrain") {
setupListeners()
}

fun show(selectedGO: GameObject?) {
this.selectedGO = selectedGO
UI.showDialog(this)
}

private fun setDefaults() {
vertexResolution.text = Terrain.DEFAULT_VERTEX_RESOLUTION.toString()
terrainWidth.text = Terrain.DEFAULT_SIZE.toString()
Expand Down Expand Up @@ -180,7 +188,11 @@ class AddTerrainDialog : BaseDialog("Add Terrain") {

val terrainGO = createTerrainGO(sceneGraph, goID, terrainName, asset)
// update sceneGraph
sceneGraph.addGameObject(terrainGO)
if (selectedGO == null) {
sceneGraph.addGameObject(terrainGO)
} else {
sceneGraph.addGameObject(selectedGO, terrainGO)
}
terrainGO.setLocalPosition(posX, posY, posZ)

terrainComponent = terrainGO.findComponentByType(Component.Type.TERRAIN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.kotcrab.vis.ui.widget.VisTable
import com.kotcrab.vis.ui.widget.VisTextButton
import com.kotcrab.vis.ui.widget.VisTextField
import com.mbrlabs.mundus.commons.assets.WaterAsset
import com.mbrlabs.mundus.commons.scene3d.GameObject
import com.mbrlabs.mundus.commons.water.Water
import com.mbrlabs.mundus.editor.Mundus
import com.mbrlabs.mundus.editor.assets.AssetAlreadyExistsException
Expand All @@ -17,6 +18,7 @@ import com.mbrlabs.mundus.editor.core.io.IOManagerProvider
import com.mbrlabs.mundus.editor.core.project.ProjectManager
import com.mbrlabs.mundus.editor.events.AssetImportEvent
import com.mbrlabs.mundus.editor.events.SceneGraphChangedEvent
import com.mbrlabs.mundus.editor.ui.UI
import com.mbrlabs.mundus.editor.ui.widgets.FloatFieldWithLabel
import com.mbrlabs.mundus.editor.ui.widgets.IntegerFieldWithLabel
import com.mbrlabs.mundus.editor.ui.widgets.ToolTipLabel
Expand All @@ -40,6 +42,8 @@ class AddWaterDialog : BaseDialog("Add Water") {
private var projectManager : ProjectManager
private var ioManager : IOManager

private var selectedGO : GameObject? = null

init {
isResizable = true

Expand All @@ -51,6 +55,11 @@ class AddWaterDialog : BaseDialog("Add Water") {
setupListeners()
}

fun show(selectedGO: GameObject?) {
this.selectedGO = selectedGO
UI.showDialog(this)
}

private fun setupUI() {
val root = Table()
// root.debugAll();
Expand Down Expand Up @@ -123,7 +132,11 @@ class AddWaterDialog : BaseDialog("Add Water") {
val waterGO = createWaterGO(sceneGraph,
null, goID, waterName, asset)
// update sceneGraph
sceneGraph.addGameObject(waterGO)
if (selectedGO == null) {
sceneGraph.addGameObject(waterGO)
} else {
sceneGraph.addGameObject(selectedGO, waterGO)
}
waterGO.setLocalPosition(posX, posY, posZ)

Mundus.postEvent(SceneGraphChangedEvent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ class Outline : VisTable(),

if (containsWater) continue

val waterComponent: WaterComponent? = go.findComponentByType(Component.Type.WATER)
if (waterComponent != null) {
val waterComponents = go.findComponentsByType(Array(), Component.Type.WATER, true)
if (waterComponents.notEmpty()) {
containsWater = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,14 +377,14 @@ class OutlineRightClickMenu(outline: Outline) : PopupMenu() {
// add terrainAsset
addTerrain.addListener(object : ClickListener() {
override fun clicked(event: InputEvent?, x: Float, y: Float) {
UI.showDialog(UI.addTerrainDialog)
UI.addTerrainDialog.show(selectedGO)
}
})

// add waterAsset
addWater.addListener(object : ClickListener() {
override fun clicked(event: InputEvent?, x: Float, y: Float) {
UI.showDialog(UI.addWaterDialog)
UI.addWaterDialog.show(selectedGO)
}
})
}
Expand Down
2 changes: 2 additions & 0 deletions gdx-runtime/CHANGES
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[0.6.0] ~
- [Breaking Change] Spotlights now use GameObjects forward direction
- [Breaking Change] Rendering logic moved into SceneRenderer class
- [Breaking Change] findComponentsByType(Array, Component.Type, boolean) method in GameObject find in the current game object too and if the boolean is true then search recursively not just in children
- Made Scene.java more post-processing friendly for runtime users
- Add 'getRayIntersection(Vector3, Ray)' method to TerrainComponent
- Add rotate(yaw,pitch,roll) and setLocalRotation(yaw,pitch,roll) methods to game objects
- Add 'addGameObject(GameObject, ModelInstance, Vector3)' and 'addGameObject(GameObject, Model, Vector3)' methods to SceneGraph to add external model to scene graph where can add parent game object
- Generic findComponentsByType and findComponentByType methods
- Fix render water if it is child game object

[0.5.1] ~ 08/08/2023
- Updated libGDX to 1.12.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.utils.Array;
import com.mbrlabs.mundus.commons.Scene;
import com.mbrlabs.mundus.commons.assets.AssetManager;
import com.mbrlabs.mundus.commons.dto.GameObjectDTO;
Expand All @@ -26,7 +27,9 @@
import com.mbrlabs.mundus.commons.mapper.BaseLightConverter;
import com.mbrlabs.mundus.commons.mapper.DirectionalLightConverter;
import com.mbrlabs.mundus.commons.mapper.FogConverter;
import com.mbrlabs.mundus.commons.scene3d.GameObject;
import com.mbrlabs.mundus.commons.scene3d.SceneGraph;
import com.mbrlabs.mundus.commons.scene3d.components.Component;
import com.mbrlabs.mundus.commons.shadows.MundusDirectionalShadowLight;
import com.mbrlabs.mundus.commons.water.WaterResolution;
import com.mbrlabs.mundus.runtime.Shaders;
Expand Down Expand Up @@ -73,6 +76,7 @@ public static Scene convert(SceneDTO dto, Shaders shaders, AssetManager assetMan
scene.sceneGraph = new SceneGraph(scene);
for (GameObjectDTO descriptor : dto.getGameObjects()) {
scene.sceneGraph.addGameObject(GameObjectConverter.convert(descriptor, scene.sceneGraph, shaders, assetManager));
scene.sceneGraph.setContainsWater(containsWaterComponent(scene.sceneGraph.getRoot()));
}

// Set cam settings
Expand All @@ -85,4 +89,21 @@ public static Scene convert(SceneDTO dto, Shaders shaders, AssetManager assetMan

return scene;
}

/**
* Checks recursively that given game object contains water component or not.
*/
private static boolean containsWaterComponent(final GameObject go) {
final Array<GameObject> children = go.getChildren();
if (go.hasWaterComponent) {
return true;
} else if (children != null && children.notEmpty()) {
for (int i = 0; i < children.size; ++i) {
final boolean hasWaterComponent = containsWaterComponent(children.get(i));
if (hasWaterComponent) return true;
}
}

return false;
}
}