Microwave UI + solids implemented.

This commit is contained in:
FL-OZ
2020-05-03 01:34:00 -05:00
parent dba0949c5b
commit 0f61c2fadf
8 changed files with 186 additions and 129 deletions

View File

@@ -1,8 +1,4 @@
using Robust.Client.GameObjects.Components.UserInterface; using Robust.Client.GameObjects.Components.UserInterface;
using System;
using System.Collections.Generic;
using System.Text;
using Content.Client.GameObjects.Components.Mobs;
using Content.Shared.Kitchen; using Content.Shared.Kitchen;
using Robust.Shared.GameObjects.Components.UserInterface; using Robust.Shared.GameObjects.Components.UserInterface;
@@ -28,9 +24,9 @@ namespace Content.Client.GameObjects.Components.Kitchen
protected override void UpdateState(BoundUserInterfaceState state) protected override void UpdateState(BoundUserInterfaceState state)
{ {
base.UpdateState(state); base.UpdateState(state);
if (!(state is MicrowaveUserInterfaceState cstate)) if (!(state is MicrowaveUpdateUserInterfaceState cstate))
return; return;
_menu.RefreshContents(cstate.ReagentsReagents, cstate.ContainedSolids); _menu.RefreshContentsDisplay(cstate.ReagentsReagents, cstate.ContainedSolids);
} }
@@ -43,5 +39,10 @@ namespace Content.Client.GameObjects.Components.Kitchen
{ {
SendMessage(new SharedMicrowaveComponent.MicrowaveEjectMessage()); SendMessage(new SharedMicrowaveComponent.MicrowaveEjectMessage());
} }
public void EjectSolidWithIndex(int index)
{
SendMessage(new SharedMicrowaveComponent.MicrowaveEjectSolidIndexedMessage(index));
}
} }
} }

View File

@@ -1,11 +1,9 @@
using System.Collections.Generic; using System.Collections.Generic;
using Content.Shared.Chemistry; using Content.Shared.Chemistry;
using Content.Shared.GameObjects;
using Content.Shared.Kitchen;
using Robust.Client.ResourceManagement;
using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.CustomControls; using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC; using Robust.Shared.IoC;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Maths; using Robust.Shared.Maths;
@@ -17,7 +15,7 @@ namespace Content.Client.GameObjects.Components.Kitchen
{ {
protected override Vector2? CustomSize => (512, 256); protected override Vector2? CustomSize => (512, 256);
public MicrowaveBoundUserInterface Owner { get; set; } private MicrowaveBoundUserInterface Owner { get; set; }
private List<Solution.ReagentQuantity> _heldReagents; private List<Solution.ReagentQuantity> _heldReagents;
@@ -35,11 +33,11 @@ namespace Content.Client.GameObjects.Components.Kitchen
var startButton = new Button() var startButton = new Button()
{ {
Label = { Text = Loc.GetString("START")} Label = { Text = Loc.GetString("START"), FontColorOverride = Color.Green}
}; };
var ejectButton = new Button() var ejectButton = new Button()
{ {
Label = { Text = Loc.GetString("EJECT REAGENTS")} Label = { Text = Loc.GetString("EJECT REAGENTS"),FontColorOverride = Color.Red}
}; };
var scrollContainer = new ScrollContainer() var scrollContainer = new ScrollContainer()
{ {
@@ -56,24 +54,12 @@ namespace Content.Client.GameObjects.Components.Kitchen
vbox.AddChild(ejectButton); vbox.AddChild(ejectButton);
vbox.AddChild(scrollContainer); vbox.AddChild(scrollContainer);
Contents.AddChild(vbox); Contents.AddChild(vbox);
startButton.OnPressed += OnCookButtonPressed; startButton.OnPressed += args => Owner.Cook();
ejectButton.OnPressed += OnEjectButtonPressed; ejectButton.OnPressed += args => Owner.Eject();
} }
private void OnEjectButtonPressed(BaseButton.ButtonEventArgs obj) public void RefreshContentsDisplay(List<Solution.ReagentQuantity> reagents, List<EntityUid> solids)
{
Owner.Eject();
}
private void OnCookButtonPressed(BaseButton.ButtonEventArgs args)
{
Owner.Cook();
}
public void RefreshContents(List<Solution.ReagentQuantity> reagents, Dictionary<string,int> solids)
{ {
InnerScrollContainer.RemoveAllChildren(); InnerScrollContainer.RemoveAllChildren();
foreach (var item in reagents) foreach (var item in reagents)
@@ -82,20 +68,20 @@ namespace Content.Client.GameObjects.Components.Kitchen
InnerScrollContainer.AddChild(new Label() InnerScrollContainer.AddChild(new Label()
{ {
Text = $"{item.Quantity} {proto.Name}" Text = $"{item.Quantity} {proto.Name}"
}); });
} }
foreach (var item in solids) foreach (var item in solids)
{ {
IoCManager.Resolve<IPrototypeManager>().TryIndex(item.Key, out EntityPrototype proto); var name = IoCManager.Resolve<IEntityManager>().GetEntity(item).Prototype.Name;
var solidLabel = new Button() var solidButton = new Button()
{ {
Text = $"{item.Value} {proto.Name}" Text = $"{name}"
}; };
InnerScrollContainer.AddChild(solidLabel); solidButton.OnPressed += args => Owner.EjectSolidWithIndex(solids.IndexOf(item));
InnerScrollContainer.AddChild(solidButton);
} }
} }

