diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index 97b64c33..74d5f049 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -167,16 +167,7 @@ namespace DanBias stateVal = true; break; case Client::GameClientState::ClientState_Game: - //if(m_data->serverOwner) - //Initiate the game server through the server API - - - //if(m_data->serverOwner) - //{ - // ((Client::GameState*)m_data->recieverObj->gameClientState)->setClientId(2); - //} - //else - // ((Client::GameState*)m_data->recieverObj->gameClientState)->setClientId(3); + break; default: return E_FAIL; @@ -198,15 +189,7 @@ namespace DanBias HRESULT DanBiasGame::Render(float deltaTime) { - int isPressed = 0; - if(m_data->inputObj->IsKeyPressed(DIK_A)) - { - isPressed = 1; - } - wchar_t title[255]; - swprintf(title, sizeof(title), L"| Pressing A: %d | \n", (int)(isPressed)); - SetWindowText(m_data->window->GetHWND(), title); m_data->recieverObj->gameClientState->Render(); diff --git a/Code/Game/DanBiasGame/GameClientRecieverFunc.h b/Code/Game/DanBiasGame/GameClientRecieverFunc.h index 6e134ff8..15b0cdab 100644 --- a/Code/Game/DanBiasGame/GameClientRecieverFunc.h +++ b/Code/Game/DanBiasGame/GameClientRecieverFunc.h @@ -110,16 +110,17 @@ namespace DanBias ((Client::GameState*)gameClientState)->Protocol(&protocolData); } break; - case protocol_Lobby_Start: + case protocol_Lobby_Create: { if(dynamic_cast(gameClientState)) { - int id = p[1].value.netInt; + GameLogic::Protocol_LobbyCreateGame tp(); + int id = p.Get(1).value.netInt; std::string name = p.Get(19).value.netCharPtr; Oyster::Math::Float4x4 w; for(int i = 0; i< 16; i++) { - w[i] = p[i+3].value.netFloat; + w[i] = p[i+2].value.netFloat; } gameClientState->Release(); @@ -130,6 +131,20 @@ namespace DanBias std::wstring temp; Utility::String::StringToWstring(name, temp); ((Client::GameState*)gameClientState)->InitiatePlayer(id, temp, w); + + //Do some wait state? + } + } + break; + case protocol_Lobby_Start: + { + if(dynamic_cast(gameClientState)) + { + //Game state should start in n seconds + GameLogic::Protocol_LobbyStartGame p(p); + p.seconds; + + //Sleep((int)(p.seconds * 1000)); } } break; diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index 7eea61eb..9f65910b 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -15,7 +15,6 @@ struct GameState::myData Oyster::Math3D::Float4x4 view; Oyster::Math3D::Float4x4 proj; std::vector object; - int modelCount; Oyster::Network::NetworkClient* nwClient; gameStateState state; @@ -88,11 +87,12 @@ bool GameState::LoadModels(std::wstring mapFile) // open file // read file // init models + int nrOfBoxex = 5; - privData->modelCount = 3 + nrOfBoxex; - int id = 100; - // add world model +// add world model + + ModelInitData modelData; Oyster::Math3D::Float4x4 translate; C_Object* obj; @@ -105,7 +105,8 @@ bool GameState::LoadModels(std::wstring mapFile) privData->object.push_back(obj); privData->object[privData->object.size() -1 ]->Init(modelData); - // add box model +/* +// add box model modelData.world = Oyster::Math3D::Float4x4::identity; modelData.modelPath = L"box.dan"; @@ -122,7 +123,7 @@ bool GameState::LoadModels(std::wstring mapFile) modelData.world = Oyster::Math3D::Float4x4::identity; } - // add crystal model +// add crystal model modelData.world = Oyster::Math3D::Float4x4::identity; translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(10, 305, 0)); @@ -135,7 +136,7 @@ bool GameState::LoadModels(std::wstring mapFile) privData->object.push_back(obj); privData->object[privData->object.size() -1 ]->Init(modelData); - // add house model +// add house model modelData.world = Oyster::Math3D::Float4x4::identity; translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(50, 300, 0)); //Oyster::Math3D::RotationMatrix_AxisZ() @@ -148,6 +149,7 @@ bool GameState::LoadModels(std::wstring mapFile) privData->object.push_back(obj); privData->object[privData->object.size() -1 ]->Init(modelData); + */ return true; } bool GameState::InitCamera(Oyster::Math::Float3 startPos) @@ -183,6 +185,34 @@ void GameState::InitiatePlayer(int id, std::wstring modelName, Oyster::Math::Flo obj = new C_Player(); privData->object.push_back(obj); privData->object[privData->object.size() -1 ]->Init(modelData); + + //printf("Move message recieved!"); + Oyster::Math::Float3 right = Oyster::Math::Float3(world[0], world[1], world[2]); + Oyster::Math::Float3 up = Oyster::Math::Float3(world[4], world[5], world[6]); + Oyster::Math::Float3 objForward = (Oyster::Math::Float3(world[8], world[9], world[10])); + Oyster::Math::Float3 pos = Oyster::Math::Float3(world[12], world[13], world[14]); + + Oyster::Math::Float3 cameraLook = camera->GetLook(); + Oyster::Math::Float3 cameraUp = camera->GetUp(); + + + + /*Oyster::Math::Float3 newUp = cameraUp.Dot(up); + up *= newUp; + up.Normalize(); + Oyster::Math::Float3 newLook = up.Cross(right); + newLook.Normalize();*/ + + camera->setRight(right); + camera->setUp(up); + camera->setLook(objForward); + + up *= 2; + objForward *= -3; + Oyster::Math::Float3 cameraPos = up + pos + objForward; + camera->SetPosition(cameraPos); + + camera->UpdateViewMatrix(); } GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyInput) @@ -422,8 +452,9 @@ void GameState::Protocol( ObjPos* pos ) { privData->object[i]->setPos(world); - if(i == myId) // playerobj + if(pos->object_ID == myId) // playerobj { + //printf("Move message recieved!"); Oyster::Math::Float3 right = Oyster::Math::Float3(world[0], world[1], world[2]); Oyster::Math::Float3 up = Oyster::Math::Float3(world[4], world[5], world[6]); Oyster::Math::Float3 objForward = (Oyster::Math::Float3(world[8], world[9], world[10])); diff --git a/Code/Game/DanBiasGame/GameClientState/LoginState.cpp b/Code/Game/DanBiasGame/GameClientState/LoginState.cpp index 0e3d46e5..9608cd48 100644 --- a/Code/Game/DanBiasGame/GameClientState/LoginState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/LoginState.cpp @@ -97,7 +97,7 @@ GameClientState::ClientState LoginState::Update(float deltaTime, InputClass* Key DanBias::GameServerAPI::ServerInitiate(desc); DanBias::GameServerAPI::ServerStart(); // my ip - nwClient->Connect(15151, "127.0.0.1"); + nwClient->Connect(15152, "127.0.0.1"); if (!nwClient->IsConnected()) { @@ -110,7 +110,8 @@ GameClientState::ClientState LoginState::Update(float deltaTime, InputClass* Key if( KeyInput->IsKeyPressed(DIK_J)) { // game ip - nwClient->Connect(15151, "127.0.0.1"); + nwClient->Connect(15152, "127.0.0.1"); + //nwClient->Connect(15152, "193.11.187.187"); if (!nwClient->IsConnected()) { diff --git a/Code/Game/GameLogic/CollisionManager.cpp b/Code/Game/GameLogic/CollisionManager.cpp index 33604f51..e4e8923d 100644 --- a/Code/Game/GameLogic/CollisionManager.cpp +++ b/Code/Game/GameLogic/CollisionManager.cpp @@ -83,7 +83,7 @@ using namespace GameLogic; if(kineticEnergyLoss > forceThreashHold) //should only take damage if the force is high enough { - damageDone = (int)kineticEnergyLoss * 0.10f; + damageDone = (int)(kineticEnergyLoss * 0.10f); //player.DamageLife(damageDone); } diff --git a/Code/Game/GameLogic/Game.cpp b/Code/Game/GameLogic/Game.cpp index ccb2a37d..939056a1 100644 --- a/Code/Game/GameLogic/Game.cpp +++ b/Code/Game/GameLogic/Game.cpp @@ -68,12 +68,12 @@ void Game::GetAllPlayerPositions() const Game::PlayerData* Game::CreatePlayer() { // Find a free space in array or insert at end - int id = InsertObject(this->players, (PlayerData*)0); + int i = InsertObject(this->players, (PlayerData*)0); - this->players[id] = new PlayerData(); - this->players[id]->player->GetRigidBody()->SetSubscription(Game::PhysicsOnMove); + this->players[i] = new PlayerData(); + this->players[i]->player->GetRigidBody()->SetSubscription(Game::PhysicsOnMove); - return this->players[id]; + return this->players[i]; } Game::LevelData* Game::CreateLevel() @@ -105,8 +105,7 @@ bool Game::NewFrame() if(this->players[i]->player) this->players[i]->player->EndFrame(); } - - gameInstance.onMoveFnc(this->level); + //gameInstance.onMoveFnc(this->level); return true; } @@ -121,25 +120,21 @@ void Game::SetFrameTimeLength( float seconds ) this->frameTime = seconds; } -void Game::SetSubscription(GameEvent::ObjectEventFunctionType type, GameEvent::ObjectEventFunction functionPointer) +void Game::SetSubscription(GameEvent::ObjectMovedFunction functionPointer) { - switch (type) - { - case GameLogic::GameEvent::ObjectEventFunctionType_OnMove: - this->onMoveFnc = functionPointer; - break; - case GameLogic::GameEvent::ObjectEventFunctionType_OnDead: - this->onDisableFnc = functionPointer; - break; - } - + this->onMoveFnc = functionPointer; +} +void Game::SetSubscription(GameEvent::ObjectDisabledFunction functionPointer) +{ + this->onDisableFnc = functionPointer; + } bool Game::Initiate() { API::Instance().Init((int)pow(2u, 9u), 1u, Oyster::Math::Float3()); API::Instance().SetSubscription(Game::PhysicsOnDestroy); - API::Instance().SetFrameTimeLength(1.0f/120.0f); + API::Instance().SetFrameTimeLength(this->frameTime); this->initiated = true; return true; } @@ -162,6 +157,6 @@ void Game::PhysicsOnMove(const ICustomBody *object) } void Game::PhysicsOnDestroy(::Utility::DynamicMemory::UniquePointer proto) { - if(gameInstance.onDisableFnc) gameInstance.onDisableFnc(0); + if(gameInstance.onDisableFnc) gameInstance.onDisableFnc(0, 0); } diff --git a/Code/Game/GameLogic/Game.h b/Code/Game/GameLogic/Game.h index b57520e0..2ae96ce3 100644 --- a/Code/Game/GameLogic/Game.h +++ b/Code/Game/GameLogic/Game.h @@ -69,7 +69,8 @@ namespace GameLogic bool NewFrame() override; void SetFPS( int FPS ) override; void SetFrameTimeLength( float seconds ) override; - void SetSubscription(GameEvent::ObjectEventFunctionType type, GameEvent::ObjectEventFunction functionPointer) override; + void SetSubscription(GameEvent::ObjectMovedFunction functionPointer) override; + void SetSubscription(GameEvent::ObjectDisabledFunction functionPointer) override; bool Initiate() override; float GetFrameTime() const; @@ -81,8 +82,8 @@ namespace GameLogic LevelData* level; float frameTime; bool initiated; - GameEvent::ObjectEventFunction onDisableFnc; - GameEvent::ObjectEventFunction onMoveFnc; + GameEvent::ObjectDisabledFunction onDisableFnc; + GameEvent::ObjectMovedFunction onMoveFnc; }; } diff --git a/Code/Game/GameLogic/GameAPI.h b/Code/Game/GameLogic/GameAPI.h index 53931063..3505b123 100644 --- a/Code/Game/GameLogic/GameAPI.h +++ b/Code/Game/GameLogic/GameAPI.h @@ -23,16 +23,8 @@ namespace GameLogic */ namespace GameEvent { - /** - * The type of event to listen on. - */ - enum ObjectEventFunctionType - { - ObjectEventFunctionType_OnMove, - ObjectEventFunctionType_OnDead, - }; - - typedef void(*ObjectEventFunction)(IObjectData* object); // Callback method that recieves and object + typedef void(*ObjectMovedFunction)(IObjectData* object); // Callback method that recieves and object + typedef void(*ObjectDisabledFunction)(IObjectData* object, float seconds); // Callback method that recieves and object //etc... }; @@ -147,17 +139,22 @@ namespace GameLogic /** Set the frame time in fps * @param FPS The fps to set */ - virtual void SetFPS( int FPS = 60 ) = 0; + virtual void SetFPS( int FPS = 120 ) = 0; /** Set the frames time in seconds * @param seconds The frame length */ - virtual void SetFrameTimeLength( float seconds ) = 0; + virtual void SetFrameTimeLength( float seconds = (1.0f/120.0f) ) = 0; /** Set a specific object event subscription callback * @param */ - virtual void SetSubscription(GameEvent::ObjectEventFunctionType type, GameEvent::ObjectEventFunction functionPointer) = 0; + virtual void SetSubscription(GameEvent::ObjectMovedFunction functionPointer) = 0; + + /** Set a specific object event subscription callback + * @param + */ + virtual void SetSubscription(GameEvent::ObjectDisabledFunction functionPointer) = 0; }; } diff --git a/Code/Game/GameLogic/Game_PlayerData.cpp b/Code/Game/GameLogic/Game_PlayerData.cpp index da441539..5667ab15 100644 --- a/Code/Game/GameLogic/Game_PlayerData.cpp +++ b/Code/Game/GameLogic/Game_PlayerData.cpp @@ -7,13 +7,14 @@ Game::PlayerData::PlayerData() { //set some stats that are appropriate to a player Oyster::Physics::API::SimpleBodyDescription sbDesc; - sbDesc.centerPosition = Oyster::Math::Float3(0,308,0); - sbDesc.size = Oyster::Math::Float3(4,7,4); + + sbDesc.size = Oyster::Math::Float3(4.0f ,4.0f,4.0f); sbDesc.mass = 70; - sbDesc.restitutionCoeff = 0.5; - sbDesc.frictionCoeff_Static = 0.4; - sbDesc.frictionCoeff_Dynamic = 0.3; - sbDesc.rotation = Oyster::Math::Float3(0, Oyster::Math::pi, 0); + sbDesc.centerPosition = Oyster::Math::Float3(0,308,0); + sbDesc.restitutionCoeff = 0.5f; + sbDesc.frictionCoeff_Static = 0.4f; + sbDesc.frictionCoeff_Dynamic = 0.3f; + sbDesc.rotation = Oyster::Math::Float3(0.0f, Oyster::Math::pi, 0.0f); //create rigid body Oyster::Physics::ICustomBody *rigidBody = Oyster::Physics::API::Instance().CreateRigidBody(sbDesc).Release(); @@ -21,11 +22,6 @@ Game::PlayerData::PlayerData() //create player with this rigid body this->player = new Player(rigidBody,Player::PlayerCollisionBefore, Player::PlayerCollision, OBJECT_TYPE::OBJECT_TYPE_PLAYER); this->player->GetRigidBody()->SetCustomTag(this); - /*Oyster::Physics::ICustomBody::State state; - this->player->GetRigidBody()->GetState(state); - state.SetRotation(Oyster::Math::Float3(0, Oyster::Math::pi, 0)); - this->player->GetRigidBody()->SetState(state); - player->EndFrame();*/ } Game::PlayerData::PlayerData(int playerID,int teamID) { diff --git a/Code/Game/GameLogic/Level.cpp b/Code/Game/GameLogic/Level.cpp index 0419e5b0..6a964840 100644 --- a/Code/Game/GameLogic/Level.cpp +++ b/Code/Game/GameLogic/Level.cpp @@ -1,5 +1,6 @@ #include "Level.h" #include "CollisionManager.h" +#include "Game.h" using namespace GameLogic; using namespace Utility::DynamicMemory; @@ -70,9 +71,10 @@ void Level::InitiateLevel(float radius) rigidBody->SetState(state); levelObj = new StaticObject(rigidBody, LevelCollisionBefore, LevelCollisionAfter, OBJECT_TYPE::OBJECT_TYPE_WORLD); + levelObj->objectID = idCount++; rigidBody->SetCustomTag(levelObj); - //this->dynamicObjects = new DynamicArray< DynamicObject>; +/* // add box API::SimpleBodyDescription sbDesc_TestBox; sbDesc_TestBox.centerPosition = Oyster::Math::Float4(10,320,0,0); @@ -91,13 +93,11 @@ void Level::InitiateLevel(float radius) rigidBody_TestBox = API::Instance().CreateRigidBody(sbDesc_TestBox).Release(); rigidBody_TestBox->SetSubscription(Level::PhysicsOnMoveLevel); - this->dynamicObjects.Push(new DynamicObject(rigidBody_TestBox,Object::DefaultCollisionBefore, Object::DefaultCollisionAfter, OBJECT_TYPE::OBJECT_TYPE_BOX)); + DynamicObject *box = new DynamicObject(rigidBody_TestBox,Object::DefaultCollisionBefore, Object::DefaultCollisionAfter, OBJECT_TYPE::OBJECT_TYPE_BOX); + box->objectID = idCount++; + this->dynamicObjects.Push(box); rigidBody_TestBox->SetCustomTag(this->dynamicObjects[i]); } - - - - // add crystal API::SimpleBodyDescription sbDesc_Crystal; @@ -108,7 +108,9 @@ void Level::InitiateLevel(float radius) ICustomBody* rigidBody_Crystal = API::Instance().CreateRigidBody(sbDesc_Crystal).Release(); rigidBody_Crystal->SetSubscription(Level::PhysicsOnMoveLevel); - this->dynamicObjects.Push(new DynamicObject(rigidBody_Crystal,Object::DefaultCollisionBefore, Object::DefaultCollisionAfter, OBJECT_TYPE::OBJECT_TYPE_BOX)); + DynamicObject *cry = new DynamicObject(rigidBody_Crystal,Object::DefaultCollisionBefore, Object::DefaultCollisionAfter, OBJECT_TYPE::OBJECT_TYPE_BOX); + cry->objectID = idCount++; + this->dynamicObjects.Push(cry); rigidBody_Crystal->SetCustomTag(this->dynamicObjects[nrOfBoxex]); @@ -123,9 +125,11 @@ void Level::InitiateLevel(float radius) ICustomBody* rigidBody_House = API::Instance().CreateRigidBody(sbDesc_House).Release(); rigidBody_House->SetSubscription(Level::PhysicsOnMoveLevel); - this->staticObjects.Push(new StaticObject(rigidBody_House,Object::DefaultCollisionBefore, Object::DefaultCollisionAfter, OBJECT_TYPE::OBJECT_TYPE_GENERIC)); + StaticObject* house = new StaticObject(rigidBody_House,Object::DefaultCollisionBefore, Object::DefaultCollisionAfter, OBJECT_TYPE::OBJECT_TYPE_GENERIC); + house->objectID = idCount++; + this->staticObjects.Push(house); rigidBody_House->SetCustomTag(this->staticObjects[0]); - +*/ // add gravitation API::Gravity gravityWell; @@ -167,4 +171,6 @@ void Level::PhysicsOnMoveLevel(const ICustomBody *object) { // function call from physics update when object was moved Object* temp = (Object*)object->GetCustomTag(); + ((Game*)&Game::Instance())->onMoveFnc(temp); + } diff --git a/Code/Game/GameLogic/Object.cpp b/Code/Game/GameLogic/Object.cpp index 68297d46..c2b99d6f 100644 --- a/Code/Game/GameLogic/Object.cpp +++ b/Code/Game/GameLogic/Object.cpp @@ -118,8 +118,6 @@ Oyster::Physics::ICustomBody* Object::GetRigidBody() void Object::BeginFrame() { - - if(currPhysicsState.GetLinearMomentum() !=currPhysicsState.GetLinearMomentum()) { //error diff --git a/Code/Game/GameLogic/Object.h b/Code/Game/GameLogic/Object.h index c484701c..48343c6a 100644 --- a/Code/Game/GameLogic/Object.h +++ b/Code/Game/GameLogic/Object.h @@ -43,7 +43,8 @@ namespace GameLogic static Oyster::Physics::ICustomBody::SubscriptMessage DefaultCollisionBefore(Oyster::Physics::ICustomBody *rigidBodyLevel, Oyster::Physics::ICustomBody *obj); static Oyster::Physics::ICustomBody::SubscriptMessage DefaultCollisionAfter(Oyster::Physics::ICustomBody *rigidBodyLevel, Oyster::Physics::ICustomBody *obj, Oyster::Math::Float kineticEnergyLoss); - private: + + public: //TODO: Hax This should be private when level is dynamic OBJECT_TYPE type; int objectID; diff --git a/Code/Game/GameLogic/Player.cpp b/Code/Game/GameLogic/Player.cpp index 9b0c29ee..6a2ff218 100644 --- a/Code/Game/GameLogic/Player.cpp +++ b/Code/Game/GameLogic/Player.cpp @@ -208,7 +208,7 @@ void Player::DamageLife(int damage) { this->life = 0; playerState = PLAYER_STATE_DEAD; - this->gameInstance->onDisableFnc(this); + this->gameInstance->onDisableFnc(this, 0.0f); } } diff --git a/Code/Game/GameLogic/Player.h b/Code/Game/GameLogic/Player.h index 4f406e31..b7791a42 100644 --- a/Code/Game/GameLogic/Player.h +++ b/Code/Game/GameLogic/Player.h @@ -85,8 +85,8 @@ namespace GameLogic int teamID; Weapon *weapon; PLAYER_STATE playerState; - Oyster::Math::Float3 lookDir; - Oyster::Math::Float dx; + Oyster::Math::Float3 lookDir; //Duplicate in Object.h? + Oyster::Math::Float dx; //dx of what? bool hasTakenDamage; float invincibleCooldown; diff --git a/Code/Game/GameLogic/TeamManager.cpp b/Code/Game/GameLogic/TeamManager.cpp index d1379c63..e99dafe1 100644 --- a/Code/Game/GameLogic/TeamManager.cpp +++ b/Code/Game/GameLogic/TeamManager.cpp @@ -35,13 +35,11 @@ TeamManager::~TeamManager(void) void TeamManager::RespawnPlayerRandom(Player *player) { - // Whats going on here? - int teamID = player->GetTeamID(); // ? - // ? - Player *respawnOnThis = this->teams[teamID]->GetPlayer(0); // ? - // ? - player->Respawn(respawnOnThis->GetPosition()); // ? - // player->Respawn(player->GetPosition()); ? + int teamID = player->GetTeamID(); + + Player *respawnOnThis = this->teams[teamID]->GetPlayer(0); + + player->Respawn(respawnOnThis->GetPosition()); } void TeamManager::CreateTeam(int teamSize) diff --git a/Code/Game/GameProtocols/LobbyProtocols.h b/Code/Game/GameProtocols/LobbyProtocols.h index 0dd563c3..b57245ba 100644 --- a/Code/Game/GameProtocols/LobbyProtocols.h +++ b/Code/Game/GameProtocols/LobbyProtocols.h @@ -21,46 +21,16 @@ namespace GameLogic { - /* struct Protocol_LobbyCreateGame :public Oyster::Network::CustomProtocolObject - { - char* mapName; - char gameId; - - Protocol_LobbyCreateGame() - { - this->protocol[0].value = protocol_Lobby_Create; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; - - this->protocol[1].type = Oyster::Network::NetAttributeType_CharArray; - this->protocol[2].type = Oyster::Network::NetAttributeType_Char; - } - Protocol_LobbyCreateGame(Oyster::Network::CustomNetProtocol& o) - { - mapName = o[1].value.netCharPtr; - gameId = o[2].value.netChar; - } - Oyster::Network::CustomNetProtocol GetProtocol() override - { - protocol[1].value = mapName; - protocol[2].value = gameId; - return &protocol; - } - - private: - Oyster::Network::CustomNetProtocol protocol; - }; - */ - struct Protocol_LobbyStartGame :public Oyster::Network::CustomProtocolObject { short clientID; // The unuiqe id reprsenting a specific client std::string modelName; float worldMatrix[16]; - Protocol_LobbyStartGame() + Protocol_LobbyCreateGame() { int c = 0; - this->protocol[c].value = protocol_Lobby_Start; + this->protocol[c].value = protocol_Lobby_Create; this->protocol[c++].type = Oyster::Network::NetAttributeType_Short; this->protocol[c++].type = Oyster::Network::NetAttributeType_Short; @@ -70,10 +40,10 @@ namespace GameLogic } this->protocol[c++].type = Oyster::Network::NetAttributeType_CharArray; } - Protocol_LobbyStartGame(short _clientID, std::string name, float world[16]) + Protocol_LobbyCreateGame(short _clientID, std::string name, float world[16]) { int c = 0; - this->protocol[c].value = protocol_Lobby_Start; + this->protocol[c].value = protocol_Lobby_Create; this->protocol[c++].type = Oyster::Network::NetAttributeType_Short; this->protocol[c++].type = Oyster::Network::NetAttributeType_Short; @@ -88,7 +58,7 @@ namespace GameLogic modelName = name; memcpy(&worldMatrix[0], &world[0], sizeof(float) * 16); } - Protocol_LobbyStartGame(Oyster::Network::CustomNetProtocol& o) + Protocol_LobbyCreateGame(Oyster::Network::CustomNetProtocol o) { int c = 1; clientID = o[c++].value.netInt; @@ -96,7 +66,7 @@ namespace GameLogic { this->worldMatrix[i] = o[c++].value.netFloat; } - modelName = o[c++].value.netCharPtr; + modelName.assign(o[c++].value.netCharPtr); } Oyster::Network::CustomNetProtocol GetProtocol() override { @@ -116,6 +86,39 @@ namespace GameLogic }; + struct Protocol_LobbyStartGame :public Oyster::Network::CustomProtocolObject + { + float seconds; + + Protocol_LobbyStartGame() + { + this->protocol[0].value = protocol_Lobby_Start; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[1].type = Oyster::Network::NetAttributeType_Float; + seconds = 0; + } + Protocol_LobbyStartGame(float _seconds) + { + this->protocol[0].value = protocol_Lobby_Start; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[1].type = Oyster::Network::NetAttributeType_Float; + seconds = _seconds; + } + Protocol_LobbyStartGame(Oyster::Network::CustomNetProtocol& o) + { + seconds = o[1].value.netFloat; + } + Oyster::Network::CustomNetProtocol GetProtocol() override + { + this->protocol[1].value = seconds; + return protocol; + } + + private: + Oyster::Network::CustomNetProtocol protocol; + + }; + struct Protocol_LobbyLogin :public Oyster::Network::CustomProtocolObject { // Login stuff diff --git a/Code/Game/GameServer/GameClient.h b/Code/Game/GameServer/GameClient.h index 097999ea..a9c33cf3 100644 --- a/Code/Game/GameServer/GameClient.h +++ b/Code/Game/GameServer/GameClient.h @@ -24,7 +24,7 @@ namespace DanBias GameLogic::IPlayerData* ReleasePlayer(); Utility::DynamicMemory::SmartPointer GetClient(); Utility::DynamicMemory::SmartPointer ReleaseClient(); - int GetID() const; + float GetSinceLastResponse() const; bool IsReady() const; bool Equals(const Oyster::Network::NetworkClient* c); @@ -35,7 +35,6 @@ namespace DanBias private: GameLogic::IPlayerData* player; Utility::DynamicMemory::SmartPointer client; - int id; bool isReady; float secondsSinceLastResponse; }; diff --git a/Code/Game/GameServer/GameServerAPI.h b/Code/Game/GameServer/GameServerAPI.h index edd9c473..7ceab867 100644 --- a/Code/Game/GameServer/GameServerAPI.h +++ b/Code/Game/GameServer/GameServerAPI.h @@ -35,7 +35,7 @@ namespace DanBias bool broadcast; //Not fully implemented! ServerInitDesc() : serverName("Game Server") - , listenPort(15151) + , listenPort(15152) , broadcast(true) {}; }; diff --git a/Code/Game/GameServer/GameSession.h b/Code/Game/GameServer/GameSession.h index c9e426d8..80d43a19 100644 --- a/Code/Game/GameServer/GameSession.h +++ b/Code/Game/GameServer/GameSession.h @@ -62,7 +62,6 @@ namespace DanBias private: // TODO: find out what this method does.. void ClientEventCallback(Oyster::Network::NetEvent e) override; - //Sends a client to the owner, if obj is NULL then all clients is sent void SendToOwner(DanBias::GameClient* obj); @@ -91,6 +90,7 @@ namespace DanBias //Callback method recieving from gamelogic static void ObjectMove ( GameLogic::IObjectData* movedObject ); + static void ObjectDisabled ( GameLogic::IObjectData* movedObject, float seconds ); //Private member variables private: @@ -102,7 +102,8 @@ namespace DanBias NetworkSession* owner; bool isCreated; bool isRunning; - float logicDeltaTime; + float logicFrameTime; + float networkFrameTime; Utility::WinTimer logicTimer; Utility::WinTimer networkTimer; GameDescription description; diff --git a/Code/Game/GameServer/Implementation/GameClient.cpp b/Code/Game/GameServer/Implementation/GameClient.cpp index 167dacc0..3956fe57 100644 --- a/Code/Game/GameServer/Implementation/GameClient.cpp +++ b/Code/Game/GameServer/Implementation/GameClient.cpp @@ -11,12 +11,10 @@ using namespace Oyster::Network; using namespace DanBias; using namespace GameLogic; -static int gameClientIDCount = 1; GameClient::GameClient(SmartPointer client, GameLogic::IPlayerData* player) { this->client = client; - this->id = gameClientIDCount++; this->player = player; isReady = false; } @@ -24,7 +22,6 @@ GameClient::~GameClient() { this->client->Disconnect(); this->player = 0; - this->id = -1; isReady = false; } @@ -48,10 +45,7 @@ SmartPointer GameClient::ReleaseClient() this->client = 0; return temp; } -int GameClient::GetID() const -{ - return this->id; -} + float GameClient::GetSinceLastResponse() const { return this->secondsSinceLastResponse; diff --git a/Code/Game/GameServer/Implementation/GameLobby.cpp b/Code/Game/GameServer/Implementation/GameLobby.cpp index df0738f0..b5fa8f40 100644 --- a/Code/Game/GameServer/Implementation/GameLobby.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby.cpp @@ -89,7 +89,7 @@ namespace DanBias if(this->gameSession) { - this->gameSession.ClientConnectedEvent(client); + this->gameSession.Attach(client); } else { diff --git a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp index 2d4453ee..2af45391 100644 --- a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp @@ -3,19 +3,16 @@ ///////////////////////////////////////////////////////////////////// #include "..\GameSession.h" #include "..\GameClient.h" +#include #include #include #include #include + #define NOMINMAX #include -#define DELTA_TIME_20 0.05f -#define DELTA_TIME_24 0.04166666666666666666666666666667f -#define DELTA_TIME_30 0.03333333333333333333333333333333f -#define DELTA_TIME_60 0.01666666666666666666666666666667f -#define DELTA_TIME_120 0.00833333333333333333333333333333f using namespace Utility::DynamicMemory; using namespace Oyster; @@ -25,19 +22,20 @@ using namespace GameLogic; namespace DanBias { + Utility::WinTimer testTimer; + int testID = -1; + bool GameSession::DoWork( ) { if(this->isRunning) { float dt = (float)this->logicTimer.getElapsedSeconds(); - this->logicDeltaTime += dt; - this->logicTimer.reset(); - while( logicDeltaTime >= DELTA_TIME_120 ) + if( dt >= this->logicFrameTime ) { this->ProcessClients(); this->gameInstance.NewFrame(); - logicDeltaTime -= DELTA_TIME_120; + this->logicTimer.reset(); } } @@ -70,11 +68,16 @@ namespace DanBias case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToRecieve: break; case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend: - printf("\t(%i : %s) - EventType_ProtocolFailedToSend\n", e.sender->GetID(), e.sender->GetIpAddress().c_str()); + printf("\t(%i : %s) - EventType_ProtocolFailedToSend\n", cl->GetClient()->GetID(), e.sender->GetIpAddress().c_str()); this->Detach(e.sender); break; case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved: - printf("\t(%i : %s) - EventType_ProtocolRecieved\n", e.sender->GetID(), e.sender->GetIpAddress().c_str()); + printf("\t(%i : %s) - EventType_ProtocolRecieved\n", cl->GetClient()->GetID(), e.sender->GetIpAddress().c_str()); + testID = 2; + if(cl->GetPlayer()->GetID() == testID)//TODO: TEST + { + testTimer.reset(); + } this->ParseProtocol(e.args.data.protocol, cl); break; } @@ -82,21 +85,32 @@ namespace DanBias void GameSession::ObjectMove(GameLogic::IObjectData* movedObject) { - if(gameSession->networkTimer.getElapsedSeconds() >= DELTA_TIME_60) + float dt = GameSession::gameSession->networkTimer.getElapsedSeconds(); + //Duh... This was causing alot of problems, it's in the wrong place... + //Need to figure out where to put this frame locker. + //We only need to send network packages when necessary, ie not 120 times per frame. + //I think it would be enough with 60-70 packages per second due to the nature of + //graphics update (60 fps) on the client side. To send more than this would be lost + //bandwidth. + //if( dt >= GameSession::gameSession->networkFrameTime ) { - gameSession->networkTimer.reset(); + GameSession::gameSession->networkTimer.reset(); GameLogic::IObjectData* obj = movedObject; - - if(dynamic_cast (movedObject)) + if(movedObject->GetID() == testID) //TODO: TEST { - int id = obj->GetID(); - Oyster::Math::Float4x4 world = obj->GetOrientation(); - - Protocol_ObjectPosition p(world, id); - GameSession::gameSession->Send(p.GetProtocol()); + float sec = (float)testTimer.getElapsedSeconds(); + sec = 0; } - else if(dynamic_cast(obj)) + + int id = obj->GetID(); + Protocol_ObjectPosition p(obj->GetOrientation(), id); + //if(id != 1) + GameSession::gameSession->Send(p.GetProtocol()); + + + /* + if(dynamic_cast(obj)) { obj = ((GameLogic::ILevelData*)movedObject)->GetObjectAt(0); if(obj) @@ -135,9 +149,14 @@ namespace DanBias } } } + */ } } + void GameSession::ObjectDisabled( GameLogic::IObjectData* movedObject, float seconds ) + { + GameSession::gameSession->Send(Protocol_ObjectDisable(movedObject->GetID(), seconds).GetProtocol()); + } //*****************************************************// //****************** Protocol methods *****************// @@ -258,7 +277,7 @@ namespace DanBias } void GameSession::General_Text ( Protocol_General_Text& p, DanBias::GameClient* c ) { - printf("Message recieved from (%i):\t %s\n", c->GetID(), p.text.c_str()); + printf("Message recieved from (%i):\t %s\n", c->GetClient()->GetID(), p.text.c_str()); } }//End namespace DanBias diff --git a/Code/Game/GameServer/Implementation/GameSession_General.cpp b/Code/Game/GameServer/Implementation/GameSession_General.cpp index baf86de4..d6e6106c 100644 --- a/Code/Game/GameServer/Implementation/GameSession_General.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_General.cpp @@ -11,6 +11,12 @@ #include #include +#define DELTA_TIME_20 0.05f +#define DELTA_TIME_24 0.04166666666666666666666666666667f +#define DELTA_TIME_30 0.03333333333333333333333333333333f +#define DELTA_TIME_60 0.01666666666666666666666666666667f +#define DELTA_TIME_120 0.00833333333333333333333333333333f + using namespace Utility::DynamicMemory; using namespace Oyster; @@ -29,7 +35,10 @@ namespace DanBias this->isCreated = false; this->isRunning = false; this->gameSession = this; - this->logicDeltaTime = 0.0f; + this->logicFrameTime = DELTA_TIME_20; + this->networkFrameTime = DELTA_TIME_20; + this->networkTimer.reset(); + this->logicTimer.reset(); memset(&this->description, 0, sizeof(GameDescription)); } @@ -47,37 +56,30 @@ namespace DanBias bool GameSession::Create(GameDescription& desc) { this->description = desc; - /* Do some error checking */ + /* Do some error checking */ if(desc.clients.Size() == 0) return false; if(!desc.owner) return false; if(this->isCreated) return false; - /* standard initialization of some data */ + /* standard initialization of some data */ NetworkSession::clients = desc.clients; - this->clients.Resize(desc.clients.Size()); + this->clients.Reserve(desc.clients.Size()); this->owner = desc.owner; - /* Initiate the game instance */ + /* Initiate the game instance */ if(!this->gameInstance.Initiate()) { printf("Failed to initiate the game instance\n"); } - /* Create the game level */ - if(!(this->levelData = this->gameInstance.CreateLevel())) - { - printf("Level not created!"); - return false; - } - - /* Create the players in the game instance */ + /* Create the players in the game instance */ GameLogic::IPlayerData* p = 0; for (unsigned int i = 0; i < desc.clients.Size(); i++) { if( (p = this->gameInstance.CreatePlayer()) ) { desc.clients[i]->SetOwner(this); - this->clients[i] = new GameClient(desc.clients[i], p); + this->clients.Push(new GameClient(desc.clients[i], p)); } else { @@ -85,19 +87,26 @@ namespace DanBias } } - /* Create the worker thread */ - if(this->worker.Create(this, false) != OYSTER_THREAD_ERROR_SUCCESS) + /* Create the game level */ + if(!(this->levelData = this->gameInstance.CreateLevel())) + { + printf("Level not created!"); return false; + } - this->worker.SetPriority(Oyster::Thread::OYSTER_THREAD_PRIORITY_3); - - /* Set some game instance data options */ - this->gameInstance.SetSubscription(GameLogic::GameEvent::ObjectEventFunctionType_OnMove, GameSession::ObjectMove); - //this->gameInstance.SetSubscription(GameLogic::GameEvent::ObjectEventFunctionType_OnDead, GameSession::ObjectDead); + /* Set some game instance data options */ + this->gameInstance.SetSubscription(GameSession::ObjectMove); + this->gameInstance.SetSubscription(GameSession::ObjectDisabled); + this->gameInstance.SetFPS(60); this->description.clients.Clear(); this->isCreated = true; + + /* Create the worker thread */ + if(this->worker.Create(this, false) != OYSTER_THREAD_ERROR_SUCCESS) + return false; + return this->isCreated; } @@ -115,12 +124,10 @@ namespace DanBias void GameSession::ThreadEntry( ) { - //A timer so we dont lock because 1 client disconnected.. - Utility::WinTimer t; - + //List with clients that we are waiting on.. DynamicArray> readyList = this->clients; - //First we need to clean invalid clients, if any, and tell them to start loading game data + //First we need to clean invalid clients, if any, and tell them to start loading game data for (unsigned int i = 0; i < readyList.Size(); i++) { if(!readyList[i]) @@ -129,13 +136,14 @@ namespace DanBias } else { - Protocol_LobbyStartGame p(readyList[i]->GetPlayer()->GetID(), "char_white.dan", readyList[i]->GetPlayer()->GetOrientation()); + Protocol_LobbyCreateGame p(readyList[i]->GetPlayer()->GetID(), "char_white.dan", readyList[i]->GetPlayer()->GetOrientation()); readyList[i]->GetClient()->Send(p); } } unsigned int readyCounter = readyList.Size(); - //Sync with clients + + //Sync with clients while (readyCounter != 0) { this->ProcessClients(); @@ -143,10 +151,10 @@ namespace DanBias { if(readyList[i] && readyList[i]->IsReady()) { - //Need to send information about other players to all players - for (unsigned int k = 0; k < readyList.Size(); k++) + //Need to send information about other players, to all players + for (unsigned int k = 0; k < this->clients.Size(); k++) { - if(k != i && this->clients[k]) + if((this->clients[k] && readyList[i]) && readyList[i]->GetClient()->GetID() != this->clients[k]->GetClient()->GetID()) { Protocol_ObjectCreate p(this->clients[k]->GetPlayer()->GetOrientation(), this->clients[k]->GetPlayer()->GetID(), "char_white.dan"); //The model name will be custom later.. readyList[i]->GetClient()->Send(p); @@ -159,6 +167,14 @@ namespace DanBias } Sleep(5); //TODO: This might not be needed here. } + + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[i]) + { + this->clients[i]->GetClient()->Send(GameLogic::Protocol_LobbyStartGame(5)); + } + } } bool GameSession::Attach(Utility::DynamicMemory::SmartPointer client) @@ -166,7 +182,11 @@ namespace DanBias if(!this->isCreated) return false; client->SetOwner(this); - SmartPointer obj = new GameClient(client, this->gameInstance.CreatePlayer()); + + IPlayerData* player = this->gameInstance.CreatePlayer(); + if(!player) return false; + + SmartPointer obj = new GameClient(client, player); for (unsigned int i = 0; i < clients.Size(); i++) { @@ -184,6 +204,7 @@ namespace DanBias void GameSession::CloseSession( bool dissconnectClients ) { + this->worker.Terminate(); NetworkSession::CloseSession(true); this->clients.Clear(); } diff --git a/Code/GamePhysics/PhysicsStructs-Impl.h b/Code/GamePhysics/PhysicsStructs-Impl.h index 30e1a605..eae72623 100644 --- a/Code/GamePhysics/PhysicsStructs-Impl.h +++ b/Code/GamePhysics/PhysicsStructs-Impl.h @@ -333,6 +333,12 @@ namespace Oyster this->isDisturbed = true; } + inline void CustomBodyState::ApplyFriction( const ::Oyster::Math::Float3 &j ) + { + this->linearImpulse += j; + this->isDisturbed = true; + } + inline void CustomBodyState::ApplyForwarding( const ::Oyster::Math::Float3 &deltaPos, const ::Oyster::Math::Float3 &deltaAxis ) { this->deltaPos += deltaPos; diff --git a/Code/GamePhysics/PhysicsStructs.h b/Code/GamePhysics/PhysicsStructs.h index 61a0569f..6bf39a67 100644 --- a/Code/GamePhysics/PhysicsStructs.h +++ b/Code/GamePhysics/PhysicsStructs.h @@ -109,6 +109,7 @@ namespace Oyster { namespace Physics void ApplyLinearImpulse( const ::Oyster::Math::Float3 &j ); void ApplyAngularImpulse( const ::Oyster::Math::Float3 &j ); void ApplyImpulse( const ::Oyster::Math::Float3 &j, const ::Oyster::Math::Float3 &at, const ::Oyster::Math::Float3 &normal ); + void CustomBodyState::ApplyFriction( const ::Oyster::Math::Float3 &j); void ApplyForwarding( const ::Oyster::Math::Float3 &deltaPos, const ::Oyster::Math::Float3 &deltaAxis ); bool IsSpatiallyAltered() const; diff --git a/Code/Misc/DynamicArray.h b/Code/Misc/DynamicArray.h index 6c3b4609..8b4c98da 100644 --- a/Code/Misc/DynamicArray.h +++ b/Code/Misc/DynamicArray.h @@ -117,7 +117,7 @@ namespace Utility template const T& DynamicArray::operator[](unsigned int index) const { - assert(index < this->size); + assert((int)index < this->size); return this->data[index]; } diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index 89653d74..01ba495e 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -300,6 +300,7 @@ void NetworkClient::Disconnect() privateData->thread.Terminate(); privateData->connection.Disconnect(); this->privateData->sendQueue.Clear(); + this->privateData->recieveQueue.Clear(); } diff --git a/Code/OysterPhysics3D/OysterCollision3D.cpp b/Code/OysterPhysics3D/OysterCollision3D.cpp index 41e11875..6cade619 100644 --- a/Code/OysterPhysics3D/OysterCollision3D.cpp +++ b/Code/OysterPhysics3D/OysterCollision3D.cpp @@ -516,7 +516,7 @@ namespace Oyster { namespace Collision3D { namespace Utility Float4 C = sphereA.center; C -= sphereB.center; Float r = (sphereA.radius + sphereB.radius); - + Float dotprod = C.Dot(C); if (r*r >= C.Dot(C)) { return true; // Intersect detected! diff --git a/Code/OysterPhysics3D/OysterPhysics3D.h b/Code/OysterPhysics3D/OysterPhysics3D.h index e1eb94e3..e8506bc7 100644 --- a/Code/OysterPhysics3D/OysterPhysics3D.h +++ b/Code/OysterPhysics3D/OysterPhysics3D.h @@ -279,7 +279,7 @@ namespace Oyster { namespace Physics3D ******************************************************************/ inline ::Oyster::Math::Float ForceField( ::Oyster::Math::Float g, ::Oyster::Math::Float massA, ::Oyster::Math::Float massB, ::Oyster::Math::Float radiusSquared ) { - return g * massA * massB / radiusSquared; + return g * massB / radiusSquared; } /****************************************************************** diff --git a/Code/OysterPhysics3D/RigidBody.cpp b/Code/OysterPhysics3D/RigidBody.cpp index 3563c77e..f7a5646e 100644 --- a/Code/OysterPhysics3D/RigidBody.cpp +++ b/Code/OysterPhysics3D/RigidBody.cpp @@ -51,12 +51,19 @@ void RigidBody::Update_LeapFrog( Float updateFrameLength ) // updating the linear //Decrease momentum with 1% as "fall-off" //! HACK: @todo Add real solution with fluid drag - this->momentum_Linear = this->momentum_Linear*0.9999f; - this->momentum_Angular = this->momentum_Angular*0.9999f; + this->momentum_Linear = this->momentum_Linear*0.99f; + this->momentum_Angular = this->momentum_Angular*0.99f; // ds = dt * Formula::LinearVelocity( m, avg_G ) = dt * avg_G / m = (dt / m) * avg_G - this->centerPos += ( updateFrameLength / this->mass ) * AverageWithDelta( this->momentum_Linear, this->impulse_Linear ); + Float3 delta = AverageWithDelta( this->momentum_Linear, this->impulse_Linear ); + Float3 newPos = ( updateFrameLength)*this->momentum_Linear; + this->centerPos += newPos; + if(this->mass == 70) + { + const char *breakpoint = "STOP"; + } + // updating the angular // dO = dt * Formula::AngularVelocity( (RI)^-1, avg_H ) = dt * (RI)^-1 * avg_H this->axis += updateFrameLength * this->momentOfInertiaTensor.CalculateAngularVelocity( this->rotation, AverageWithDelta(this->momentum_Angular, this->impulse_Angular) );