Skip to content

Commit 5d8c3a5

Browse files
committed
1 parent a3d8df7 commit 5d8c3a5

29 files changed

Lines changed: 447 additions & 174 deletions

src/CameraController.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
class GameCamera;
4+
35
// base camera controller interface
46
class CameraController
57
{
@@ -8,7 +10,7 @@ class CameraController
810
{
911
}
1012
// reset camera and controller both to default state
11-
virtual void SetupInitial()
13+
virtual void Setup(GameCamera* gameCamera)
1214
{
1315
}
1416
// handle camera controller logic
@@ -30,4 +32,7 @@ class CameraController
3032
virtual void InputEvent(MouseScrollInputEvent& inputEvent)
3133
{
3234
}
35+
36+
public:
37+
GameCamera* mCamera = nullptr;
3338
};

src/Carnage3D.vcxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ copy "$(SDKDIR)\glfw\lib-vc2015\glfw3.dll" "$(TargetDir)"</Command>
158158
<ClInclude Include="AiCharacterController.h" />
159159
<ClInclude Include="aux_math.h" />
160160
<ClInclude Include="Convert.h" />
161+
<ClInclude Include="HumanCharacterView.h" />
161162
<ClInclude Include="macro.h" />
162163
<ClInclude Include="MemoryManager.h" />
163164
<ClInclude Include="mem_allocators.h" />
@@ -175,6 +176,7 @@ copy "$(SDKDIR)\glfw\lib-vc2015\glfw3.dll" "$(TargetDir)"</Command>
175176
<ClInclude Include="MapRenderer.h" />
176177
<ClInclude Include="GameMapManager.h" />
177178
<ClInclude Include="HumanCharacterController.h" />
179+
<ClInclude Include="RenderView.h" />
178180
<ClInclude Include="Sprite2D.h" />
179181
<ClInclude Include="SpriteBatch.h" />
180182
<ClInclude Include="StyleData.h" />
@@ -240,6 +242,7 @@ copy "$(SDKDIR)\glfw\lib-vc2015\glfw3.dll" "$(TargetDir)"</Command>
240242
<ClCompile Include="AiCharacterController.cpp" />
241243
<ClCompile Include="enums_impl.cpp" />
242244
<ClCompile Include="GameObjectsManager.cpp" />
245+
<ClCompile Include="HumanCharacterView.cpp" />
243246
<ClCompile Include="GpuBufferTexture.cpp" />
244247
<ClCompile Include="CharacterController.cpp" />
245248
<ClCompile Include="MemoryManager.cpp" />
@@ -251,6 +254,7 @@ copy "$(SDKDIR)\glfw\lib-vc2015\glfw3.dll" "$(TargetDir)"</Command>
251254
<ClCompile Include="GameMapHelpers.cpp" />
252255
<ClCompile Include="GameMapManager.cpp" />
253256
<ClCompile Include="HumanCharacterController.cpp" />
257+
<ClCompile Include="RenderView.cpp" />
254258
<ClCompile Include="Sprite2D.cpp" />
255259
<ClCompile Include="SpriteBatch.cpp" />
256260
<ClCompile Include="StyleData.cpp" />

src/Carnage3D.vcxproj.filters

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@
304304
<ClInclude Include="TrimeshBuffer.h">
305305
<Filter>Game\Rendering</Filter>
306306
</ClInclude>
307+
<ClInclude Include="RenderView.h">
308+
<Filter>Game\Rendering</Filter>
309+
</ClInclude>
310+
<ClInclude Include="HumanCharacterView.h">
311+
<Filter>Game</Filter>
312+
</ClInclude>
307313
</ItemGroup>
308314
<ItemGroup>
309315
<ClCompile Include="stdafx.cpp">
@@ -473,6 +479,12 @@
473479
<ClCompile Include="TrimeshBuffer.cpp">
474480
<Filter>Game\Rendering</Filter>
475481
</ClCompile>
482+
<ClCompile Include="HumanCharacterView.cpp">
483+
<Filter>Game</Filter>
484+
</ClCompile>
485+
<ClCompile Include="RenderView.cpp">
486+
<Filter>Game\Rendering</Filter>
487+
</ClCompile>
476488
</ItemGroup>
477489
<ItemGroup>
478490
<None Include="..\gamedata\config\sys_config.json.default">

