public void UseObject(Map gameMap) { if (usingValve) { return; } if (teleporting) { return; } if (Dead) { return; } if (TriggerController.Instance.AtValve) { usingValve = true; valveUseTime = 0; PromptController.Instance.RemovePrompt("valve1"); PromptController.Instance.RemovePrompt("valve2"); return; } MapObjectLayer portalLayer = gameMap.GetLayer("Portals") as MapObjectLayer; foreach (MapObject o in portalLayer.Objects) { if (o.Location.Contains(Helper.VtoP(Position))) { if (Convert.ToInt16(o.Properties["In"]) == Layer) { teleportingDir = Convert.ToInt16(o.Properties["Out"]); teleporting = true; teleportScale = 1f; AudioController.PlaySFX("teleport_in", 0.6f, 0f, 0f); return; } if (Convert.ToInt16(o.Properties["Out"]) == Layer) { teleportingDir = Convert.ToInt16(o.Properties["In"]); teleporting = true; teleportScale = 1f; AudioController.PlaySFX("teleport_in", 0.6f, 0f, 0f); return; } } } }
/// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { if (IsActive) { KeyboardState ks = Keyboard.GetState(); if (ks.IsKeyDown(Keys.Left) || ks.IsKeyDown(Keys.A)) { gameHero.MoveLeftRight(-1f); } else if (ks.IsKeyDown(Keys.Right) || ks.IsKeyDown(Keys.D)) { gameHero.MoveLeftRight(1f); } if (ks.IsKeyDown(Keys.Up) || ks.IsKeyDown(Keys.W)) { gameHero.Jump(); } if (ks.IsKeyDown(Keys.Down) || ks.IsKeyDown(Keys.D)) { gameHero.Crouch(); } if (ks.IsKeyDown(Keys.Space) && !lks.IsKeyDown(Keys.Space)) { if (gameHero.Complete && !resetting && Hud.Instance.ReadyForRestart) { resetting = true; } if (gameHero.Dead && !resetting && Hud.Instance.ReadyForRestart) { resetting = true; } gameHero.UseObject(gameMap); } //if (ks.IsKeyDown(Keys.F10) && !lks.IsKeyDown(Keys.F10)) //{ // graphics.ToggleFullScreen(); // if (graphics.IsFullScreen) // { // graphics.PreferredBackBufferWidth = GraphicsDevice.DisplayMode.Width; // graphics.PreferredBackBufferHeight = GraphicsDevice.DisplayMode.Height; // } // else // { // graphics.PreferredBackBufferWidth = 1280; // graphics.PreferredBackBufferHeight = 720; // } // graphics.ApplyChanges(); //} gameHero.waterLevel = (gameMap.Height * gameMap.TileHeight) - waterLevel; gameHero.Update(gameTime, gameCamera, gameMap); gameCamera.Target = gameHero.Position; gameCamera.Update(GraphicsDevice.Viewport.Bounds); gameTriggerController.Update(gameTime, gameHero); gamePromptController.Update(gameTime); gameHud.Update(gameTime, waterLevel, gameHero, gameMap); // Scale layers according to player's layer float targetScale = 1f; for (int l = gameHero.Layer; l < LayerDepths.Length; l++) { LayerDepths[l] = MathHelper.Lerp(LayerDepths[l], targetScale, 0.1f); if (targetScale > 0f) { LayerColors[l] = Color.Lerp(LayerColors[l], new Color((1f - (targetScale * 0.5f)) * 0.4f, (1f - (targetScale * 0.5f)) * 0.5f, (1f - (targetScale * 0.5f)) * 0.9f), 0.1f); //* (targetScale * 0.5f) targetScale -= 0.333f; } else { LayerColors[l] = Color.Lerp(LayerColors[l], new Color((1f - (targetScale * 0.5f)) * 0.4f, (1f - (targetScale * 0.5f)) * 0.5f, (1f - (targetScale * 0.5f))) * 0f, 0.1f); } } if (gameHero.Layer > 0) { targetScale = 1.5f; for (int l = gameHero.Layer - 1; l >= 0; l--) { LayerDepths[l] = MathHelper.Lerp(LayerDepths[l], targetScale, 0.1f); if (gameHero.Layer - l == 1) { LayerColors[l] = Color.Lerp(LayerColors[l], new Color(targetScale * 0.01f, targetScale * 0.02f, targetScale * 0.1f) * 0.85f, 0.1f); } else if (gameHero.Layer == l) { LayerColors[l] = Color.Lerp(LayerColors[l], Color.White * 1f, 0.1f); } else { LayerColors[l] = Color.Lerp(LayerColors[l], new Color(targetScale * 0.01f, targetScale * 0.02f, targetScale * 0.1f) * 0f, 0.1f); } targetScale += 0.5f; } } if (LayerDepths[gameHero.Layer] > 0.98f && LayerDepths[gameHero.Layer] < 1.02f && !gameHero.teleportFinished) { gameHero.teleportFinished = true; AudioController.PlaySFX("teleport_out", 0.6f, 0f, 0f); } lks = ks; waterRiseTime += gameTime.ElapsedGameTime.TotalMilliseconds; if (waterRiseTime >= (((((gameMap.Height * gameMap.TileHeight) - gameHero.checkPointPosition.Y) - waterLevel) < 600) ? 70 : 30)) { waterRiseTime = 0; if (TriggerController.Instance.WaterTriggered) { waterLevel += 2; if (waterLevel > highestWaterLevel) { highestWaterLevel = waterLevel; if (!gameHero.Complete) { gameHud.SoulsPerished += 100; } } else if (!gameHero.Complete) { gameHud.SoulsPerished += 10; } foreach (Water w in Waters) { w.bounds.Offset(new Point(0, -2)); w.bounds.Height += 2; } } } if ((gameMap.Height * gameMap.TileHeight) - waterLevel < gameHero.Position.Y - 200f) { gameHero.UnderWater = true; } //emptying = true; if (gameHero.usingValve || emptying || gameHero.Complete) { waterLevel -= 4; foreach (Water w in Waters) { w.bounds.Offset(new Point(0, 4)); w.bounds.Height -= 4; } } if (waterLevel < 200) { emptying = false; } float startScale = 1.5f; foreach (Water w in Waters.OrderByDescending(wat => wat.Scale)) { w.Scale = MathHelper.Lerp(w.Scale, startScale + (gameHero.Layer * 0.25f), 0.1f); if (w.Scale > 0.25f) { w.Alpha = MathHelper.Lerp(w.Alpha, w.Scale, 0.1f); } else { w.Alpha = MathHelper.Lerp(w.Alpha, 0f, 0.1f); } if (w.Scale > 0f) { w.Update(gameTime); } startScale -= 0.1f; } startScale = 1.5f; for (int c = 0; c < Clouds.Count; c++) { Vector4 cl = Clouds[c]; cl.Z = MathHelper.Lerp(cl.Z, startScale + (gameHero.Layer * 0.25f), 0.01f); cl.Y = ((((gameCamera.Position.Y) - (GraphicsDevice.Viewport.Height / 2)) - ((((float)GraphicsDevice.Viewport.Height / 2) / (float)(gameMap.Height * gameMap.TileHeight)) * (gameHero.Position.Y * 3))) + 100 * cl.Z); //cl.Y = ((gameCamera.Position.Y) - (GraphicsDevice.Viewport.Height/2)) * -(cl.Z * 1.5f); cl.X -= 0.1f; if (cl.X <= -cloudTexture.Width) { cl.X = 0; } if (cl.Z > 0.5f && cl.Z <= 1f) { cl.W = MathHelper.Lerp(cl.W, 1f, 0.01f); } else if (cl.Z > 0f && cl.Z <= 1f) { cl.W = MathHelper.Lerp(cl.W, cl.Z, 0.01f); } else if (cl.Z > 1f && cl.Z <= 1.25f) { cl.W = MathHelper.Lerp(cl.W, 1f, 0.01f); } else if (cl.Z > 1.25f) { cl.W = MathHelper.Lerp(cl.W, 0f, 0.01f); } else { cl.W = MathHelper.Lerp(cl.W, 0f, 0.01f); } //if (cl.Z < 2f) cl.W = MathHelper.Lerp(cl.W, 1f, 0.03f); Clouds[c] = cl; startScale -= 0.1f; } if (!resetting) { fadeAlpha = MathHelper.Lerp(fadeAlpha, 0f, 0.05f); } else { fadeAlpha = MathHelper.Lerp(fadeAlpha, 1f, 0.05f); if (fadeAlpha > 0.99f) { Reset(); } } if (gameHero.Position.Y > (gameMap.Height * gameMap.TileHeight) / 2) { ambient1.Volume = MathHelper.Lerp(ambient1.Volume, 1f, 0.001f); ambient2.Volume = MathHelper.Lerp(ambient2.Volume, 0f, 0.001f); } else { ambient1.Volume = MathHelper.Lerp(ambient1.Volume, 0f, 0.001f); ambient2.Volume = MathHelper.Lerp(ambient2.Volume, 1f, 0.001f); } float wdistance = ((gameMap.Height * gameMap.TileHeight) - waterLevel) - gameHero.Position.Y; float vol = 0.5f - ((0.5f / 800f) * wdistance); water.Volume = MathHelper.Clamp(vol, 0f, 0.5f); } base.Update(gameTime); }
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; } }
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); } } }