Exemple #1
0
        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);
            }
        }