/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { graphics.PreferredBackBufferWidth = WIDTH; graphics.PreferredBackBufferHeight = HEIGHT; graphics.ApplyChanges(); spriteBatch = new SpriteBatch(GraphicsDevice); random = new Random(); world = new EcsWorld(1000); world.RegisterComponentType <Sprite>(); world.RegisterComponentType <Physics>(); world.RegisterComponentType <Spring>(); world.RegisterComponentType <Drift>(); world.RegisterComponentType <Player>(); world.RegisterComponentType <Kite>(); world.RegisterComponentType <Rope>(); world.RegisterComponentType <CatInterest>(); world.RegisterComponentType <CatPounce>(); world.RegisterComponentType <CatSlide>(); world.RegisterComponentType <CatAbscond>(); world.RegisterUpdateSystem(new PlayerSystem(world)); world.RegisterUpdateSystem(new KiteSystem(world)); world.RegisterUpdateSystem(new CatInterestSystem(world)); world.RegisterUpdateSystem(new CatPounceSystem(world)); world.RegisterUpdateSystem(new CatSlideSystem(world)); world.RegisterUpdateSystem(new CatAbscondSystem(world)); windSystem = new WindSystem(world); world.RegisterUpdateSystem(windSystem); world.RegisterUpdateSystem(new SpringSystem(world)); world.RegisterUpdateSystem(new PhysicsSystem(world)); world.RegisterUpdateSystem(new RopeSystem(world)); // should this just be a render system instead of giving the ropes sprites world.RegisterDrawSystem(new SpriteSystem(world, spriteBatch)); var playerTexture = Content.Load <Texture2D>("Debugging/player"); var kiteTexture = Content.Load <Texture2D>("Debugging/kite"); var treeNodeTexture = Content.Load <Texture2D>("Debugging/tree"); var ropeTexture = Content.Load <Texture2D>("Debugging/rope"); var catTexture = Content.Load <Texture2D>("Debugging/cat"); var playerEntity = CreatePlayer(playerTexture); var kiteEntity = CreateKite(kiteTexture); var ropeEntities = CreateRope(ropeTexture, 32, 200f, world.GetComponent <Transform>(playerEntity).Position, world.GetComponent <Transform>(kiteEntity).Position); ConnectPlayerAndKiteWithRope(playerEntity, kiteEntity, ropeEntities); var cat1 = CreateCat(catTexture, 200f, 200f); var cat2 = CreateCat(catTexture, 300f, 300f); var cat3 = CreateCat(catTexture, 800f, 300f); }
private void Test() { try { Console.WriteLine("Test: Register an already registered component type.\n===="); try { world.RegisterComponentType <Physics>(); } catch (Exception e) { Console.WriteLine(e.Message); } int a = world.CreateEntity(); world.AddComponent(a, new Transform(new Vector2(413, 413), 0f)); world.AddComponent(a, new Physics(1f)); Console.WriteLine("Test: Add a duplicate component.\n===="); try { world.AddComponent(a, new Transform(new Vector2(612, 612), 0f)); } catch (Exception e) { Console.WriteLine(e.Message); } int b = world.CreateEntity(); int c = world.CreateEntity(); Console.WriteLine("Test: Add an entity over the limit.\n===="); try { int d = world.CreateEntity(); } catch (Exception e) { Console.WriteLine(e.Message); } world.AddComponent(b, new Transform(new Vector2(2, 2), 0f)); world.AddComponent(c, new Transform(new Vector2(1, 1), 0f)); Console.WriteLine("Test: Add a component of an unregistered type.\n===="); try { world.AddComponent(b, new Drift(new Vector2(1, 1))); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("Test: Remove a component of a type not on an object.\n===="); try { world.RemoveComponent <Physics>(b); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("Test: Remove a component that isn't on the object.\n===="); try { world.RemoveComponent(b, new Transform(new Vector2(1, 1), 0f)); } catch (Exception e) { Console.WriteLine(e.Message); } Transform cTransform = world.RemoveComponent <Transform>(c); Console.WriteLine("Test: Remove the same type of component again.\n===="); try { Transform cTransform2 = world.RemoveComponent <Transform>(c); } catch (Exception e) { Console.WriteLine(e.Message); } Console.WriteLine("Test: Remove the same component again.\n===="); try { Transform cTransform3 = world.RemoveComponent(c, cTransform); } catch (Exception e) { Console.WriteLine(e.Message); } world.AddComponent(c, new Transform(new Vector2(41, 41), 0f)); Console.WriteLine("Test: Add the removed component when it has been replaced.\n===="); try { world.AddComponent(c, cTransform); } catch (Exception e) { Console.WriteLine(e.Message); } world.RemoveComponent <Transform>(c); world.AddComponent(c, cTransform); world.AddComponent(c, new Physics(1f)); world.RegisterComponentType <Drift>(); Console.WriteLine("Test: Component can be detected on entity.\n===="); Console.WriteLine(world.HasComponent <Transform>(a) + " should be true"); Console.WriteLine(world.HasComponent <Physics>(a) + " should be true"); Console.WriteLine(world.HasComponent <Transform>(b) + " should be true"); Console.WriteLine(world.HasComponent <Physics>(b) + " should be false"); Console.WriteLine("Test: Components can be detected on entity.\n===="); Console.WriteLine(world.HasComponents(a, typeof(Transform), typeof(Physics)) + " should be true"); Console.WriteLine(world.HasComponents(a, typeof(Physics), typeof(Transform)) + " should be true"); Console.WriteLine(world.HasComponents(a, typeof(Physics), typeof(Transform), typeof(Drift)) + " should be false"); Console.WriteLine(world.HasComponents(b, typeof(Transform)) + " should be true"); Console.WriteLine(world.HasComponents(b, typeof(Physics)) + " should be false"); Console.WriteLine(world.HasComponents(b, typeof(Transform), typeof(Physics)) + " should be false"); Console.WriteLine("Test: Entities can be collected based on component.\n===="); Console.WriteLine(string.Join(", ", world.GetEntities <Transform>()) + " should be 0, 1, 2"); Console.WriteLine(string.Join(", ", world.GetEntities <Physics>()) + " should be 0, 2"); Console.WriteLine(string.Join(", ", world.GetEntities <Drift>()) + " should be empty"); Console.WriteLine("Test: Entities can be collected based on components.\n===="); Console.WriteLine(string.Join(", ", world.GetEntities(typeof(Transform))) + " should be 0, 1, 2"); Console.WriteLine(string.Join(", ", world.GetEntities(typeof(Physics))) + " should be 0, 2"); Console.WriteLine(string.Join(", ", world.GetEntities(typeof(Transform), typeof(Physics))) + " should be 0, 2"); Console.WriteLine(string.Join(", ", world.GetEntities(typeof(Physics), typeof(Transform))) + " should be 0, 2"); Console.WriteLine(string.Join(", ", world.GetEntities(typeof(Drift))) + " should be empty"); Console.WriteLine(string.Join(", ", world.GetEntities(typeof(Transform), typeof(Physics), typeof(Drift))) + " should be empty"); } catch (Exception e) { Console.WriteLine("UNCAUGHT EXCEPTION!"); Console.WriteLine(e.Message); } }