View File

@@ -20,8 +20,6 @@ namespace Content.Client.GameObjects.Components.Kitchen
public override void LoadData(YamlMappingNode node) public override void LoadData(YamlMappingNode node)
{ {
base.LoadData(node); base.LoadData(node);
//_audioSystem = IoCManager.Resolve<IEntitySystemManager>().GetEntitySystem<AudioSystem>();
} }
public override void OnChangeData(AppearanceComponent component) public override void OnChangeData(AppearanceComponent component)

View File

@@ -20,6 +20,8 @@ using Content.Server.GameObjects.Components.Power;
using Robust.Server.GameObjects.Components.UserInterface; using Robust.Server.GameObjects.Components.UserInterface;
using Robust.Server.Interfaces.GameObjects; using Robust.Server.Interfaces.GameObjects;
using Robust.Shared.Prototypes; using Robust.Shared.Prototypes;
using Robust.Shared.Localization;
using Content.Server.Interfaces;
namespace Content.Server.GameObjects.Components.Kitchen namespace Content.Server.GameObjects.Components.Kitchen
{ {
@@ -32,20 +34,23 @@ namespace Content.Server.GameObjects.Components.Kitchen
[Dependency] private readonly IEntityManager _entityManager; [Dependency] private readonly IEntityManager _entityManager;
[Dependency] private readonly RecipeManager _recipeManager; [Dependency] private readonly RecipeManager _recipeManager;
[Dependency] private readonly IPrototypeManager _prototypeManager; [Dependency] private readonly IPrototypeManager _prototypeManager;
[Dependency] private readonly IServerNotifyManager _notifyManager;
#pragma warning restore 649 #pragma warning restore 649
private int _cookTimeDefault; private int _cookTimeDefault;
private int _cookTimeMultiplier; //For upgrades and stuff I guess? private int _cookTimeMultiplier; //For upgrades and stuff I guess?
private string _badRecipeName; private string _badRecipeName;
private string _startCookingSound;
private string _cookingCompleteSound;
[ViewVariables] [ViewVariables]
private SolutionComponent _contents; private SolutionComponent _solution;
[ViewVariables] [ViewVariables]
public bool _busy = false; private bool _busy = false;
private bool Powered => _powerDevice.Powered; private bool Powered => _powerDevice.Powered;
private bool HasContents => _contents.ReagentList.Count > 0 || _entityContents.Count > 0; private bool HasContents => _solution.ReagentList.Count > 0 || _storage.ContainedEntities.Count > 0;
private AppearanceComponent _appearance; private AppearanceComponent _appearance;
@@ -55,7 +60,8 @@ namespace Content.Server.GameObjects.Components.Kitchen
private Container _storage; private Container _storage;
private Dictionary<string, int> _entityContents; private Dictionary<string, int> _solids;
private List<EntityUid> _solidsVisualList;
private BoundUserInterface _userInterface; private BoundUserInterface _userInterface;
void ISolutionChange.SolutionChanged(SolutionChangeEventArgs eventArgs) => UpdateUserInterface(); void ISolutionChange.SolutionChanged(SolutionChangeEventArgs eventArgs) => UpdateUserInterface();
@@ -65,12 +71,14 @@ namespace Content.Server.GameObjects.Components.Kitchen
serializer.DataField(ref _badRecipeName, "failureResult", "FoodBadRecipe"); serializer.DataField(ref _badRecipeName, "failureResult", "FoodBadRecipe");
serializer.DataField(ref _cookTimeDefault, "cookTime", 5); serializer.DataField(ref _cookTimeDefault, "cookTime", 5);
serializer.DataField(ref _cookTimeMultiplier, "cookTimeMultiplier", 1000); serializer.DataField(ref _cookTimeMultiplier, "cookTimeMultiplier", 1000);
serializer.DataField(ref _startCookingSound, "beginCookingSound","/Audio/machines/microwave_start_beep.ogg" );
serializer.DataField(ref _cookingCompleteSound, "foodDoneSound","/Audio/machines/microwave_done_beep.ogg" );
} }
public override void Initialize() public override void Initialize()
{ {
base.Initialize(); base.Initialize();
_contents ??= Owner.TryGetComponent(out SolutionComponent solutionComponent) _solution ??= Owner.TryGetComponent(out SolutionComponent solutionComponent)
? solutionComponent ? solutionComponent
: Owner.AddComponent<SolutionComponent>(); : Owner.AddComponent<SolutionComponent>();
@@ -80,33 +88,53 @@ namespace Content.Server.GameObjects.Components.Kitchen
_audioSystem = _entitySystemManager.GetEntitySystem<AudioSystem>(); _audioSystem = _entitySystemManager.GetEntitySystem<AudioSystem>();
_userInterface = Owner.GetComponent<ServerUserInterfaceComponent>() _userInterface = Owner.GetComponent<ServerUserInterfaceComponent>()
.GetBoundUserInterface(MicrowaveUiKey.Key); .GetBoundUserInterface(MicrowaveUiKey.Key);
_entityContents = new Dictionary<string, int>();
_solids = new Dictionary<string, int>();
_solidsVisualList = new List<EntityUid>();
_userInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage; _userInterface.OnReceiveMessage += UserInterfaceOnReceiveMessage;
} }
private void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage message) private void UserInterfaceOnReceiveMessage(ServerBoundUserInterfaceMessage message)
{ {
if (!Powered || _busy) return; if (!Powered || _busy || !HasContents) return;
switch (message.Message) switch (message.Message)
{ {
case MicrowaveStartCookMessage msg : case MicrowaveStartCookMessage msg :
if (!HasContents) return;
UpdateUserInterface();
wzhzhzh(); wzhzhzh();
break; break;
case MicrowaveEjectMessage msg : case MicrowaveEjectMessage msg :
if (!HasContents) return; VaporizeReagents();
DestroyReagents();
EjectSolids(); EjectSolids();
UpdateUserInterface(); UpdateUserInterface();
break; break;
case MicrowaveEjectSolidIndexedMessage msg:
EjectIndexedSolid(msg.index);
UpdateUserInterface();
break;
} }
} }
private void SetAppearance(MicrowaveVisualState state)
{
if (_appearance != null || Owner.TryGetComponent(out _appearance))
_appearance.SetData(PowerDeviceVisuals.VisualState, state);
}
private void UpdateUserInterface()
{
_solidsVisualList.Clear();
foreach(var item in _storage.ContainedEntities.ToList())
{
_solidsVisualList.Add(item.Uid);
}
_userInterface.SetState(new MicrowaveUpdateUserInterfaceState(_solution.Solution.Contents.ToList(), _solidsVisualList));
}
void IActivate.Activate(ActivateEventArgs eventArgs) void IActivate.Activate(ActivateEventArgs eventArgs)
{ {
@@ -121,25 +149,51 @@ namespace Content.Server.GameObjects.Components.Kitchen
public bool AttackBy(AttackByEventArgs eventArgs) public bool AttackBy(AttackByEventArgs eventArgs)
{ {
var itemEntity = eventArgs.User.GetComponent<HandsComponent>().GetActiveHand.Owner; var itemEntity = eventArgs.User.GetComponent<HandsComponent>().GetActiveHand.Owner;
if (itemEntity.TryGetComponent(typeof(FoodComponent), out var food))
if(itemEntity.TryGetComponent<PourableComponent>(out var attackPourable))
{ {
if (_entityContents.TryGetValue(itemEntity.Prototype.ID, out var quantity) && quantity > 0) //Get target and check if it can be poured into
if (!Owner.TryGetComponent<SolutionComponent>(out var mySolution)
|| !mySolution.CanPourIn)
{ {
quantity++; return false;
food.Owner.Delete();
UpdateUserInterface();
return true;
}
else
{
_storage.Insert(food.Owner);
} }
_entityContents.Add(itemEntity.Prototype.ID, 1); if (!itemEntity.TryGetComponent<SolutionComponent>(out var attackSolution)
UpdateUserInterface(); || !attackSolution.CanPourOut)
{
return false;
}
//Get transfer amount. May be smaller than _transferAmount if not enough room
var realTransferAmount = ReagentUnit.Min(attackPourable.TransferAmount, mySolution.EmptyVolume);
if (realTransferAmount <= 0) //Special message if container is full
{
_notifyManager.PopupMessage(Owner.Transform.GridPosition, eventArgs.User,
Loc.GetString("Container is full"));
return false;
}
//Move units from attackSolution to targetSolution
var removedSolution = attackSolution.SplitSolution(realTransferAmount);
if (!mySolution.TryAddSolution(removedSolution))
return false;
_notifyManager.PopupMessage(Owner.Transform.GridPosition, eventArgs.User,
Loc.GetString("Transferred {0}u", removedSolution.TotalVolume));
return true; return true;
} }
if (itemEntity.TryGetComponent(typeof(FoodComponent), out var food))
{
var ent = food.Owner; //Get the entity of the ItemComponent.
_storage.Insert(ent);
UpdateUserInterface();
return true;
}
return false; return false;
} }
@@ -147,13 +201,28 @@ namespace Content.Server.GameObjects.Components.Kitchen
private void wzhzhzh() private void wzhzhzh()
{ {
_busy = true; _busy = true;
// Convert storage into Dictionary of ingredients
_solids.Clear();
foreach(var item in _storage.ContainedEntities.ToList())
{
if(_solids.ContainsKey(item.Prototype.ID))
{
_solids[item.Prototype.ID]++;
}
else
{
_solids.Add(item.Prototype.ID, 1);
}
}
// Check recipes
foreach(var r in _recipeManager.Recipes) foreach(var r in _recipeManager.Recipes)
{ {
var success = CanSatisfyRecipe(r); var success = CanSatisfyRecipe(r);
SetAppearance(MicrowaveVisualState.Cooking); SetAppearance(MicrowaveVisualState.Cooking);
_audioSystem.Play("/Audio/machines/microwave_start_beep.ogg"); _audioSystem.Play(_startCookingSound);
var time = success ? r._cookTime : _cookTimeDefault; var time = success ? r.CookTime : _cookTimeDefault;
Timer.Spawn(time * _cookTimeMultiplier, () => Timer.Spawn(time * _cookTimeMultiplier, () =>
{ {
@@ -163,28 +232,33 @@ namespace Content.Server.GameObjects.Components.Kitchen
} }
else else
{ {
DestroyReagents(); VaporizeReagents();
EjectSolids(); VaporizeSolids();
} }
var entityToSpawn = success ? r._result : _badRecipeName; var entityToSpawn = success ? r.Result : _badRecipeName;
_entityManager.SpawnEntity(entityToSpawn, Owner.Transform.GridPosition); _entityManager.SpawnEntity(entityToSpawn, Owner.Transform.GridPosition);
_audioSystem.Play("/Audio/machines/microwave_done_beep.ogg"); _audioSystem.Play(_cookingCompleteSound);
SetAppearance(MicrowaveVisualState.Idle); SetAppearance(MicrowaveVisualState.Idle);
_busy = false; _busy = false;
}); });
_busy = false;
UpdateUserInterface(); UpdateUserInterface();
return; return;
} }
} }
/// <summary> private void VaporizeReagents()
/// This actually deletes all the reagents.
/// </summary>
private void DestroyReagents()
{ {
_contents.RemoveAllSolution(); _solution.RemoveAllSolution();
}
private void VaporizeSolids()
{
foreach (var item in _storage.ContainedEntities.ToList())
{
item.Delete();
}
} }
private void EjectSolids() private void EjectSolids()
@@ -195,23 +269,34 @@ namespace Content.Server.GameObjects.Components.Kitchen
_storage.Remove(item); _storage.Remove(item);
} }
foreach (var kvp in _entityContents) _solids.Clear();
{ }
if (kvp.Value > 1 && _prototypeManager.TryIndex(kvp.Key, out EntityPrototype proto))
{
for(int i = 0; i <= kvp.Value - 1; i++)
_entityManager.SpawnEntity(proto.Name, Owner.Transform.GridPosition);
} private void EjectIndexedSolid(int index)
{
var entityToRemove = _storage.ContainedEntities.ToArray()[index];
_storage.Remove(entityToRemove);
}
private void SubtractContents(FoodRecipePrototype recipe)
{
foreach(var kvp in recipe.IngredientsReagents)
{
_solution.TryRemoveReagent(kvp.Key, ReagentUnit.New(kvp.Value));
} }
_entityContents.Clear(); foreach (var solid in recipe.IngredientsSolids)
{
_solids[solid.Key] -= solid.Value;
}
} }
private bool CanSatisfyRecipe(FoodRecipePrototype recipe) private bool CanSatisfyRecipe(FoodRecipePrototype recipe)
{ {
foreach (var reagent in recipe._ingReagents) foreach (var reagent in recipe.IngredientsReagents)
{ {
if (!_contents.ContainsReagent(reagent.Key, out var amount)) if (!_solution.ContainsReagent(reagent.Key, out var amount))
{ {
return false; return false;
} }
@@ -222,14 +307,14 @@ namespace Content.Server.GameObjects.Components.Kitchen
} }
} }
foreach (var solid in recipe._ingSolids) foreach (var solid in recipe.IngredientsSolids)
{ {
if (!_entityContents.TryGetValue(solid.Key, out var amount)) if (!_solids.ContainsKey(solid.Key))
{ {
return false; return false;
} }
if (amount < solid.Value) if (_solids[solid.Key] < solid.Value)
{ {
return false; return false;
} }
@@ -238,31 +323,5 @@ namespace Content.Server.GameObjects.Components.Kitchen
return true; return true;
} }
private void SubtractContents(FoodRecipePrototype recipe)
{
foreach(var item in recipe._ingReagents)
{
_contents.TryRemoveReagent(item.Key, ReagentUnit.New(item.Value));
}
foreach(var item in recipe._ingSolids)
{
_entityContents.TryGetValue(item.Key, out var value);
value -= item.Value;
}
}
private void SetAppearance(MicrowaveVisualState state)
{
if (_appearance != null || Owner.TryGetComponent(out _appearance))
_appearance.SetData(PowerDeviceVisuals.VisualState, state);
}
private void UpdateUserInterface()
{
_userInterface.SetState(new MicrowaveUserInterfaceState(_contents.Solution.Contents.ToList(), solids:_entityContents));
}
} }
} }

