Example #1
0
        protected virtual void ClientSetupExtractorActiveAnimation(
            IStaticWorldObject worldObject,
            ObjectManufacturerPublicState serverPublicState,
            ITextureAtlasResource textureAtlasResource,
            Vector2D positionOffset,
            double frameDurationSeconds,
            bool autoInverseAnimation      = false,
            bool randomizeInitialFrame     = false,
            bool playAnimationSounds       = true,
            OnRefreshActiveState onRefresh = null)
        {
            var clientState = worldObject.GetClientState <StaticObjectClientState>();
            var sceneObject = worldObject.ClientSceneObject;

            var overlayRenderer = Client.Rendering.CreateSpriteRenderer(
                sceneObject,
                TextureResource.NoTexture,
                DrawOrder.Default,
                positionOffset: positionOffset,
                spritePivotPoint: Vector2D.Zero);

            overlayRenderer.DrawOrderOffsetY = -positionOffset.Y - 0.01;

            var spriteSheetAnimator = sceneObject.AddComponent <ClientComponentSpriteSheetAnimator>();

            spriteSheetAnimator.Setup(
                overlayRenderer,
                ClientComponentSpriteSheetAnimator.CreateAnimationFrames(
                    textureAtlasResource,
                    autoInverse: autoInverseAnimation),
                isLooped: true,
                frameDurationSeconds: frameDurationSeconds,
                randomizeInitialFrame: randomizeInitialFrame);

            // we play Active sound for pumping on both up and down position of the oil pump
            var componentActiveState = sceneObject.AddComponent <ClientComponentOilPumpActiveState>();

            componentActiveState.Setup(overlayRenderer, spriteSheetAnimator, worldObject, playAnimationSounds);

            serverPublicState.ClientSubscribe(
                s => s.IsActive,
                callback: RefreshActiveState,
                subscriptionOwner: clientState);

            spriteSheetAnimator.IsEnabled = overlayRenderer.IsEnabled = false;

            RefreshActiveState(serverPublicState.IsActive);

            void RefreshActiveState(bool isActive)
            {
                // ReSharper disable once PossibleNullReferenceException
                componentActiveState.IsActive = isActive;
                onRefresh?.Invoke(isActive);
            }
        }
        protected void ClientSetupManufacturerActiveAnimation(
            IStaticWorldObject worldObject,
            ObjectManufacturerPublicState serverPublicState,
            ITextureAtlasResource textureAtlasResource,
            Vector2D positionOffset,
            double frameDurationSeconds,
            double drawOrderOffsetY        = 0,
            bool autoInverseAnimation      = false,
            OnRefreshActiveState onRefresh = null)
        {
            var clientState = worldObject.GetClientState <StaticObjectClientState>();

            var sceneObject = Client.Scene.GetSceneObject(worldObject);

            var overlayRenderer = Client.Rendering.CreateSpriteRenderer(
                sceneObject,
                TextureResource.NoTexture,
                DrawOrder.Default,
                positionOffset: positionOffset,
                spritePivotPoint: Vector2D.Zero);

            overlayRenderer.DrawOrderOffsetY = drawOrderOffsetY - positionOffset.Y - 0.01;

            var spriteSheetAnimator = sceneObject.AddComponent <ClientComponentSpriteSheetAnimator>();

            spriteSheetAnimator.Setup(
                overlayRenderer,
                ClientComponentSpriteSheetAnimator.CreateAnimationFrames(
                    textureAtlasResource,
                    autoInverse: autoInverseAnimation),
                frameDurationSeconds: frameDurationSeconds);

            var soundEmitterActiveState = this.ClientCreateActiveStateSoundEmitterComponent(worldObject, sceneObject);

            serverPublicState.ClientSubscribe(
                s => s.IsManufacturingActive,
                callback: RefreshActiveState,
                subscriptionOwner: clientState);

            RefreshActiveState(serverPublicState.IsManufacturingActive);

            void RefreshActiveState(bool isActive)
            {
                overlayRenderer.IsEnabled     = isActive;
                spriteSheetAnimator.IsEnabled = isActive;
                if (soundEmitterActiveState != null)
                {
                    soundEmitterActiveState.IsEnabled = isActive;
                }

                onRefresh?.Invoke(isActive);
            }
        }
Example #3
0
        public static void ClientRefreshRenderer(IStaticWorldObject worldObject)
        {
            var isDestroyedWall = false;

            if (!(worldObject.ProtoGameObject is IProtoObjectWall protoWall))
            {
                if (!(worldObject.ProtoGameObject is ObjectWallDestroyed))
                {
                    return;
                }

                isDestroyedWall = true;
                protoWall       = ObjectWallDestroyed.GetPublicState(worldObject)
                                  .OriginalProtoObjectWall;
            }

            var result = SharedGetAtlasTextureChunkPosition(worldObject.OccupiedTile,
                                                            protoWall,
                                                            isConsiderDestroyed: !isDestroyedWall,
                                                            isConsiderConstructionSites: !isDestroyedWall);

            try
            {
                var textureAtlas           = ClientGetTextureAtlas(worldObject);
                var primaryTextureResource = textureAtlas.Chunk(
                    (byte)result.Primary.AtlasChunkPosition.X,
                    (byte)result.Primary.AtlasChunkPosition.Y);

                var clientState = worldObject.GetClientState <ObjectWallClientState>();
                clientState.Renderer.TextureResource = primaryTextureResource;
                var destroyedWallYOffset = isDestroyedWall
                                               ? result.Primary.DrawOffsetDestroyed
                                               : result.Primary.DrawOffsetNormal;

                clientState.Renderer.DrawOrderOffsetY = isDestroyedWall
                                                            ? destroyedWallYOffset
                                                            : result.Primary.DrawOffsetNormal;

                // destroy previous overlay renderers
                var overlayRenderers = clientState.RenderersObjectOverlay;
                if (overlayRenderers?.Count > 0)
                {
                    foreach (var renderer in overlayRenderers)
                    {
                        renderer.Destroy();
                    }

                    overlayRenderers.Clear();
                }

                var overlayChunkPreset = result.Overlay;
                if (overlayChunkPreset == null ||
                    overlayChunkPreset.Count == 0)
                {
                    // no overlay renderers needed
                    return;
                }

                // add overlay renderers
                foreach (var preset in overlayChunkPreset.AsList())
                {
                    var overlayRenderer = Api.Client.Rendering.CreateSpriteRenderer(
                        worldObject,
                        textureAtlas.Chunk((byte)preset.AtlasChunkPosition.X,
                                           (byte)preset.AtlasChunkPosition.Y));

                    if (overlayRenderers == null)
                    {
                        overlayRenderers = new List <IComponentSpriteRenderer>();
                        clientState.RenderersObjectOverlay = overlayRenderers;
                    }

                    overlayRenderers.Add(overlayRenderer);
                    overlayRenderer.DrawOrderOffsetY = isDestroyedWall
                                                           ? destroyedWallYOffset
                                                           : preset.DrawOffsetNormal;
                }
            }
            finally
            {
                result.Dispose();
            }
        }