Beispiel #1
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            base.LoadContent();

            Music1 = new SoundAsset("Portal2-23-You_Saved_Science", true);
            Music2 = new SoundAsset("Portal2-14-Spaaaaace", true);

            Playlist = new MusicPlaylist();
            Playlist.SetMusicAsset("music1", Music1);
            Playlist.SetMusicAsset("music2", Music2);

            Sounds = new SoundEnvironment();

            Assets = AssetCollection.FromFile("assets", true);
            Particles = AssetCollection.FromFile("SharedAssets/sharedAssets", true);
        }
        /// <summary>
        /// Creates an AssetCollection from the given DataAsset.
        /// </summary>
        /// <param name="file">The DataAsset to use for data.</param>
        /// <param name="load">Whether the assets should load immediately.</param>
        public static AssetCollection FromFile(DataAsset file, bool load)
        {
            AssetCollection collection = new AssetCollection();
            collection.SourceFile = file;

            DataNode firstNode = file.HeadNode;

            int framesPerSecond = firstNode.IntValue("FramesPerSecond", (int)Engine.Instance.FramesPerSecond);
            float frameDivisor = Engine.Instance.FramesPerSecond / framesPerSecond;
            float frameMultiplier = framesPerSecond / Engine.Instance.FramesPerSecond;

            collection.FramesPerSecond = framesPerSecond;
            collection.FrameDivisor = frameDivisor;
            collection.FrameMultiplier = frameMultiplier;

            if (firstNode.ContainsKey("Textures"))
                foreach (var value in firstNode.Child("Textures").AllValues)
                    collection.assets[value.Key] = new ConcreteTexture(Path.Combine(Path.GetDirectoryName(file.FilePath), value.StringValue), load);

            if (firstNode.ContainsKey("TextureAtlases"))
                foreach (var value in firstNode.Child("TextureAtlases").AllValues)
                    collection.assets[value.Key] = new TextureAtlas(Path.Combine(Path.GetDirectoryName(file.FilePath), value.StringValue), load);

            if (firstNode.ContainsKey("Sounds"))
                foreach (var value in firstNode.Child("Sounds").AllValues)
                    collection.assets[value.Key] = new SoundAsset(Path.Combine(Path.GetDirectoryName(file.FilePath), value.StringValue), load);

            if (firstNode.ContainsKey("Fonts"))
                foreach (var value in firstNode.Child("Fonts").AllValues)
                    collection.assets[value.Key] = new FontAsset(Path.Combine(Path.GetDirectoryName(file.FilePath), value.StringValue), load);

            if (firstNode.ContainsKey("Sprites"))
                foreach (var value in firstNode.Child("Sprites").AllChildren)
                {
                    collection.sprites[value.Key] = new SpriteRecord(value.StringValue("Texture"), value.StringValue("Subtexture", null), value.IntValue("Width"), value.IntValue("Height"));
                    if (value.ContainsKey("Animations"))
                    {
                        foreach (var anim in value.Child("Animations").AllChildren)
                        {
                            var record = collection.sprites[value.Key].Animations[anim.Key] = new SpriteAnimation(anim.FloatValue("Delay"), anim.BoolValue("Loop"));
                            record.DelayInSeconds = !anim.BoolValue("DelayFrames", false);
                            record.Delay = anim.FloatValue("Delay");
                            List<int> frames = new List<int>();
                            if (anim.ListLength("Frames") > 0)
                            {
                                foreach (var frame in anim.List("Frames"))
                                    frames.Add(frame.IntValue);
                            }
                            else
                                frames.Add(anim.IntValue("Frame", 0));
                            record.Frames = frames.ToArray();
                        }
                    }
                }

            if ( firstNode.ContainsKey("Particles"))
                foreach (var value in firstNode.Child("Particles").AllChildren)
                {
                    string spriteName = value.StringValue("Sprite", null);
                    var spriteRecord = ( spriteName != null && collection.sprites.ContainsKey(spriteName) ? collection.sprites[spriteName] : null);
                    var record = collection.particles[value.Key] = new ParticleRecord(value.StringValue("Texture", null), value.StringValue("Subtexture", null), spriteRecord);
                    if (value.ContainsKey("Size"))
                    {
                        var r = value.Child("Size");

                        bool pixels = r.BoolValue("Pixels", false);

                        Range size = r.Vector2Value("Size", Vector2.One).ToRange();
                        Range x = r.Vector2Value("X", Vector2.One).ToRange();
                        Range y = r.Vector2Value("Y", Vector2.One).ToRange();
                        Range modifier = r.Vector2Value("Modifier", Vector2.One).ToRange();

                        if (pixels)
                            record.Type.SetSize(size, modifier);
                        else
                            record.Type.SetScale(size, modifier);

                        record.Type.SetScaleIndividual(x, y);
                    }
                    if (value.ContainsKey("Angle"))
                    {
                        var r = value.Child("Angle");

                        Range angle = r.Vector2Value("Angle", new Vector2(0, 0)).ToRange();
                        Range modifier = r.Vector2Value("Modifier", new Vector2(0, 360)).ToRange();

                        record.Type.SetAngle(angle, modifier);
                    }
                    if (value.ContainsKey("Alpha"))
                    {
                        var r = value.Child("Alpha");

                        Range alpha = r.Vector2Value("Alpha", Vector2.One).ToRange();
                        Range modifier = r.Vector2Value("Modifier", new Vector2(0, 360)).ToRange();

                        record.Type.SetAlpha(alpha, modifier);
                    }
                    if (value.ContainsKey("Colors"))
                    {
                        var l = value.List("Colors");
                        List<Color> colors = new List<Color>(l.Length);
                        foreach (var c in l)
                            colors.Add(c.ColorValue);
                        record.Type.SetColor(value.BoolValue("ColorSync", false), colors.ToArray());
                    }
                    if (value.ContainsKey("Step"))
                    {
                        var r = value.Child("Step");

                        record.StepParticle = r.StringValue("Particle", null);
                        record.Type.StepParticleAmount = r.IntValue("Amount", 1);
                        record.Type.StepParticleChance = r.FloatValue("Chance", 0);
                    }
                    if (value.ContainsKey("Death"))
                    {
                        var r = value.Child("Death");

                        record.DeathParticle = r.StringValue("Particle", null);
                        record.Type.DeathParticleAmount = r.IntValue("Amount", 1);
                        record.Type.DeathParticleChance = r.FloatValue("Chance", 0);
                    }
                    if (value.ContainsKey("Acceleration"))
                    {
                        var r = value.Child("Acceleration");

                        if (r.ContainsKey("Vector"))
                            record.Type.SetAcceleration(r.Vector2Value("Vector", Vector2.Zero) * frameMultiplier);
                        else
                            record.Type.SetAcceleration(r.FloatValue("Speed", 0) * frameMultiplier, new Angle(r.FloatValue("Direction", 0), true));
                    }
                    record.Type.LifespanInSeconds = value.BoolValue("LifeSeconds", false);
                    record.Type.SetLife(value.Vector2Value("Lifespan", new Vector2(12, 24)).ToRange(record.Type.LifespanInSeconds ? 1 : frameDivisor));
                    record.Type.SetDirection(value.Vector2Value("Direction", new Vector2(0, 360)).ToRange());
                    record.Type.SetSpeed(value.Vector2Value("Speed", new Vector2(4, 8)).ToRange(frameMultiplier));
                    record.Type.OriginInPixels = value.BoolValue("OriginPixels", true);
                    record.Type.Origin = value.Vector2Value("Origin", Vector2.One * 0.5f);
                    if (value.BoolValue("FrameSync", false))
                        record.Type.Mode = ParticleType.FrameMode.Sync;
                }

            if (firstNode.ContainsKey("Files"))
                foreach (var value in firstNode.Child("Files").AllValues)
                    collection.assets[value.Key] = new DataAsset(Path.Combine(Path.GetDirectoryName(file.FilePath), value.StringValue), load);

            collection.LinkParticles();

            if (load)
                collection.Load();

            return collection;
        }