Ejemplo n.º 1
0
        private Entity createBulletTemplate(Game game)
        {
            Entity bullet = new Entity();

            //Component: Damage entities.
            DamageOnContactComponent damage = new DamageOnContactComponent();
            damage.Damage = 1;
            bullet.AddComponent(damage);

            //Component: Has health. Used to simulate destruction on contact with an entity
            HealthComponent health = new HealthComponent();
            health.Health = 1;
            bullet.AddComponent(health);

            //Component: Has a texture
            TextureComponent tex = new TextureComponent();
            tex.Texture = game.Content.Load<Texture2D>("spaceArt/png/laserGreen");
            tex.SourceRect = tex.Texture.Bounds;
            bullet.AddComponent(tex);

            //Component: Moves linearly
            LinearMovementComponent movement = new LinearMovementComponent();
            bullet.AddComponent(movement);

            //Component: Moves at constant speed
            MoveSpeedComponent speed = new MoveSpeedComponent();
            speed.MoveSpeed = 18;
            bullet.AddComponent(speed);

            //Component: Has a bounding box
            AABBComponent aabb = new AABBComponent();
            aabb.Height = tex.SourceRect.Height;
            aabb.Width = tex.SourceRect.Width;
            bullet.AddComponent(aabb);

            //Component: Is rendered at a specific layer - just above the enemies
            RenderLayerComponent layer = new RenderLayerComponent();
            layer.LayerID = 12;
            bullet.AddComponent(layer);

            //Component: Is destroyed when it ventures off the (right side of the) screen
            DestroyedWhenOffScreenComponent destroyer = new DestroyedWhenOffScreenComponent();
            bullet.AddComponent(destroyer);

            //Component: Is destroyed when it runs out of health
            DestroyedWhenNoHealthComponent destroyer2 = new DestroyedWhenNoHealthComponent();
            bullet.AddComponent(destroyer2);

            //Component: Plays a laser sound
            SoundEffectComponent soundEffect = new SoundEffectComponent();
            soundEffect.effect = game.Content.Load<SoundEffect>("sound/39459__the-bizniss__laser");
            bullet.AddComponent(soundEffect);

            return bullet;
        }
        private void generateStar(IEntityManager entityStore, Game game, Texture2D bigStarTex, Texture2D littleStarTex, Frustum generationVolume, double speedAtUnitDepth, double screenDepth, double screenWidth, double screenHeight)
        {
            //sample a 3d location for the star
            Vector3 starLoc = generationVolume.samplePoint(rand);

            //determine the speed as a function of the depth
            double speed = speedAtUnitDepth / starLoc.Z * screenDepth * screenDepth;

            //determine the depth/speed of the star

            Entity star = new Entity();

            //Component: Has a position, determined by starLoc
            PositionComponent pos = new PositionComponent();
            Vector3 starLoc2 = generationVolume.TransformFromEuclideanSpaceToPseudoSphericalSpace(starLoc);
            pos.Position = new Vector2(starLoc2.X * game.GraphicsDevice.Viewport.Width, starLoc2.Y * game.GraphicsDevice.Viewport.Height);
            star.AddComponent(pos);

            //Component: Moves at a constant speed
            LinearMovementComponent movementStrat = new LinearMovementComponent();
            star.AddComponent(movementStrat);

            //Component: Has a move speed
            MoveSpeedComponent speedComponent = new MoveSpeedComponent();
            speedComponent.MoveSpeed = (float)speed;
            star.AddComponent(speedComponent);

            //Component: Wraps around the screen
            ScreenWrappingComponent wrapper = new ScreenWrappingComponent();
            star.AddComponent(wrapper);

            //Component: Has a texture.  This should be little star for far away/slow stars, big otherwise
            TextureComponent tex = new TextureComponent();
            tex.Texture = (speed > -2.5) ? littleStarTex : bigStarTex;
            tex.SourceRect = tex.Texture.Bounds;
            star.AddComponent(tex);

            //Component: Has a bounding box
            AABBComponent aabb = new AABBComponent();
            aabb.Width = tex.Texture.Width;
            aabb.Height = tex.Texture.Height;
            star.AddComponent(aabb);

            //Component: Is rendered at a specific layer (just above the background)
            RenderLayerComponent layer = new RenderLayerComponent();
            layer.LayerID = 1;
            star.AddComponent(layer);

            entityStore.Add(star);
        }
        private Entity createExplosionEntity(Game game, AABBComponent parentAABB)
        {
            Entity expl = new Entity();

            //Component: Has a texture
            TextureComponent tex = new TextureComponent();
            tex.Texture = game.Content.Load<Texture2D>("explosion");
            tex.SourceRect = tex.Texture.Bounds;
            expl.AddComponent(tex);

            //Component: Has an animation
            AnimationComponent anim = new AnimationComponent();
            anim.CurrentFrameIndex = 0;
            anim.FrameDuration = 45;
            anim.Looping = false;
            anim.NumFrames = 12;
            anim.TimeSinceFrameChange = 0;
            expl.AddComponent(anim);

            //Component: Is rendered at a specific layer (just behind the player)
            RenderLayerComponent layer = new RenderLayerComponent();
            layer.LayerID = 9;
            expl.AddComponent(layer);

            //Component: Has a bounding box
            AABBComponent aabb = new AABBComponent();
            aabb.Height = 134;
            aabb.Width = 134;
            expl.AddComponent(aabb);

            //Component: Destroys itself once its animation completes
            DestroyedWhenAnimationCompleteComponent destructor = new DestroyedWhenAnimationCompleteComponent();
            expl.AddComponent(destructor);

            //Component: Is offset such that it is concentric with its parent
            PositionDeltaComponent delta = new PositionDeltaComponent();
            float deltaX = parentAABB.Width / 2.0f - aabb.Width / 2.0f;
            float deltaY = parentAABB.Height / 2.0f - aabb.Height / 2.0f;
            delta.Delta = new Vector2(deltaX, deltaY);
            expl.AddComponent(delta);

            //Component: Plays an explosion sound
            SoundEffectComponent soundEffect = new SoundEffectComponent();
            soundEffect.effect = game.Content.Load<SoundEffect>("freesoundsorg/87529__robinhood76__01448-distant-big-explosion-2");
            expl.AddComponent(soundEffect);

            return expl;
        }
