コード例 #1
0
ファイル: Tiles.cs プロジェクト: ongamex/winter_game_csharp
        public void Update(GameUpdateSets u)
        {
            float animLength = u.game.timeswitchBlueAnim._totalAnimationTime;

            if (timeInCurrentState >= 2f)
            {
                _isSolid           = !_isSolid;
                timeInCurrentState = 0f;
            }
            timeInCurrentState += u.dt;


            if (timeInCurrentState > 1.5f)
            {
                int k = (int)((timeInCurrentState - 2.5f) / 0.1f);
                if (k % 2 == 0)
                {
                    tint = new Microsoft.Xna.Framework.Color(0.5f, 0.5f, 0.5f);
                }
                else
                {
                    tint = new Microsoft.Xna.Framework.Color(1f, 1f, 1f);
                }
            }
            else
            {
                tint = new Microsoft.Xna.Framework.Color(1f, 1f, 1f);
            }

            animEvalTime += _isSolid ? -u.dt : u.dt;
            animEvalTime  = Math.Clamp(animEvalTime, 0f, animLength);
        }
コード例 #2
0
        public void Update(Game1 game, float dt)
        {
            GameUpdateSets u = new GameUpdateSets();

            u.dt    = dt;
            u.level = this;
            u.game  = game;

            timeSpentPlaying += dt;

            if (isComplete)
            {
                timeSpentComplete += dt;
            }

            if (snowman != null)
            {
                snowman.Update(u);
            }

            foreach (var a in walkers)
            {
                a.Update(u);
            }

            foreach (TimeSwitch tile in timeSwitches)
            {
                tile.Update(u);
            }

            foreach (Fire f in fires)
            {
                f.Update(u);
            }

            foreach (Ghosty f in ghosties)
            {
                f.Update(u);
            }

            foreach (IceSpike f in iceSpikes)
            {
                f.Update(u);
            }

            foreach (Letter f in letters)
            {
                f.Update(u);
            }

            for (int t = 0; t < fireProjectiles.Count; ++t)
            {
                if (fireProjectiles[t].Update(u))
                {
                    fireProjectiles.RemoveAt(t);
                    t--;
                }
            }
        }
コード例 #3
0
        public bool Update(GameUpdateSets u)
        {
            age += u.dt;
            if (u.level.snowman.isDead)
            {
                return(false);
            }

            pos += u.dt * vel;

            return(age > 5f);
        }
コード例 #4
0
 public void Update(GameUpdateSets u)
 {
     if (isCollected)
     {
         timeSpentCollected += u.dt;
         Vector2 diff = (u.level.snowman.pos - pos);
         if (diff.Length() > 1e-6f)
         {
             diff.Normalize();
         }
         float t = MathF.Pow(timeSpentCollected * 2f, 2f) * u.dt * 64f;
         pos.X = MyMath.lerpClamp(pos.X, u.level.snowman.pos.X, t * MathF.Abs(diff.X));
         pos.Y = MyMath.lerpClamp(pos.Y, u.level.snowman.pos.Y, t * MathF.Abs(diff.Y));
     }
 }
コード例 #5
0
        public void Update(GameUpdateSets u)
        {
            if (u.level.snowman.isDead)
            {
                return;
            }

            if (MathF.Abs(u.level.snowman.pos.X - pos.X) <= 32f)
            {
                isFalling = true;
            }

            if (isFalling && pos.Y < 1000f)
            {
                vel.Y += 800f * u.dt;
                pos   += vel * u.dt;
            }
        }
コード例 #6
0
        public void Update(GameUpdateSets u)
        {
            if (u.level.snowman.isDead)
            {
                return;
            }

            Vector2 targetPos = new Vector2(0f, 0f);
            Vector2 toSnowman = u.level.snowman.pos - pos;

            if (state == State.WaitingForPlayer)
            {
                targetPos = waitingPos;
                if ((pos - waitingPos).Length() < 8f && toSnowman.Length() < 160f)
                {
                    state = State.ChasingPlayer;
                }
            }
            else if (state == State.ChasingPlayer)
            {
                if ((pos - waitingPos).Length() > 160f)
                {
                    state = State.WaitingForPlayer;
                }
                targetPos = u.level.snowman.pos + new Vector2(0f, 12f);
            }


            Vector2 diff  = targetPos - pos;
            float   diffL = diff.Length();
            float   speed = MathF.Max(0.75f * diffL, 48f) * u.dt;

            if (diffL < speed)
            {
                pos = targetPos;
            }
            else if (diffL > 1e-6f)
            {
                pos += diff * (speed / diffL);
            }


            isLookingRight = diff.X > 0f;
        }
