Example #1
0
 public ParticleManager(ComponentManager Components)
 {
     // Todo: Better modding support - make it a list of named emitters.
     Effects = new Dictionary <string, ParticleEffect>();
     Load(Components, FileUtils.LoadJsonFromResolvedPath <Dictionary <string, List <EmitterData> > >(ContentPaths.Particles.particles));
 }
Example #2
0
 public static T LoadFromJson <T>(string asset)
 {
     return(FileUtils.LoadJsonFromString <T>(ContentPaths.GetFileAsString(asset)));
 }
        public static void Render()
        {
            var FPS = 0;

            if (FPSWatch == null)
            {
                FPSWatch = Stopwatch.StartNew();
            }
            else
            {
                FPSWatch.Stop();
                FPS      = (int)Math.Floor(1.0f / (float)FPSWatch.Elapsed.TotalSeconds);
                FPSWatch = Stopwatch.StartNew();
            }

            FPSBuffer[k % 100] = FPS;
            k++;
            var avgFPS = (int)FPSBuffer.Average();

#if XNA_BUILD
            if (!SentPerfReport && GameSettings.Default.AllowReporting && avgFPS < 20)
            {
                if (FPSFaultTimer != null && FPSFaultTimer.Elapsed.TotalSeconds > 5)
                {
                    var settings      = FileUtils.SerializeBasicJSON <GameSettings.Settings>(GameSettings.Default);
                    var adapter       = GameStates.GameState.Game.GraphicsDevice.Adapter;
                    var deviceDetails = String.Format("Num Cores: {4}\nDevice:\nName: {0}\n ID: {1}\n Description: {2}\n Vendor: {3}", adapter.DeviceName, adapter.DeviceId, adapter.Description, adapter.VendorId, Environment.ProcessorCount);
                    var memory        = GameStates.PlayState.BytesToString(System.GC.GetTotalMemory(false));
                    (GameStates.GameState.Game as DwarfGame).TriggerRavenEvent("Low performance detected", String.Format("Average FPS: {0}\nSettings:\n{1}\n{2}\nRAM: {3} {4}", avgFPS, settings, deviceDetails, memory, GameStates.PlayState.BytesToString(Environment.WorkingSet)));
                    SentPerfReport = true;
                }
                else if (FPSFaultTimer == null)
                {
                    FPSFaultTimer = Stopwatch.StartNew();
                }
            }
            else if (!SentPerfReport && GameSettings.Default.AllowReporting)
            {
                FPSFaultTimer = null;
            }
#endif

            if (DwarfGame.IsConsoleVisible)
            {
                PopFrame();

                var output = DwarfGame.GetConsoleTile("PERFORMANCE");
                output.Lines.Clear();
                output.Lines.Add(String.Format("Frame time: {0:000.000}", FPSWatch.Elapsed.TotalMilliseconds));

                foreach (var function in Functions)
                {
                    output.Lines.Add(String.Format("{1:0000} {2:000} {0}\n", function.Value.Name, function.Value.FrameCalls, function.Value.FrameTicks / 1000));
                }

                output.Invalidate();

                var fps = DwarfGame.GetConsoleTile("FPS");
                if (fps.Children[0] is Gui.Widgets.TextGrid)
                {
                    fps.RemoveChild(fps.Children[0]);
                    fps.AddChild(new Gui.Widgets.Graph()
                    {
                        AutoLayout = AutoLayout.DockFill,
                    });

                    fps.Layout();
                }

                var graph = fps.Children[0] as Gui.Widgets.Graph;
                graph.Values.Add((float)FPSWatch.Elapsed.TotalMilliseconds);
                while (graph.Values.Count > graph.GraphWidth)
                {
                    graph.Values.RemoveAt(0);
                }

                graph.MinLabelString = String.Format("FPS: {0:000} (avg: {1})", FPS, avgFPS);

                graph.Invalidate();
            }
        }
Example #4
0
        public bool ReadChunks(string filePath)
        {
            if (Metadata == null)
            {
                throw new InvalidProgramException("MetaData must be loaded before chunk data.");
            }

            ChunkData = new List <ChunkFile>();

            var chunkDirs = System.IO.Directory.GetDirectories(filePath, "Chunks");

            if (chunkDirs.Length > 0)
            {
                foreach (string chunk in Directory.GetFiles(chunkDirs[0], "*." + (DwarfGame.COMPRESSED_BINARY_SAVES ? ChunkFile.CompressedExtension : ChunkFile.Extension)))
                {
                    if (DwarfGame.COMPRESSED_BINARY_SAVES)
                    {
                        ChunkData.Add(FileUtils.LoadBinary <ChunkFile>(chunk));
                    }
                    else
                    {
                        ChunkData.Add(FileUtils.LoadJsonFromAbsolutePath <ChunkFile>(chunk));
                    }
                }
            }
            else
            {
                Console.Error.WriteLine("Can't load chunks {0}, no chunks found", filePath);
                return(false);
            }

            // Remap the saved voxel ids to the ids of the currently loaded voxels.
            if (Metadata.VoxelTypeMap != null)
            {
                // First build a replacement mapping.

                var newVoxelMap   = VoxelLibrary.GetVoxelTypeMap();
                var newReverseMap = new Dictionary <String, int>();
                foreach (var mapping in newVoxelMap)
                {
                    newReverseMap.Add(mapping.Value, mapping.Key);
                }

                var replacementMap = new Dictionary <int, int>();
                foreach (var mapping in Metadata.VoxelTypeMap)
                {
                    if (newReverseMap.ContainsKey(mapping.Value))
                    {
                        var newId = newReverseMap[mapping.Value];
                        if (mapping.Key != newId)
                        {
                            replacementMap.Add(mapping.Key, newId);
                        }
                    }
                }

                // If there are no changes, skip the expensive iteration.
                if (replacementMap.Count != 0)
                {
                    foreach (var chunk in ChunkData)
                    {
                        for (var i = 0; i < chunk.Types.Length; ++i)
                        {
                            if (replacementMap.ContainsKey(chunk.Types[i]))
                            {
                                chunk.Types[i] = (byte)replacementMap[chunk.Types[i]];
                            }
                        }
                    }
                }
            }

            return(true);
        }
