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
This commit is contained in:
DamianX
2020-01-24 00:56:26 +01:00
committed by Pieter-Jan Briers
parent 5af5a02e31
commit 46ce6bf45e
5 changed files with 125 additions and 42 deletions

View File

@@ -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);
}
}
}

View File

@@ -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();
}
}
}

View File

@@ -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<HumanoidCharacterProfile> OnProfileChanged;
private void NameChanged(string newName)
{
Profile = Profile?.WithName(newName);
IsDirty = true;
}
public HumanoidProfileEditor(ILocalizationManager localization,
IClientPreferencesManager preferencesManager, IPrototypeManager prototypeManager)
{
_random = IoCManager.Resolve<IRobustRandom>();
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<IRobustRandom>();
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();

View File

@@ -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<Color> RealisticHairColors = new List<Color>
{
Color.Yellow,
Color.Black,
Color.SandyBrown,
Color.Brown,
Color.Wheat,
Color.Gray
};
}
}

View File

@@ -9,6 +9,8 @@ namespace Content.Shared.Preferences
public class HumanoidCharacterProfile : ICharacterProfile
{
private readonly Dictionary<string, JobPriority> _jobPriorities;
public static int MinimumAge = 18;
public static int MaximumAge = 90;
private HumanoidCharacterProfile(
string name,