I have a Steering class that has a pointer to a Pathfinder (nodes, goal target etc.).
Another class is the Character that have pointers to Input State and Input Axis.
I have tags for my actors (character, enemy etc.) and a scene that have queries like find all actors of type etc.
My question is: is better to put character/enemies behaviours explicity inside the Gameplay State/Objective class or should I go to the OOP side and increase the dependencies (put a pointer to InputState and Pathfuider in character). The OOP side doesn't make sense!
The "player" is just an actor with a energy bar and the enemy is just another actor with steering forces applied and etc.
Another problem is: the "player" can only jump if is on ground and now it has to be acess to the physics class.
My Gameplay State code:
void Gameplay::Update(Game* pGame, float dt)
{
Physics* pPhysics = pGame->pPhysics;
Scene* pScene = pGame->pScene;
pPhysics->Update(dt, pGame); //pGame is a abstract Event Listener
pScene->Update(dt); //manages actors
Core* pCore = pGame->pCore;
InputAxis* pInputAxis = pCore->GetInputAxis();
InputState* pInputState = pCore->GetInputState();
std::vector<Actor*> characters;
pScene->GetActorsOfType(MASK_CHARACTER, characters);
for (size_t i = 0; i < characters.size(); ++i)
{
RigidBody* pCharacterBody = characters[i]->GetRigidBody();
const glm::vec3& normal = pCharacterBody->orientationMatrix[2];
const float speed = 100.0f;
pCharacterBody->Rotate(-pInputAxis->y * dt, -pInputAxis->x * dt, 0.0f);
if (pInputState->isUp)
{
pCharacterBody->ApplyForce(normal * speed * dt);
}
if (pInputState->isDown)
{
pCharacterBody->ApplyForce(-normal * speed * dt);
}
std::vector<Actor*> enemies;
pScene->GetActorsOfType(MASK_ENEMY, enemies);
for (size_t j = 0; j < enemies.size(); ++j)
{
RigidBody* pEnemyBody = enemies[j]->GetRigidBody();
const glm::vec3& position = pEnemyBody->GetPosition();
const glm::vec3& target = pCharacterBody->GetPosition();
const glm::vec3& toTarget = target - position;
float distance = glm::length(toTarget);
const float maxDistance = 10.0f;
if (distance > 0.0f && distance < maxDistance)
{
glm::vec3 normal = glm::normalize(toTarget);
glm::vec3 force = normal * 10.0f;
glm::mat4 lookAt = glm::lookAt(position, target, glm::vec3(0.0f, 1.0f, 0.0f);
glm::mat3 orientationMatrix = glm::transpose(glm::mat3(lookAt));
glm::quat orientation = glm::toQuat(orientationMatrix);
pEnemyBody->ApplyForce(force * dt);
pEnemyBody->SetOrientation(orientation);
}
}
}
}