Last active
March 4, 2023 00:20
-
-
Save PumpkinPaul/dae2f2993f8abf8c7b831f27c5e04971 to your computer and use it in GitHub Desktop.
MoonToolsECSTest
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Microsoft.Xna.Framework; | |
| using MoonTools.ECS; | |
| using MoonTools.Structs; | |
| using MoonToolsECSTest.Components; | |
| using MoonToolsECSTest.Renderers; | |
| using MoonToolsECSTest.Systems; | |
| //Entities are ids and the components are just structs but this gives us a way to classify entities | |
| //(e.g. equivalent to 'type' system) | |
| //NOTE - DON'T think of entities as types - they are still defined by components and behaviour. | |
| ushort ENTITY_CLASSIFICATION_ID = 0; | |
| //---------------------------------------------------------------------------------------------------- | |
| //All the Resources | |
| //---------------------------------------------------------------------------------------------------- | |
| var entityNameResources = new Dictionary<Entity, string>(); //Maybe to help with debugging e.g. EntityID: 1, "Player" | |
| //Texture2DResources | |
| //SoundEffectResources | |
| //Particle System Resources | |
| //Add some common effects (e.g. do this sorta stuff in LoadContent phases) | |
| var particleSystemResources = new Dictionary<string, ParticleSystemTemplate> | |
| { | |
| { "CommonSmoke", new ParticleSystemTemplate(Count: 15, Color: Color.Black) }, | |
| { "CommonBoom", new ParticleSystemTemplate(Count: 25, Color: Color.Red) }, | |
| { "CommonSparks", new ParticleSystemTemplate(Count: 100, Color: Color.White) }, | |
| }; | |
| //Table to link many entity 'classifications' to many particle system template keys (names) | |
| //e.g. a crate entity (CRATE_CLASSIFICATION) might create CommonBoom and CommonSparks when the ExplosionSystem processes the ExplosionComponents | |
| //ExplosionSystem knows nothing about 'crates' (or indeed any other entity 'type') it just creates a number of explosions | |
| //based on our setup code | |
| var particleSystemClassifications = new Dictionary<int, List<string>>(); | |
| //---------------------------------------------------------------------------------------------------- | |
| var world = new World(); | |
| //---------------------------------------------------------------------------------------------------- | |
| //Systems | |
| var nameSystem = new NameSystem(world, entityNameResources); | |
| var playerInputSystem = new PlayerInputSystem(world); | |
| var velocitySystem = new VelocitySystem(world); | |
| var motionSystem = new MotionSystem(world); | |
| var damageSystem = new DamageSystem(world); | |
| var deathSystem = new DeathSystem(world); | |
| //---------------------------------------------------------------------------------------------------- | |
| //Renderers | |
| var renderer = new ConsoleRenderer(world); | |
| //---------------------------------------------------------------------------------------------------- | |
| //Player.cs | |
| //Static data - template data for all players | |
| //Players are important - they should have their own specific effects so we'll need to add them to the | |
| //classification <-> effects lookup table | |
| //For this we'll need a classification type generating | |
| var PLAYER_ID = ENTITY_CLASSIFICATION_ID++; | |
| particleSystemResources["PlayerExplosion"] = new ParticleSystemTemplate(10, Color.Red); | |
| particleSystemResources["PlayerExplosionShrapnel"] = new ParticleSystemTemplate(50, Color.Green); | |
| particleSystemClassifications[PLAYER_ID] = new List<string> { "CommonBoom", "CommonSparks", "PlayerExplosion", "PlayerExplosionShrapnel" }; | |
| //Player spawner - this is how we create players at run time - all values types no garbage | |
| var player = world.CreateEntity(); | |
| #if DEBUG | |
| entityNameResources[player] = "Player 1"; | |
| world.Set(player, new NameComponent()); | |
| #endif | |
| world.Set(player, new Transform2D()); | |
| world.Set(player, new Velocity2D()); | |
| world.Set(player, new PlayerInputComponent(PlayerIndex.One)); | |
| world.Set(player, new HealthComponent(5)); | |
| //Players should create particle explosions when they die! | |
| //I think this component tells the death system that particles should be created? | |
| //DeathSystem... Has<ExplosionComponent>? Yes, then Send CreateExplosionMessage | |
| //ExplosionSystem... Process all CreateExplosionMessages | |
| world.Set(player, new ExplosionComponent(PLAYER_ID)); | |
| //---------------------------------------------------------------------------------------------------- | |
| //GruntEnemy.cs | |
| //Grunt spawner | |
| var grunt = world.CreateEntity(); | |
| world.Set(grunt, new Transform2D()); | |
| world.Set(grunt, new Velocity2D()); | |
| world.Set(grunt, new HealthComponent(3)); | |
| world.Set(grunt, new ScoreBountyComponent(100)); | |
| //world.Set(grunt, new ExplosionComponent(...)); | |
| //---------------------------------------------------------------------------------------------------- | |
| //BruteEnemy.cs | |
| //Brute spawner | |
| var brute = world.CreateEntity(); | |
| world.Set(brute, new Transform2D()); | |
| world.Set(brute, new Velocity2D()); | |
| world.Set(brute, new HealthComponent(10)); | |
| world.Set(brute, new ScoreBountyComponent(1000)); | |
| //world.Set(brute, new ExplosionComponent(...)); | |
| //---------------------------------------------------------------------------------------------------- | |
| //Simulation | |
| var delta = TimeSpan.FromSeconds(1); | |
| nameSystem.Update(delta); | |
| for (var i = 0; i < 5; i++) | |
| { | |
| playerInputSystem.Update(delta); | |
| velocitySystem.Update(delta); | |
| motionSystem.Update(delta); | |
| damageSystem.Update(delta); | |
| deathSystem.Update(delta); | |
| world.FinishUpdate(); | |
| renderer.Draw(delta); | |
| } | |
| public record class ParticleSystemTemplate | |
| ( | |
| int Count, | |
| Color Color | |
| ); | |
| //What's missing | |
| //Movement can be blocked by a skill | |
| //Speed can be modified by skill / upgrade | |
| //Collisions |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment