Object Behavior
This outline describes the implementation of object behavior in the oglr
project. The goal is to provide developers with a comprehensive understanding of how different behaviors, such as movement, collision, and interaction, are implemented and managed within the framework.
Behavior Components
Object behavior is implemented using a component-based system. Each behavior is represented by a dedicated component that encapsulates the logic and data associated with that behavior. This allows for modularity and flexibility in defining and attaching behaviors to objects.
Example: Movement Component
The MovementComponent
class defines the logic for moving an object in the scene. It provides methods for setting the movement direction, speed, and other relevant parameters.
// Source: https://github.com/stevedunn/oglr/blob/main/src/oglr/components/MovementComponent.cpp
class MovementComponent : public Component {
public:
MovementComponent() : Component("Movement") {}
void SetDirection(glm::vec3 direction) {
m_direction = direction;
}
glm::vec3 GetDirection() const {
return m_direction;
}
void SetSpeed(float speed) {
m_speed = speed;
}
float GetSpeed() const {
return m_speed;
}
private:
glm::vec3 m_direction;
float m_speed;
};
Behavior Management
The BehaviorManager
class is responsible for managing all behaviors in the scene. It provides methods for adding, removing, and updating behaviors attached to objects.
Adding Behaviors:
// Source: https://github.com/stevedunn/oglr/blob/main/src/oglr/systems/BehaviorManager.cpp
void BehaviorManager::AddBehavior(GameObject* gameObject, Component* behavior) {
m_behaviors[gameObject].push_back(behavior);
}
Updating Behaviors:
The BehaviorManager
updates all active behaviors in the scene each frame. This involves calling the Update()
method of each component.
// Source: https://github.com/stevedunn/oglr/blob/main/src/oglr/systems/BehaviorManager.cpp
void BehaviorManager::Update(float deltaTime) {
for (auto& [gameObject, behaviors] : m_behaviors) {
for (auto& behavior : behaviors) {
behavior->Update(deltaTime);
}
}
}
Example Use Cases
Movement
The MovementComponent
can be used to define the movement behavior of an object.
// Source: https://github.com/stevedunn/oglr/blob/main/src/oglr/examples/BasicScene.cpp
// Create a new GameObject
GameObject* player = scene.CreateGameObject("Player");
// Add a MovementComponent
player->AddComponent(new MovementComponent());
// Set the movement direction and speed
player->GetComponent<MovementComponent>()->SetDirection(glm::vec3(1.0f, 0.0f, 0.0f));
player->GetComponent<MovementComponent>()->SetSpeed(5.0f);
Collision
The CollisionComponent
can be used to detect collisions between objects.
// Source: https://github.com/stevedunn/oglr/blob/main/src/oglr/components/CollisionComponent.cpp
class CollisionComponent : public Component {
public:
CollisionComponent() : Component("Collision") {}
// Method to check for collisions with other objects
bool CheckCollision(GameObject* other) {
// Collision detection logic goes here
return false;
}
};
Interaction
The InteractionComponent
can be used to define how objects interact with each other.
// Source: https://github.com/stevedunn/oglr/blob/main/src/oglr/components/InteractionComponent.cpp
class InteractionComponent : public Component {
public:
InteractionComponent() : Component("Interaction") {}
// Method to handle interactions with other objects
void Interact(GameObject* other) {
// Interaction logic goes here
}
};
Custom Behaviors
Developers can create their own custom behavior components to implement specific game logic. These components can be integrated into the BehaviorManager
and used in the same way as built-in behaviors.