Danbias/Code/Game/GameLogic/Game.cpp

181 lines
3.4 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
2014-02-19 10:15:52 +01:00
int insert = InsertObject(this->players, (PlayerData*)0);
int freeID = 0;
bool found = false;
2014-01-20 15:47:52 +01:00
2014-02-19 10:15:52 +01:00
for(int i = 0; i < 100; i++)
{
found = true;
freeID = i;
2014-02-19 15:57:52 +01:00
for(int j = 0; j < (int)players.Size(); j++)
2014-02-19 10:15:52 +01:00
{
if(this->players[j] && this->players[j]->GetID() == freeID)
{
found = false;
}
2014-01-20 15:47:52 +01:00
2014-02-19 10:15:52 +01:00
if(!found) break;
}
if(found) break;
}
2014-01-20 15:47:52 +01:00
2014-02-19 10:15:52 +01:00
this->players[insert] = new PlayerData(freeID, 0); // user constructor with objectID and teamID
this->players[insert]->player->GetRigidBody()->SetSubscription(Game::PhysicsOnMove);
2014-01-20 15:47:52 +01:00
2014-02-19 10:15:52 +01:00
return this->players[insert];
}
Game::LevelData* Game::CreateLevel(const wchar_t mapName[255])
{
2014-01-20 15:47:52 +01:00
if(this->level) return this->level;
this->level = new LevelData();
this->level->level->InitiateLevel(mapName);
2014-01-20 15:47:52 +01:00
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++)
{
2014-02-19 22:07:53 +01:00
if(this->players[i] && this->players[i]->player) this->players[i]->player->BeginFrame();
2014-01-29 16:01:56 +01:00
}
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++)
{
2014-02-19 22:07:53 +01:00
if(this->players[i] && this->players[i]->player) this->players[i]->player->EndFrame();
2014-01-29 16:01:56 +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
}