Danbias/Code/Game/GameLogic/Game.cpp

167 lines
3.2 KiB
C++
Raw Normal View History

#include "Game.h"
using namespace GameLogic;
using namespace Utility::DynamicMemory;
using namespace Oyster::Physics;
2014-01-20 15:47:52 +01:00
template<typename T> int InsertObject(DynamicArray<T*>& list, T* obj)
{
for (unsigned int i = 0; i < list.Size(); i++)
{
if(!list[i])
{
list[i] = obj;
return i;
}
2014-01-16 11:07:45 +01:00
}
list.Push(obj);
return list.Size() - 1;
}
2014-01-20 15:47:52 +01:00
template<typename T> int RemoveObject(DynamicArray<T*>& list, T* obj)
{
for (unsigned int i = 0; i < list.Size(); i++)
{
if(!list[i])
{
list[i] = obj;
return i;
}
}
list.Push(obj);
return list.Size() - 1;
}
2014-01-20 15:47:52 +01:00
Game gameInstance;
GameAPI& GameAPI::Instance()
{
return gameInstance;
}
Game::Game(void)
2014-01-20 15:47:52 +01:00
: initiated(false)
, onMoveFnc(0)
, onDisableFnc(0)
2014-01-20 15:47:52 +01:00
, frameTime(1.0f/120.0f)
{}
Game::~Game(void)
2014-01-20 15:47:52 +01:00
{
for (unsigned int i = 0; i < gameInstance.players.Size(); i++)
{
2014-01-20 15:47:52 +01:00
delete gameInstance.players[i];
}
2014-01-20 15:47:52 +01:00
gameInstance.players.Clear();
delete this->level;
this->level = 0;
initiated = false;
}
void Game::GetAllPlayerPositions() const
{
}
Game::PlayerData* Game::CreatePlayer()
{
2014-01-20 15:47:52 +01:00
// Find a free space in array or insert at end
int i = InsertObject(this->players, (PlayerData*)0);
2014-01-20 15:47:52 +01:00
this->players[i] = new PlayerData();
2014-02-10 13:55:01 +01:00
this->players[i]->player->GetRigidBody()->SetSubscription(Game::PhysicsOnMove);
2014-01-20 15:47:52 +01:00
return this->players[i];
}
Game::LevelData* Game::CreateLevel()
{
2014-01-20 15:47:52 +01:00
if(this->level) return this->level;
this->level = new LevelData();
this->level->level->InitiateLevel(1000);
return this->level;
}
void Game::CreateTeam()
{
}
2014-01-20 15:47:52 +01:00
bool Game::NewFrame()
{
2014-01-29 16:01:56 +01:00
for (unsigned int i = 0; i < this->players.Size(); i++)
{
if(this->players[i]->player) this->players[i]->player->BeginFrame();
}
2014-02-09 21:24:09 +01:00
API::Instance().UpdateWorld();
2014-01-29 16:01:56 +01:00
for (unsigned int i = 0; i < this->players.Size(); i++)
{
if(this->players[i]->player) this->players[i]->player->EndFrame();
2014-02-10 12:42:31 +01:00
gameInstance.onMoveFnc(this->players[i]);
2014-01-29 16:01:56 +01:00
}
2014-02-10 13:25:28 +01:00
for (unsigned int i = 0; i < this->level->level->dynamicObjects.Size(); i++)
{
gameInstance.onMoveFnc(this->level->level->dynamicObjects[i]);
}
2014-02-10 12:42:31 +01:00
2014-01-30 10:57:44 +01:00
2014-01-20 15:47:52 +01:00
return true;
}
2014-01-20 15:47:52 +01:00
void Game::SetFPS(int FPS)
{
this->frameTime = 1.0f / FPS;
}
void Game::SetFrameTimeLength( float seconds )
{
this->frameTime = seconds;
}
void Game::SetSubscription(GameEvent::ObjectMovedFunction functionPointer)
2014-01-20 15:47:52 +01:00
{
this->onMoveFnc = functionPointer;
}
void Game::SetSubscription(GameEvent::ObjectDisabledFunction functionPointer)
{
this->onDisableFnc = functionPointer;
2014-01-20 15:47:52 +01:00
}
bool Game::Initiate()
{
2014-02-09 21:24:09 +01:00
API::Instance().Init();
//API::Instance().SetSubscription(Game::PhysicsOnDestroy);
2014-02-10 12:42:31 +01:00
//API::Instance().SetFrameTimeLength(this->frameTime);
2014-01-20 15:47:52 +01:00
this->initiated = true;
return true;
}
float Game::GetFrameTime() const
{
return this->frameTime;
}
/**********************************************/
/*********** Private methods ******************/
/***************************************************************************************************/
void Game::PhysicsOnMove(const ICustomBody *object)
{
IObjectData* temp = (IObjectData*)object->GetCustomTag();
if(gameInstance.onMoveFnc && temp) gameInstance.onMoveFnc(temp);
2014-01-20 15:47:52 +01:00
}
void Game::PhysicsOnDestroy(::Utility::DynamicMemory::UniquePointer<ICustomBody> proto)
{
if(gameInstance.onDisableFnc) gameInstance.onDisableFnc(0, 0);
2014-01-20 15:47:52 +01:00
}