initial engine

pull/5/head
Evan Hemsley 2019-06-14 17:51:06 -07:00
parent f88870e2d8
commit 40763d2559
9 changed files with 134 additions and 29 deletions

View File

@ -36,6 +36,10 @@ namespace Encompass {
return activeComponents[typeof(TComponent)].Cast<TComponent>();
}
internal TComponent GetActiveComponentByType<TComponent>() where TComponent : struct, IComponent {
return GetActiveComponentsByType<TComponent>().Single();
}
internal IEnumerable<TComponent> GetComponentsByEntityAndType<TComponent>(uint entityID) where TComponent : struct, IComponent {
var entity_components = GetComponentsByEntity(entityID).Cast<TComponent>();
var active_components_by_type = GetActiveComponentsByType<TComponent>();

30
src/Engine.cs Normal file
View File

@ -0,0 +1,30 @@
using System.Collections.Generic;
namespace Encompass {
public abstract class Engine {
private EntityManager entityManager;
private ComponentManager componentManager;
internal void AssignEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
internal void AssignComponentManager(ComponentManager componentManager) {
this.componentManager = componentManager;
}
public abstract void Update(float dt);
protected Entity CreateEntity() {
return this.entityManager.CreateEntity();
}
protected IEnumerable<TComponent> ReadComponents<TComponent>() where TComponent : struct, IComponent {
return this.componentManager.GetActiveComponentsByType<TComponent>();
}
protected TComponent ReadComponent<TComponent>() where TComponent : struct, IComponent {
return this.componentManager.GetActiveComponentByType<TComponent>();
}
}
}

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
namespace Encompass

View File

@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.Linq;
namespace Encompass {
internal class EntityManager {

View File

@ -1,22 +1,29 @@
using System.Collections.Generic;
namespace Encompass {
public class World {
EntityManager entityManager;
ComponentManager componentManager;
private List<Engine> engines;
private EntityManager entityManager;
private ComponentManager componentManager;
internal World(EntityManager entityManager, ComponentManager componentManager) {
internal World(
List<Engine> engines,
EntityManager entityManager,
ComponentManager componentManager
) {
this.engines = engines;
this.entityManager = entityManager;
this.componentManager = componentManager;
}
public void Update() {
public void Update(float dt) {
foreach (var engine in engines) {
engine.Update(dt);
}
entityManager.DestroyMarkedEntities();
componentManager.ActivateComponents();
componentManager.RemoveComponents();
}
public Entity CreateEntity() {
return entityManager.CreateEntity();
}
}
}

View File

@ -2,6 +2,8 @@ using System.Collections.Generic;
namespace Encompass {
public class WorldBuilder {
private List<Engine> engines = new List<Engine>();
private ComponentManager componentManager;
private EntityManager entityManager;
@ -14,8 +16,23 @@ namespace Encompass {
return this.entityManager.CreateEntity();
}
public Engine AddEngine<TEngine>() where TEngine : Engine, new() {
var engine = new TEngine();
engine.AssignEntityManager(this.entityManager);
engine.AssignComponentManager(this.componentManager);
engines.Add(engine);
return engine;
}
public World Build() {
var world = new World(this.entityManager, this.componentManager);
var world = new World(
this.engines,
this.entityManager,
this.componentManager
);
this.componentManager.ActivateComponents();
this.componentManager.RemoveComponents();

View File

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

47
test/EngineTest.cs Normal file
View File

@ -0,0 +1,47 @@
using NUnit.Framework;
using System.Linq;
using Encompass;
using System.Collections.Generic;
namespace Tests {
public class EngineTest
{
static IEnumerable<MockComponent> components;
public class TestEngine : Engine
{
public override void Update(float dt)
{
components = this.ReadComponents<MockComponent>();
}
}
[Test]
public void ReadComponents()
{
var worldBuilder = new WorldBuilder();
worldBuilder.AddEngine<TestEngine>();
var entity = worldBuilder.CreateEntity();
MockComponent mockComponent;
mockComponent.myInt = 0;
mockComponent.myString = "hello";
MockComponent mockComponentB;
mockComponentB.myInt = 1;
mockComponentB.myString = "howdy";
entity.AddComponent(mockComponent);
entity.AddComponent(mockComponentB);
var world = worldBuilder.Build();
world.Update(0.01f);
Assert.Contains(mockComponent, components.ToList());
Assert.Contains(mockComponentB, components.ToList());
}
}
}

View File

@ -1,16 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<Content Include="EntityTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\encompass-cs.csproj" />
</ItemGroup>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<Content Include="EntityTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\encompass-cs.csproj" />
<Content Include="EngineTest.cs" />
</ItemGroup>
</Project>