Commit c864ba35 authored by Pierre TESSIER's avatar Pierre TESSIER

Added AI waves, but bugs remain

parent 34f42689
......@@ -5,9 +5,10 @@
struct AIPlayer
{
AIPlayer(ecs::Entity target_) : target { target_ }{};
AIPlayer(ecs::Entity target_, size_t idx) : target{ target_ }, hierarchyIdx{ idx }{};
ecs::Entity target;
size_t hierarchyIdx;
};
using AIPlayerComponent = ecs::Component<AIPlayer>;
......
......@@ -88,10 +88,12 @@ void scene_model::buildEntities(std::map<std::string,GLuint>& shaders)
vec3 playerPosition = m_resources.terrainGeometry.evaluate(0.5, 0.5) + vec3 { 0, 0, 5 };
m_player = builder.buildPlayer(playerPosition, shaders);
vec3 AIPosition = m_resources.terrainGeometry.evaluate(0.4, 0.6) + vec3{ 0, 0, 5 };
vec3 AIPosition = m_resources.terrainGeometry.evaluate(0.8, 0.9) + vec3{ 0, 0, 5 };
builder.buildAICharacter(m_player, AIPosition, shaders);
AIPosition = m_resources.terrainGeometry.evaluate(0.2, 0.3) + vec3{ 0, 0, 5 };
AIPosition = m_resources.terrainGeometry.evaluate(0.2, 0.1) + vec3{ 0, 0, 5 };
builder.buildAICharacter(m_player, AIPosition, shaders);
m_nbAI = 2;
m_waveSize = 2;
m_aimCursor = m_ecs.createEntity();
m_ecs.add<TransformComponent>(m_aimCursor, vec3(0,0,2));
......@@ -150,8 +152,14 @@ void scene_model::logicLoop(std::map<std::string,GLuint>& shaders, scene_structu
m_ecs.update<HierarchyPositionUpdater::Transform2Collision>();
m_ecs.update<LockdownSystem>();
m_ecs.update<HealthSystem>(m_ecs);
m_ecs.getSystem<HealthSystem>().destroyDeadCharacter(m_ecs);
m_ecs.getSystem<HealthSystem>().destroyDeadCharacter(m_ecs, m_resources, m_score, m_nbAI);
std::cout << "score = " << m_score << " , nbAI = " << m_nbAI << " , wave_size = " << m_waveSize << std::endl;
if (m_nbAI == 0)
{
m_waveSize++;
createAI(shaders, m_waveSize);
}
}
void scene_model::renderLoop(std::map<std::string,GLuint>& shaders, scene_structure& scene)
......@@ -323,6 +331,21 @@ void scene_model::setupCollisionRates()
completeWithSymetricals(m_resources.collisionRates);
}
void scene_model::createAI(std::map<std::string, GLuint>& shaders, const int nbAI)
{
EntityBuilder builder{ m_ecs, m_resources };
std::mt19937 gen(m_rd());
std::uniform_real_distribution<float> dis(0.0f, 1.0f);
for (int i = 0; i < nbAI; i++)
{
vec3 position = m_resources.terrainGeometry.evaluate(dis(gen), dis(gen)) + vec3(0,0,3);
builder.buildAICharacter(m_player, position, shaders);
}
m_nbAI = nbAI;
}
void scene_model::setupCamera(scene_structure& scene)
{
scene.camera.camera_type = camera_control_spherical_coordinates;
......
......@@ -68,12 +68,17 @@ private:
void setupPlayerAnimation();
void setupCollisionRates();
void setupAI();
void createAI(std::map<std::string, GLuint>& shaders, const int nbAI);
private:
MyEcs m_ecs;
CommonSceneResources m_resources;
std::random_device m_rd;
int m_score;
int m_nbAI;
int m_waveSize;
bool m_gameOver = false;
Skybox m_skybox;
......
......@@ -45,7 +45,7 @@ struct CommonSceneResources
KeyboardMapping mouseMapping;
CharacterDescription standardCharacter;
Pool<CompletedMeshHierarchy> AIHierarchyPool{ 5 };
Pool<CompletedMeshHierarchy> AIHierarchyPool{ 20 };
};
#endif // !OTHERS_COMMON_SCENE_RESOURCES_HPP_INCLUDED
\ No newline at end of file
......@@ -95,9 +95,10 @@ ecs::Entity EntityBuilder::buildPlayer(vcl::vec3 const& position, std::map<std::
ecs::Entity EntityBuilder::buildAICharacter(ecs::Entity const target, vcl::vec3 const& position, std::map<std::string,GLuint>& shaders)
{
auto& hierarchy = m_resources.AIHierarchyPool.getFreeElement().first;
auto& hierarchyPair = m_resources.AIHierarchyPool.getFreeElement();
auto& hierarchy = hierarchyPair.first;
ecs::Entity ai = buildCharacter(m_resources.standardCharacter, hierarchy, position, shaders);
m_ecs.add<CharacterDesignComponent>(ai, 3.0f, 1.0f);
m_ecs.add<AIPlayerComponent>(ai, target);
m_ecs.add<AIPlayerComponent>(ai, target, hierarchyPair.second);
return ai;
}
......@@ -2,7 +2,7 @@
#define OTHERS_POOL_HPP_INCLUDED
#include <vector>
#include <stack>
#include <deque>
#include <algorithm>
template<typename T, typename Container = std::vector<T>>
......@@ -19,7 +19,7 @@ public:
for(size_type i = 0; i < numElems; ++i)
{
m_elements.emplace_back(std::forward<Args>(args)...);
m_freeElems.push(i);
m_freeElems.push_back(i);
}
}
......@@ -39,15 +39,15 @@ public:
std::pair<T&, Handle> getFreeElement()
{
assert(hasFreeElements() && "No free elements left");
auto idx = m_freeElems.top();
m_freeElems.pop();
auto idx = m_freeElems.back();
m_freeElems.pop_back();
return { m_elements[idx], idx };
}
void freeElement(Handle h)
{
assert(!isFree(h) && "Element already free");
m_freeElems.push(h);
m_freeElems.push_back(h);
}
size_t size() { return m_elements.size(); }
......@@ -60,7 +60,7 @@ private:
private:
Container m_elements;
std::stack<size_type> m_freeElems;
std::deque<size_type> m_freeElems;
};
#endif // !OTHERS_POOL_HPP_INCLUDED
\ No newline at end of file
......@@ -8,16 +8,21 @@ void HealthSystem::update(MyEcs& myEcs, RayComponent& ray)
auto& target = myEcs.get<CharacterDesignComponent>(*ray.endEntity);
target.currentHealth -= ray.damage;
ray.damage = 0;
std::cout << "Ray endEntityHealth = " << target.currentHealth << std::endl;
if (target.currentHealth <= 0)
m_deadCharacters.push_back(*ray.endEntity);
}
}
void HealthSystem::destroyDeadCharacter(MyEcs& myEcs)
void HealthSystem::destroyDeadCharacter(MyEcs& myEcs, CommonSceneResources& ressources, int& score, int& nbAI)
{
for (auto entity = m_deadCharacters.begin(); entity != m_deadCharacters.end(); entity++)
{
if (myEcs.has<AIPlayerComponent>(*entity))
{
score++;
nbAI--;
ressources.AIHierarchyPool.freeElement(myEcs.get<AIPlayerComponent>(*entity).hierarchyIdx);
}
myEcs.destroy(*entity);
}
......
......@@ -3,6 +3,7 @@
#include "ECS.hpp"
#include "../components.hpp"
#include "../others/common_scene_resources.hpp"
struct MyEcs;
......@@ -10,7 +11,7 @@ struct HealthSystem
{
public:
void update(MyEcs& myEcs, RayComponent& ray);
void destroyDeadCharacter(MyEcs& myEcs);
void HealthSystem::destroyDeadCharacter(MyEcs& myEcs, CommonSceneResources& ressources, int& score, int& nbAI);
using Requirements = ecs::Requirements<ecs::Auto>;
using UpdatePolicy = ecs::OncePerEntity;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment