public void Draw(Level level, SpriteBatch spriteBatch) { if (!level.EntityWorld.TryGetEntity(_entityID, out Entity? entity)) { return; } if (!level.PhysicsWorld.TryGetBody(entity.BodyID, out Body? body)) { return; } _sprite.Rotation = entity.Rotation; _animation?.Apply(_sprite, _animationTimer); _sprite.Draw(spriteBatch, Vector2.Round(GraphicsConstants.PhysicsToView(body.Position + body.Bounds.Center))); }
public void Update(Level level, float deltaTime) { _shaderTimer += deltaTime; if (_headerTimer < _headerTime) { _headerTimer += deltaTime; } Entities.Update(level, deltaTime); _waterView.Update(deltaTime); Particles.Update(deltaTime); if (_showLoseScreen) { _loseScreenOpacity += 0.3f * deltaTime; _loseScreenOpacity = MathHelper.Min(_loseScreenOpacity, 1f); } if (_fadeOut) { _fadeOutOpacity += 1f * deltaTime; _fadeOutOpacity = MathHelper.Min(_fadeOutOpacity, 1f); } _flickerTimer += deltaTime; if (_flickerTimer >= _flickerTime) { _flickerTimer -= _flickerTime; _lightRadius = _radius + (float)_random.NextDouble() * 20f; } for (int i = 0; i < level.WindChannels.Count; i++) { float channel = level.WindChannels[i]; Particles.Add(_particleFactory.CreateWindParticle( new Vector2((i % 2 == 0 ? -500f : 500f) + level.TileMap.Width * GraphicsConstants.TileSize * (float)_random.NextDouble(), GraphicsConstants.PhysicsToView(channel) - GraphicsConstants.TileSize * 4f + (float)_random.NextDouble() * 8f * GraphicsConstants.TileSize), new Vector2(i % 2 == 0 ? 1f : -1f, 0f))); } }
public void Draw(Level level) { if (RenderTargetsAreOutdated()) { UpdateRenderTargets(); } _camera.Position = Vector2.Round(GraphicsConstants.PhysicsToView(level.CameraCenter) - _graphicsDevice.Viewport.Bounds.Size.ToVector2() / 2f); _renderTargetStack.Push(_worldTarget); { _graphicsDevice.Clear(Color.Transparent); Background.Draw(_camera); TileMap.Draw(level, _camera); if (HideProgress) { _spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: _camera.GetTransformMatrix()); _spriteBatch.DrawString(_regularFont, "Press SPACE BAR to jump/kick.", new Vector2(256f, 2048f), Color.White); _spriteBatch.DrawString(_regularFont, "Use the ARROW KEYS to move.", new Vector2(60f, 1700f), Color.White); _spriteBatch.DrawString(_regularFont, "Tap DOWN to fall through platforms.", new Vector2(140f, 1520f), Color.White); _spriteBatch.DrawString(_regularFont, "Strike R to restart.", new Vector2(670f, 1800f), Color.White); _spriteBatch.DrawString(_regularFont, "Slap SHIFT while running to dash.", new Vector2(200f, 1190f), Color.White); _spriteBatch.DrawString(_regularFont, "You can chain kicks and dashes...", new Vector2(80f, 900f), Color.White); _spriteBatch.DrawString(_regularFont, "... if you hit your torch.", new Vector2(300f, 800f), Color.White); _spriteBatch.DrawString(_regularFont, "Psst... you can press P\nto skip this tutorial.", new Vector2(-350f, 2170f), Color.White); _spriteBatch.End(); } if (TheWinnerIsYou) { _spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: _camera.GetTransformMatrix()); _spriteBatch.DrawString(_regularFont, "You win! Sleep tight.", new Vector2(80f, 270f), Color.White); _spriteBatch.End(); } _spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: _camera.GetTransformMatrix()); int startX = (int)Math.Floor(_camera.Position.X / _finishTexture.Width); int endX = (int)Math.Floor((_camera.Position.X + _spriteBatch.GraphicsDevice.Viewport.Width) / _finishTexture.Width); float y = GraphicsConstants.PhysicsToView(level.FinishHeight); for (int x = startX; x <= endX; x++) { _spriteBatch.Draw(_finishTexture, new Vector2(x * _finishTexture.Width, y - _finishTexture.Height / 2f), Color.White); } _spriteBatch.End(); Particles.Draw(_camera); Entities.Draw(level, _camera); TileMap.DrawGrates(level, _camera); } _renderTargetStack.Pop(); _renderTargetStack.Push(_waterTarget); { _graphicsDevice.Clear(Color.Transparent); _waterView.DrawMask(level, _camera); } _renderTargetStack.Pop(); _waterEffect.Parameters["Time"].SetValue(_shaderTimer); Vector2 camera = _camera.Position / _graphicsDevice.Viewport.Bounds.Size.ToVector2(); _waterEffect.Parameters["Camera"].SetValue(camera); _waterEffect.Parameters["Position"].SetValue(_camera.Position); _waterEffect.Parameters["Dimensions"].SetValue(_graphicsDevice.Viewport.Bounds.Size.ToVector2() + new Vector2(128f)); if (level.EntityWorld.TryGetEntity(TorchEntityID, out Entity? te)) { if (!te.IsPutOut && level.PhysicsWorld.TryGetBody(te.BodyID, out Body? torchBody)) { _waterEffect.Parameters["Light1"].SetValue(GraphicsConstants.PhysicsToView(torchBody.Position + torchBody.Bounds.Center)); } else { _waterEffect.Parameters["Light1"].SetValue(new Vector2(-1000f)); } } _waterEffect.Parameters["Radius"].SetValue(_lightRadius); _waterEffect.Parameters["Ambience"].SetValue(_ambience); _spriteBatch.Begin(effect: _waterEffect); _spriteBatch.Draw(_worldTarget, Vector2.Zero, Color.White); _spriteBatch.End(); _waterView.Draw(level, _camera); if (HideProgress) { _spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: _camera.GetTransformMatrix()); //_spriteBatch.DrawString(_regularFont, "Press SPACE BAR to jump/kick.", new Vector2(256f, 2048f), Color.White); //_spriteBatch.DrawString(_regularFont, "Use the ARROW KEYS to move.", new Vector2(60f, 1700f), Color.White); _spriteBatch.End(); } _spriteBatch.Begin(samplerState: SamplerState.PointClamp); Vector2 progPos = new Vector2(32f, _graphicsDevice.Viewport.Height / 2f); if (!HideProgress) { _spriteBatch.Draw(_progressTexture, progPos + new Vector2(-_progressTexture.Width / 2f, -_progressTexture.Height / 2f), Color.White); } Vector2 startView = GraphicsConstants.PhysicsToView(Start); float finishView = GraphicsConstants.PhysicsToView(level.FinishHeight); if (level.EntityWorld.TryGetEntity(TorchEntityID, out Entity? torchEntity)) { if (level.PhysicsWorld.TryGetBody(torchEntity.BodyID, out Body? torchBody)) { Vector2 torchPosition = GraphicsConstants.PhysicsToView(torchBody.Position + torchBody.Bounds.Center); if (!torchEntity.IsPutOut) { Vector2 arrowPosition = torchPosition; arrowPosition.X = MathHelper.Clamp(arrowPosition.X, _camera.Position.X + 32f, _camera.Position.X + _graphicsDevice.Viewport.Width - 32f); arrowPosition.Y = MathHelper.Clamp(arrowPosition.Y, _camera.Position.Y + 32f, _camera.Position.Y + _graphicsDevice.Viewport.Height - 32f); _arrowSprite.Rotation = (torchPosition - arrowPosition).GetAngle(); _arrowSprite.Draw(_spriteBatch, arrowPosition - _camera.Position); } float tiy = Math.Clamp((torchPosition.Y - finishView) / (startView.Y - finishView), 0f, 1f); if (!HideProgress) { _spriteBatch.Draw(_torchIconTexture, progPos + new Vector2(-_torchIconTexture.Width / 2f, (tiy - 0.5f) * (_progressTexture.Height - 32f) - _torchIconTexture.Height / 2f), Color.White); } } } if (level.EntityWorld.TryGetEntity(PlayerEntityID, out Entity? playerEntity)) { if (level.PhysicsWorld.TryGetBody(playerEntity.BodyID, out Body? playerBody)) { Vector2 playerPosition = GraphicsConstants.PhysicsToView(playerBody.Position + playerBody.Bounds.Center); float piy = Math.Clamp((playerPosition.Y - finishView) / (startView.Y - finishView), 0f, 1f); if (!HideProgress) { _spriteBatch.Draw(_playerIconTexture, progPos + new Vector2(-_playerIconTexture.Width / 2f, (piy - 0.5f) * (_progressTexture.Height - 32f) - _playerIconTexture.Height / 2f), Color.White); } } } _spriteBatch.Draw(_pixelTexture, _graphicsDevice.Viewport.Bounds, Color.Black * 0.5f * _loseScreenOpacity); _spriteBatch.DrawString(_regularFont, "Press R to restart.", _graphicsDevice.Viewport.Bounds.Center.ToVector2() - _regularFont.MeasureString("Press R to restart.") / 2f, Color.White * _loseScreenOpacity); _spriteBatch.Draw(_pixelTexture, _graphicsDevice.Viewport.Bounds, Color.Black * _fadeOutOpacity); if (Header != null) { float headerOpacity = MathHelper.Clamp(1f - _headerTimer / _headerTime, 0f, 1f); _spriteBatch.Draw(_gradientTexture, Vector2.Zero, Color.White * headerOpacity); _spriteBatch.DrawString(_regularFont, Header, new Vector2(_graphicsDevice.Viewport.Bounds.Center.X - _regularFont.MeasureString(Header).X / 2f, 24f), Color.White * headerOpacity); } _spriteBatch.End(); }
public void DrawMask(Level level, Camera2D camera) { _waterMaskAnimation.Apply(_waterMaskSprite, _animationTimer); _spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: camera.GetTransformMatrix()); int startX = (int)Math.Floor(camera.Position.X / _waterSize); int endX = (int)Math.Floor((camera.Position.X + _spriteBatch.GraphicsDevice.Viewport.Width) / _waterSize); int top = level.TileMap.Height * GraphicsConstants.TileSize - (int)Math.Round(GraphicsConstants.PhysicsToView(level.WaterLevel)); int bottom = level.TileMap.Height * GraphicsConstants.TileSize + 300; for (int x = startX; x <= endX; x++) { _waterMaskSprite.Draw(_spriteBatch, new Vector2(x * _waterSize, top - _waterSize / 2f)); } _spriteBatch.Draw(_pixelTexture, new Rectangle(startX * _waterSize, top + _waterSize / 2, (endX - startX + 1) * _waterSize, bottom - top), Color.White); _spriteBatch.End(); }
public void Draw(Level level, Camera2D camera) { _waterOutlineAnimation.Apply(_waterOutlineSprite, _animationTimer); _spriteBatch.Begin(samplerState: SamplerState.PointClamp, transformMatrix: camera.GetTransformMatrix()); int startX = (int)Math.Floor(camera.Position.X / _waterSize); int endX = (int)Math.Floor((camera.Position.X + _spriteBatch.GraphicsDevice.Viewport.Width) / _waterSize); int top = level.TileMap.Height * GraphicsConstants.TileSize - (int)Math.Round(GraphicsConstants.PhysicsToView(level.WaterLevel)); for (int x = startX; x <= endX; x++) { _waterOutlineSprite.Draw(_spriteBatch, new Vector2(x * _waterSize, top - _waterSize / 2f)); } _spriteBatch.End(); }
public void Update(Level level, float deltaTime) { if (!level.EntityWorld.TryGetEntity(_entityID, out Entity? entity)) { return; } if (!level.PhysicsWorld.TryGetBody(entity.BodyID, out Body? body)) { return; } if (body.Velocity.X > 0f) { _facingRight = true; } else if (body.Velocity.X < 0f) { _facingRight = false; } switch (_profile) { case EntityViewProfile.Player: { if (entity.Sleeping) { PlayAnimation(_animations.PlayerSleeping); } else if (entity.HasLostAllHope) { PlayAnimation(_animations.PlayerDefeatedRight, _animations.PlayerDefeatedLeft); } else if (entity.Kick) { ReplayAnimation(_animations.PlayerKickRight, _animations.PlayerKickLeft); entity.Kick = false; } else if ((_animation != _animations.PlayerKickRight && _animation != _animations.PlayerKickLeft) || _animationTimer >= 0.3f) { if (body.Contact.Y > 0f) { if (body.Velocity.X != 0f) { PlayAnimation(_animations.PlayerRunRight, _animations.PlayerRunLeft); } else { PlayAnimation(_animations.PlayerIdleRight, _animations.PlayerIdleLeft); } } else { PlayAnimation(_animations.PlayerFallRight, _animations.PlayerFallLeft); } } break; } case EntityViewProfile.Torch: { if (body.Contact != Vector2.Zero && body.Velocity.Length() > 5f) { _soundEffects .GetRandom(_soundEffects.Thud1, _soundEffects.Thud2, _soundEffects.Thud3) .Play(); } if (entity.Sleeping) { PlayAnimation(_animations.Fireplace); } else if (entity.IsPutOut) { PlayAnimation(_animations.TorchOut); if (!entity.PlayedHiss) { _soundEffects.Hiss.Play(); entity.PlayedHiss = true; } } else { PlayAnimation(_animations.Torch); _torchParticleTimer += deltaTime; var torchCenter = new Vector2(16f, 16f); var torchTip = new Vector2(14f, 6f); var particlePosition = GraphicsConstants.PhysicsToView(body.Position + body.Bounds.Center) + Rotate(torchTip - torchCenter, entity.Rotation); if (_previousTorchPosition != null) { Particles?.Add(_particleFactory.CreateBlackLineParticle(particlePosition, _previousTorchPosition.Value)); Particles?.Add(_particleFactory.CreateRedLineParticle(particlePosition, _previousTorchPosition.Value)); Particles?.Add(_particleFactory.CreateOrangeLineParticle(particlePosition, _previousTorchPosition.Value)); Particles?.Add(_particleFactory.CreateYellowLineParticle(particlePosition, _previousTorchPosition.Value)); } _previousTorchPosition = particlePosition; } break; } } _animationTimer += deltaTime; }