Ejemplo n.º 4
0
        //Create the player entity and its associated entities
        private void initializePlayer(IEntityManager entityStore, Game game)
        {
            Entity player = new Entity();

            //give the player health
            HealthComponent hp = new HealthComponent();
            hp.Health = 100;
            player.AddComponent(hp);

            //Give the player a position
            PositionComponent pos = new PositionComponent();
            Vector2 playerPosition = new Vector2(game.GraphicsDevice.Viewport.TitleSafeArea.X, game.GraphicsDevice.Viewport.TitleSafeArea.Y + game.GraphicsDevice.Viewport.TitleSafeArea.Height / 2);
            pos.Position = playerPosition;
            player.AddComponent(pos);

            //And a texture
            TextureComponent tex = new TextureComponent();
            tex.Texture = game.Content.Load<Texture2D>("spaceArt/png/player");
            tex.SourceRect = tex.Texture.Bounds;
            player.AddComponent(tex);

            //And the ability to react to inputs
            PlayerMovementComponent inputManager = new PlayerMovementComponent();
            player.AddComponent(inputManager);

            //And a constant movement speed
            MoveSpeedComponent speed = new MoveSpeedComponent();
            speed.MoveSpeed = 8.0f;
            player.AddComponent(speed);

            //And a bounding box for clamping (and collisions in the future!)
            AABBComponent aabb = new AABBComponent();
            aabb.Width = tex.Texture.Width;
            aabb.Height = tex.Texture.Height;
            player.AddComponent(aabb);

            //And render layer information.  We'll have the player render at level 10 for now.
            RenderLayerComponent layer = new RenderLayerComponent();
            layer.LayerID = 10;
            player.AddComponent(layer);

            //And a component to indicate the the player needs to be clamped to the screen
            ScreenClampedComponent clamper = new ScreenClampedComponent();
            player.AddComponent(clamper);

            //And a component that indicates that the player can be destroyed if it runs out of health
            DestroyedWhenNoHealthComponent destruct = new DestroyedWhenNoHealthComponent();
            player.AddComponent(destruct);

            //And the ability to damage entities on contact with them
            DamageOnContactComponent damager = new DamageOnContactComponent();
            damager.Damage = 10;
            player.AddComponent(damager);

            entityStore.Add(player);

            //also create the player's gun
            initializePlayerGuns(entityStore, game, player);
        }
        private Entity createMineTemplateEntity(Game game)
        {
            Entity mineTemplate = new Entity();

            //Component: Damage the player.
            DamageOnContactComponent damage = new DamageOnContactComponent();
            damage.Damage = 10;
            mineTemplate.AddComponent(damage);

            //Component: Has health
            HealthComponent health = new HealthComponent();
            health.Health = 10;
            mineTemplate.AddComponent(health);

            //Component: Has a texture
            TextureComponent tex = new TextureComponent();
            tex.Texture = game.Content.Load<Texture2D>("spaceArt/png/meteorBig");
            tex.SourceRect = tex.Texture.Bounds;
            mineTemplate.AddComponent(tex);

            //Component: Has a position.
            PositionComponent pos = new PositionComponent();
            pos.Position.X = game.GraphicsDevice.Viewport.Width - 1;
            pos.Position.Y = game.GraphicsDevice.Viewport.Height / 2 - tex.SourceRect.Height / 2;
            pos.Rotation = Math.PI / 4;
            mineTemplate.AddComponent(pos);

            //Component: Given a random starting vertical position
            RandomPositionOffsetComponent randomOffset = new RandomPositionOffsetComponent();
            randomOffset.Minimum = new Vector2(0.0f, -0.4f * game.GraphicsDevice.Viewport.Height);
            randomOffset.Maximum = new Vector2(0.0f, 0.4f * game.GraphicsDevice.Viewport.Height);
            mineTemplate.AddComponent(randomOffset);

            //Component: Moves linearly, continuously rotates
            LinearMovementComponent movement = new LinearMovementComponent();
            movement.RotationRate = 0.01f;
            mineTemplate.AddComponent(movement);

            //Component: Moves at constant speed
            MoveSpeedComponent speed = new MoveSpeedComponent();
            speed.MoveSpeed = -10;
            mineTemplate.AddComponent(speed);

            //Component: Has a bounding box
            AABBComponent aabb = new AABBComponent();
            aabb.Height = tex.SourceRect.Height;
            aabb.Width = tex.SourceRect.Width;
            mineTemplate.AddComponent(aabb);

            //Component: Is rendered at a specific layer - just above the player
            RenderLayerComponent layer = new RenderLayerComponent();
            layer.LayerID = 11;
            mineTemplate.AddComponent(layer);

            //Component: Is destroyed when it ventures off the (left side of the) screen
            DestroyedWhenOffScreenComponent destroyer = new DestroyedWhenOffScreenComponent();
            mineTemplate.AddComponent(destroyer);

            //Component: Has a score value
            // TODO this is more complex than the other ones, involves the changing of global state!

            //Component: Is destroyed when it runs out of health
            DestroyedWhenNoHealthComponent destroyer2 = new DestroyedWhenNoHealthComponent();
            mineTemplate.AddComponent(destroyer2);

            //Component: Leaves behind an explosion entity when it is destroyed
            AddComponentOnDestructionComponent explosionSpawnTriggerer = new AddComponentOnDestructionComponent();
            SpawnEntityComponent explosionSpawner = new SpawnEntityComponent();
            explosionSpawner.Factory = new ComposedEntityFactory(new List<IEntityFactory>() {
                new CloneEntityFactory(createExplosionEntity(game, aabb)),
                new InheritParentComponentEntityFactory(typeof(PositionComponent)) });
            explosionSpawnTriggerer.ToAdd = explosionSpawner;
            mineTemplate.AddComponent(explosionSpawnTriggerer);

            return mineTemplate;
        }
        private void initializePurpleBackground(IEntityManager entityStore, Game game)
        {
            Entity bg = new Entity();

            //Component: Has a position
            PositionComponent pos = new PositionComponent();
            pos.Position = new Vector2(0, 0);
            bg.AddComponent(pos);

            //Component: Has an AABB
            AABBComponent aabb = new AABBComponent();
            aabb.Width = game.GraphicsDevice.Viewport.Width;
            aabb.Height = game.GraphicsDevice.Viewport.Height;
            bg.AddComponent(aabb);

            //Component: Has a texture
            TextureComponent tex = new TextureComponent();
            tex.Texture = game.Content.Load<Texture2D>("spaceArt/png/Background/backgroundColor");
            tex.SourceRect = tex.Texture.Bounds;
            bg.AddComponent(tex);

            //Component: Is rendered at a specific layer (the backmost one!)
            RenderLayerComponent layer = new RenderLayerComponent();
            layer.LayerID = 0;
            bg.AddComponent(layer);

            entityStore.Add(bg);
        }