コード例 #7
0
        public void Update(GameUpdateSets u)
        {
            animTime += u.dt;
            timeSinceLastProjectalieSpawn += u.dt;

            if (timeSinceLastProjectalieSpawn >= 2.5f && (pos - u.level.snowman.pos).Length() < 320f)
            {
                timeSinceLastProjectalieSpawn = 0;
                FireProjectile p   = new FireProjectile();
                Vector2        dir = u.level.snowman.pos - pos;
                if (dir.Length() > 1e-3f)
                {
                    dir.Normalize();
                }
                p.pos = pos + new Vector2(8f, 8f);
                p.vel = dir * 64f;

                u.level.fireProjectiles.Add(p);
            }
        }
コード例 #8
0
        public void Update(GameUpdateSets u)
        {
            animEvalTime += u.dt;

            if (u.level.snowman.isDead)
            {
                return;
            }

            Rectf groundSearchRect = new Rectf();

            groundSearchRect.X      = MathF.Floor(pos.X / 32f) * 32f + (isWalkingRight ? 32f : 0f);
            groundSearchRect.Y      = (float)(1 + (int)(pos.Y / 32f)) * 32f;
            groundSearchRect.Width  = 32f;
            groundSearchRect.Height = 32f;

            bool hasGroundTowardsNextPos = false;

            foreach (Tile tile in u.level.tiles)
            {
                Rectf   snowmanRect = GetRectWs();
                Vector2 depth       = snowmanRect.GetIntersectionDepth(tile.GetRectWs());
                hasGroundTowardsNextPos |= groundSearchRect.GetIntersectionDepth(tile.GetRectWs()) != Vector2.Zero;
                if (depth.X != 0f && MathF.Abs(depth.X) < MathF.Abs(depth.Y))
                {
                    pos.X         += depth.X;
                    isWalkingRight = !isWalkingRight;
                }
            }


            pos.X += (isWalkingRight ? 1f : -1f) * 32f * u.dt;

            if (hasGroundTowardsNextPos == false)
            {
                isWalkingRight = !isWalkingRight;
            }
        }
