├── README.md └── ecs ├── .gitignore ├── .vs └── TestECS │ └── v16 │ └── .suo ├── Readme.md ├── TestECS.sln └── TestECS ├── BaseSystem.cs ├── Collider.cs ├── Component.cs ├── Entity.cs ├── Game.cs ├── MyAwesomeCharacter.cs ├── Program.cs ├── Sprite.cs ├── TestECS.csproj ├── Transform.cs ├── Vector2.cs └── bin └── Debug └── netcoreapp3.1 ├── TestECS.deps.json ├── TestECS.dll ├── TestECS.exe ├── TestECS.pdb ├── TestECS.runtimeconfig.dev.json └── TestECS.runtimeconfig.json /README.md: -------------------------------------------------------------------------------- 1 | # blog-examples 2 | Examples from my blog, [matthall.codes](https://matthall.codes). -------------------------------------------------------------------------------- /ecs/.gitignore: -------------------------------------------------------------------------------- 1 | TestECS/obj -------------------------------------------------------------------------------- /ecs/.vs/TestECS/v16/.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MH15/blog-examples/71a6042d69d2f3455cf4db996cc19a92314b9392/ecs/.vs/TestECS/v16/.suo -------------------------------------------------------------------------------- /ecs/Readme.md: -------------------------------------------------------------------------------- 1 | # ecs 2 | C# Entity Component System example from my blog 3 | -------------------------------------------------------------------------------- /ecs/TestECS.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30413.136 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestECS", "TestECS\TestECS.csproj", "{B36EF42C-135A-4D32-84B7-E0EDA35912BA}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {B36EF42C-135A-4D32-84B7-E0EDA35912BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {B36EF42C-135A-4D32-84B7-E0EDA35912BA}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {B36EF42C-135A-4D32-84B7-E0EDA35912BA}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {B36EF42C-135A-4D32-84B7-E0EDA35912BA}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {90BB0A04-91BA-4509-A855-787FA46C6095} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /ecs/TestECS/BaseSystem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestECS 6 | { 7 | class BaseSystem where T : Component 8 | { 9 | 10 | public static List components = new List(); 11 | 12 | public static void Register(T component) 13 | { 14 | components.Add(component); 15 | } 16 | 17 | public static void Update(float gameTime) 18 | { 19 | foreach (T component in components) 20 | { 21 | component.Update(gameTime); 22 | } 23 | } 24 | 25 | } 26 | 27 | 28 | class TransformSystem : BaseSystem { } 29 | class SpriteSystem : BaseSystem { } 30 | class ColliderSystem : BaseSystem { } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /ecs/TestECS/Collider.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestECS 6 | { 7 | class Collider : Component 8 | { 9 | // implementation of a Collider component 10 | 11 | public Collider() 12 | { 13 | ColliderSystem.Register(this); 14 | } 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ecs/TestECS/Component.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestECS 6 | { 7 | class Component 8 | { 9 | public Entity entity; 10 | 11 | public virtual void Update(float gameTime) { } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /ecs/TestECS/Entity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestECS 6 | { 7 | class Entity 8 | { 9 | public int ID { get; set; } 10 | 11 | List components = new List(); 12 | 13 | public void AddComponent(Component component) 14 | { 15 | components.Add(component); 16 | component.entity = this; 17 | } 18 | public T GetComponent() where T : Component 19 | { 20 | foreach (Component component in components) 21 | { 22 | if (component.GetType().Equals(typeof(T))) 23 | { 24 | return (T)component; 25 | } 26 | } 27 | return null; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /ecs/TestECS/Game.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TestECS 4 | { 5 | class Game 6 | { 7 | public void Update(float gameTime) 8 | { 9 | TransformSystem.Update(gameTime); 10 | SpriteSystem.Update(gameTime); 11 | ColliderSystem.Update(gameTime); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /ecs/TestECS/MyAwesomeCharacter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestECS 6 | { 7 | class MyAwesomeCharacter : Entity 8 | { 9 | public MyAwesomeCharacter(/* Texture2D tex */) 10 | { 11 | // add a `Transform` component to store the character's position 12 | Transform transform = new Transform(); 13 | transform.position = new Vector2(100, 100); 14 | AddComponent(transform); 15 | 16 | // add a `Sprite` component to store the character's texture 17 | Sprite sprite = new Sprite(); 18 | // sprite.texture = tex; 19 | AddComponent(sprite); // Assume `Sprite : Component` 20 | } 21 | } 22 | 23 | } 24 | -------------------------------------------------------------------------------- /ecs/TestECS/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TestECS 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | var game = new Game(); 10 | 11 | // main game loop 12 | while (true) 13 | { 14 | game.Update(0.01f); // assume the proper frame delta is passed 15 | } 16 | } 17 | 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ecs/TestECS/Sprite.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestECS 6 | { 7 | class Sprite : Component 8 | { 9 | // implementation of a Collider component 10 | 11 | public Sprite() 12 | { 13 | SpriteSystem.Register(this); 14 | } 15 | 16 | public override void Update(float gameTime) 17 | { 18 | // We'd like to do something like this: 19 | Transform t = entity.GetComponent(); 20 | // GameEngine.DrawSprite(texture, t.position); // assume the fictitious GameEngine class 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ecs/TestECS/TestECS.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /ecs/TestECS/Transform.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestECS 6 | { 7 | class Transform : Component 8 | { 9 | public Vector2 position = Vector2.Zero; 10 | public Vector2 scale = Vector2.Zero; 11 | public float layerDepth = 0; 12 | public float rotation = 0; 13 | 14 | public Transform() 15 | { 16 | TransformSystem.Register(this); 17 | } 18 | 19 | 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ecs/TestECS/Vector2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace TestECS 6 | { 7 | class Vector2 8 | { 9 | public float x = 0; 10 | public float y = 0; 11 | 12 | public static Vector2 Zero = new Vector2(0, 0); 13 | public static Vector2 One = new Vector2(0, 0); 14 | 15 | public Vector2(float x, float y) 16 | { 17 | this.x = x; 18 | this.y = y; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /ecs/TestECS/bin/Debug/netcoreapp3.1/TestECS.deps.json: -------------------------------------------------------------------------------- 1 | { 2 | "runtimeTarget": { 3 | "name": ".NETCoreApp,Version=v3.1", 4 | "signature": "" 5 | }, 6 | "compilationOptions": {}, 7 | "targets": { 8 | ".NETCoreApp,Version=v3.1": { 9 | "TestECS/1.0.0": { 10 | "runtime": { 11 | "TestECS.dll": {} 12 | } 13 | } 14 | } 15 | }, 16 | "libraries": { 17 | "TestECS/1.0.0": { 18 | "type": "project", 19 | "serviceable": false, 20 | "sha512": "" 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /ecs/TestECS/bin/Debug/netcoreapp3.1/TestECS.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MH15/blog-examples/71a6042d69d2f3455cf4db996cc19a92314b9392/ecs/TestECS/bin/Debug/netcoreapp3.1/TestECS.dll -------------------------------------------------------------------------------- /ecs/TestECS/bin/Debug/netcoreapp3.1/TestECS.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MH15/blog-examples/71a6042d69d2f3455cf4db996cc19a92314b9392/ecs/TestECS/bin/Debug/netcoreapp3.1/TestECS.exe -------------------------------------------------------------------------------- /ecs/TestECS/bin/Debug/netcoreapp3.1/TestECS.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MH15/blog-examples/71a6042d69d2f3455cf4db996cc19a92314b9392/ecs/TestECS/bin/Debug/netcoreapp3.1/TestECS.pdb -------------------------------------------------------------------------------- /ecs/TestECS/bin/Debug/netcoreapp3.1/TestECS.runtimeconfig.dev.json: -------------------------------------------------------------------------------- 1 | { 2 | "runtimeOptions": { 3 | "additionalProbingPaths": [ 4 | "C:\\Users\\Matt\\.dotnet\\store\\|arch|\\|tfm|", 5 | "C:\\Users\\Matt\\.nuget\\packages" 6 | ] 7 | } 8 | } -------------------------------------------------------------------------------- /ecs/TestECS/bin/Debug/netcoreapp3.1/TestECS.runtimeconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "runtimeOptions": { 3 | "tfm": "netcoreapp3.1", 4 | "framework": { 5 | "name": "Microsoft.NETCore.App", 6 | "version": "3.1.0" 7 | } 8 | } 9 | } --------------------------------------------------------------------------------