Example #1
0
        public void Serialize(AnimationSerializeContext context)
        {
            context.bw.Write(delay);
            context.bw.Write(positionDelta);
            context.bw.Write(shadowOffset);

            context.bw.Write(SnapToGround);

            // NOTE: This walks the layer linked list twice, but is only O(n), so no biggie
            int layerCount = layers.Count;

            context.bw.Write(layerCount);
            foreach (var cel in layers)
            {
                cel.Serialize(context);
            }

            masks.SerializeOrderedDictionary(context, m => m.Serialize(context));

            outgoingAttachments.SerializeOrderedDictionary(context, oa => oa.Serialize(context));
            incomingAttachments.SerializeTagLookup(context, p => context.bw.Write(p));

            if (triggers == null)
            {
                context.bw.Write(0);
            }
            else
            {
                context.bw.Write(triggers.Count);
                for (var i = 0; i < triggers.Count; i++)
                {
                    context.bw.Write(triggers[i]);
                }
            }

            context.bw.Write(attachAtLayer.Clamp(0, layers.Count));
            context.bw.Write(canDrawLayersAboveSortedAttachees);

            context.bw.WriteNullableString(cue);
        }
Example #2
0
        public void Serialize(AnimationSerializeContext context)
        {
            if (Asserts.enabled && !ValidateAlphaMasks())
            {
                throw new InvalidOperationException(
                          "Attempting to save animation set with missing or invalid alpha masks");
            }

            context.bw.WriteNullableString(friendlyName);
            context.bw.Write(importOrigin);
            context.bw.WriteNullableString(behaviour);

            if (context.bw.WriteBoolean(Heightmap != null))
            {
                Heightmap.Serialize(context);
            }


            // If you don't seem to have set any physics bounds, I will just generate them...
            if (physicsStartX == 0 && physicsEndX == 1 && physicsStartZ == 0 &&
                physicsEndZ == 1 && physicsHeight == 0 ||
                physicsEndX - physicsStartX <= 0)
            {
                AutoGeneratePhysicsAndDepthBounds();
            }


            // NOTE: only writing out values that cannot be auto-generated
            if (Heightmap != null)
            {
                Debug.Assert(!Asserts.enabled || Heightmap.IsObjectHeightmap ||
                             physicsHeight == 0);
                context.bw.Write(physicsHeight);
                if (physicsHeight > 0)
                {
                    depthBounds.Serialize(context);
                }
                context.bw.Write(flatDirection); // <- for the sake of editing, keep this value around
            }
            else
            {
                context.bw.Write(physicsStartX);
                context.bw.Write(physicsEndX);
                context.bw.Write(physicsStartZ);
                context.bw.Write(physicsHeight);
                context.bw.Write(flatDirection);

                if (physicsHeight == 0)
                {
                    context.bw.Write(physicsEndZ);
                }
            }

            if (context.Version >= 38)
            {
                context.bw.Write(coplanarPriority);
            }

            if (context.Version >= 36)
            {
                context.bw.Write(doAboveCheck);
            }

            if (context.bw.WriteBoolean(Ceiling != null))
            {
                Ceiling.Serialize(context);
            }

            animations.SerializeTagLookup(context, a => a.Serialize(context));

            // Unused Animations
            {
                if (unusedAnimations == null)
                {
                    context.bw.Write(0); // unused animations is lazy-initialized
                }
                else
                {
                    context.bw.Write(unusedAnimations.Count);
                    foreach (var animation in unusedAnimations)
                    {
                        animation.Serialize(context);
                    }
                }
            }

            context.bw.WriteNullableString(cue);

            // Shadow layers:
            {
                if (shadowLayers == null)
                {
                    context.bw.Write(0);
                }
                else
                {
                    context.bw.Write(shadowLayers.Count);
                    foreach (var sl in shadowLayers)
                    {
                        context.bw.Write(sl.startHeight);
                        sl.shadowSpriteRef.Serialize(context);
                    }

                    RecalculateCachedShadowBounds();
                    context.bw.Write(cachedShadowBounds);
                }
            }

            // Properties:
            if (context.Version >= 40)
            {
                context.bw.Write(properties.Count);
                foreach (var kvp in properties)
                {
                    context.bw.Write(kvp.Key);
                    context.bw.Write(kvp.Value ?? string.Empty); // (null value should probably be blocked by editor, but being safe...)
                }
            }
        }