@@ -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
155163void 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
163177void 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
171191void 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
183209void 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
209223bool 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