View File

@@ -33,12 +33,12 @@ namespace Content.Shared.Kitchen
return 0; return 0;
} }
if (x._ingReagents.Count < y._ingReagents.Count) if (x.IngredientsReagents.Count < y.IngredientsReagents.Count)
{ {
return 1; return 1;
} }
if (x._ingReagents.Count > y._ingReagents.Count) if (x.IngredientsReagents.Count > y.IngredientsReagents.Count)
{ {
return -1; return -1;
} }

View File

@@ -6,6 +6,7 @@ using Robust.Shared.GameObjects;
using Robust.Shared.Serialization; using Robust.Shared.Serialization;
using Robust.Shared.GameObjects.Components; using Robust.Shared.GameObjects.Components;
using Robust.Shared.GameObjects.Components.UserInterface; using Robust.Shared.GameObjects.Components.UserInterface;
using Robust.Shared.Interfaces.GameObjects;
namespace Content.Shared.Kitchen namespace Content.Shared.Kitchen
@@ -32,14 +33,25 @@ namespace Content.Shared.Kitchen
{ {
} }
} }
[Serializable, NetSerializable]
public class MicrowaveEjectSolidIndexedMessage : BoundUserInterfaceMessage
{
public int index;
public MicrowaveEjectSolidIndexedMessage(int i)
{
index = i;
}
}
} }
[NetSerializable, Serializable] [NetSerializable, Serializable]
public class MicrowaveUserInterfaceState : BoundUserInterfaceState public class MicrowaveUpdateUserInterfaceState : BoundUserInterfaceState
{ {
public readonly List<Solution.ReagentQuantity> ReagentsReagents; public readonly List<Solution.ReagentQuantity> ReagentsReagents;
public readonly Dictionary<string, int> ContainedSolids; public readonly List<EntityUid> ContainedSolids;
public MicrowaveUserInterfaceState(List<Solution.ReagentQuantity> reagents, Dictionary<string,int> solids) public MicrowaveUpdateUserInterfaceState(List<Solution.ReagentQuantity> reagents, List<EntityUid> solids)
{ {
ReagentsReagents = reagents; ReagentsReagents = reagents;
ContainedSolids = solids; ContainedSolids = solids;

View File

@@ -18,16 +18,17 @@ namespace Content.Shared.Prototypes.Kitchen
public class FoodRecipePrototype : IPrototype, IIndexedPrototype public class FoodRecipePrototype : IPrototype, IIndexedPrototype
{ {
public string _id; private string _id;
public string _name => Loc.GetString(Name); public string Name => Loc.GetString(Name);
private string Name; private string _name;
public string _result; public string Result;
public IReadOnlyDictionary<string, int> _ingReagents => IngredientsReagents; public int CookTime;
public IReadOnlyDictionary<string, int> _ingSolids => IngredientsSolids; public IReadOnlyDictionary<string, int> IngredientsReagents => _ingsReagents;
public IReadOnlyDictionary<string, int> IngredientsSolids => _ingsSolids;
private Dictionary<string, int> _ingsReagents;
private Dictionary<string, int> _ingsSolids;
private Dictionary<string, int> IngredientsReagents;
private Dictionary<string, int> IngredientsSolids;
public int _cookTime;
public string ID => _id; public string ID => _id;
@@ -36,11 +37,11 @@ namespace Content.Shared.Prototypes.Kitchen
var serializer = YamlObjectSerializer.NewReader(mapping); var serializer = YamlObjectSerializer.NewReader(mapping);
serializer.DataField(ref _id, "id", string.Empty); serializer.DataField(ref _id, "id", string.Empty);
serializer.DataField(ref Name, "name", string.Empty); serializer.DataField(ref _name, "name", string.Empty);
serializer.DataField(ref _result, "result", string.Empty); serializer.DataField(ref Result, "result", string.Empty);
serializer.DataField(ref IngredientsReagents, "reagents", new Dictionary<string, int>()); serializer.DataField(ref _ingsReagents, "reagents", new Dictionary<string, int>());
serializer.DataField(ref IngredientsSolids, "solids", new Dictionary<string, int>()); serializer.DataField(ref _ingsSolids, "solids", new Dictionary<string, int>());
serializer.DataField(ref _cookTime, "time", 5); serializer.DataField(ref CookTime, "time", 5);
} }
} }

View File

@@ -2,10 +2,10 @@
id: RecipeCheeseburger id: RecipeCheeseburger
name: Cheeseburger Recipe name: Cheeseburger Recipe
result: FoodCheeseburger result: FoodCheeseburger
time: 10 time: 1
reagents: reagents:
chem.H2O: 15 chem.H2O: 15
chem.Nutriment: 5 chem.Nutriment: 5
solids: solids:
FoodMeatBreadSlice: 1 Food4NoRaisins: 2