src/CarnageGame.cpp

Lines changed: 117 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,22 @@ bool CarnageGame::Initialize()
4949
//glm::vec3 pos { 91.0f, 2.0f, 236.0f };
5050
//glm::vec3 pos { 121.0f, 2.0f, 200.0f };
5151
//glm::vec3 pos { 174.0f, 2.0f, 230.0f };
52-
glm::vec3 pos;
52+
53+
const int numPlayers = glm::clamp(gSystem.mStartupParams.mPlayersCount, 1, GAME_MAX_PLAYERS);
54+
gConsole.LogMessage(eLogMessage_Info, "Num players: %d", numPlayers);
55+
56+
glm::vec3 pos[GAME_MAX_PLAYERS];
5357

5458
// choose spawn point
5559
// it is temporary!
56-
for (int yBlock = 10; yBlock < 20; ++yBlock)
60+
int currFindPosIter = 0;
61+
for (int yBlock = 10; yBlock < 20 && currFindPosIter < numPlayers; ++yBlock)
5762
{
58-
for (int xBlock = 10; xBlock < 20; ++xBlock)
63+
for (int xBlock = 10; xBlock < 20 && currFindPosIter < numPlayers; ++xBlock)
5964
{
60-
pos = glm::ivec3(xBlock, MAP_LAYERS_COUNT - 1, yBlock);
65+
pos[currFindPosIter] = glm::ivec3(xBlock, MAP_LAYERS_COUNT - 1, yBlock);
6166

62-
float currHeight = gGameMap.GetHeightAtPosition(pos);
67+
float currHeight = gGameMap.GetHeightAtPosition(pos[currFindPosIter]);
6368
int zBlock = static_cast<int>(currHeight);
6469
if (zBlock > MAP_LAYERS_COUNT - 1)
6570
continue;
@@ -69,19 +74,21 @@ bool CarnageGame::Initialize()
6974
currBlock->mGroundType == eGroundType_Pawement ||
7075
currBlock->mGroundType == eGroundType_Road)
7176
{
72-
pos.x += MAP_BLOCK_LENGTH * 0.5f;
73-
pos.z += MAP_BLOCK_LENGTH * 0.5f;
74-
pos.y = currHeight;
75-
break;
77+
pos[currFindPosIter].x += MAP_BLOCK_LENGTH * 0.5f;
78+
pos[currFindPosIter].z += MAP_BLOCK_LENGTH * 0.5f;
79+
pos[currFindPosIter].y = currHeight;
80+
++currFindPosIter;
7681
}
7782
}
7883
}
7984

80-
mPlayerPedestrian = mObjectsManager.CreatePedestrian(pos);
81-
mHumanCharacters[0].SetCharacter(mPlayerPedestrian);
82-
83-
SetCameraController(&mFollowCameraController);
85+
for (int icurr = 0; icurr < numPlayers; ++icurr)
86+
{
87+
Pedestrian* pedestrian = mObjectsManager.CreatePedestrian(pos[icurr]);
88+
SetupHumanCharacter(icurr, pedestrian);
89+
}
8490

91+
SetupScreenLayout(numPlayers);
8592
mGameTime = 0;
8693
return true;
8794
}
@@ -101,9 +108,14 @@ void CarnageGame::UpdateFrame(Timespan deltaTime)
101108
gSpriteManager.UpdateBlocksAnimations(deltaTime);
102109
gPhysics.UpdateFrame(deltaTime);
103110
mObjectsManager.UpdateFrame(deltaTime);
104-
if (mCameraController)
111+
112+
for (int ihuman = 0; ihuman < GAME_MAX_PLAYERS; ++ihuman)
105113
{
106-
mCameraController->UpdateFrame(deltaTime);
114+
if (mHumanCharacters[ihuman].mCharPedestrian == nullptr)
115+
continue;
116+
117+
mHumanCharacters[ihuman].mCharController.UpdateFrame(mHumanCharacters[ihuman].mCharPedestrian, deltaTime);
118+
mHumanCharacters[ihuman].mCharView.UpdateFrame(deltaTime);
107119
}
108120
}
109121

