Creating a Game¶
How to create a game using the BBOC engine.
Creating a Game¶
To create a game, you need to create a new class that inherits from the Game class. The Game class is an abstract class that provides the basic functionality for a game.
Here is an example of a simple game class:
class RType : public BBOC::Game {
public:
RType();
void initialize() override;
void updateState(double delta) override;
void end() override;
private:
std::weak_ptr<BBOC::World> _worldOne;
double _delta{0.};
};
In this example, we have created a new class called RType that inherits from the Game class. We have also added a private member variable _delta that will be used to store the delta time between frames. We have also added a private member variable _worldOne that will be used to store the world of the game.
Implementing the Game¶
To implement the game, you need to define the initialize(), updateState(), and end() methods. These methods are called at different stages of the game’s lifecycle.
Here is an example of the initialize() method:
void RType::initialize() {
{
auto &actorFactory = BBOC::engine->getActorFactory();
actorFactory.registerActor<Player>();
}
this->setActiveWorld(this->_worldOne);
#if defined(BBOC_SERVER)
{
_worldOne.lock()->addActor<Player>();
}
#endif
}
In this example, we have implemented the initialize() method to register the Player actor with the actor factory and set the active world to the _worldOne.
Use the network in the game¶
To use the network in the game, you need to subscribe to the BBOC::NetworkClientConnectedEvent event. This event is called whenever a new client connects to the server.
Here is an example of how to subscribe to the BBOC::NetworkClientConnectedEvent event:
auto netSystem = BBOC::engine->getSubsystem<BBOC::NetworkSubsystem>();
netEventBus.subscribe<BBOC::NetworkClientConnectedEvent>(this, &RType::createPlayer);
In this example, we have subscribed to the BBOC::NetworkClientConnectedEvent event and called the createPlayer() method whenever a new client connects to the server.
Note
Refer to the Event bus page for more information on how to use the events in the game.
Running the Game¶
You need to add this game to the current running game on the engine. To do this, you need to call the setGame() method on the engine.
Here is an example of how to add the game to the engine:
BBOC::engine->setGame<RType::RType>();
BBOC::engine->run(argc, argv);