Fixed characters not appearing in the round summary in mp, minor CrewManager refactoring

This commit is contained in:
Joonas Rikkonen
2017-11-20 20:21:18 +02:00
parent 765587efd7
commit fce7d43ef3
13 changed files with 89 additions and 57 deletions
@@ -198,8 +198,8 @@
<Compile Include="Source\Screens\BlurEffect.cs" /> <Compile Include="Source\Screens\BlurEffect.cs" />
<Compile Include="Source\Screens\CampaignSetupUI.cs" /> <Compile Include="Source\Screens\CampaignSetupUI.cs" />
<Compile Include="Source\Screens\CampaignUI.cs" /> <Compile Include="Source\Screens\CampaignUI.cs" />
<Compile Include="Source\Screens\EditCharacterScreen.cs" /> <Compile Include="Source\Screens\CharacterEditorScreen.cs" />
<Compile Include="Source\Screens\EditMapScreen.cs" /> <Compile Include="Source\Screens\SubEditorScreen.cs" />
<Compile Include="Source\Screens\GameScreen.cs" /> <Compile Include="Source\Screens\GameScreen.cs" />
<Compile Include="Source\Screens\LobbyScreen.cs" /> <Compile Include="Source\Screens\LobbyScreen.cs" />
<Compile Include="Source\Screens\MainMenuScreen.cs" /> <Compile Include="Source\Screens\MainMenuScreen.cs" />
@@ -148,7 +148,7 @@ namespace Barotrauma
frame.RemoveChild(child); frame.RemoveChild(child);
} }
List<Character> aliveCharacters = crewManager.characters.FindAll(c => !c.IsDead && c.AIController is HumanAIController); List<Character> aliveCharacters = crewManager.GetCharacters().FindAll(c => !c.IsDead && c.AIController is HumanAIController);
characterFrameBottom = 0; characterFrameBottom = 0;
@@ -192,9 +192,9 @@ namespace Barotrauma
if (controlled == this) controlled = null; if (controlled == this) controlled = null;
if (GameMain.GameSession?.CrewManager != null && if (GameMain.GameSession?.CrewManager != null &&
GameMain.GameSession.CrewManager.characters.Contains(this)) GameMain.GameSession.CrewManager.GetCharacters().Contains(this))
{ {
GameMain.GameSession.CrewManager.characters.Remove(this); GameMain.GameSession.CrewManager.RemoveCharacter(this);
} }
if (GameMain.Client != null && GameMain.Client.Character == this) GameMain.Client.Character = null; if (GameMain.Client != null && GameMain.Client.Character == this) GameMain.Client.Character = null;
@@ -291,7 +291,7 @@ namespace Barotrauma
if (configPath == Character.HumanConfigFile) if (configPath == Character.HumanConfigFile)
{ {
GameMain.GameSession.CrewManager.characters.Add(character); GameMain.GameSession.CrewManager.AddCharacter(character);
} }
} }
@@ -9,8 +9,8 @@ namespace Barotrauma
{ {
class CrewManager class CrewManager
{ {
public List<Character> characters; private List<CharacterInfo> characterInfos;
public List<CharacterInfo> CharacterInfos; private List<Character> characters;
public int WinningTeam = 1; public int WinningTeam = 1;
@@ -27,7 +27,7 @@ namespace Barotrauma
public CrewManager() public CrewManager()
{ {
characters = new List<Character>(); characters = new List<Character>();
CharacterInfos = new List<CharacterInfo>(); characterInfos = new List<CharacterInfo>();
guiFrame = new GUIFrame(new Rectangle(0, 50, 150, 450), Color.Transparent); guiFrame = new GUIFrame(new Rectangle(0, 50, 150, 450), Color.Transparent);
guiFrame.Padding = Vector4.One * 5.0f; guiFrame.Padding = Vector4.One * 5.0f;
@@ -50,10 +50,20 @@ namespace Barotrauma
{ {
if (subElement.Name.ToString().ToLowerInvariant() != "character") continue; if (subElement.Name.ToString().ToLowerInvariant() != "character") continue;
CharacterInfos.Add(new CharacterInfo(subElement)); characterInfos.Add(new CharacterInfo(subElement));
} }
} }
public List<Character> GetCharacters()
{
return characters;
}
public List<CharacterInfo> GetCharacterInfos()
{
return characterInfos;
}
public bool SelectCharacter(GUIComponent component, object selection) public bool SelectCharacter(GUIComponent component, object selection)
{ {
//listBox.Select(selection); //listBox.Select(selection);
@@ -108,28 +118,44 @@ namespace Barotrauma
public void AddCharacter(Character character) public void AddCharacter(Character character)
{ {
characters.Add(character); characters.Add(character);
if (!CharacterInfos.Contains(character.Info)) if (!characterInfos.Contains(character.Info))
{ {
CharacterInfos.Add(character.Info); characterInfos.Add(character.Info);
} }
if (character is AICharacter) if (character is AICharacter)
{ {
commander.UpdateCharacters(); commander.UpdateCharacters();
character.Info.CreateCharacterFrame(listBox, character.Info.Name.Replace(' ', '\n'), character);
GUIFrame orderFrame = new GUIFrame(new Rectangle(0, 0, 40, 40), Color.Transparent, "ListBoxElement", orderListBox);
orderFrame.UserData = character;
var ai = character.AIController as HumanAIController;
if (ai == null)
{
DebugConsole.ThrowError("Error in crewmanager - attempted to give orders to a character with no HumanAIController");
return;
}
SetCharacterOrder(character, ai.CurrentOrder);
} }
}
character.Info.CreateCharacterFrame(listBox, character.Info.Name.Replace(' ', '\n'), character); public void RemoveCharacter(Character character)
{
characters.Remove(character);
}
GUIFrame orderFrame = new GUIFrame(new Rectangle(0, 0, 40, 40), Color.Transparent, "ListBoxElement", orderListBox); public void AddCharacterInfo(CharacterInfo characterInfo)
orderFrame.UserData = character; {
if (characterInfos.Contains(characterInfo))
var ai = character.AIController as HumanAIController;
if (ai == null)
{ {
DebugConsole.ThrowError("Error in crewmanager - attempted to give orders to a character with no HumanAIController"); DebugConsole.ThrowError("Tried to add the same character info to CrewManager twice.\n" +Environment.StackTrace);
return; return;
} }
SetCharacterOrder(character, ai.CurrentOrder);
characterInfos.Add(characterInfo);
} }
public void AddToGUIUpdateList() public void AddToGUIUpdateList()
@@ -181,10 +207,6 @@ namespace Barotrauma
{ {
commander.UpdateCharacters(); commander.UpdateCharacters();
} }
//if (characters.Find(c => !c.IsDead)==null)
//{
// Game1.GameSession.EndShift(null, null);
//}
} }
public void CreateCrewFrame(List<Character> crew, GUIFrame crewFrame) public void CreateCrewFrame(List<Character> crew, GUIFrame crewFrame)
@@ -231,8 +253,6 @@ namespace Barotrauma
y += crewList.Rect.Height + 30; y += crewList.Rect.Height + 30;
} }
} }
protected virtual bool SelectCrewCharacter(Character character, GUIComponent crewList) protected virtual bool SelectCrewCharacter(Character character, GUIComponent crewList)
@@ -255,34 +275,27 @@ namespace Barotrauma
return true; return true;
} }
//private bool ToggleCrewFrame(GUIButton button, object obj)
//{
// if (crewFrame == null) CreateCrewFrame(characters);
// crewFrameOpen = !crewFrameOpen;
// return true;
//}
public void StartRound() public void StartRound()
{ {
listBox.ClearChildren(); listBox.ClearChildren();
characters.Clear(); characters.Clear();
WayPoint[] waypoints = WayPoint.SelectCrewSpawnPoints(CharacterInfos, Submarine.MainSub, false); WayPoint[] waypoints = WayPoint.SelectCrewSpawnPoints(characterInfos, Submarine.MainSub, false);
for (int i = 0; i < waypoints.Length; i++) for (int i = 0; i < waypoints.Length; i++)
{ {
Character character; Character character;
if (CharacterInfos[i].HullID != null) if (characterInfos[i].HullID != null)
{ {
var hull = Entity.FindEntityByID((ushort)CharacterInfos[i].HullID) as Hull; var hull = Entity.FindEntityByID((ushort)characterInfos[i].HullID) as Hull;
if (hull == null) continue; if (hull == null) continue;
character = Character.Create(CharacterInfos[i], hull.WorldPosition); character = Character.Create(characterInfos[i], hull.WorldPosition);
} }
else else
{ {
character = Character.Create(CharacterInfos[i], waypoints[i].WorldPosition); character = Character.Create(characterInfos[i], waypoints[i].WorldPosition);
Character.Controlled = character; Character.Controlled = character;
if (character.Info != null && !character.Info.StartItemsGiven) if (character.Info != null && !character.Info.StartItemsGiven)
@@ -308,18 +321,26 @@ namespace Barotrauma
continue; continue;
} }
CharacterInfos.Remove(c.Info); characterInfos.Remove(c.Info);
} }
//remove characterinfos whose character doesn't exist anymore //remove characterinfos whose character doesn't exist anymore
//(i.e. character was removed during the round) //(i.e. character was removed during the round)
CharacterInfos.RemoveAll(c => c.Character == null); characterInfos.RemoveAll(c => c.Character == null);
characters.Clear(); characters.Clear();
listBox.ClearChildren(); listBox.ClearChildren();
orderListBox.ClearChildren(); orderListBox.ClearChildren();
} }
public void Reset()
{
characters.Clear();
characterInfos.Clear();
listBox.ClearChildren();
orderListBox.ClearChildren();
}
public void Draw(SpriteBatch spriteBatch) public void Draw(SpriteBatch spriteBatch)
{ {
if (commander.IsOpen) if (commander.IsOpen)
@@ -336,7 +357,7 @@ namespace Barotrauma
{ {
XElement element = new XElement("crew"); XElement element = new XElement("crew");
foreach (CharacterInfo ci in CharacterInfos) foreach (CharacterInfo ci in characterInfos)
{ {
ci.Save(element); ci.Save(element);
} }
@@ -48,7 +48,7 @@ namespace Barotrauma
break; break;
} }
CrewManager.CharacterInfos.Add(new CharacterInfo(Character.HumanConfigFile, "", Gender.None, jobPrefab)); CrewManager.AddCharacterInfo(new CharacterInfo(Character.HumanConfigFile, "", Gender.None, jobPrefab));
} }
} }
@@ -74,7 +74,7 @@ namespace Barotrauma
if (Money < characterInfo.Salary) return false; if (Money < characterInfo.Salary) return false;
hireManager.availableCharacters.Remove(characterInfo); hireManager.availableCharacters.Remove(characterInfo);
CrewManager.CharacterInfos.Add(characterInfo); CrewManager.AddCharacterInfo(characterInfo);
Money -= characterInfo.Salary; Money -= characterInfo.Salary;
return true; return true;
@@ -155,7 +155,7 @@ namespace Barotrauma
if (!crewDead) if (!crewDead)
{ {
if (!CrewManager.characters.Any(c => !c.IsDead)) crewDead = true; if (!CrewManager.GetCharacters().Any(c => !c.IsDead)) crewDead = true;
} }
else else
{ {
@@ -169,7 +169,7 @@ namespace Barotrauma
{ {
isRunning = false; isRunning = false;
bool success = CrewManager.characters.Any(c => !c.IsDead); bool success = CrewManager.GetCharacters().Any(c => !c.IsDead);
if (success) if (success)
{ {
@@ -290,7 +290,7 @@ namespace Barotrauma
var cinematic = new TransitionCinematic(leavingSubs, GameMain.GameScreen.Cam, 5.0f); var cinematic = new TransitionCinematic(leavingSubs, GameMain.GameScreen.Cam, 5.0f);
SoundPlayer.OverrideMusicType = CrewManager.characters.Any(c => !c.IsDead) ? "endround" : "crewdead"; SoundPlayer.OverrideMusicType = CrewManager.GetCharacters().Any(c => !c.IsDead) ? "endround" : "crewdead";
CoroutineManager.StartCoroutine(EndCinematic(cinematic), "EndCinematic"); CoroutineManager.StartCoroutine(EndCinematic(cinematic), "EndCinematic");
@@ -77,7 +77,7 @@ namespace Barotrauma
switch (selectedTab) switch (selectedTab)
{ {
case InfoFrameTab.Crew: case InfoFrameTab.Crew:
CrewManager.CreateCrewFrame(CrewManager.characters, infoFrame.children[0] as GUIFrame); CrewManager.CreateCrewFrame(CrewManager.GetCharacters(), infoFrame.children[0] as GUIFrame);
break; break;
case InfoFrameTab.Mission: case InfoFrameTab.Mission:
CreateMissionInfo(infoFrame.children[0] as GUIFrame); CreateMissionInfo(infoFrame.children[0] as GUIFrame);
@@ -26,7 +26,7 @@ namespace Barotrauma
{ {
bool singleplayer = GameMain.NetworkMember == null; bool singleplayer = GameMain.NetworkMember == null;
bool gameOver = gameSession.CrewManager.characters.All(c => c.IsDead); bool gameOver = gameSession.CrewManager.GetCharacters().All(c => c.IsDead);
bool progress = Submarine.MainSub.AtEndPosition; bool progress = Submarine.MainSub.AtEndPosition;
GUIFrame frame = new GUIFrame(new Rectangle(0, 0, GameMain.GraphicsWidth, GameMain.GraphicsHeight), Color.Black * 0.8f, null); GUIFrame frame = new GUIFrame(new Rectangle(0, 0, GameMain.GraphicsWidth, GameMain.GraphicsHeight), Color.Black * 0.8f, null);
@@ -59,7 +59,7 @@ namespace Barotrauma
GUIListBox listBox = new GUIListBox(new Rectangle(0,y,0,90), null, Alignment.TopLeft, "", innerFrame, true); GUIListBox listBox = new GUIListBox(new Rectangle(0,y,0,90), null, Alignment.TopLeft, "", innerFrame, true);
int x = 0; int x = 0;
foreach (CharacterInfo characterInfo in gameSession.CrewManager.CharacterInfos) foreach (CharacterInfo characterInfo in gameSession.CrewManager.GetCharacterInfos())
{ {
if (GameMain.GameSession.Mission is CombatMission && if (GameMain.GameSession.Mission is CombatMission &&
characterInfo.TeamID != GameMain.GameSession.CrewManager.WinningTeam) characterInfo.TeamID != GameMain.GameSession.CrewManager.WinningTeam)
@@ -718,6 +718,7 @@ namespace Barotrauma.Networking
} }
else else
{ {
if (GameMain.GameSession?.CrewManager != null) GameMain.GameSession.CrewManager.Reset();
GameMain.GameSession.StartRound(campaign.Map.SelectedConnection.Level, true, false); GameMain.GameSession.StartRound(campaign.Map.SelectedConnection.Level, true, false);
} }
@@ -181,7 +181,7 @@ namespace Barotrauma
public void UpdateCharacterLists() public void UpdateCharacterLists()
{ {
characterList.ClearChildren(); characterList.ClearChildren();
foreach (CharacterInfo c in GameMain.GameSession.CrewManager.CharacterInfos) foreach (CharacterInfo c in GameMain.GameSession.CrewManager.GetCharacterInfos())
{ {
c.CreateCharacterFrame(characterList, c.Name + " (" + c.Job.Name + ") ", c); c.CreateCharacterFrame(characterList, c.Name + " (" + c.Job.Name + ") ", c);
} }
@@ -186,6 +186,9 @@ namespace Barotrauma
if (GameMain.Client != null) if (GameMain.Client != null)
{ {
GameMain.GameSession.EndRound(""); GameMain.GameSession.EndRound("");
#if CLIENT
GameMain.GameSession.CrewManager.EndRound();
#endif
return; return;
} }
@@ -221,6 +224,10 @@ namespace Barotrauma
} }
} }
#if CLIENT
GameMain.GameSession.CrewManager.EndRound();
#endif
if (success) if (success)
{ {
bool atEndPosition = Submarine.MainSub.AtEndPosition; bool atEndPosition = Submarine.MainSub.AtEndPosition;
@@ -1189,6 +1189,9 @@ namespace Barotrauma.Networking
if (campaign != null) if (campaign != null)
{ {
#if CLIENT
if (GameMain.GameSession?.CrewManager != null) GameMain.GameSession.CrewManager.Reset();
#endif
GameMain.GameSession.StartRound(campaign.Map.SelectedConnection.Level, true, teamCount > 1); GameMain.GameSession.StartRound(campaign.Map.SelectedConnection.Level, true, teamCount > 1);
} }
else else
@@ -1258,7 +1261,7 @@ namespace Barotrauma.Networking
teamClients[i].Character = spawnedCharacter; teamClients[i].Character = spawnedCharacter;
#if CLIENT #if CLIENT
GameMain.GameSession.CrewManager.characters.Add(spawnedCharacter); GameMain.GameSession.CrewManager.AddCharacter(spawnedCharacter);
#endif #endif
} }
@@ -1271,7 +1274,7 @@ namespace Barotrauma.Networking
Character.Controlled = myCharacter; Character.Controlled = myCharacter;
GameMain.GameSession.CrewManager.characters.Add(myCharacter); GameMain.GameSession.CrewManager.AddCharacter(myCharacter);
} }
#endif #endif
} }
@@ -481,10 +481,10 @@ namespace Barotrauma.Networking
if (scooterPrefab != null && batteryPrefab != null) if (scooterPrefab != null && batteryPrefab != null)
{ {
var scooter = new Item(scooterPrefab, pos, respawnShuttle); var scooter = new Item(scooterPrefab, pos, respawnShuttle);
Entity.Spawner.CreateNetworkEvent(scooter, false); Spawner.CreateNetworkEvent(scooter, false);
var battery = new Item(batteryPrefab, pos, respawnShuttle); var battery = new Item(batteryPrefab, pos, respawnShuttle);
Entity.Spawner.CreateNetworkEvent(battery, false); Spawner.CreateNetworkEvent(battery, false);
scooter.Combine(battery); scooter.Combine(battery);
} }
@@ -502,7 +502,7 @@ namespace Barotrauma.Networking
} }
} }
#if CLIENT #if CLIENT
GameMain.GameSession.CrewManager.characters.Add(character); GameMain.GameSession.CrewManager.AddCharacter(character);
#endif #endif
} }