@@ -135,44 +147,58 @@ void CarnageGame::InputEvent(KeyInputEvent& inputEvent)
135147
return;
136148
}
137149

138-
if (mCameraController)
150+
for (int ihuman = 0; ihuman < GAME_MAX_PLAYERS; ++ihuman)
139151
{
140-
mCameraController->InputEvent(inputEvent);
141-
}
152+
if (mHumanCharacters[ihuman].mCharPedestrian == nullptr)
153+
continue;
154+
155+
mHumanCharacters[ihuman].mCharController.InputEvent(inputEvent);
156+
mHumanCharacters[ihuman].mCharView.InputEvent(inputEvent);
142157

143-
for (HumanCharacterController& currentController: mHumanCharacters)
144-
{
145158
if (inputEvent.mConsumed)
146159
break;
147-
148-
if (currentController.mCharacter)
149-
{
150-
currentController.InputEvent(inputEvent);
151-
}
152160
}
153161
}
154162

155163
void CarnageGame::InputEvent(MouseButtonInputEvent& inputEvent)
156164
{
157-
if (mCameraController)
165+
for (int ihuman = 0; ihuman < GAME_MAX_PLAYERS; ++ihuman)
158166
{
159-
mCameraController->InputEvent(inputEvent);
167+
if (mHumanCharacters[ihuman].mCharPedestrian == nullptr)
168+
continue;
169+
170+
mHumanCharacters[ihuman].mCharView.InputEvent(inputEvent);
171+
172+
if (inputEvent.mConsumed)
173+
break;
160174
}
161175
}
162176

163177
void CarnageGame::InputEvent(MouseMovedInputEvent& inputEvent)
164178
{
165-
if (mCameraController)
179+
for (int ihuman = 0; ihuman < GAME_MAX_PLAYERS; ++ihuman)
166180
{
167-
mCameraController->InputEvent(inputEvent);
181+
if (mHumanCharacters[ihuman].mCharPedestrian == nullptr)
182+
continue;
183+
184+
mHumanCharacters[ihuman].mCharView.InputEvent(inputEvent);
185+
186+
if (inputEvent.mConsumed)
187+
break;
168188
}
169189
}
170190

171191
void CarnageGame::InputEvent(MouseScrollInputEvent& inputEvent)
172192
{
173-
if (mCameraController)
193+
for (int ihuman = 0; ihuman < GAME_MAX_PLAYERS; ++ihuman)
174194
{
175-
mCameraController->InputEvent(inputEvent);
195+
if (mHumanCharacters[ihuman].mCharPedestrian == nullptr)
196+
continue;
197+
198+
mHumanCharacters[ihuman].mCharView.InputEvent(inputEvent);
199+
200+
if (inputEvent.mConsumed)
201+
break;
176202
}
177203
}
178204

@@ -182,40 +208,28 @@ void CarnageGame::InputEvent(KeyCharEvent& inputEvent)
182208

183209
void CarnageGame::InputEvent(GamepadInputEvent& inputEvent)
184210
{
185-
for (HumanCharacterController& currentController: mHumanCharacters)
211+
for (int ihuman = 0; ihuman < GAME_MAX_PLAYERS; ++ihuman)
186212
{
213+
if (mHumanCharacters[ihuman].mCharPedestrian == nullptr)
214+
continue;
215+
216+
mHumanCharacters[ihuman].mCharController.InputEvent(inputEvent);
217+
187218
if (inputEvent.mConsumed)
188219
break;
189-
190-
if (currentController.mCharacter)
191-
{
192-
currentController.InputEvent(inputEvent);
193-
}
194-
}
195-
}
196-
197-
void CarnageGame::SetCameraController(CameraController* controller)
198-
{
199-
if (mCameraController == controller)
200-
return;
201-
202-
mCameraController = controller;
203-
if (mCameraController)
204-
{
205-
mCameraController->SetupInitial();
206220
}
207221
}
208222

