Creating an actor

This page will guide you through the process of creating an actor using the BBOC engine.

Actors are the main objects in the game world. They can be anything from a player character to a simple obstacle. Actors are composed of components that define their behavior and appearance.

Creating an Actor

To create an actor, you need to create a new class that inherits from the Actor class. The Actor class is an abstract class that provides the basic functionality for an actor.

Here is an example of a simple actor class:

class Player : public BBOC::Actor {
public:
    Player(BBOC::UID id = 0);

    void keyPressedEvent(const BBOC::NetworkInputKeyPressedEvent &event);
    void keyReleasedEvent(const BBOC::NetworkInputKeyReleasedEvent &event);

    void collisionEvent(const BBOC::CollisionEvent &event);

private:
    int _hp = 3;
};

In this example, we have created a new class called Player that inherits from the Actor class. We have also added private member variables _hp that will be used to store the player’s health points. We have also added methods to handle key pressed, key released, and collision events.

Implementing the Actor

To implement the actor, you need to define the keyPressedEvent(), keyReleasedEvent(), and collisionEvent() methods. These methods are called when the actor receives input or collides with another actor. When creating an actor, you should define these methods to handle the actor’s behavior and subscribe to the corresponding bus events.

Note

Refer to the Event bus page for more information on how to use the events in the Actor.

Here is an example of the keyPressedEvent() method:

void Player::keyPressedEvent(const BBOC::NetworkInputKeyPressedEvent &event) {
    if (event.key == BBOC::Key::Space) {
        // Shoot a bullet
    }
}

In this example, we have implemented the keyPressedEvent() method to check if the space key is pressed and shoot a bullet if it is.

Adding Components to the Actor

Actors are composed of components that define their behavior and appearance. To add a component to an actor, you need to create a new class that inherits from the Component class and add an instance of it to the actor.

Here is an example of adding a AnimatedDrawableComponent to the Player actor:

std::vector<BBOC::Rectangle2d> animation_basic;
std::vector<BBOC::Rectangle2d> animation_death;
std::vector<std::vector<BBOC::Rectangle2d>> animations;
BBOC::Point2d position(100, 0);
BBOC::Point2d size(33, 15);
BBOC::Point2d position2(68, 342);
BBOC::Point2d size2(34, 30);
for (int i = 0; i < 4; i++) {
    animation_basic.push_back(BBOC::Rectangle2d(position, size));
    position.x() += 33;
}
for (int i = 0; i < 12; i++) {
    animation_death.push_back(BBOC::Rectangle2d(position2, size2));
    position2.x() += 34;
}
animations.push_back(animation_basic);
animations.push_back(animation_death);

this->setFollowCamera(true);

this->addComponent<BBOC::AnimatedDrawableComponent>(200, animations, Assets::PLAYER_SPRITESHEET, true);

In this example, we have created two animations for the player actor: a basic animation and a death animation. We have added these animations to the Player actor using the AnimatedDrawableComponent.

Running the Actor

Actors are automatically updated by the engine every frame. You do not need to call any methods to update the actor.

Note

Refer to the Creating a Game page for more information on how to create a game using the BBOC engine.