initial engine
parent
f88870e2d8
commit
40763d2559
|
@ -36,6 +36,10 @@ namespace Encompass {
|
||||||
return activeComponents[typeof(TComponent)].Cast<TComponent>();
|
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 {
|
internal IEnumerable<TComponent> GetComponentsByEntityAndType<TComponent>(uint entityID) where TComponent : struct, IComponent {
|
||||||
var entity_components = GetComponentsByEntity(entityID).Cast<TComponent>();
|
var entity_components = GetComponentsByEntity(entityID).Cast<TComponent>();
|
||||||
var active_components_by_type = GetActiveComponentsByType<TComponent>();
|
var active_components_by_type = GetActiveComponentsByType<TComponent>();
|
||||||
|
|
|
@ -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>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
using System;
|
using System.Collections.Generic;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
namespace Encompass
|
namespace Encompass
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Encompass {
|
namespace Encompass {
|
||||||
internal class EntityManager {
|
internal class EntityManager {
|
||||||
|
|
25
src/World.cs
25
src/World.cs
|
@ -1,22 +1,29 @@
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Encompass {
|
namespace Encompass {
|
||||||
public class World {
|
public class World {
|
||||||
EntityManager entityManager;
|
private List<Engine> engines;
|
||||||
ComponentManager componentManager;
|
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.entityManager = entityManager;
|
||||||
this.componentManager = componentManager;
|
this.componentManager = componentManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Update() {
|
public void Update(float dt) {
|
||||||
|
foreach (var engine in engines) {
|
||||||
|
engine.Update(dt);
|
||||||
|
}
|
||||||
|
|
||||||
entityManager.DestroyMarkedEntities();
|
entityManager.DestroyMarkedEntities();
|
||||||
componentManager.ActivateComponents();
|
componentManager.ActivateComponents();
|
||||||
componentManager.RemoveComponents();
|
componentManager.RemoveComponents();
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public Entity CreateEntity() {
|
|
||||||
return entityManager.CreateEntity();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ using System.Collections.Generic;
|
||||||
|
|
||||||
namespace Encompass {
|
namespace Encompass {
|
||||||
public class WorldBuilder {
|
public class WorldBuilder {
|
||||||
|
private List<Engine> engines = new List<Engine>();
|
||||||
|
|
||||||
private ComponentManager componentManager;
|
private ComponentManager componentManager;
|
||||||
private EntityManager entityManager;
|
private EntityManager entityManager;
|
||||||
|
|
||||||
|
@ -14,8 +16,23 @@ namespace Encompass {
|
||||||
return this.entityManager.CreateEntity();
|
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() {
|
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.ActivateComponents();
|
||||||
this.componentManager.RemoveComponents();
|
this.componentManager.RemoveComponents();
|
||||||
|
|
|
@ -12,5 +12,6 @@
|
||||||
<Content Include="EntityManager.cs" />
|
<Content Include="EntityManager.cs" />
|
||||||
<Content Include="ComponentManager.cs" />
|
<Content Include="ComponentManager.cs" />
|
||||||
<Content Include="WorldBuilder.cs" />
|
<Content Include="WorldBuilder.cs" />
|
||||||
|
<Content Include="Engine.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,16 +1,17 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp2.2</TargetFramework>
|
<TargetFramework>netcoreapp2.2</TargetFramework>
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="nunit" Version="3.11.0" />
|
<PackageReference Include="nunit" Version="3.11.0" />
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
|
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
|
||||||
<Content Include="EntityTest.cs" />
|
<Content Include="EntityTest.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\src\encompass-cs.csproj" />
|
<ProjectReference Include="..\src\encompass-cs.csproj" />
|
||||||
</ItemGroup>
|
<Content Include="EngineTest.cs" />
|
||||||
</Project>
|
</ItemGroup>
|
||||||
|
</Project>
|
Loading…
Reference in New Issue