Esempio n. 1
0
        static void Main(string[] args)
        {
            // create engine
            ECSEngine engine = new ECSEngine();

            // register components (NO MORE NEEDED)
            //engine.RegisterComponent<PositionComponent>();
            //engine.RegisterComponent<VelocityComponent>();

            // add systems
            MoveSystem moveSystem = new MoveSystem();

            engine.AddSystem(moveSystem);

            for (int i = 0; i < 5; i++)
            {
                // create entities
                ECSEntity entity = engine.CreateEntity();

                // add components
                engine.AddComponent(entity, new PositionComponent());
                engine.AddComponent(entity, new VelocityComponent()
                {
                    Velocity = new Vector3(0f, -1f * (i + 1), 0f)
                });
            }

            // init engine
            engine.Init();

            while (true)
            {
                // run engine
                engine.Run();
                System.Threading.Thread.Sleep((int)(Time.DeltaTime * 1000));
            }
        }
Esempio n. 2
0
    public static void Init()
    {
        // init window
        Game.Window.SetDefaultOrthographicSize(20f);

        // add textures
        TextureManager.AddTexture("Player", new Texture(@"Assets\Player.png"));
        TextureManager.AddTexture("EnemyBlack2", new Texture(@"Assets\EnemyBlack2.png"));
        TextureManager.AddTexture("LaserRed01", new Texture(@"Assets\LaserRed01.png"));

        //add systems
        engine.AddSystem(new PlayerMoveSystem());
        engine.AddSystem(new PlayerShootSystem());

        engine.AddSystem(new AISpawnSystem());
        engine.AddSystem(new AIMoveSystem());

        engine.AddSystem(new DestroyEntityWhenOutOfScreenSystem());

        engine.AddSystem(new CollisionDetectionSystem());
        engine.AddSystem(new CollisionSolverSystem());

        engine.AddSystem(new SpriteRendererSystem());

        //create entities & add components
        #region Player
        ECSEntity player = engine.CreateEntity();

        var halfOrthoSize = Game.Window.CurrentOrthoGraphicSize * 0.5f;

        engine.AddComponent(player, new NameComponent()
        {
            Name = "PLAYER"
        });
        engine.AddComponent(player, new InputReceiverComponent());
        engine.AddComponent(player, new PositionComponent()
        {
            Position = new Vector2(halfOrthoSize * Window.aspectRatio, halfOrthoSize * 1.5f)
        });
        engine.AddComponent(player, new VelocityComponent()
        {
            Velocity = new Vector2(5f, 5f)
        });
        engine.AddComponent(player, new BoxColliderComponent()
        {
            Size = new Vector2(1f, 1f)
        });
        engine.AddComponent(player, new SpriteRendererComponent()
        {
            RenderOffset = RenderOffset.Player,
            Texture      = TextureManager.GetTexture("Player"),
            Sprite       = new Sprite(1f, 1f)
            {
                pivot = Vector2.One * 0.5f
            }
        });
        engine.AddComponent(player, new WeaponComponent()
        {
            BulletSpawnOffset = new Vector2(0f, -1f)
        });
        #endregion

        #region EnemySpawner(s)
        int   spawnersCount = 5;
        float step          = Game.Window.OrthoWidth / (float)(spawnersCount + 1);
        float offset        = -1f;

        float minTime = 1f;
        float maxTime = 3f;

        for (int i = 0; i < spawnersCount; i++)
        {
            var spawner = engine.CreateEntity();

            engine.AddComponent(spawner, new NameComponent()
            {
                Name = "SPAWNER " + i
            });

            engine.AddComponent(spawner,
                                new SpawnPointComponent()
            {
                SpawnPoint = new Vector2(step * (i + 1), offset)
            });

            var time = (float)(minTime + Game.Random.NextDouble() * (maxTime - minTime));

            engine.AddComponent(spawner,
                                new TimeComponent()
            {
                Time        = time,
                CurrentTime = time
            });
        }
        #endregion

        // init engine
        Game.engine.Init();
    }