Components

Learn about using Components with the Kakara Engine.

Components are what give GameItems functionality. The engine provides several pre-made components to provide common functionality to your GameItems. We have already used two major components: Transform and MeshRenderer.

Add Components

Before we talk about the existing components and creating our own, we need to learn how to add them. Component can be added through the addComponent() method.

gameItem.addComponent(MyComponent.class);

That adds the MyComponent component to the GameItem. You cannot add multiple of a single type of component to a GameItem.

Removing Components

Components can be removed through the removeComponent() method.

gameItem.removeComponent(MyComponent.class);

A component will only be removed if it exists in the GameItem.

Getting Components

You can retrieve components through the getComponent() method.

MyComponent myComp = gameItem.getComponent(MyComponent.class);

If the specified component does not exist in the game item than the method will return null.

Transform Component

The transform component is the most common component and holds the position, rotation, and scale of the game item. The transform component is mandatory and all game items have it by default.

Unlike other components you cannot add or remove the transform component (An exception will be thrown if you try). The transform component also has a special public field so you don't have to use the getComponent() method.

So you can use

gameItem.transform.setPosition(0, 0, -10);

instead of:

gameItem.getComponent(Transform.class).setPosition(0, 0, -10);

Both will work.

Mesh Renderer Component

The mesh renderer component handles the rendering of a mesh for a GameItem. If you use the GameItem constructor that accepts a mesh or array of meshes, than this component is added automatically. The following code snippets are equivalent:

GameItem gameItem = new GameItem(cubeMesh);
GameItem gameItem = new GameItem();
MeshRenderer meshRenderer = gameItem.addComponent(MeshRenderer.class);
meshRenderer.setMesh(cubeMesh);

Since the MeshRenderer component is such a common component, it also has a direct way to reference it. You can use the getMeshRenderer() method to retrieve the MeshRenderer. It is important to note that the getMeshRenderer() method returns an Optional since a MeshRender is not required.

MeshRenderer meshRenderer = gameItem.getMeshRenderer().get();

The mesh render has special behavior when added and deleted. Due to the way the Standard Render Pipeline works, when a MeshRenderer is added or removed, the game item is temporary removed than added back to the list of game items in the scene.

Custom Components

Now that we know about components, let's add in functionality to make our cube rotate. To do that we need to make a new class for our component. Lets make a new class called RotateCube and have it extend the Component abstract class.

import org.kakara.engine.components.Component;

public class RotateCube extends Component {
    @Override
    public void start() {
        
    }

    @Override
    public void update() {

    }
}

The start method is called when the component is added to a game item and the update method is called every update. Components additional methods that you can override for additional functionality. We will not talk about any other methods for now.

Since we want our cube to rotate, we need to modify the transform component. The Component abstract class provided us with the getGameItem() method so we can easily get the game item that the component is on. So in the update method let's add the following:

getGameItem().transform.getRotation().rotateXYZ((float) Math.toRadians(5), 
    (float) Math.toRadians(5), 0);

The code above causes the cube to rate 5 degrees in the x and y direction every frame. Note: That code is dependent on your FPS.

Adding the Custom Component

Now that we made our custom component, it is time to add it to the cube. Go back to the MainScene class and add in the following below setting the position of the cube:

gameItem.addComponent(RotateCube.class);

Now if you run the code you should see a rotating cube:

Final Code

import org.kakara.engine.GameHandler;
import org.kakara.engine.engine.CubeData;
import org.kakara.engine.gameitems.GameItem;
import org.kakara.engine.gameitems.Material;
import org.kakara.engine.gameitems.mesh.Mesh;
import org.kakara.engine.scene.AbstractGameScene;

public class MainScene extends AbstractGameScene {
    public MainScene(GameHandler gameHandler) {
        super(gameHandler);
    }

    @Override
    public void work() {

    }

    @Override
    public void loadGraphics(GameHandler gameHandler) throws Exception {
        Mesh cubeMesh = new Mesh(CubeData.vertex, CubeData.texture, 
            CubeData.normal, CubeData.indices);
        Material cubeMaterial = new Material();
        cubeMesh.setMaterial(cubeMaterial);

        GameItem gameItem = new GameItem(cubeMesh);
        gameItem.transform.setPosition(0, 0, -10);
        gameItem.addComponent(RotateCube.class);

        add(gameItem);
    }

    @Override
    public void update(float v) {

    }
}

Last updated