Skip to content

Entity class

Pegacraffft edited this page Feb 16, 2021 · 10 revisions

Entity class

This class is the ground structure for creating an entity/object for a scene. Another class has to extend it.

Examples:

//implementation of the entity class.
pulbic class Player extends Entity {
    @Override
    public void init() {
    }

    @Override
    public void logicLoop() {
    }

    @Override
    public void renderLoop() {
    }
}

Every class that extends this will be called entity in this documentation.

Use cases:

One of the most important use cases is creating an entity, that can be added to a scene.

The Player class from above could be added to a scene like in the following example:

Example:

//The scene class
public class GameScene extends Scene {
    
    @Override
    public void init() {
        //initialisation of the player class.
        Player player = new Player();
        //assignment of the player entity to the scene.
        addObject(player);
    }

    @Override
    public void logicLoop() {
    }

    @Override
    public void renderLoop() {
    }
}

1. init()

This method is executed once, when the entity is initialized.

2. logicLoop()

This method gets executed 60 times per second (if you didn't change it with Game.setFrameRate()). It is most commonly used for the logic of the entity.

3. renderLoop()

This method gets executed 60 times per second (if you didn't change it with Game.setFrameRate()). It is most commonly used for the graphics and design of the entity.

Every entity can store other entities. If you want to learn more about the commands used for these actions, click here

Every entity has an x and y position, a width and a height, a rotation and a rotationPos. None of these variables have to assigned in the constructor. If you dont set them, they will be 0 or null. All of these variables are public, so they can be changed directly.

X and Y variables:

These variables set the position of your entity in the scene.

Width and height variables

These variables set the dimensions of your entity.

The rotation:

This is the rotation of the entity in radiants. The engine will automaticly rotate all graphics in the renderLoop to fit the set rotation.

The RotationPos:

This variable is the point, where the the graphics should be rotated around.

Methods:

rotateTo(Point p):

This method is used to rotate the object, so it faces the given Point p.

moveTo(Point p, int speed):

This method moves the entity to the given point in the set speed. (I recommend using values over 3 because with lower values, the result would be greatly impacted)

move(int x, int y)

This method is used to move the entity.

isAnchored(boolean b)

This method anchores the x and y coordinates to the position of the camera. While this is set to true, you wont be able to modify these values. If you want to offset the position of the entity, use the setAnchorX(int offsetX) and the setAnchorY(int offsetY) or the setAnchor(int offsetX, int offsetY) method.