Skip to content

Commit

Permalink
Develop (#13)
Browse files Browse the repository at this point in the history
* changed components to suit the discussion in issue #17

* removed AddComponent helper functions
  • Loading branch information
fcheadle authored Jan 9, 2021
1 parent 502529e commit 9070e07
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 46 deletions.
2 changes: 1 addition & 1 deletion ExampleGame/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion TheSadRogue.Integration.Tests/RogueLikeEntityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
48 changes: 6 additions & 42 deletions TheSadRogue.Integration/RogueLikeEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

/// <summary>
/// Each and every component on this entity
/// </summary>
Expand Down Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -103,45 +107,5 @@ private void Position_Changed(object? sender, ValueChangedEventArgs<Point> e)
=> Moved?.Invoke(sender, new GameObjectPropertyChanged<Point>(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<object> components)
{
foreach (var component in components)
AddComponent(component);
}

public T GetComponent<T>(string tag = null)
{
//temporary
// if (tag is "")
// {
return GetComponents<T>().Distinct().FirstOrDefault();
// }
// else
// {
// return GetComponents<T>().Distinct().FirstOrDefault();
// }
}

//public T GetComponent<T>() => GoRogueComponents.GetFirst<T>();

public IEnumerable<T> GetComponents<T>()
{
foreach (var component in GoRogueComponents)
if (component.Component is T rlComponent)
yield return rlComponent;
}

//todo - RemoveComponent<T>()
//todo - RemoveComponents(???)
#endregion
}
}

0 comments on commit 9070e07

Please sign in to comment.