public void SpriteFromSheetTests()
        {
            var fromNullSheet = new SpriteFromSheet();
            Assert.AreEqual(0, fromNullSheet.SpritesCount);
            Assert.IsNull(fromNullSheet.GetSprite(0));

            var emptySheet = new SpriteSheet();
            var fromEmptySheet = new SpriteFromSheet { Sheet = emptySheet };
            Assert.AreEqual(0, fromEmptySheet.SpritesCount);
            Assert.IsNull(fromEmptySheet.GetSprite(0));

            var nullList = new SpriteSheet { Sprites = null };
            var fromnullListSheet = new SpriteFromSheet { Sheet = nullList };
            Assert.AreEqual(0, fromnullListSheet.SpritesCount);
            Assert.IsNull(fromnullListSheet.GetSprite(0));
            
            var emptyList = new SpriteSheet { Sprites = new List<Sprite>() };
            var fromEmptyListSheet = new SpriteFromSheet { Sheet = emptyList };
            Assert.AreEqual(0, fromEmptyListSheet.SpritesCount);
            Assert.IsNull(fromEmptyListSheet.GetSprite(0));

            var validSheet = new SpriteSheet() { Sprites = new List<Sprite> { new Sprite("0"), new Sprite("1") } };
            var fromValidSheet = new SpriteFromSheet { Sheet = validSheet };
            Assert.AreEqual(2, fromValidSheet.SpritesCount);
            for (int i = 0; i < fromValidSheet.SpritesCount; i++)
            {
                Assert.AreEqual(i.ToString(), fromValidSheet.GetSprite(i).Name);
            }
        }
示例#2
0
        protected override async Task LoadContent()
        {
            await base.LoadContent();

            Sprites = Asset.Load<SpriteSheet>("UIImages");

            var lifeBar = new ImageElement { Source = Sprites["Logo"], HorizontalAlignment = HorizontalAlignment.Center };
            lifeBar.DependencyProperties.Set(GridBase.ColumnSpanPropertyKey, 3);

            var quitGameButton = new Button
                {
                    Content = new TextBlock { Text = "Quit Game", Font = Asset.Load<SpriteFont>("MicrosoftSansSerif15") },
                    VerticalAlignment = VerticalAlignment.Bottom,
                    HorizontalAlignment = HorizontalAlignment.Left,
                    Padding = Thickness.UniformRectangle(10),
                };
            quitGameButton.DependencyProperties.Set(GridBase.ColumnPropertyKey, 0);
            quitGameButton.DependencyProperties.Set(GridBase.RowPropertyKey, 2);
            quitGameButton.Click += (sender, args) => Exit();

            modalButton1Text = new TextBlock { Text = "Close Modal window 1", Font = Asset.Load<SpriteFont>("MicrosoftSansSerif15") };
            var modalButton1 = new Button
            {
                Name = "Button Modal 1",
                Content = modalButton1Text,
                VerticalAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Center,
                Padding = Thickness.UniformRectangle(10),
            };
            modalButton1.Click += ModalButton1OnClick;
            modal1 = new ModalElement { Content = modalButton1, Name = "Modal 1"};
            modal1.DependencyProperties.Set(Panel.ZIndexPropertyKey, 1);
            modal1.DependencyProperties.Set(GridBase.ColumnPropertyKey, 1);
            modal1.DependencyProperties.Set(GridBase.RowPropertyKey, 1);
            modal1.OutsideClick += Modal1OnOutsideClick;

            modalButton2Text = new TextBlock { Text = "Close Modal window 2", Font = Asset.Load<SpriteFont>("MicrosoftSansSerif15") };
            var modalButton2 = new Button
            {
                Name = "Button Modal 2",
                Content = modalButton2Text,
                VerticalAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Center,
                Padding = Thickness.UniformRectangle(10),
            };
            modalButton2.Click += ModalButton2OnClick;
            modal2 = new ModalElement { Content = modalButton2, Name = "Modal 2" };
            modal2.DependencyProperties.Set(Panel.ZIndexPropertyKey, 2);
            modal2.DependencyProperties.Set(GridBase.ColumnPropertyKey, 1);
            modal2.DependencyProperties.Set(GridBase.RowPropertyKey, 2);
            modal2.OutsideClick += Modal2OnOutsideClick;

            uniformGrid = new UniformGrid { Columns = 3, Rows = 3 };
            uniformGrid.Children.Add(modal1);
            uniformGrid.Children.Add(modal2);
            uniformGrid.Children.Add(lifeBar);
            uniformGrid.Children.Add(quitGameButton);
            
            UIComponent.RootElement = uniformGrid;
        }
