コード例 #1
0
ファイル: Player.cs プロジェクト: lcraver/TM2
        public override void Update(GameTime gameTime, InputManager input, Map map, Camera camera, EntityManager entityManager, SoundEngine soundEngine)
        {
            if (shaking)
                camera.Zoom = 1.2f;
            else
                camera.Zoom = 1.0f;

            base.Update(gameTime, input, map, camera, entityManager, soundEngine);
            this.moveAnimation.IsActive = true;
            this.Bleeding = false;
            this.shaking = false;

            syncTilePosition = true;

            if (input.KeyDown(Keys.Right, Keys.D))
            {
                moveAnimation.CurrentFrame = new Vector2(moveAnimation.CurrentFrame.X, 3);
                velocity.X = moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                this.direction = 0;
            }
            else if (input.KeyDown(Keys.Left, Keys.A))
            {
                moveAnimation.CurrentFrame = new Vector2(moveAnimation.CurrentFrame.X, 2);
                velocity.X = -moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                this.direction = 1;
            }
            else
            {
                velocity.X = 0;
            }

            if (input.KeyDown(Keys.Up, Keys.W) && !activateGravity)
            {
                velocity.Y = -jumpSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                activateGravity = true;
            }

            if (activateGravity && this.velocity.Y > 0)
            {
                moveAnimation.IsActive = true;
                if (this.Direction == 1)
                    moveAnimation.CurrentFrame = new Vector2(moveAnimation.CurrentFrame.X, 4);
                else
                    moveAnimation.CurrentFrame = new Vector2(moveAnimation.CurrentFrame.X, 5);
            }
            else if (activateGravity && this.velocity.Y < 0)
            {
                moveAnimation.IsActive = true;
                if (this.Direction == 1)
                    moveAnimation.CurrentFrame = new Vector2(moveAnimation.CurrentFrame.X, 6);
                else
                    moveAnimation.CurrentFrame = new Vector2(moveAnimation.CurrentFrame.X, 7);
            }
            else if (!input.KeyDown(Keys.Up, Keys.W, Keys.Left, Keys.A, Keys.Right, Keys.D))
            {
                if (this.Direction == 1)
                {
                    moveAnimation.CurrentFrame = new Vector2(moveAnimation.CurrentFrame.X, 2);
                }
                else
                {
                    moveAnimation.CurrentFrame = new Vector2(moveAnimation.CurrentFrame.X, 3);
                }
            }

            if (!input.KeyDown(Keys.Up, Keys.W, Keys.Left, Keys.A, Keys.Right, Keys.D) & velocity.X == 0)
            {
                moveAnimation.IsActive = true;
                if (this.Direction == 1)
                {
                    moveAnimation.CurrentFrame = new Vector2(moveAnimation.CurrentFrame.X, 0);
                }
                else
                {
                    moveAnimation.CurrentFrame = new Vector2(moveAnimation.CurrentFrame.X, 1);
                }
            }

            if (ActivateGravity)
                velocity.Y += gravity * (float)gameTime.ElapsedGameTime.TotalSeconds;
            else
                velocity.Y = 0;

            position += velocity;

            moveAnimation.Position = position;
            ssAnimation.Update(gameTime, ref moveAnimation);

            particleEngine.EmitterLocation = new Vector2(this.Position.X + this.Animation.FrameWidth / 2, this.Position.Y + this.Animation.FrameHeight / 2);
            particleEngine.Update(map, gameTime);
        }
コード例 #2
0
ファイル: Tile.cs プロジェクト: lcraver/TM2
        public void UpdateCollision(ref Entity entity, InputManager inputManager, SoundEngine soundEngine)
        {
            FloatRect rect = new FloatRect(position.X, position.Y, layer.TileDimensions.X, layer.TileDimensions.Y);

            if (entity.OnTile && containsEntity)
            {
                if (!entity.SyncTilePosition)
                {
                    entity.Position += this.velocity;
                    entity.SyncTilePosition = true;
                }

                if (entity.Rect.Right < rect.Left || entity.Rect.Left > rect.Right || entity.Rect.Bottom != rect.Top)
                {
                    containsEntity = false;
                    entity.ActivateGravity = true;
                    entity.CanJump = false;
                }
            }

            if (entity.Rect.Intersects(rect) && state == State.Solid)
            {
                FloatRect preventity = new FloatRect(entity.PrevPosition.X, entity.PrevPosition.Y, entity.Animation.FrameWidth, entity.Animation.FrameHeight);

                FloatRect prevTile = new FloatRect(this.prevPosition.X, this.prevPosition.Y, layer.TileDimensions.X, layer.TileDimensions.Y);

                if (entity.Rect.Bottom >= rect.Top && preventity.Bottom <= prevTile.Top)
                {
                    //bottom collision
                    entity.Position = new Vector2(entity.Position.X, position.Y - entity.Animation.FrameHeight);
                    entity.ActivateGravity = false;
                    entity.OnTile = true;
                    containsEntity = true;
                }
                else if (entity.Rect.Top <= rect.Bottom && preventity.Top >= prevTile.Bottom)
                {
                    //top collision
                    entity.Position = new Vector2(entity.Position.X, position.Y + layer.TileDimensions.Y);
                    entity.ActivateGravity = true;
                }
                else if (entity.Rect.Right >= rect.Left && preventity.Right <= prevTile.Left)
                {
                    entity.Position = new Vector2(position.X - entity.Animation.FrameWidth, entity.Position.Y);
                    entity.Direction = (entity.Direction == 1) ? entity.Direction = 2 : entity.Direction = 1;
                    entity.CanJump = true;
                    entity.Velocity = new Vector2(0, entity.Velocity.Y);
                }
                else if (entity.Rect.Left <= rect.Right && preventity.Left >= prevTile.Left)
                {
                    entity.Position = new Vector2(position.X + layer.TileDimensions.X, entity.Position.Y);
                    entity.Direction = (entity.Direction == 1) ? entity.Direction = 2 : entity.Direction = 1;
                    entity.CanJump = true;
                    entity.Velocity = new Vector2(0, entity.Velocity.Y);
                }
            }
            else if (entity.Rect.Intersects(rect) && state == State.Platform)
            {
                FloatRect preventity = new FloatRect(entity.PrevPosition.X, entity.PrevPosition.Y, entity.Animation.FrameWidth, entity.Animation.FrameHeight);

                FloatRect prevTile = new FloatRect(this.prevPosition.X, this.prevPosition.Y, layer.TileDimensions.X, layer.TileDimensions.Y);

                if (entity.Rect.Bottom >= rect.Top && preventity.Bottom <= prevTile.Top && inputManager.KeyDown(Keys.Down))
                {
                    //bottom collision
                    //entity.Position = new Vector2(entity.Position.X, position.Y - entity.Animation.FrameHeight);
                    entity.ActivateGravity = true;
                    entity.OnTile = false;
                    containsEntity = false;
                }
                else if (entity.Rect.Bottom >= rect.Top && preventity.Bottom <= prevTile.Top)
                {
                    //bottom collision
                    entity.Position = new Vector2(entity.Position.X, position.Y - entity.Animation.FrameHeight);
                    entity.ActivateGravity = false;
                    entity.OnTile = true;
                    containsEntity = true;
                }
            }

            entity.Animation.Position = entity.Position;
        }