2014-01-10 12:33:04 +01:00
|
|
|
#include "Game.h"
|
|
|
|
|
|
|
|
using namespace GameLogic;
|
2014-01-14 10:28:12 +01:00
|
|
|
using namespace Utility::DynamicMemory;
|
2014-01-15 11:03:25 +01:00
|
|
|
using namespace Oyster::Physics;
|
2014-01-10 12:33:04 +01:00
|
|
|
|
2014-01-20 15:47:52 +01:00
|
|
|
template<typename T> int InsertObject(DynamicArray<T*>& list, T* obj)
|
2014-01-10 12:33:04 +01:00
|
|
|
{
|
2014-01-15 11:03:25 +01:00
|
|
|
for (unsigned int i = 0; i < list.Size(); i++)
|
2014-01-10 12:33:04 +01:00
|
|
|
{
|
2014-01-15 11:03:25 +01:00
|
|
|
if(!list[i])
|
|
|
|
{
|
|
|
|
list[i] = obj;
|
|
|
|
return i;
|
|
|
|
}
|
2014-01-16 11:07:45 +01:00
|
|
|
}
|
2014-01-15 11:03:25 +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)
|
2014-01-15 11:03:25 +01:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < list.Size(); i++)
|
|
|
|
{
|
|
|
|
if(!list[i])
|
|
|
|
{
|
|
|
|
list[i] = obj;
|
|
|
|
return i;
|
|
|
|
}
|
2014-01-10 12:33:04 +01:00
|
|
|
}
|
2014-01-15 11:03:25 +01:00
|
|
|
list.Push(obj);
|
|
|
|
return list.Size() - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-20 15:47:52 +01:00
|
|
|
Game gameInstance;
|
|
|
|
GameAPI& GameAPI::Instance()
|
|
|
|
{
|
|
|
|
return gameInstance;
|
|
|
|
}
|
2014-01-10 12:33:04 +01:00
|
|
|
|
|
|
|
|
|
|
|
Game::Game(void)
|
2014-01-20 15:47:52 +01:00
|
|
|
: initiated(false)
|
|
|
|
, onMoveFnc(0)
|
2014-02-04 16:24:37 +01:00
|
|
|
, onDisableFnc(0)
|
2014-01-20 15:47:52 +01:00
|
|
|
, frameTime(1.0f/120.0f)
|
|
|
|
{}
|
2014-01-10 12:33:04 +01:00
|
|
|
|
|
|
|
Game::~Game(void)
|
2014-01-20 15:47:52 +01:00
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < gameInstance.players.Size(); i++)
|
2014-01-10 12:33:04 +01:00
|
|
|
{
|
2014-01-20 15:47:52 +01:00
|
|
|
delete gameInstance.players[i];
|
2014-01-10 12:33:04 +01:00
|
|
|
}
|
2014-01-20 15:47:52 +01:00
|
|
|
gameInstance.players.Clear();
|
|
|
|
|
|
|
|
delete this->level;
|
|
|
|
this->level = 0;
|
|
|
|
|
|
|
|
initiated = false;
|
2014-01-10 12:33:04 +01:00
|
|
|
}
|
|
|
|
|
2014-01-15 11:03:25 +01:00
|
|
|
void Game::GetAllPlayerPositions() const
|
2014-01-10 12:33:04 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-15 11:03:25 +01:00
|
|
|
Game::PlayerData* Game::CreatePlayer()
|
2014-01-10 12:33:04 +01:00
|
|
|
{
|
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;
|
|
|
|
|
|
|
|
for(int j = 0; j < players.Size(); j++)
|
|
|
|
{
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->players[insert] = new PlayerData(freeID, 0); // user constructor with objectID and teamID
|
|
|
|
this->players[insert]->player->GetRigidBody()->SetSubscription(Game::PhysicsOnMove);
|
|
|
|
|
|
|
|
return this->players[insert];
|
2014-01-10 12:33:04 +01:00
|
|
|
}
|
|
|
|
|
2014-02-18 21:24:21 +01:00
|
|
|
Game::LevelData* Game::CreateLevel(const wchar_t mapName[255])
|
2014-01-16 12:08:24 +01:00
|
|
|
{
|
2014-01-20 15:47:52 +01:00
|
|
|
if(this->level) return this->level;
|
|
|
|
|
|
|
|
this->level = new LevelData();
|
2014-02-18 21:24:21 +01:00
|
|
|
this->level->level->InitiateLevel(mapName);
|
2014-02-14 09:53:02 +01:00
|
|
|
|
2014-01-20 15:47:52 +01:00
|
|
|
return this->level;
|
2014-01-16 12:08:24 +01:00
|
|
|
}
|
|
|
|
|
2014-01-10 12:33:04 +01:00
|
|
|
void Game::CreateTeam()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-20 15:47:52 +01:00
|
|
|
bool Game::NewFrame()
|
2014-01-10 12:33:04 +01:00
|
|
|
{
|
2014-01-29 16:01:56 +01:00
|
|
|
for (unsigned int i = 0; i < this->players.Size(); i++)
|
|
|
|
{
|
2014-02-19 10:15:52 +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 10:15:52 +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-30 10:57:44 +01:00
|
|
|
|
2014-01-20 15:47:52 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-01-15 11:03:25 +01:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-09 16:42:26 +01:00
|
|
|
void Game::SetSubscription(GameEvent::ObjectMovedFunction functionPointer)
|
2014-01-20 15:47:52 +01:00
|
|
|
{
|
2014-02-09 16:42:26 +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)
|
|
|
|
{
|
2014-01-21 09:52:48 +01:00
|
|
|
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)
|
|
|
|
{
|
2014-02-09 16:42:26 +01:00
|
|
|
if(gameInstance.onDisableFnc) gameInstance.onDisableFnc(0, 0);
|
2014-01-20 15:47:52 +01:00
|
|
|
}
|
|
|
|
|