示例#3
0
        /// <summary>
        /// Creates a new instance of <see cref="SpriteFromSheet"/> with the specified <see cref="SpriteSheet"/>.
        /// <see cref="CurrentFrame"/> is initialized according to the specified <paramref name="spriteName"/>.
        /// </summary>
        /// <param name="sheet"></param>
        /// <param name="spriteName">The name of the sprite.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">sheet</exception>
        /// <exception cref="System.Collections.Generic.KeyNotFoundException">No sprite in the sheet has the given name.</exception>
        /// <remarks>If two sprites have the provided name then the first sprite found is used.</remarks>
        public static SpriteFromSheet Create(SpriteSheet sheet, string spriteName)
        {
            if (sheet == null) throw new ArgumentNullException(nameof(sheet));

            return new SpriteFromSheet
            {
                Sheet = sheet,
                CurrentFrame = sheet.FindImageIndex(spriteName)
            };
        }
 private static Entity CreateSpriteEntity(SpriteSheet sheet, string frameName)
 {
     return new Entity(frameName)
     {
         new SpriteComponent
         {
             SpriteProvider = SpriteFromSheet.Create(sheet, frameName)
         }
     };
 }
 private Entity CreateSpriteEntity(SpriteSheet sheet, string frameName)
 {
     return new Entity(frameName)
     {
         new SpriteComponent
         {
             SpriteProvider = new SpriteFromSheet { Sheet = sheet },
             CurrentFrame = sheet.FindImageIndex(frameName)
         }
     };
 }
示例#6
0
        public override void Start()
        {
            spriteSheet = SpriteSheet;
            enemySpriteComponent = Entity.Get<SpriteComponent>();
             
            if (!IsLiveReloading)
            {
                // Register our-self to the logic to detect collision
                Logic.WatchEnemy(Entity);

                Reset();
            }
        }
示例#7
0
        protected override async Task LoadContent()
        {
            await base.LoadContent();

            sliderImages = Content.Load<SpriteSheet>("DebugSlider");

            slider = new Slider { TrackStartingOffsets = new Vector2(10, 6), TickOffset = 10 };
            SetSliderImages(isRotatedImages);

            grid = new UniformGrid { Children = { slider } };

            UIComponent.Page = new Engine.UIPage { RootElement = grid };
        }
        private Entity CreateSpriteEntity(SpriteSheet sheet, string frameName)
        {
            var entity = new Entity(frameName)
            {
                new SpriteComponent
                {
                    SpriteProvider = SpriteFromSheet.Create(sheet, frameName)
                }
            };

            entities.Add(entity);

            return entity;
        }
        private Entity CreateSpriteEntity(SpriteSheet sheet, string frameName, bool addToScene = true)
        {
            var entity = new Entity(frameName)
            {
                new SpriteComponent
                {
                    SpriteProvider = new SpriteFromSheet { Sheet = sheet },
                    CurrentFrame = sheet.FindImageIndex(frameName)
                }
            };

            if (addToScene)
                entities.Add(entity);

            return entity;
        }
        public void SpriteFromSheetTests()
        {
            var fromNullSheet = new SpriteFromSheet();
            Assert.AreEqual(0, fromNullSheet.SpritesCount);
            Assert.IsNull(fromNullSheet.GetSprite());

            var emptySheet = new SpriteSheet();
            var fromEmptySheet = new SpriteFromSheet { Sheet = emptySheet };
            Assert.AreEqual(0, fromEmptySheet.SpritesCount);
            Assert.IsNull(fromEmptySheet.GetSprite());

            var validSheet = new SpriteSheet { Sprites = { new Sprite("0"), new Sprite("1") } };
            var fromValidSheet = new SpriteFromSheet { Sheet = validSheet };
            Assert.AreEqual(2, fromValidSheet.SpritesCount);
            for (var i = 0; i < fromValidSheet.SpritesCount; i++)
            {
                fromValidSheet.CurrentFrame = i;
                Assert.AreEqual(i.ToString(), fromValidSheet.GetSprite().Name);
            }
        }