Example #5
0
        /// <summary>
        /// Initialize function creates all the required components for the bird.
        /// </summary>
        /// <param name="sprites">The sprite sheet to use for the bird</param>
        public void Initialize(string sprites)
        {
            // When true, causes the bird to face the direction its moving in
            Physics.Orientation = Physics.OrientMode.RotateY;


            // Create the sprite component for the bird.
            Sprite = new CharacterSprite
                         (Graphics,
                         Manager,
                         "Rabbit Sprite",
                         Physics,
                         Matrix.CreateTranslation(0, 0.5f, 0)
                         );

            CompositeAnimation.Descriptor descriptor =
                FileUtils.LoadJsonFromString <CompositeAnimation.Descriptor>(
                    ContentPaths.GetFileAsString(sprites));

            List <CompositeAnimation> animations = descriptor.GenerateAnimations("Rabbit");

            foreach (CompositeAnimation animation in animations)
            {
                Sprite.AddAnimation(animation);
            }

            // Used to grab other components
            Hands = new Grabber("hands", Physics, Matrix.Identity, new Vector3(0.1f, 0.1f, 0.1f), Vector3.Zero);

            // Used to sense hostile creatures
            Sensors = new EnemySensor(Manager, "EnemySensor", Physics, Matrix.Identity, new Vector3(20, 5, 20), Vector3.Zero);

            // Controls the behavior of the creature
            AI = new PacingCreatureAI(this, "Rabbit AI", Sensors, PlanService);

            // The bird can peck at its enemies (0.1 damage)
            Attacks = new List <Attack> {
                new Attack("Bite", 0.01f, 2.0f, 1.0f, ContentPaths.Audio.bunny, ContentPaths.Effects.bite)
            };


            // The bird can hold one item at a time in its inventory
            Inventory = new Inventory("Inventory", Physics)
            {
                Resources = new ResourceContainer
                {
                    MaxResources = 1
                }
            };

            // The shadow is rotated 90 degrees along X, and is 0.25 blocks beneath the creature
            Matrix shadowTransform = Matrix.CreateRotationX((float)Math.PI * 0.5f);

            shadowTransform.Translation = new Vector3(0.0f, -0.25f, 0.0f);
            shadowTransform            *= Matrix.CreateScale(0.75f);

            SpriteSheet shadowTexture = new SpriteSheet(ContentPaths.Effects.shadowcircle);

            Shadow = new Shadow(Manager, "Shadow", Physics, shadowTransform, shadowTexture);

            // We set up the shadow's animation so that it's just a static black circle
            // TODO: Make the shadow set this up automatically
            List <Point> shP = new List <Point>
            {
                new Point(0, 0)
            };
            Animation shadowAnimation = new Animation(Graphics, new SpriteSheet(ContentPaths.Effects.shadowcircle), "sh", 32, 32, shP, false, Color.Black, 1, 0.7f, 0.7f, false);

            Shadow.AddAnimation(shadowAnimation);
            shadowAnimation.Play();
            Shadow.SetCurrentAnimation("sh");

            // The bird will emit a shower of blood when it dies
            DeathParticleTrigger = new ParticleTrigger("blood_particle", Manager, "Death Gibs", Physics, Matrix.Identity, Vector3.One, Vector3.Zero)
            {
                TriggerOnDeath = true,
                TriggerAmount  = 1
            };

            // The bird is flammable, and can die when exposed to fire.
            Flames = new Flammable(Manager, "Flames", Physics, this);

            // Tag the physics component with some information
            // that can be used later
            Physics.Tags.Add("Rabbit");
            Physics.Tags.Add("Animal");

            Stats.FullName     = TextGenerator.GenerateRandom("$firstname") + " the rabbit";
            Stats.CurrentClass = new EmployeeClass()
            {
                Name   = "Rabbit",
                Levels = new List <EmployeeClass.Level>()
                {
                    new EmployeeClass.Level()
                    {
                        Index = 0, Name = "Rabbit"
                    }
                }
            };


            NoiseMaker.Noises.Add("Hurt", new List <string>()
            {
                ContentPaths.Audio.bunny
            });
        }
Example #6
0
 public static void Save(string file)
 {
     FileUtils.SaveJSON(Mappings, file);
 }
Example #7
0
        public void Save()
        {
            var metaDataPath = Directory + ProgramData.DirChar + "meta.json";

            FileUtils.SaveBasicJson(this, metaDataPath);
        }
Example #8
0
 public static void Save(string file)
 {
     FileUtils.SaveBasicJson(Mappings, file);
 }
Example #9
0
 public override string ToString()
 {
     return(FileUtils.SerializeBasicJSON(this));
 }