コード例 #9
0
        public void Update(GameUpdateSets u)
        {
            if (u.level.timeSpentPlaying <= 0.75f)
            {
                u.level.camera.viewHeigthWs = MyMath.lerp(u.level.timeSpentPlaying / 0.75f, 160f, 240f);
            }
            else
            {
                u.level.camera.viewHeigthWs = 240;
            }

            float jumpHeight               = 36f;
            float jumpTimeApex             = 0.3f;
            float minJumpHeight            = 8f;
            float fallingGravityMultiplier = 1f;
            float gravity         = 2f * jumpHeight / (jumpTimeApex * jumpTimeApex);
            float fallingGravity  = gravity * fallingGravityMultiplier;
            float maxJumpVelocity = gravity * jumpTimeApex;
            float minJumpVelocity = System.MathF.Sqrt(2f * gravity * minJumpHeight);

            // Update
            if (isDead)
            {
                vel            = Vector2.Zero;
                timeSpentDead += u.dt;

                if (timeSpentDead > 1f)
                {
                    u.level.shouldRestart = true;
                }
                return;
            }

            // Input, don't take it if the level is complete.

            if ((Keyboard.GetState().IsKeyDown(Keys.F5) && !u.game.oldks.IsKeyDown(Keys.F5)))
            {
                u.level.isComplete        = true;
                u.level.timeSpentComplete = 100f;
            }

            if (u.level.isComplete == false)
            {
                isCrouched = Keyboard.GetState().IsKeyDown(Keys.Down) || GamePad.GetState(0).IsButtonDown(Buttons.DPadDown) || (GamePad.GetState(0).ThumbSticks.Left.Y < -0.1f);

                if (!isCrouched && Keyboard.GetState().IsKeyDown(Keys.Left) || GamePad.GetState(0).IsButtonDown(Buttons.DPadLeft))
                {
                    vel.X             = MyMath.lerpClamp(vel.X, -140f, 350 * u.dt);
                    walkAnimEvalTime += (float)(u.dt);
                    _isFacingRight    = false;
                }
                if (!isCrouched && Keyboard.GetState().IsKeyDown(Keys.Right) || GamePad.GetState(0).IsButtonDown(Buttons.DPadRight))
                {
                    vel.X             = MyMath.lerpClamp(vel.X, 140f, 350 * u.dt);
                    walkAnimEvalTime += (float)(u.dt);
                    _isFacingRight    = true;
                }

                if (!isCrouched && MathF.Abs(GamePad.GetState(0).ThumbSticks.Left.X) > 0.01f)
                {
                    float k = GamePad.GetState(0).ThumbSticks.Left.X;
                    vel.X             = MyMath.lerpClamp(vel.X, 140f * k, 350 * u.dt);
                    walkAnimEvalTime += (float)(u.dt);
                    _isFacingRight    = k > 0f;
                }

                bool isJumpBtnPressed  = (Keyboard.GetState().IsKeyDown(Keys.Space) && !u.game.oldks.IsKeyDown(Keys.Space)) || (u.game.oldgs.IsButtonUp(Buttons.A) && GamePad.GetState(0).IsButtonDown(Buttons.A));
                bool isJumpBtnReleased = !Keyboard.GetState().IsKeyDown(Keys.Space) && u.game.oldks.IsKeyDown(Keys.Space) || (u.game.oldgs.IsButtonDown(Buttons.A) && GamePad.GetState(0).IsButtonUp(Buttons.A));

                if ((timeSpentInAir < 0.15f || jumpCounter == 1) && isJumpBtnPressed)
                {
                    // pressed
                    vel.Y = -maxJumpVelocity;
                    jumpCounter++;
                    foreach (JumpSwitch tile in u.level.jumpSwitches)
                    {
                        tile._isSolid = !tile._isSolid;
                    }
                    u.game.jumpSfx.Play();
                }

                if (isJumpBtnReleased)
                {
                    // released
                    if (vel.Y < -minJumpVelocity)
                    {
                        vel.Y = -minJumpVelocity;
                    }
                }
            }

            // Physics movement with no collisions (they are handled below).
            pos += vel * u.dt;

            if (vel.Y < 0)
            {
                vel.Y += gravity * u.dt;
            }
            else
            {
                vel.Y += fallingGravity * u.dt;
            }

            vel.X -= vel.X * (isCrouched ? 0.1f : 0.05f);

            // Collision check and response.
            bool isGrounded = false;

            // Non-harmful walkable tiles.
            CollisionTiles(u.level.tiles, ref isGrounded);
            CollisionTiles(u.level.oneWayTiles, ref isGrounded);
            CollisionTiles(u.level.jumpSwitches, ref isGrounded);
            CollisionTiles(u.level.timeSwitches, ref isGrounded);

            if (isGrounded)
            {
                jumpCounter    = 0;
                timeSpentInAir = 0f;
            }
            else
            {
                timeSpentInAir += u.dt;
            }

            // Enemies collision and death.
            CollisionTestEnemies(u.level.walkers, u.level);
            CollisionTestEnemies(u.level.fires, u.level);
            CollisionTestEnemies(u.level.fireProjectiles, u.level);
            CollisionTestEnemies(u.level.ghosties, u.level);
            CollisionTestEnemies(u.level.iceSpikes, u.level);

            // Letters collecting
            foreach (Letter letter in u.level.letters)
            {
                if (letter.isCollected == false)
                {
                    Rectf   snowmanRect = GetRectWs();
                    Vector2 depth       = snowmanRect.GetIntersectionDepth(letter.GetRectWs());
                    if (depth != Vector2.Zero)
                    {
                        letter.isCollected = true;
                        u.game.pickupSfx.Play();
                    }
                }
            }

            // Mailbox level ending.
            if (u.level.letterBox != null)
            {
                Rectf   snowmanRect = GetRectWs();
                Vector2 depth       = snowmanRect.GetIntersectionDepth(u.level.letterBox.GetRectWs());
                if (depth != Vector2.Zero)
                {
                    bool areAllLeteterCollected = true;
                    foreach (Letter l in u.level.letters)
                    {
                        areAllLeteterCollected &= l.isCollected;
                    }
                    if (areAllLeteterCollected && u.level.isComplete == false)
                    {
                        u.level.isComplete = true;
                        u.game.levelWinSfx.Play();
                    }
                }
            }

            // Kill the player if he falls. Don't kill him if the level is completed.
            if (pos.Y >= u.level.deathYCoord && !u.level.isComplete)
            {
                isDead = true;
            }

            // Acumulated values to check for events.
            if (isDead && !wasDead)
            {
                u.game.hitSfx.Play();
            }

            wasDead     = isDead;
            wasGrounded = isGrounded;

            // Move the camera.
            Camera cam = u.level.camera;

            if (cam.pos.X - pos.X < -16f)
            {
                cam.pos.X = pos.X - 16f;
            }

            if (cam.pos.X - pos.X > 100f)
            {
                cam.pos.X = pos.X + 100f;
            }

            if (cam.pos.Y - (pos.Y + 50f) < -50f)
            {
                cam.pos.Y = (pos.Y + 50f) - 50f;
            }

            if (cam.pos.Y - pos.Y > 100f)
            {
                cam.pos.Y = pos.Y + 100f;
            }
        }