示例#11
0
        protected override async Task LoadContent()
        {
            await base.LoadContent();
            
            // sets the virtual resolution
            areaSize = new Vector2(GraphicsDevice.BackBuffer.Width, GraphicsDevice.BackBuffer.Height);

            // Creates the camera
            CameraComponent.UseCustomProjectionMatrix = true;
            CameraComponent.ProjectionMatrix = Matrix.OrthoRH(areaSize.X, areaSize.Y, -2, 2);

            // Load assets
            groundSprites = Asset.Load<SpriteSheet>("GroundSprite");
            ballSprite1 = Asset.Load<SpriteSheet>("BallSprite1");
            ballSprite2 = Asset.Load<SpriteSheet>("BallSprite2");
            ball = new Entity { new SpriteComponent { SpriteProvider = new SpriteFromSheet { Sheet = Asset.Load<SpriteSheet>("BallSprite1") } } };

            // create fore/background entities
            foreground = new Entity();
            background = new Entity();
            foreground.Add(new SpriteComponent { SpriteProvider = new SpriteFromSheet { Sheet = groundSprites }, CurrentFrame = 1 });
            background.Add(new SpriteComponent { SpriteProvider = new SpriteFromSheet { Sheet = groundSprites }, CurrentFrame = 0 });

            Scene.Entities.Add(ball);
            Scene.Entities.Add(foreground);
            Scene.Entities.Add(background);

            spriteComponent = ball.Get<SpriteComponent>();
            transfoComponent = ball.Get<TransformComponent>();
            
            var decorationScalings = new Vector3(areaSize.X, areaSize.Y, 1);
            background.Get<TransformComponent>().Scale = decorationScalings;
            foreground.Get<TransformComponent>().Scale = decorationScalings/2;
            background.Get<TransformComponent>().Position = new Vector3(0, 0, -1);
            foreground.Get<TransformComponent>().Position = new Vector3(0, 0, 1);

            SpriteAnimation.Play(spriteComponent, 0, spriteComponent.SpriteProvider.SpritesCount-1, AnimationRepeatMode.LoopInfinite, 30);
        }
示例#12
0
 private void SetSpriteImage(SpriteSheet sprite)
 {
     spriteComponent.SpriteProvider = new SpriteFromSheet { Sheet = sprite };
 }
示例#13
0
        public override async Task Execute()
        {
            spriteSheet = SpriteSheet;
            agentSpriteComponent = Entity.Get<SpriteComponent>();

            // Calculate offset of the bullet from the Agent if he is facing left and right side // TODO improve this
            var bulletOffset = new Vector3(1f, 0.2f, 0f);

            // Initialize game entities
            if(!IsLiveReloading)
            {
                shootDelayCounter = 0f;
                isAgentFacingRight = true;
                currentAgentAnimation = AgentAnimation.Idle;
            }
            CurrentAgentAnimation = currentAgentAnimation;

            while (Game.IsRunning)
            {
                await Script.NextFrame();

                var inputState = GetKeyboardInputState();

                if (inputState == InputState.None)
                    inputState = GetPointerInputState();

                // Reset the shoot delay, if state changes
                if (inputState != InputState.Shoot && CurrentAgentAnimation == AgentAnimation.Shoot)
                    shootDelayCounter = 0;

                if (inputState == InputState.RunLeft || inputState == InputState.RunRight)
                {
                    // Update Agent's position
                    var dt = (float) Game.UpdateTime.Elapsed.TotalSeconds;

                    Entity.Transform.Position.X += ((inputState == InputState.RunRight) ? AgentMoveDistance : -AgentMoveDistance)*dt;

                    if (Entity.Transform.Position.X < -gameWidthHalfX)
                        Entity.Transform.Position.X = -gameWidthHalfX;

                    if (Entity.Transform.Position.X > gameWidthHalfX)
                        Entity.Transform.Position.X = gameWidthHalfX;

                    isAgentFacingRight = inputState == InputState.RunRight;

                    // If agent face left, flip the sprite
                    Entity.Transform.Scale.X = isAgentFacingRight ? 1f : -1f;

                    // Update the sprite animation and state
                    CurrentAgentAnimation = AgentAnimation.Run;
                }
                else if (inputState == InputState.Shoot)
                {
                    // Update shootDelayCounter, and check whether it is time to create a new bullet
                    shootDelayCounter -= (float) Game.UpdateTime.Elapsed.TotalSeconds;

                    if (shootDelayCounter > 0)
                        continue;


                    // Reset shoot delay
                    shootDelayCounter = AgentShootDelay;

                    // Spawns a new bullet
                    var bullet = new Entity
                    {
                        new SpriteComponent { SpriteProvider = SpriteFromSheet.Create(spriteSheet, "bullet") },

                        // Will make the beam move along a direction at each frame
                        new BeamScript {DirectionX = isAgentFacingRight ? 1f : -1f, SpriteSheet = SpriteSheet},
                    };

                    bullet.Transform.Position = (isAgentFacingRight) ? Entity.Transform.Position + bulletOffset : Entity.Transform.Position + (bulletOffset*new Vector3(-1, 1, 1));

                    SceneSystem.SceneInstance.Scene.Entities.Add(bullet);
                    Logic.WatchBullet(bullet);

                    // Start animation for shooting
                    CurrentAgentAnimation = AgentAnimation.Shoot;
                }
                else
                {
                    CurrentAgentAnimation = AgentAnimation.Idle;
                }
            }
        }
