diff --git a/ExampleGame/Program.cs b/ExampleGame/Program.cs index 58cb674..c114935 100644 --- a/ExampleGame/Program.cs +++ b/ExampleGame/Program.cs @@ -73,7 +73,7 @@ private static RogueLikeEntity GeneratePlayerCharacter() var player = new RogueLikeEntity(position, 1, false, true, 1); var motionControl = new PlayerControlsComponent(); - player.AddComponent(motionControl); + player.AllComponents.Add(motionControl); player.IsFocused = true; return player; } diff --git a/TheSadRogue.Integration.Tests/Components/ComponentBaseTests.cs b/TheSadRogue.Integration.Tests/Components/ComponentBaseTests.cs index 14fd2d0..f9fdb5f 100644 --- a/TheSadRogue.Integration.Tests/Components/ComponentBaseTests.cs +++ b/TheSadRogue.Integration.Tests/Components/ComponentBaseTests.cs @@ -18,7 +18,7 @@ public void AddTest() Assert.Empty(entity.SadComponents); Assert.Empty(entity.GoRogueComponents); - entity.AddComponent(component); + entity.AllComponents.Add(component); Assert.Single(entity.SadComponents); Assert.Single(entity.GoRogueComponents); } diff --git a/TheSadRogue.Integration.Tests/Components/PlayerControlsTest.cs b/TheSadRogue.Integration.Tests/Components/PlayerControlsTest.cs index 2a69eb4..6a6c3b9 100644 --- a/TheSadRogue.Integration.Tests/Components/PlayerControlsTest.cs +++ b/TheSadRogue.Integration.Tests/Components/PlayerControlsTest.cs @@ -16,7 +16,7 @@ public void NewPlayerControlsComponent() Assert.Equal(4, component.Motions.Count); Assert.Empty(component.Actions); - player.AddComponent(component); + player.AllComponents.Add(component); Assert.Single(player.SadComponents); Assert.Single(player.GoRogueComponents); diff --git a/TheSadRogue.Integration.Tests/RogueLikeEntityTests.cs b/TheSadRogue.Integration.Tests/RogueLikeEntityTests.cs index dafa08c..daa73a2 100644 --- a/TheSadRogue.Integration.Tests/RogueLikeEntityTests.cs +++ b/TheSadRogue.Integration.Tests/RogueLikeEntityTests.cs @@ -48,7 +48,7 @@ public void AddComponentTest() Assert.Equal(4, component.Motions.Count); Assert.Empty(component.Actions); - entity.AddComponent(component); + entity.AllComponents.Add(component); Assert.Single(entity.SadComponents); Assert.Single(entity.GoRogueComponents); diff --git a/TheSadRogue.Integration/RogueLikeEntity.cs b/TheSadRogue.Integration/RogueLikeEntity.cs index dae1dd2..96d22b4 100644 --- a/TheSadRogue.Integration/RogueLikeEntity.cs +++ b/TheSadRogue.Integration/RogueLikeEntity.cs @@ -20,6 +20,7 @@ public class RogueLikeEntity : Entity, IGameObject public int Layer { get; } public Map? CurrentMap { get; private set; } public ITaggableComponentCollection GoRogueComponents { get; private set; } + /// /// Each and every component on this entity /// @@ -73,8 +74,8 @@ public RogueLikeEntity(Point position, Color foreground, Color background, int g Layer = layer; GoRogueComponents = new ComponentCollection(); - GoRogueComponents.ComponentAdded += On_GoRogueComponentAdded; - GoRogueComponents.ComponentRemoved += On_GoRogueComponentRemoved; + AllComponents.ComponentAdded += On_GoRogueComponentAdded; + AllComponents.ComponentRemoved += On_GoRogueComponentRemoved; } #endregion @@ -91,6 +92,9 @@ public void On_GoRogueComponentAdded(object? s, ComponentChangedEventArgs e) { if (e.Component is IComponent sadComponent) SadComponents.Add(sadComponent); + if (e.Component is IGameObjectComponent goRogueComponent) + goRogueComponent.Parent = this; + } public void On_GoRogueComponentRemoved(object? s, ComponentChangedEventArgs e) @@ -103,45 +107,5 @@ private void Position_Changed(object? sender, ValueChangedEventArgs e) => Moved?.Invoke(sender, new GameObjectPropertyChanged(this, e.OldValue, e.NewValue)); #endregion - - #region components - public void AddComponent(object component, string tag = null) - { - if (component is IGameObjectComponent goc) - goc.Parent = this; - - GoRogueComponents.Add(component, tag); - } - public void AddComponents(IEnumerable components) - { - foreach (var component in components) - AddComponent(component); - } - - public T GetComponent(string tag = null) - { - //temporary - // if (tag is "") - // { - return GetComponents().Distinct().FirstOrDefault(); - // } - // else - // { - // return GetComponents().Distinct().FirstOrDefault(); - // } - } - - //public T GetComponent() => GoRogueComponents.GetFirst(); - - public IEnumerable GetComponents() - { - foreach (var component in GoRogueComponents) - if (component.Component is T rlComponent) - yield return rlComponent; - } - - //todo - RemoveComponent() - //todo - RemoveComponents(???) - #endregion } } \ No newline at end of file