Esempio n. 1
0
        protected override void Initialize()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            Type[] types = new Type[] { typeof(Enemy), typeof(Expires), typeof(Health), typeof(SpatialForm), typeof(Transform), typeof(Velocity), typeof(Weapon) };
            pool = new GamePool(100, types);
            pool.Initialize();

            spriteBatch = new SpriteBatch(GraphicsDevice);
            world       = new EntityWorld();
            world.GetEntityManager().RemovedComponentEvent += new RemovedComponentHandler(RemovedComponent);
            world.SetPool(pool);

            font = Content.Load <SpriteFont>("Arial");
            SystemManager systemManager = world.GetSystemManager();

            renderSystem            = systemManager.SetSystem(new RenderSystem(GraphicsDevice, spriteBatch, Content), ExecutionType.Draw);
            hudRenderSystem         = systemManager.SetSystem(new HudRenderSystem(spriteBatch, font), ExecutionType.Draw);
            controlSystem           = systemManager.SetSystem(new MovementSystem(spriteBatch), ExecutionType.Update, 1);
            movementSystem          = systemManager.SetSystem(new PlayerShipControlSystem(spriteBatch), ExecutionType.Update);
            enemyShooterSystem      = systemManager.SetSystem(new EnemyShipMovementSystem(spriteBatch), ExecutionType.Update, 1);
            enemyShipMovementSystem = systemManager.SetSystem(new EnemyShooterSystem(), ExecutionType.Update);
            collisionSystem         = systemManager.SetSystem(new CollisionSystem(), ExecutionType.Update, 1);
            healthBarRenderSystem   = systemManager.SetSystem(new HealthBarRenderSystem(spriteBatch, font), ExecutionType.Draw);
            enemySpawnSystem        = systemManager.SetSystem(new EnemySpawnSystem(500, spriteBatch), ExecutionType.Update);
            expirationSystem        = systemManager.SetSystem(new ExpirationSystem(), ExecutionType.Update);

            systemManager.InitializeAll();

            InitPlayerShip();
            InitEnemyShips();

            base.Initialize();
        }
        public static Entity CreateMissile(EntityWorld world)
        {
            Entity   missile = world.CreateEntity();
            GamePool pool    = (GamePool)world.GetPool();

            missile.SetGroup("BULLETS");

            missile.AddComponent(pool.TakeComponent <Transform>());
            missile.AddComponent(pool.TakeComponent <SpatialForm>());
            missile.AddComponent(pool.TakeComponent <Velocity>());
            missile.AddComponent(pool.TakeComponent <Expires>());
            missile.GetComponent <SpatialForm>().SetSpatialFormFile("Missile");
            missile.GetComponent <Expires>().SetLifeTime(2000);
            return(missile);
        }
        public static Entity CreateShipExplosion(EntityWorld world, float x, float y)
        {
            Entity   e    = world.CreateEntity();
            GamePool pool = (GamePool)world.GetPool();

            e.SetGroup("EFFECTS");

            e.AddComponent(pool.TakeComponent <Transform>());
            e.AddComponent(pool.TakeComponent <SpatialForm>());
            e.AddComponent(pool.TakeComponent <Expires>());
            e.GetComponent <SpatialForm>().SetSpatialFormFile("ShipExplosion");
            e.GetComponent <Transform>().SetCoords(new Vector3(x, y, 0));
            e.GetComponent <Expires>().SetLifeTime(1000);
            return(e);
        }
        public static Entity CreateBulletExplosion(EntityWorld world, float x, float y)
        {
            Entity   e    = world.CreateEntity();
            GamePool pool = (GamePool)world.Pool;

            e.SetGroup("EFFECTS");

            e.AddComponent(pool.TakeComponent <Transform>());
            e.AddComponent(pool.TakeComponent <SpatialForm>());
            e.AddComponent(pool.TakeComponent <Expires>());
            e.GetComponent <SpatialForm>().SpatialFormFile = "BulletExplosion";
            e.GetComponent <Expires>().LifeTime            = 1000;
            e.GetComponent <Transform>().Coords            = new Vector3(x, y, 0);
            return(e);
        }
        public static Entity CreateEnemyShip(EntityWorld world)
        {
            Entity e = world.CreateEntity();

            e.SetGroup("SHIPS");
            GamePool pool = (GamePool)world.GetPool();

            e.AddComponent(pool.TakeComponent <Transform>());
            e.AddComponent(pool.TakeComponent <SpatialForm>());
            e.AddComponent(pool.TakeComponent <Health>());
            e.AddComponent(pool.TakeComponent <Weapon>());
            e.AddComponent(pool.TakeComponent <Enemy>());
            e.AddComponent(pool.TakeComponent <Velocity>());
            e.GetComponent <SpatialForm>().SetSpatialFormFile("EnemyShip");
            e.GetComponent <Health>().SetHealth(10);
            return(e);
        }