using System; using System.Collections.Generic; using System.Linq; using System.IO; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using System.Xml.Linq; namespace Barotrauma { class CrewManager { public List characters; public List characterInfos; private int money; private GUIFrame guiFrame; private GUIListBox listBox; private bool crewFrameOpen; private GUIButton crewButton; protected GUIFrame crewFrame; private CrewCommander commander; public int Money { get { return money; } set { money = (int)Math.Max(value, 0.0f); } } public CrewManager() { characters = new List(); characterInfos = new List(); guiFrame = new GUIFrame(new Rectangle(0, 50, 150, 450), Color.Transparent); listBox = new GUIListBox(new Rectangle(0, 30, 150, 0), Color.Transparent, null, guiFrame); listBox.ScrollBarEnabled = false; listBox.OnSelected = SelectCharacter; crewButton = new GUIButton(new Rectangle(0, 00, 100, 20), "Crew", GUI.Style, guiFrame); crewButton.OnClicked = ToggleCrewFrame; commander = new CrewCommander(this); money = 10000; } public CrewManager(XElement element) : this() { money = ToolBox.GetAttributeInt(element, "money", 0); foreach (XElement subElement in element.Elements()) { if (subElement.Name.ToString().ToLower()!="character") continue; characterInfos.Add(new CharacterInfo(subElement)); } } public bool SelectCharacter(GUIComponent component, object selection) { //listBox.Select(selection); Character character = selection as Character; if (character == null) return false; if (characters.Contains(character)) { Character.Controlled = character; return true; } return false; } public void AddCharacter(Character character) { characters.Add(character); if (!characterInfos.Contains(character.Info)) { characterInfos.Add(character.Info); } if (character is AICharacter) { commander.UpdateCharacters(); } character.Info.CreateCharacterFrame(listBox, character.Info.Name.Replace(' ', '\n'), character); //GUIFrame frame = new GUIFrame(new Rectangle(0, 0, 0, 40), Color.Transparent, null, listBox); //frame.UserData = character; //frame.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f); //frame.HoverColor = Color.LightGray * 0.5f; //frame.SelectedColor = Color.Gold * 0.5f; //string name = character.Info.Name.Replace(' ', '\n'); //GUITextBlock textBlock = new GUITextBlock( // new Rectangle(40, 0, 0, 25), // name, // Color.Transparent, Color.White, // Alignment.Left, Alignment.Left, // null, frame, false); //textBlock.Font = GUI.SmallFont; //textBlock.Padding = new Vector4(5.0f, 0.0f, 5.0f, 0.0f); //new GUIImage(new Rectangle(-10, -5, 0, 0), character.AnimController.Limbs[0].sprite, Alignment.Left, frame); } public void Update(float deltaTime) { guiFrame.Update(deltaTime); if (PlayerInput.KeyHit(Microsoft.Xna.Framework.Input.Keys.C)) { //only allow opening the command UI if there are AICharacters in the crew if (commander.IsOpen || characters.Any(c => c is AICharacter)) commander.ToggleGUIFrame(); } if (commander.Frame != null) commander.Frame.Update(deltaTime); if (crewFrameOpen) crewFrame.Update(deltaTime); } public void KillCharacter(Character killedCharacter) { GUIComponent characterBlock = listBox.GetChild(killedCharacter) as GUIComponent; if (characterBlock != null) characterBlock.Color = Color.DarkRed * 0.5f; if (killedCharacter is AICharacter) { commander.UpdateCharacters(); } //if (characters.Find(c => !c.IsDead)==null) //{ // Game1.GameSession.EndShift(null, null); //} } public void CreateCrewFrame(List crew) { int width = 600, height = 400; crewFrame = new GUIFrame(new Rectangle(GameMain.GraphicsWidth / 2 - width / 2, GameMain.GraphicsHeight / 2 - height / 2, width, height), GUI.Style); crewFrame.Padding = new Vector4(10.0f, 10.0f, 10.0f, 10.0f); GUIListBox crewList = new GUIListBox(new Rectangle(0, 0, 280, 300), Color.White * 0.7f, GUI.Style, crewFrame); crewList.Padding = new Vector4(10.0f, 10.0f, 10.0f, 10.0f); crewList.OnSelected = SelectCrewCharacter; foreach (Character character in crew) { GUIFrame frame = new GUIFrame(new Rectangle(0, 0, 0, 40), Color.Transparent, null, crewList); frame.UserData = character; frame.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f); frame.Color = (GameMain.NetworkMember != null && GameMain.NetworkMember.Character == character) ? Color.Gold * 0.2f : Color.Transparent; frame.HoverColor = Color.LightGray * 0.5f; frame.SelectedColor = Color.Gold * 0.5f; GUITextBlock textBlock = new GUITextBlock( new Rectangle(40, 0, 0, 25), character.Info.Name + " (" + character.Info.Job.Name + ")", Color.Transparent, Color.White, Alignment.Left, Alignment.Left, null, frame); textBlock.Padding = new Vector4(5.0f, 0.0f, 5.0f, 0.0f); new GUIImage(new Rectangle(-10, 0, 0, 0), character.AnimController.Limbs[0].sprite, Alignment.Left, frame); } var closeButton = new GUIButton(new Rectangle(0, 0, 80, 20), "Close", Alignment.BottomCenter, GUI.Style, crewFrame); closeButton.OnClicked = ToggleCrewFrame; } protected virtual bool SelectCrewCharacter(GUIComponent component, object obj) { Character character = obj as Character; if (character == null) return false; GUIComponent existingFrame = crewFrame.FindChild("selectedcharacter"); if (existingFrame != null) crewFrame.RemoveChild(existingFrame); var previewPlayer = new GUIFrame( new Rectangle(0, 0, 230, 300), new Color(0.0f, 0.0f, 0.0f, 0.8f), Alignment.TopRight, GUI.Style, crewFrame); previewPlayer.Padding = new Vector4(5.0f, 5.0f, 5.0f, 5.0f); previewPlayer.UserData = "selectedcharacter"; character.Info.CreateInfoFrame(previewPlayer); if (GameMain.NetworkMember != null) GameMain.NetworkMember.SelectCrewCharacter(component, obj); return true; } private bool ToggleCrewFrame(GUIButton button, object obj) { if (crewFrame == null) CreateCrewFrame(characters); crewFrameOpen = !crewFrameOpen; return true; } public void StartShift() { listBox.ClearChildren(); characters.Clear(); WayPoint[] waypoints = WayPoint.SelectCrewSpawnPoints(characterInfos); for (int i = 0; i < waypoints.Length; i++) { //WayPoint randomWayPoint = WayPoint.GetRandom(SpawnType.Human); //Vector2 position = (randomWayPoint == null) ? Vector2.Zero : randomWayPoint.SimPosition; Character character = Character.Create(characterInfos[i], waypoints[i].WorldPosition); Character.Controlled = character; if (!character.Info.StartItemsGiven) { character.GiveJobItems(waypoints[i]); character.Info.StartItemsGiven = true; } AddCharacter(character); } if (characters.Any()) listBox.Select(0);// SelectCharacter(null, characters[0]); } public void EndShift() { foreach (Character c in characters) { if (!c.IsDead) { c.Info.UpdateCharacterItems(); continue; } CharacterInfo deadInfo = characterInfos.Find(x => c.Info == x); if (deadInfo != null) characterInfos.Remove(deadInfo); } characters.Clear(); listBox.ClearChildren(); } public void Draw(SpriteBatch spriteBatch) { if (commander.IsOpen) { commander.Draw(spriteBatch); } else { guiFrame.Draw(spriteBatch); if (crewFrameOpen) crewFrame.Draw(spriteBatch); } } public void Save(XElement parentElement) { XElement element = new XElement("crew"); element.Add(new XAttribute("money", money)); foreach (CharacterInfo ci in characterInfos) { ci.Save(element); } parentElement.Add(element); } } }