209223
bool CarnageGame::SetInputActionsFromConfig()
210224
{
211225
// force default mapping for first player
212-
for (int iplayer = 0; iplayer < GAME_MAX_PLAYERS; ++iplayer)
226+
for (int ihuman = 0; ihuman < GAME_MAX_PLAYERS; ++ihuman)
213227
{
214-
HumanCharacterController& currentChar = mHumanCharacters[iplayer];
215-
currentChar.mInputs.SetNull();
216-
if (iplayer == 0)
228+
HumanCharacterSlot& currentChar = mHumanCharacters[ihuman];
229+
currentChar.mCharController.mInputs.SetNull();
230+
if (ihuman == 0)
217231
{
218-
currentChar.mInputs.SetDefaults();
232+
currentChar.mCharController.mInputs.SetDefaults();
219233
}
220234
}
221235

@@ -235,15 +249,60 @@ bool CarnageGame::SetInputActionsFromConfig()
235249
}
236250

237251
cxx::string_buffer_32 tempString;
238-
for (int iplayer = 0; iplayer < GAME_MAX_PLAYERS; ++iplayer)
252+
for (int ihuman = 0; ihuman < GAME_MAX_PLAYERS; ++ihuman)
239253
{
240-
HumanCharacterController& currentChar = mHumanCharacters[iplayer];
254+
HumanCharacterController& currentChar = mHumanCharacters[ihuman].mCharController;
241255

242-
tempString.printf("player%d", iplayer + 1);
256+
tempString.printf("player%d", ihuman + 1);
243257

244258
cxx::config_node configNode = configDocument.get_root_node().get_child(tempString.c_str());
245259
currentChar.mInputs.SetFromConfig(configNode);
246260
}
247261

248262
return true;
249-
}
263+
}
264+
265+
void CarnageGame::SetupHumanCharacter(int humanIndex, Pedestrian* pedestrian)
266+
{
267+
// todo: what a mess
268+
debug_assert(humanIndex < GAME_MAX_PLAYERS);
269+
debug_assert(pedestrian);
270+
debug_assert(mHumanCharacters[humanIndex].mCharPedestrian == nullptr);
271+
if (mHumanCharacters[humanIndex].mCharPedestrian)
272+
return;
273+
274+
mHumanCharacters[humanIndex].mCharPedestrian = pedestrian;
275+
mHumanCharacters[humanIndex].mCharController.SetCharacter(pedestrian);
276+
mHumanCharacters[humanIndex].mCharView.mFollowCameraController.SetFollowTarget(pedestrian);
277+
}
278+
279+
void CarnageGame::SetupScreenLayout(int playersCount)
280+
{
281+
// todo: what a mess
282+
const int MaxCols = 2;
283+
284+
Rect2D fullViewport = gGraphicsDevice.mViewportRect;
285+
286+
int numRows = (playersCount + MaxCols - 1) / MaxCols;
287+
debug_assert(numRows > 0);
288+
289+
int frameSizePerH = fullViewport.h / numRows;
290+
291+
for (int icurr = 0; icurr < playersCount; ++icurr)
292+
{
293+
int currRow = icurr / MaxCols;
294+
int currCol = icurr % MaxCols;
295+
296+
int colsOnCurrentRow = ((playersCount - currRow) + MaxCols - 1) / MaxCols;
297+
int frameSizePerW = fullViewport.w / colsOnCurrentRow;
298+
299+
mHumanCharacters[icurr].mCharView.mRenderCamera.mViewportRect.h = frameSizePerH;
300+
mHumanCharacters[icurr].mCharView.mRenderCamera.mViewportRect.x = currCol * (frameSizePerW + 1);
301+
mHumanCharacters[icurr].mCharView.mRenderCamera.mViewportRect.y = (numRows - currRow - 1) * (frameSizePerH + 1);
302+
mHumanCharacters[icurr].mCharView.mRenderCamera.mViewportRect.w = frameSizePerW;
303+
304+
mHumanCharacters[icurr].mCharView.SetCameraController(&mHumanCharacters[icurr].mCharView.mFollowCameraController);
305+
gRenderManager.AttachRenderView(&mHumanCharacters[icurr].mCharView);
306+
}
307+
308+
}

0 commit comments

Comments
 (0)