public static void Explode(double delaySeconds, Vector2D position)
        {
            var tilePosition = position.ToVector2Ushort();

            CurrentExplosions.Add(tilePosition);

            ClientComponentTimersManager.AddAction(
                delaySeconds,
                () =>
            {
                var groundSceneObject = Api.Client.Scene.CreateSceneObject("Temp explosion ground",
                                                                           position);

                var groundSpriteRenderer = Api.Client.Rendering.CreateSpriteRenderer(
                    groundSceneObject,
                    TextureResource.NoTexture,
                    drawOrder: DrawOrder.FloorCharredGround,
                    spritePivotPoint: (0.5, 0.5),
                    scale: ObjectCharredGround.Scale);

                if (IsGroundSpriteFlipped(tilePosition))
                {
                    groundSpriteRenderer.DrawMode = DrawMode.FlipHorizontally;
                }

                var duration = ExplosionGroundDuration;
                ClientComponentOneShotSpriteSheetAnimationHelper.Setup(
                    groundSpriteRenderer,
                    ExplosionGroundTextureAtlas,
                    duration);

                groundSceneObject.Destroy(duration);

                ClientComponentTimersManager.AddAction(
                    duration,
                    () => CurrentExplosions.Remove(tilePosition));
            });
예제 #2
0
        public static void ClientExplode(
            Vector2D position,
            ExplosionPreset explosionPreset,
            float volume = 1.0f)
        {
            // add screen shakes
            ClientComponentCameraScreenShakes.AddRandomShakes(
                duration: explosionPreset.ScreenShakesDuration,
                worldDistanceMin: explosionPreset.ScreenShakesWorldDistanceMin,
                worldDistanceMax: explosionPreset.ScreenShakesWorldDistanceMax);

            // play sound
            var explosionSoundEmitter = Client.Audio.PlayOneShot(explosionPreset.SoundSet.GetSound(),
                                                                 worldPosition: position,
                                                                 volume: volume,
                                                                 pitch: RandomHelper.Range(0.95f, 1.05f));

            // extend explosion sound distance
            explosionSoundEmitter.CustomMinDistance = (float)explosionPreset.LightWorldSize / 3;
            explosionSoundEmitter.CustomMaxDistance = (float)explosionPreset.LightWorldSize;

            // create explosion renderer
            var explosionSpriteAnimationDuration = explosionPreset.SpriteAnimationDuration;
            var explosionSceneObject             = Client.Scene.CreateSceneObject("Temp explosion",
                                                                                  position);

            explosionSceneObject.Destroy(delay: explosionSpriteAnimationDuration);

            var explosionSpriteRenderer = Client.Rendering.CreateSpriteRenderer(
                explosionSceneObject,
                TextureResource.NoTexture,
                drawOrder: DrawOrder.Explosion,
                spritePivotPoint: (0.5, 0.5));

            explosionSpriteRenderer.Color = explosionPreset.SpriteColorMultiplicative;
            explosionSpriteRenderer.Size  = explosionPreset.SpriteSize;
            if (explosionPreset.SpriteColorAdditive.HasValue
                // ReSharper disable once CompareOfFloatsByEqualityOperator
                || explosionPreset.SpriteBrightness != 1)
            {
                var renderingMaterial = RenderingMaterial.Create(EffectResourceAdditiveColorEffect);
                renderingMaterial.EffectParameters
                .Set("ColorAdditive", explosionPreset.SpriteColorAdditive ?? Colors.Black)
                .Set("Brightness", explosionPreset.SpriteBrightness);
                explosionSpriteRenderer.RenderingMaterial = renderingMaterial;
            }

            var isFlipped = 0
                            == PositionalRandom.Get(position.ToVector2Ushort(),
                                                    minInclusive: 0,
                                                    maxExclusive: 2,
                                                    seed: 893243289);

            if (isFlipped)
            {
                explosionSpriteRenderer.DrawMode = DrawMode.FlipHorizontally;
            }

            ClientComponentOneShotSpriteSheetAnimationHelper.Setup(
                explosionSpriteRenderer,
                explosionPreset.SpriteAtlasResources.TakeByRandom(),
                explosionSpriteAnimationDuration);

            // add light source for the explosion
            var explosionLight = ClientLighting.CreateLightSourceSpot(
                explosionSceneObject,
                explosionPreset.LightColor,
                size: explosionPreset.LightWorldSize,
                positionOffset: (0, 0),
                spritePivotPoint: (0.5, 0.5));

            ClientComponentOneShotLightAnimation.Setup(
                explosionLight,
                explosionPreset.LightDuration);

            // add blast wave
            var blastAnimationDuration = explosionPreset.BlastwaveAnimationDuration;

            if (blastAnimationDuration > 0)
            {
                ClientTimersSystem.AddAction(
                    explosionPreset.BlastwaveDelay,
                    () =>
                {
                    var blastSceneObject = Client.Scene.CreateSceneObject("Temp explosion",
                                                                          position);

                    blastSceneObject.Destroy(delay: blastAnimationDuration);

                    var blastSpriteRenderer = Client.Rendering.CreateSpriteRenderer(
                        blastSceneObject,
                        new TextureResource("FX/ExplosionBlast"),
                        drawOrder: DrawOrder.Explosion - 1,
                        spritePivotPoint: (0.5, 0.5));

                    // animate blast wave
                    ClientComponentGenericAnimationHelper.Setup(
                        blastSceneObject,
                        blastAnimationDuration,
                        updateCallback: alpha =>
                    {
                        var blastwaveAlpha        = (byte)(byte.MaxValue * (1 - alpha));
                        blastSpriteRenderer.Color = explosionPreset.BlastWaveColor
                                                    .WithAlpha(blastwaveAlpha);

                        var sizeX = MathHelper.Lerp(explosionPreset.BlastwaveSizeFrom.X,
                                                    explosionPreset.BlastwaveSizeTo.X,
                                                    alpha);
                        var sizeY = MathHelper.Lerp(explosionPreset.BlastwaveSizeFrom.Y,
                                                    explosionPreset.BlastwaveSizeTo.Y,
                                                    alpha);
                        blastSpriteRenderer.Size = new Size2F(sizeX, sizeY);
                    });
                });
            }

            ClientGroundExplosionAnimationHelper.OnExplode(
                delaySeconds: explosionSpriteAnimationDuration / 2,
                position: position);
        }