示例#1
0
        void CheckCollision(Map gameMap)
        {
            //collisionRect.Offset(new Point((int)Speed.X, (int)Speed.Y));

            if (respawning)
            {
                return;
            }

            Rectangle?collRect;

            // Check for ledge grabs
            if ((jumping || falling) && !justUngrabbed)
            {
                if (Speed.X < 0 && gameMap.CheckTileCollision(new Vector2(collisionRect.Left, collisionRect.Top), Layer))
                {
                    if (!gameMap.CheckTileCollision(new Vector2(collisionRect.Left, collisionRect.Top - gameMap.TileHeight), Layer) &&
                        !gameMap.CheckTileCollision(new Vector2(collisionRect.Left + gameMap.TileWidth, collisionRect.Top - gameMap.TileHeight), Layer) &&
                        !gameMap.CheckTileCollision(new Vector2(collisionRect.Left + gameMap.TileWidth, collisionRect.Top), Layer))
                    {
                        grabbed         = true;
                        jumping         = false;
                        falling         = false;
                        crouching       = false;
                        Speed.Y         = 0;
                        Speed.X         = 0;
                        grabbedPosition = new Vector2((int)(collisionRect.Left / gameMap.TileWidth) * gameMap.TileWidth, (int)(collisionRect.Top / gameMap.TileHeight) * gameMap.TileHeight) + new Vector2(gameMap.TileWidth, collisionRect.Height + 30);
                        faceDir         = -1;
                    }
                }

                if (Speed.X > 0 && gameMap.CheckTileCollision(new Vector2(collisionRect.Right, collisionRect.Top), Layer))
                {
                    if (!gameMap.CheckTileCollision(new Vector2(collisionRect.Right, collisionRect.Top - gameMap.TileHeight), Layer) &&
                        !gameMap.CheckTileCollision(new Vector2(collisionRect.Right - gameMap.TileWidth, collisionRect.Top - gameMap.TileHeight), Layer) &&
                        !gameMap.CheckTileCollision(new Vector2(collisionRect.Right - gameMap.TileWidth, collisionRect.Top), Layer))
                    {
                        grabbed         = true;
                        jumping         = false;
                        falling         = false;
                        crouching       = false;
                        Speed.Y         = 0;
                        Speed.X         = 0;
                        grabbedPosition = new Vector2((int)(collisionRect.Right / gameMap.TileWidth) * gameMap.TileWidth, (int)(collisionRect.Top / gameMap.TileHeight) * gameMap.TileHeight) + new Vector2(0, collisionRect.Height + 30);
                        faceDir         = 1;
                    }
                }
            }

            if (grabbed || climbing)
            {
                return;
            }



            collRect = CheckCollisionBottom(gameMap);
            if (collRect.HasValue)
            {
                if (falling)
                {
                    Speed.Y     = 0f;
                    Position.Y -= collRect.Value.Height;
                    collisionRect.Offset(0, -collRect.Value.Height);
                    jumping       = false;
                    falling       = false;
                    justUngrabbed = false;
                    Tile t = gameMap.GetTile(Position + new Vector2(0, 30), Layer);
                    if (t != null)
                    {
                        if (t.Properties.Contains("Wood"))
                        {
                            AudioController.PlaySFX("fstep-wood", -0.2f, 0.2f);
                        }
                        else if (t.Properties.Contains("Metal"))
                        {
                            AudioController.PlaySFX("fstep-metal", -0.2f, 0.2f);
                        }
                        else
                        {
                            AudioController.PlaySFX("fstep-grass", -0.2f, 0.2f);
                        }
                    }
                    else
                    {
                        AudioController.PlaySFX("fstep-grass", -0.2f, 0.2f);
                    }
                }
            }
            else
            {
                falling = true;
            }

            if (Speed.Y < 0f)
            {
                collRect = CheckCollisionTop(gameMap);
                if (collRect.HasValue)
                {
                    Speed.Y     = 0f;
                    Position.Y += collRect.Value.Height;
                    collisionRect.Offset(justUngrabbed ? (collRect.Value.Width * (-faceDir)) : 0, collRect.Value.Height);
                    falling = true;
                    jumping = false;
                }
            }



            if (Speed.X > 0f)
            {
                collRect = CheckCollisionRight(gameMap);
                if (collRect.HasValue)
                {
                    Speed.X     = 0f;
                    Position.X -= (collRect.Value.Width);
                    collisionRect.Offset(-collRect.Value.Width, 0);
                }
            }
            if (Speed.X < 0f)
            {
                collRect = CheckCollisionLeft(gameMap);
                if (collRect.HasValue)
                {
                    Speed.X     = 0f;
                    Position.X += collRect.Value.Width;
                    collisionRect.Offset(collRect.Value.Width, 0);
                }
            }


            bool collided = false;

            for (int y = -1; y > -15; y--)
            {
                collisionRect.Offset(0, -1);
                collRect = CheckCollisionTop(gameMap);
                if (collRect.HasValue)
                {
                    collided = true;
                }
            }
            if (!collided)
            {
                crouching = false;
            }
        }
