Beispiel #1
0
        private void MovePlayer(KeyboardState currentKeyboardState, KeyboardState lastKeyboardState, GameTime gameTime)
        {
            var delta     = (float)gameTime.ElapsedGameTime.TotalMilliseconds;
            var curpos    = new VT2(CollisionBox.X, CollisionBox.Y);
            var direction = Direction.none;

            if (HelperMethod.IsKeyHold(Keys.A, currentKeyboardState, lastKeyboardState))
            {
                direction = Direction.left;
            }
            if (HelperMethod.IsKeyHold(Keys.D, currentKeyboardState, lastKeyboardState))
            {
                direction = Direction.right;
            }
            if (HelperMethod.IsKeyHold(Keys.W, currentKeyboardState, lastKeyboardState))
            {
                direction = Direction.up;
            }
            if (HelperMethod.IsKeyHold(Keys.S, currentKeyboardState, lastKeyboardState))
            {
                direction = Direction.down;
            }

            if (direction != Direction.none && !timer.IsRunning)
            {
                timer.Start();
            }

            if (timer.IsRunning && timer.ElapsedTime <= 1f)
            {
                walksfx.Volume = timer.ElapsedTime;
            }

            switch (direction)
            {
            case Direction.up:
                Velocity = new VT2(0, -MovementSpeed);
                dir      = direction;
                if (animatedEntity.CurntAnimationName != AnimationName.walk_up.ToString())
                {
                    animatedEntity.PlayAnimation(AnimationName.walk_up.ToString());
                }
                if (walksfx.State != SoundState.Playing)
                {
                    walksfx.Play();
                }
                break;

            case Direction.down:
                Velocity = new VT2(0, MovementSpeed);
                dir      = direction;
                if (animatedEntity.CurntAnimationName != AnimationName.walk_down.ToString())
                {
                    animatedEntity.PlayAnimation(AnimationName.walk_down.ToString());
                }
                if (walksfx.State != SoundState.Playing)
                {
                    walksfx.Play();
                }
                break;

            case Direction.right:
                Velocity = new VT2(MovementSpeed, 0);
                dir      = direction;
                if (animatedEntity.CurntAnimationName != AnimationName.walk_right.ToString())
                {
                    animatedEntity.PlayAnimation(AnimationName.walk_right.ToString());
                }
                if (walksfx.State != SoundState.Playing)
                {
                    walksfx.Play();
                }
                break;

            case Direction.left:
                Velocity = new VT2(-MovementSpeed, 0);
                dir      = direction;
                if (animatedEntity.CurntAnimationName != AnimationName.walk_left.ToString())
                {
                    animatedEntity.PlayAnimation(AnimationName.walk_left.ToString());
                }
                if (walksfx.State != SoundState.Playing)
                {
                    walksfx.Play();
                }
                break;

            case Direction.none:
                var anme = "idle_" + dir.ToString();
                if (animatedEntity.CurntAnimationName != anme)
                {
                    animatedEntity.PlayAnimation(anme);
                }
                walksfx.Stop();
                walksfx.Volume = 0;
                timer.Reset();
                break;
            }

            var move = CollisionBox.Move(curpos.X + Velocity.X, curpos.Y + Velocity.Y, x => {
                if (x.Other.HasTag(CollisionTag.FloorSwitch))
                {
                    return(CollisionResponses.Cross);
                }

                if (x.Other.HasTag(CollisionTag.Lever))
                {
                    return(CollisionResponses.Cross);
                }

                if (x.Other.HasTag(CollisionTag.DoorOpened))
                {
                    return(CollisionResponses.Cross);
                }

                if (x.Other.HasTag(CollisionTag.EndPoint))
                {
                    return(CollisionResponses.Cross);
                }

                if (x.Other.HasTag(CollisionTag.PushableBlock))
                {
                    return(CollisionResponses.Slide);
                }

                return(CollisionResponses.Slide);
            });

            Object.WorldPos = new Vector3(CollisionBox.X, CollisionBox.Y, 0);

            var floorswitch = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.FloorSwitch));

            if (floorswitch != null)
            {
                Object obj = (Object)floorswitch.Box.Data;
                if (lastInteractableObject != obj && obj.TileType.IsOff() && floorswitchpressedsfx.State != SoundState.Playing)
                {
                    obj.TileType = SpriteSheetRectName.ButtonPressed_E;
                    floorswitchpressedsfx.Play();
                    obj.Activate(Object, obj);
                    lastInteractableObject = obj;
                }
            }
            else
            {
                if (lastInteractableObject != null && lastInteractableObject.TileType.IsFloorSwitch())
                {
                    if (floorswitchreleasedsfx.State != SoundState.Playing)
                    {
                        floorswitchreleasedsfx.Play();
                    }
                    lastInteractableObject.TileType = SpriteSheetRectName.Button_E;
                    lastInteractableObject.Deactivate(Object, lastInteractableObject);
                    lastInteractableObject = null;
                }
            }

            var endpoint = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.EndPoint));

            if (endpoint != null)
            {
                Object obj = (Object)endpoint.Box.Data;
                if (lastInteractableObject != obj)
                {
                    obj.Activate(Object, obj);
                    lastInteractableObject = obj;
                }
            }
            else
            {
                if (lastInteractableObject != null && lastInteractableObject.TileType.GetCollisionTag() == CollisionTag.EndPoint)
                {
                    lastInteractableObject = null;
                }
            }

            var lever = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.Lever));

            if (lever != null)
            {
                Object obj = (Object)lever.Box.Data;
                lastInteractableObject = obj;
            }
            else
            {
                if (lastInteractableObject != null && lastInteractableObject.TileType.IsLever())
                {
                    lastInteractableObject = null;
                }
            }

            var block = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.PushableBlock));

            if (block != null)
            {
                Block obj = (Block)block.Box.Data;
                var   n   = block.Normal;
                obj.Velocity = new VT2(n.X * n.X, n.Y * n.Y) * Velocity;
            }

            var portal = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.Portal));

            if (portal != null)
            {
                Object obj = (Object)portal.Box.Data;
                if (obj.TileType.IsOn())
                {
                    obj.Activate(Object, obj);
                }
            }

            animatedEntity.Position = IsoPos;
            Velocity = VT2.Zero;
        }
        public void Update(GameTime gameTime, KeyboardState currentKeyboardState, KeyboardState lastKeyboardState)
        {
            var delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            Velocity *= 0.1f;
            var move = CollisionBox.Move(CollisionBox.X + delta * Velocity.X, CollisionBox.Y + delta * Velocity.Y, x => {
                if (x.Other.HasTag(CollisionTag.FloorSwitch))
                {
                    return(CollisionResponses.Cross);
                }

                if (x.Other.HasTag(CollisionTag.Lever))
                {
                    return(CollisionResponses.Cross);
                }

                if (x.Other.HasTag(CollisionTag.DoorOpened))
                {
                    return(CollisionResponses.Cross);
                }

                if (x.Other.HasTag(CollisionTag.Portal))
                {
                    return(CollisionResponses.Cross);
                }

                return(CollisionResponses.Slide);
            });

            var floorswitch = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.FloorSwitch));

            if (floorswitch != null)
            {
                Object obj = (Object)floorswitch.Box.Data;
                obj.TileType = SpriteSheetRectName.ButtonPressed_E;
                obj.Activate(Object, obj);
                lastSteppedFloorSwitch = obj;
            }
            else
            {
                if (lastSteppedFloorSwitch != null)
                {
                    lastSteppedFloorSwitch.TileType = SpriteSheetRectName.Button_E;
                    lastSteppedFloorSwitch.Deactivate(Object, lastSteppedFloorSwitch);
                    lastSteppedFloorSwitch = null;
                }
            }

            var block = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.PushableBlock));

            if (block != null)
            {
                Block obj = (Block)block.Box.Data;
                var   n   = block.Normal;
                obj.Velocity = new VT2(n.X * n.X, n.Y * n.Y) * Velocity;
            }

            var portal = move.Hits.FirstOrDefault(c => c.Box.HasTag(CollisionTag.Portal));

            if (portal != null)
            {
                Object obj = (Object)portal.Box.Data;
                obj.Activate(Object, obj);
            }

            Object.WorldPos = new Vector3(WorldPos.X, WorldPos.Y, 0);
        }