world builder

pull/5/head
Evan Hemsley 2019-06-14 17:03:56 -07:00
parent 56bf01f03c
commit f88870e2d8
4 changed files with 101 additions and 77 deletions

View File

@ -1,14 +1,11 @@
using System.Collections.Generic;
using System.Linq;
namespace Encompass { namespace Encompass {
public class World { public class World {
EntityManager entityManager; EntityManager entityManager;
ComponentManager componentManager; ComponentManager componentManager;
public World() { internal World(EntityManager entityManager, ComponentManager componentManager) {
this.componentManager = new ComponentManager(); this.entityManager = entityManager;
this.entityManager = new EntityManager(componentManager); this.componentManager = componentManager;
} }
public void Update() { public void Update() {

26
src/WorldBuilder.cs Normal file
View File

@ -0,0 +1,26 @@
using System.Collections.Generic;
namespace Encompass {
public class WorldBuilder {
private ComponentManager componentManager;
private EntityManager entityManager;
public WorldBuilder() {
componentManager = new ComponentManager();
entityManager = new EntityManager(componentManager);
}
public Entity CreateEntity() {
return this.entityManager.CreateEntity();
}
public World Build() {
var world = new World(this.entityManager, this.componentManager);
this.componentManager.ActivateComponents();
this.componentManager.RemoveComponents();
return world;
}
}
}

View File

@ -11,5 +11,6 @@
<Content Include="World.cs" /> <Content Include="World.cs" />
<Content Include="EntityManager.cs" /> <Content Include="EntityManager.cs" />
<Content Include="ComponentManager.cs" /> <Content Include="ComponentManager.cs" />
<Content Include="WorldBuilder.cs" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,8 +1,8 @@
using NUnit.Framework; using NUnit.Framework;
using System.Linq; using System.Linq;
namespace Encompass using Encompass;
{
namespace Tests { namespace Tests {
struct MockComponent : IComponent { struct MockComponent : IComponent {
public string myString; public string myString;
@ -14,8 +14,8 @@ namespace Encompass
[Test] [Test]
public void AddComponent() public void AddComponent()
{ {
var world = new World(); var worldBuilder = new WorldBuilder();
var entity = world.CreateEntity(); var entity = worldBuilder.CreateEntity();
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 3; mockComponent.myInt = 3;
@ -23,7 +23,8 @@ namespace Encompass
entity.AddComponent<MockComponent>(mockComponent); entity.AddComponent<MockComponent>(mockComponent);
world.Update(); var world = worldBuilder.Build();
// world.Update();
Assert.IsTrue(entity.HasComponent<MockComponent>()); Assert.IsTrue(entity.HasComponent<MockComponent>());
Assert.AreEqual(mockComponent, entity.GetComponent<MockComponent>()); Assert.AreEqual(mockComponent, entity.GetComponent<MockComponent>());
@ -32,8 +33,8 @@ namespace Encompass
[Test] [Test]
public void GetComponents() public void GetComponents()
{ {
var world = new World(); var worldBuilder = new WorldBuilder();
var entity = world.CreateEntity(); var entity = worldBuilder.CreateEntity();
MockComponent mockComponentA; MockComponent mockComponentA;
mockComponentA.myInt = 3; mockComponentA.myInt = 3;
@ -51,7 +52,7 @@ namespace Encompass
entity.AddComponent<MockComponent>(mockComponentB); entity.AddComponent<MockComponent>(mockComponentB);
entity.AddComponent<MockComponent>(mockComponentC); entity.AddComponent<MockComponent>(mockComponentC);
world.Update(); var world = worldBuilder.Build();
Assert.Contains(mockComponentA, entity.GetComponents<MockComponent>().ToList()); Assert.Contains(mockComponentA, entity.GetComponents<MockComponent>().ToList());
Assert.Contains(mockComponentB, entity.GetComponents<MockComponent>().ToList()); Assert.Contains(mockComponentB, entity.GetComponents<MockComponent>().ToList());
@ -61,8 +62,8 @@ namespace Encompass
[Test] [Test]
public void GetComponent() public void GetComponent()
{ {
var world = new World(); var worldBuilder = new WorldBuilder();
var entity = world.CreateEntity(); var entity = worldBuilder.CreateEntity();
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 3; mockComponent.myInt = 3;
@ -70,7 +71,7 @@ namespace Encompass
entity.AddComponent<MockComponent>(mockComponent); entity.AddComponent<MockComponent>(mockComponent);
world.Update(); var world = worldBuilder.Build();
Assert.AreEqual(mockComponent, entity.GetComponent<MockComponent>()); Assert.AreEqual(mockComponent, entity.GetComponent<MockComponent>());
} }
@ -78,8 +79,8 @@ namespace Encompass
[Test] [Test]
public void HasComponent() public void HasComponent()
{ {
var world = new World(); var worldBuilder = new WorldBuilder();
var entity = world.CreateEntity(); var entity = worldBuilder.CreateEntity();
MockComponent mockComponent; MockComponent mockComponent;
mockComponent.myInt = 3; mockComponent.myInt = 3;
@ -87,10 +88,9 @@ namespace Encompass
entity.AddComponent<MockComponent>(mockComponent); entity.AddComponent<MockComponent>(mockComponent);
world.Update(); var world = worldBuilder.Build();
Assert.IsTrue(entity.HasComponent<MockComponent>()); Assert.IsTrue(entity.HasComponent<MockComponent>());
} }
} }
} }
}