示例#2
0
        public void Update(GameTime gameTime, Camera gameCamera, Map gameMap)
        {
            if (!walking && !jumping && !crouching && !grabbed)
            {
                skeleton.SetToBindPose();
            }

            if (walking && !jumping && !grabbed)
            {
                animTime += gameTime.ElapsedGameTime.Milliseconds / 1000f;
                if (!crouching)
                {
                    Animations["walk"].Mix(skeleton, animTime, true, 0.3f);
                }
                else
                {
                    Animations["crawl"].Mix(skeleton, animTime, true, 0.5f);
                }


                fstepTime += gameTime.ElapsedGameTime.TotalMilliseconds;
                if (fstepTime >= 500)
                {
                    fstepTime = 0;
                    if (!crouching && !falling)
                    {
                        Tile t = gameMap.GetTile(Position + new Vector2(0, 30), Layer);
                        if (t != null)
                        {
                            if (t.Properties.Contains("Wood"))
                            {
                                AudioController.PlaySFX("fstep-wood", -0.2f, 0.2f);
                            }
                            else if (t.Properties.Contains("Metal"))
                            {
                                AudioController.PlaySFX("fstep-metal", -0.2f, 0.2f);
                            }
                            else
                            {
                                AudioController.PlaySFX("fstep-grass", -0.2f, 0.2f);
                            }
                        }
                        else
                        {
                            AudioController.PlaySFX("fstep-grass", -0.2f, 0.2f);
                        }
                    }
                }
            }

            if (jumping)
            {
                animTime += gameTime.ElapsedGameTime.Milliseconds / 1000f;
                Animations["jump"].Mix(skeleton, animTime, false, 0.5f);
            }

            if (crouching && !jumping)
            {
                collisionRect.Width  = 100;
                collisionRect.Height = 110;

                if (!walking)
                {
                    animTime = 0;
                    Animations["crawl"].Mix(skeleton, animTime, false, 0.5f);
                }
            }
            else
            {
                collisionRect.Width  = 85;
                collisionRect.Height = 150;
            }

            if (falling)
            {
                Speed += gravity;

                if (Speed.Y > 1)
                {
                    animTime += gameTime.ElapsedGameTime.Milliseconds / 1000f;
                    Animations["fall"].Mix(skeleton, animTime, true, 0.75f);
                }
            }

            if (grabbed)
            {
                Position  = Vector2.Lerp(Position, grabbedPosition, 0.1f);
                animTime += gameTime.ElapsedGameTime.Milliseconds / 1000f;
                Animations["grab"].Mix(skeleton, animTime, true, 0.3f);
            }

            if (climbing)
            {
                //Position = Vector2.Lerp(Position, grabbedPosition, 0.1f);
                animTime += gameTime.ElapsedGameTime.Milliseconds / 500f;
                Animations["climb"].Apply(skeleton, animTime, false);

                Position = Vector2.Lerp(Position, grabbedPosition - new Vector2(20 * (-faceDir), 185), (0.1f / Animations["grab"].Duration) * animTime);

                if ((Position - (grabbedPosition - new Vector2(20 * (-faceDir), 185))).Length() < 5f)
                {
                    climbing = false;
                }
            }

            if (teleporting)
            {
                teleportScale = MathHelper.Lerp(teleportScale, 0f, 0.1f);
                if (teleportScale < 0.02f)
                {
                    Layer            = teleportingDir;
                    teleporting      = false;
                    teleportFinished = false;

                    checkPointLayer    = Layer;
                    checkPointPosition = Position + new Vector2(0, -30f);
                }
            }
            else
            {
                if (teleportFinished)
                {
                    teleportScale = MathHelper.Lerp(teleportScale, 1f, 0.1f);
                }
            }

            if (usingValve)
            {
                animTime += gameTime.ElapsedGameTime.Milliseconds / 1000f;
                Animations["turnvalve"].Apply(skeleton, animTime, true);

                valveUseTime   += gameTime.ElapsedGameTime.TotalMilliseconds;
                valveSoundTime += gameTime.ElapsedGameTime.TotalMilliseconds;

                if (valveUseTime >= 5000)
                {
                    usingValve = false;
                    TriggerController.Instance.DeactivateValve(this);
                }

                if (valveSoundTime >= 750)
                {
                    valveSoundTime = 0;
                    AudioController.PlaySFX("valve", -0.2f, 0.2f);
                }
            }

            if (UnderWater)
            {
                drownTime += gameTime.ElapsedGameTime.TotalMilliseconds;

                if (drownTime >= 2000)
                {
                    respawning = true;

                    if ((Position - checkPointPosition).Length() < 50f || drownTime >= 5000)
                    {
                        // checkpoint is underwater, game over!
                        Dead = true;
                    }
                }
            }
            else
            {
                drownTime = 0;
            }

            if (respawning)
            {
                climbing = false;
                grabbed  = false;

                if ((Position - checkPointPosition).Length() > 1f)
                {
                    teleportScale = 0f;
                    Position      = Vector2.Lerp(Position, checkPointPosition, 0.1f);
                }
                else
                {
                    teleportScale = MathHelper.Lerp(teleportScale, 1f, 0.1f);
                    if (teleportScale > 0.99f)
                    {
                        respawning = false;
                    }
                }
            }

            skeleton.RootBone.ScaleX = Scale * teleportScale;
            skeleton.RootBone.ScaleY = Scale * teleportScale;

            if (!respawning)
            {
                Position += Speed;
                collisionRect.Location = new Point((int)Position.X - (collisionRect.Width / 2), (int)Position.Y - (collisionRect.Height));
                CheckCollision(gameMap);
            }

            Position.X = MathHelper.Clamp(Position.X, 0, gameMap.Width * gameMap.TileWidth);
            Position.Y = MathHelper.Clamp(Position.Y, 0, gameMap.Height * gameMap.TileHeight);


            skeleton.RootBone.X = Position.X;
            skeleton.RootBone.Y = Position.Y;


            if (faceDir == -1)
            {
                skeleton.FlipX = true;
            }
            else
            {
                skeleton.FlipX = false;
            }

            skeleton.UpdateWorldTransform();

            walking           = false;
            oppositeDirPushed = false;
            Speed.X           = 0f;

            UnderWater = false;

            if (Position.Y < waterLevel)
            {
                splashPlayed = false;
            }
            else
            {
                if (falling && !splashPlayed)
                {
                    splashPlayed = true;
                    AudioController.PlaySFX("splash", 0.5f, 0f, 0f);
                }
            }
        }