Class GameObject

java.lang.Object
net.risingworld.api.worldelements.GameObject
Direct Known Subclasses:
Area3D, Light, Model, Prefab, Text3D

public class GameObject extends Object
"Empty" base class for all custom elements. You can still use this class to set up parent/child hierarchies, and you can also assign colliders to it (which may act as invisible barriers, interaction areas or triggers).
Example: Spawn GameObject and a child
1//Example to create a game object
2GameObject gameObject = new GameObject();
3
4//Assign a box collider (size: 2x2x2) to it
5gameObject.setCollider(new BoxCollider(2f, 2f, 2f));
6
7//Set position of the game object
8gameObject.setLocalPosition(2000f, 90f, -2000f);
9
10//Register to player (so it shows up for this player)
11player.addGameObject(gameObject);
12
13
14//Now we want to create a child object
15GameObject child = new GameObject();
16
17//The position is always relative to the parent
18child.setLocalPosition(0f, 5f, 0f);
19
20//Add child to parent object. NOTE: No need to add this object to
21//the player as well, because this is handled automatically
22gameObject.addChild(child);
  • Constructor Details

    • GameObject

      public GameObject()
    • GameObject

      public GameObject(int layer)
  • Method Details

    • getID

      public int getID()
      Gets the unique ID of this object. Note that this ID is only valid during the session.
      Returns:
      the unique ID of this object.
    • isDisposed

      public boolean isDisposed()
      Gets whether or not this game object is disposed.
      Returns:
      true if this game object is disposed, false if not.
    • setActive

      public void setActive(boolean set)
      Toggles the active state of this object. Setting this to false will disable the object in the game scene, i.e it is no longer visible and no longer collides with the player or the world. This automatically affects all child elements as well.
      By default, an object is set to active.
      Parameters:
      set - true to set the object active, false to disable it.
    • isActive

      public boolean isActive()
      Gets whether or not this object is currently active. Does not check the hierarchy recursively! If the parent of this element is not active, but childs are active, they are treated as non-active elements by the game, but this method would still return true.
      Returns:
      true if this element is set to active, false if not.
    • setLayer

      public void setLayer(int layer)
      Sets the Layer for this game object.
      Parameters:
      layer - the layer you want to set.
      See Also:
    • getLayer

      public int getLayer()
      Gets the layer that is assigned to this game object.
      Returns:
      the layer.
      See Also:
    • setLocalPosition

      public void setLocalPosition(Vector3f position)
      Sets the position of this object, relative to its parent. If this object has no parent, this is the world position.
      Parameters:
      position - a vector which holds the new position coordinates.
    • setLocalPosition

      public void setLocalPosition(float x, float y, float z)
      Sets the position of this object, relative to its parent. If this object has no parent, this is the world position.
      Parameters:
      x - the x position (horizontally).
      y - the y position (vertically).
      z - the z position (horizontally).
    • getLocalPosition

      public Vector3f getLocalPosition()
      Gets the local position of this object, relative to its parent. If this object has no parent, this is the world position.
      Returns:
      the local position, represented as a Vector3f.
    • setLocalRotation

      public void setLocalRotation(Quaternion rotation)
      Sets the rotation of this object, relative to its parent. If this object has no parent, this is the world rotation.
      Parameters:
      rotation - the quaternion which represents the new rotation.
    • setLocalRotation

      public void setLocalRotation(float pitch, float yaw, float roll)
      Sets the rotation of this object, relative to its parent. If this object has no parent, this is the world rotation.
      Parameters:
      pitch - the pitch (rotation around X axis).
      yaw - the yaw (rotation around Y axis, i.e heading).
      roll -
    • getLocalRotation

      public Quaternion getLocalRotation()
      Gets the rotation of this object, relative to its parent. If this object has no parent, this is the world rotation.
      Returns:
      a quaternion which represents the current rotation.
    • setLocalScale

      public void setLocalScale(Vector3f scale)
      Sets the local scale of this object, relative to its parent. If this object has no parent, this is the world scale.
      Parameters:
      scale - the new scale along the X, Y and Z axis.
    • setLocalScale

      public void setLocalScale(float x, float y, float z)
      Sets the local scale of this object, relative to its parent. If this object has no parent, this is the world scale.
      Parameters:
      x - the scale along the X axis.
      y - the scale along the Y axis.
      z - the scale along the Z axis.
    • getLocalScale

      public Vector3f getLocalScale()
      Gets the current local scale of this game object.
      Returns:
      a Vector3f representing the local scale of the object (along the X, Y and Z axis).
    • moveToLocalPosition

      public void moveToLocalPosition(Vector3f position, float speed)
      Smoothly moves this object to a target local position.
      Parameters:
      position - the new target position.
      speed - the speed at which the object should move (units/blocks per second, e.g when setting it to 10, the object will move 10 units/blocks per second etc).
      Example: Move game object 20 blocks upwards within 5 seconds
      1Prefab prefab = new Prefab(PrefabAsset.loadFromAssetbundle(bundle, "Test.prefab"));
      2prefab.setLocalPosition(player.getPosition());
      3player.addGameObject(prefab);
      4
      5//New target position (20 blocks upwards)
      6Vector3f targetPosition = prefab.getLocalPosition().add(0f, 20f, 0f);
      7
      8//Move to target. It should take 5 seconds ("20 * 5" is the speed factor then)
      9prefab.moveToLocalPosition(targetPosition, 20 * 5);
    • moveToLocalPosition

      public void moveToLocalPosition(float x, float y, float z, float speed)
      Smoothly moves this object to a target local position.
      Parameters:
      x - the target x position (horizontally).
      y - the target y position (vertically).
      z - the target z position (horizontally).
      speed - the speed at which the object should move (units/blocks per second, e.g when setting it to 10, the object will move 10 units/blocks per second etc).
    • moveToLocalPosition

      public void moveToLocalPosition(Vector3f position, float speed, Callback<Consumer<Vector3f>> callback)
      Smoothly moves this object to a target local position.
      Parameters:
      position - the new target position.
      speed - the speed at which the object should move (units/blocks per second, e.g when setting it to 10, the object will move 10 units/blocks per second etc).
      callback - optional callback that gets invoked every tick (while the object is moving). This enables you to provide a new position through the Consumer object (see Consumer.accept(java.lang.Object) method).
    • moveToLocalPosition

      public void moveToLocalPosition(float x, float y, float z, float speed, Callback<Consumer<Vector3f>> callback)
      Smoothly moves this object to a target local position.
      Parameters:
      x - the target x position (horizontally).
      y - the target y position (vertically).
      z - the target z position (horizontally).
      speed - the speed at which the object should move (units/blocks per second, e.g when setting it to 10, the object will move 10 units/blocks per second etc).
      callback - optional callback that gets invoked every tick (while the object is moving). This enables you to provide a new position through the Consumer object (see Consumer.accept(java.lang.Object) method).
    • rotateToLocalRotation

      public void rotateToLocalRotation(Quaternion rotation, float speed)
      Smoothly rotates this object to a target local rotation.
      Parameters:
      rotation - the new target rotation.
      speed - the speed at which the object should rotate.
    • moveToLocalTransform

      public void moveToLocalTransform(Vector3f position, Quaternion rotation, float speed)
      Smoothly moves and rotates this object to a target local position and rotation.
      Parameters:
      position - the new target position.
      rotation - the new target rotation.
      speed - the speed at which the object should move and rotate.
    • setCollider

      public void setCollider(Collider collider)
      Assigns a collider to this game object.
      Parameters:
      collider - the new collider. Set to null to remove a collider.
      Example: Add a box collider with size 2x2x2 to a game object
      1//Create a new game object
      2GameObject go = new GameObject();
      3
      4//Assign new box collider (size: 2x2x2)
      5go.setCollider(new BoxCollider(2f, 2f, 2f));
      6
      7//Set position of game object
      8go.setLocalPosition(2000f, 90f, -2000f);
      9
      10//Register to player
      11player.addGameObject(go);
    • getCollider

      public Collider getCollider()
      Gets the collider that was assigned via setCollider(net.risingworld.api.collider.Collider). Returns null if no collider was assigned.
      Returns:
      the collider assigned to this game object, or null if no collider has been assigned yet.
    • setColliderVisible

      public void setColliderVisible(boolean set)
      A debug method to visualize the collider of this game object. Only works if a collider is assigned.
      Parameters:
      set - true to show a debug visualization of this game object, false to hide it.
    • isColliderVisible

      public boolean isColliderVisible()
    • addComponent

      public void addComponent(String component)
    • removeComponent

      public void removeComponent(String component)
    • setComponentEnabled

      public void setComponentEnabled(String component, boolean enabled)
    • setComponentProperty

      public void setComponentProperty(String component, String property, Object value)
      Assigns a value to any component property via reflection.
      Parameters:
      component - the name/type of the component (e.g "Animator" if you want to access an animator component).
      property - the name of the property you want to change.
      value - the new value you want to assign.
    • invokeComponentMethod

      public void invokeComponentMethod(String component, String method, Object... parameters)
    • addChild

      public void addChild(GameObject child)
    • removeChild

      public void removeChild(GameObject child)
      Removes a child from this object. It will also be removed from the player world.
      Parameters:
      child - the child object you want to remove.
    • removeAllChilds

      public void removeAllChilds()
      Removes all children from this element. They will also be detached from the player UI.
    • getParent

      public GameObject getParent()
      Gets the current parent UI element, or null if this element has no parent.
      Returns:
      the parent element, or null if no parent is set.
    • removeFromParent

      public void removeFromParent()
      Removes this object from its parent. Same as calling removeChild() on the parent object.
    • getChilds

      public List<GameObject> getChilds()
      Gets an unmodifiable list of all children of this object.
      Returns:
      a new, unmodifiable list containing all child objects.
    • getChildCount

      public int getChildCount()
      Gets the number of attached child objects on this object.
      Returns:
      the amount of children this object has. 0 if this object has no child objects.