From 46ce6bf45e36047fe338a2a9ead1d29628669781 Mon Sep 17 00:00:00 2001 From: DamianX Date: Fri, 24 Jan 2020 00:56:26 +0100 Subject: [PATCH] Implemented random character creation (#548) * Implemented random character creation * Pick from a list and apply a bit of randomness instead * Rename SetInitialData, unselect list entries properly --- .../MagicMirrorBoundUserInterface.cs | 11 +-- .../HumanoidProfileEditor.Random.cs | 67 ++++++++++++++++ .../UserInterface/HumanoidProfileEditor.cs | 76 ++++++++++--------- .../Preferences/Appearance/HairStyles.cs | 11 +++ .../Preferences/HumanoidCharacterProfile.cs | 2 + 5 files changed, 125 insertions(+), 42 deletions(-) create mode 100644 Content.Client/UserInterface/HumanoidProfileEditor.Random.cs diff --git a/Content.Client/GameObjects/Components/MagicMirrorBoundUserInterface.cs b/Content.Client/GameObjects/Components/MagicMirrorBoundUserInterface.cs index 93eb3d6184..4c9245b9b3 100644 --- a/Content.Client/GameObjects/Components/MagicMirrorBoundUserInterface.cs +++ b/Content.Client/GameObjects/Components/MagicMirrorBoundUserInterface.cs @@ -97,7 +97,7 @@ namespace Content.Client.GameObjects.Components private Color _lastColor; - public void SetInitialData(Color color, string styleName) + public void SetData(Color color, string styleName) { _lastColor = color; @@ -107,10 +107,7 @@ namespace Content.Client.GameObjects.Components foreach (var item in Items) { - if (item.Text == styleName) - { - item.Selected = true; - } + item.Selected = item.Text == styleName; } UpdateStylePickerColor(); @@ -301,8 +298,8 @@ namespace Content.Client.GameObjects.Components public void SetInitialData(MagicMirrorInitialDataMessage initialData) { - _facialHairStylePicker.SetInitialData(initialData.FacialHairColor, initialData.FacialHairName); - _hairStylePicker.SetInitialData(initialData.HairColor, initialData.HairName); + _facialHairStylePicker.SetData(initialData.FacialHairColor, initialData.FacialHairName); + _hairStylePicker.SetData(initialData.HairColor, initialData.HairName); } } } diff --git a/Content.Client/UserInterface/HumanoidProfileEditor.Random.cs b/Content.Client/UserInterface/HumanoidProfileEditor.Random.cs new file mode 100644 index 0000000000..2a563cc457 --- /dev/null +++ b/Content.Client/UserInterface/HumanoidProfileEditor.Random.cs @@ -0,0 +1,67 @@ +using System.Linq; +using Content.Shared.Preferences; +using Content.Shared.Preferences.Appearance; +using Content.Shared.Text; +using Robust.Shared.Interfaces.Random; +using Robust.Shared.Random; + +namespace Content.Client.UserInterface +{ + public partial class HumanoidProfileEditor + { + private readonly IRobustRandom _random; + + private void RandomizeEverything() + { + RandomizeSex(); + RandomizeAge(); + RandomizeName(); + RandomizeAppearance(); + } + + private void RandomizeSex() + { + SetSex(_random.Prob(0.5f) ? Sex.Male : Sex.Female); + UpdateSexControls(); + } + + private void RandomizeAge() + { + SetAge(_random.Next(HumanoidCharacterProfile.MinimumAge, HumanoidCharacterProfile.MaximumAge)); + UpdateAgeEdit(); + } + + private void RandomizeName() + { + var firstName = _random.Pick(Profile.Sex == Sex.Male + ? Names.MaleFirstNames + : Names.FemaleFirstNames); + var lastName = _random.Pick(Names.LastNames); + SetName($"{firstName} {lastName}"); + UpdateNameEdit(); + } + + private void RandomizeAppearance() + { + var newHairStyle = _random.Pick(HairStyles.HairStylesMap.Keys.ToList()); + + var newFacialHairStyle = Profile.Sex == Sex.Female + ? HairStyles.DefaultFacialHairStyle + : _random.Pick(HairStyles.FacialHairStylesMap.Keys.ToList()); + + var newHairColor = _random.Pick(HairStyles.RealisticHairColors); + newHairColor = newHairColor + .WithRed(newHairColor.R + _random.Next(-25, 25) / 100f) + .WithGreen(newHairColor.G + _random.Next(-25, 25) / 100f) + .WithBlue(newHairColor.B + _random.Next(-25, 25) / 100f); + + Profile = Profile.WithCharacterAppearance( + Profile.Appearance + .WithHairStyleName(newHairStyle) + .WithFacialHairStyleName(newFacialHairStyle) + .WithHairColor(newHairColor) + .WithFacialHairColor(newHairColor)); + UpdateHairPickers(); + } + } +} diff --git a/Content.Client/UserInterface/HumanoidProfileEditor.cs b/Content.Client/UserInterface/HumanoidProfileEditor.cs index 709fd1629a..630573317b 100644 --- a/Content.Client/UserInterface/HumanoidProfileEditor.cs +++ b/Content.Client/UserInterface/HumanoidProfileEditor.cs @@ -6,7 +6,6 @@ using Content.Client.Interfaces; using Content.Shared; using Content.Shared.Jobs; using Content.Shared.Preferences; -using Content.Shared.Text; using Robust.Client.Graphics.Drawing; using Robust.Client.UserInterface; using Robust.Client.UserInterface.Controls; @@ -15,11 +14,10 @@ using Robust.Shared.IoC; using Robust.Shared.Localization; using Robust.Shared.Maths; using Robust.Shared.Prototypes; -using Robust.Shared.Random; namespace Content.Client.UserInterface { - public class HumanoidProfileEditor : Control + public partial class HumanoidProfileEditor : Control { private static readonly StyleBoxFlat HighlightedStyle = new StyleBoxFlat { @@ -31,7 +29,6 @@ namespace Content.Client.UserInterface }; private readonly LineEdit _ageEdit; - private readonly LineEdit _nameEdit; private readonly IClientPreferencesManager _preferencesManager; private readonly Button _saveButton; @@ -47,15 +44,10 @@ namespace Content.Client.UserInterface public HumanoidCharacterProfile Profile; public event Action OnProfileChanged; - private void NameChanged(string newName) - { - Profile = Profile?.WithName(newName); - IsDirty = true; - } - public HumanoidProfileEditor(ILocalizationManager localization, IClientPreferencesManager preferencesManager, IPrototypeManager prototypeManager) { + _random = IoCManager.Resolve(); Profile = (HumanoidCharacterProfile) preferencesManager.Preferences.SelectedCharacter; CharacterSlot = preferencesManager.Preferences.SelectedCharacterIndex; _preferencesManager = preferencesManager; @@ -87,10 +79,9 @@ namespace Content.Client.UserInterface var panel = HighlightedContainer(); var randomizeEverythingButton = new Button { - Text = localization.GetString("Randomize everything"), - Disabled = true, - ToolTip = "Not yet implemented!" + Text = localization.GetString("Randomize everything") }; + randomizeEverythingButton.OnPressed += args => { RandomizeEverything(); }; panel.AddChild(randomizeEverythingButton); leftColumn.AddChild(panel); } @@ -111,22 +102,12 @@ namespace Content.Client.UserInterface CustomMinimumSize = (270, 0), SizeFlagsVertical = SizeFlags.ShrinkCenter }; - _nameEdit.OnTextChanged += args => { NameChanged(args.Text); }; + _nameEdit.OnTextChanged += args => { SetName(args.Text); }; var nameRandomButton = new Button { Text = localization.GetString("Randomize"), }; - nameRandomButton.OnPressed += args => - { - var random = IoCManager.Resolve(); - var firstName = random.Pick( - Profile.Sex == Sex.Male - ? Names.MaleFirstNames - : Names.FemaleFirstNames); - var lastName = random.Pick(Names.LastNames); - _nameEdit.Text = $"{firstName} {lastName}"; - NameChanged(_nameEdit.Text); - }; + nameRandomButton.OnPressed += args => RandomizeName(); hBox.AddChild(nameLabel); hBox.AddChild(_nameEdit); hBox.AddChild(nameRandomButton); @@ -169,8 +150,7 @@ namespace Content.Client.UserInterface }; _sexMaleButton.OnPressed += args => { - Profile = Profile?.WithSex(Sex.Male); - IsDirty = true; + SetSex(Sex.Male); }; _sexFemaleButton = new Button { @@ -179,8 +159,7 @@ namespace Content.Client.UserInterface }; _sexFemaleButton.OnPressed += args => { - Profile = Profile?.WithSex(Sex.Female); - IsDirty = true; + SetSex(Sex.Female); }; hBox.AddChild(sexLabel); hBox.AddChild(_sexMaleButton); @@ -202,8 +181,7 @@ namespace Content.Client.UserInterface { if (!int.TryParse(args.Text, out var newAge)) return; - Profile = Profile?.WithAge(newAge); - IsDirty = true; + SetAge(newAge); }; hBox.AddChild(ageLabel); hBox.AddChild(_ageEdit); @@ -395,6 +373,24 @@ namespace Content.Client.UserInterface IsDirty = false; } + private void SetAge(int newAge) + { + Profile = Profile?.WithAge(newAge); + IsDirty = true; + } + + private void SetSex(Sex newSex) + { + Profile = Profile?.WithSex(newSex); + IsDirty = true; + } + + private void SetName(string newName) + { + Profile = Profile?.WithName(newName); + IsDirty = true; + } + public void Save() { IsDirty = false; @@ -420,6 +416,16 @@ namespace Content.Client.UserInterface }; } + private void UpdateNameEdit() + { + _nameEdit.Text = Profile.Name; + } + + private void UpdateAgeEdit() + { + _ageEdit.Text = Profile.Age.ToString(); + } + private void UpdateSexControls() { if (Profile.Sex == Sex.Male) @@ -430,10 +436,10 @@ namespace Content.Client.UserInterface private void UpdateHairPickers() { - _hairPicker.SetInitialData( + _hairPicker.SetData( Profile.Appearance.HairColor, Profile.Appearance.HairStyleName); - _facialHairPicker.SetInitialData( + _facialHairPicker.SetData( Profile.Appearance.FacialHairColor, Profile.Appearance.FacialHairStyleName); } @@ -446,9 +452,9 @@ namespace Content.Client.UserInterface public void UpdateControls() { if (Profile is null) return; - _nameEdit.Text = Profile?.Name; + UpdateNameEdit(); UpdateSexControls(); - _ageEdit.Text = Profile?.Age.ToString(); + UpdateAgeEdit(); UpdateHairPickers(); UpdateSaveButton(); UpdateJobPriorities(); diff --git a/Content.Shared/Preferences/Appearance/HairStyles.cs b/Content.Shared/Preferences/Appearance/HairStyles.cs index d9fdef29af..a0461a4cdd 100644 --- a/Content.Shared/Preferences/Appearance/HairStyles.cs +++ b/Content.Shared/Preferences/Appearance/HairStyles.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using Robust.Shared.Maths; namespace Content.Shared.Preferences.Appearance { @@ -268,5 +269,15 @@ namespace Content.Shared.Preferences.Appearance return string.Compare(styleA, styleB, StringComparison.CurrentCulture); }); + + public static IReadOnlyList RealisticHairColors = new List + { + Color.Yellow, + Color.Black, + Color.SandyBrown, + Color.Brown, + Color.Wheat, + Color.Gray + }; } } diff --git a/Content.Shared/Preferences/HumanoidCharacterProfile.cs b/Content.Shared/Preferences/HumanoidCharacterProfile.cs index 0c11579ffb..5b061d5a3f 100644 --- a/Content.Shared/Preferences/HumanoidCharacterProfile.cs +++ b/Content.Shared/Preferences/HumanoidCharacterProfile.cs @@ -9,6 +9,8 @@ namespace Content.Shared.Preferences public class HumanoidCharacterProfile : ICharacterProfile { private readonly Dictionary _jobPriorities; + public static int MinimumAge = 18; + public static int MaximumAge = 90; private HumanoidCharacterProfile( string name,