示例#14
0
        private static SpriteComponent CreateSpriteComponent(int nbOfFrames)
        {
            var spriteGroup = new SpriteSheet { Sprites = new List<Sprite>() };
            var sprite = new SpriteComponent { SpriteProvider = new SpriteFromSheet { Sheet = spriteGroup } };

            // add a few sprites
            for (int i = 0; i < nbOfFrames; i++)
            {
                spriteGroup.Sprites.Add(new Sprite(Guid.NewGuid().ToString()));
            }

            return sprite;
        }
示例#15
0
        public override async Task Execute()
        {
            spriteSheet = BulletSheet;
            agentSpriteComponent = Entity.Get<SpriteComponent>();
            var animComponent = Entity.Get<AnimationComponent>();
            PlayingAnimation playingAnimation = null;

            // Calculate offset of the bullet from the Agent if he is facing left and right side // TODO improve this
            var bulletOffset = new Vector3(1.3f, 1.65f, 0f);

            // Initialize game entities
            if (!IsLiveReloading)
            {
                shootDelayCounter = 0f;
                isAgentFacingRight = true;
                currentAgentAnimation = AgentAnimation.Idle;
            }
            CurrentAgentAnimation = currentAgentAnimation;

            var normalScaleX = Entity.Transform.Scale.X;

            var bulletCS = BulletColliderShape;

            Task animTask = null;

            while (Game.IsRunning)
            {
                await Script.NextFrame();

                var inputState = GetKeyboardInputState();

                if (inputState == InputState.None)
                    inputState = GetPointerInputState();

                if (inputState == InputState.RunLeft || inputState == InputState.RunRight)
                {
                    // Update Agent's position
                    var dt = (float)Game.UpdateTime.Elapsed.TotalSeconds;

                    Entity.Transform.Position.X += ((inputState == InputState.RunRight) ? AgentMoveDistance : -AgentMoveDistance) * dt;

                    if (Entity.Transform.Position.X < -gameWidthHalfX)
                        Entity.Transform.Position.X = -gameWidthHalfX;

                    if (Entity.Transform.Position.X > gameWidthHalfX)
                        Entity.Transform.Position.X = gameWidthHalfX;

                    isAgentFacingRight = inputState == InputState.RunRight;

                    // If agent face left, flip the sprite
                    Entity.Transform.Scale.X = isAgentFacingRight ? normalScaleX : -normalScaleX;

                    // Update the sprite animation and state
                    CurrentAgentAnimation = AgentAnimation.Run;
                    if (playingAnimation == null || playingAnimation.Name != "Run")
                    {
                        playingAnimation = animComponent.Play("Run");
                    }
                }
                else if (inputState == InputState.Shoot)
                {
                    if(animTask != null && !animTask.IsCompleted) continue;
                    if (animTask != null && animTask.IsCompleted) playingAnimation = null;

                    animTask = null;

                    var rb = new RigidbodyComponent { CanCollideWith = CollisionFilterGroupFlags.CustomFilter1, CollisionGroup = CollisionFilterGroups.DefaultFilter };
                    rb.ColliderShapes.Add(new ColliderShapeAssetDesc { Shape = bulletCS });

                    // Spawns a new bullet
                    var bullet = new Entity
                    {
                        new SpriteComponent { SpriteProvider = SpriteFromSheet.Create(spriteSheet, "bullet") },
                        rb,
                        new BeamScript()
                    };
                    bullet.Name = "bullet";

                    bullet.Transform.Position = (isAgentFacingRight) ? Entity.Transform.Position + bulletOffset : Entity.Transform.Position + (bulletOffset * new Vector3(-1, 1, 1));
                    bullet.Transform.UpdateWorldMatrix();

                    SceneSystem.SceneInstance.Scene.Entities.Add(bullet);

                    rb.LinearFactor = new Vector3(1, 0, 0);
                    rb.AngularFactor = new Vector3(0, 0, 0);
                    rb.ApplyImpulse(isAgentFacingRight ? new Vector3(25, 0, 0) : new Vector3(-25, 0, 0));

                    // Start animation for shooting
                    CurrentAgentAnimation = AgentAnimation.Shoot;
                    if (playingAnimation == null || playingAnimation.Name != "Attack")
                    {
                        playingAnimation = animComponent.Play("Attack");
                        animTask = playingAnimation.Ended();
                    }
                }
                else
                {
                    CurrentAgentAnimation = AgentAnimation.Idle;
                    if (playingAnimation == null || playingAnimation.Name != "Stance")
                    {
                        playingAnimation = animComponent.Play("Stance");
                    }
                }
            }
        }
示例#16
0
 private void SetSpriteImage(SpriteSheet sprite)
 {
     // Keep the current frame when changing sprite provider
     var currentFrame = spriteComponent.CurrentFrame;
     spriteComponent.SpriteProvider = new SpriteFromSheet { Sheet = sprite, CurrentFrame = currentFrame };
 }