public override void ComponentLoaded(Entity e, Component c, JObject o)
        {
            // Do custom handling of the component
            switch (c.GetType().Name)
            {
            case "DrawableComponent":
                DrawableComponent drawable = (DrawableComponent)c;
                drawable.Drawables.Clear();
                if (o["Sources"] != null)
                {
                    IEnumerable <string> sources = o["Sources"].Values <string>();
                    foreach (string str in sources)
                    {
                        GameDrawable d = (GameDrawable)Library[str];

                        drawable.Add(d.Layer, d);
                    }
                }
                if (o["Drawables"] != null)
                {
                    DrawableComponent dd = JsonConvert.DeserializeObject <DrawableComponent>(o.ToString(), new DrawableConverter());

                    foreach (KeyValuePair <string, List <GameDrawable> > kvp in dd.Drawables)
                    {
                        foreach (GameDrawable d in kvp.Value)
                        {
                            drawable.Add(kvp.Key, d);
                        }
                    }
                }
                break;

            case "FoundationComponent":
                FoundationComponent floor = (FoundationComponent)c;
                switch (floor.PlanType)
                {
                case "Fill":
                    Point start = floor.Plan[0].Offset;
                    Point end   = floor.Plan[1].Offset;

                    floor.Plan.Clear();         // clear the plan, the for loops will fill it
                    for (int xx = start.X; xx <= end.X; xx++)
                    {
                        for (int yy = start.Y; yy <= end.Y; yy++)
                        {
                            floor.Plan.Add(new LocationValue()
                            {
                                Offset = new Point(xx, yy)
                            });
                        }
                    }
                    break;
                }
                break;
            }
        }
示例#2
0
        public void Add(string layer, GameDrawable drawable)
        {
            try
            {
                if (!Drawables.ContainsKey(layer))
                {
                    Drawables.Add(layer, new List <GameDrawable>());
                }

                if (!Drawables[layer].Contains(drawable))
                {
                    Drawables[layer].Add(drawable);
                }
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine(e.StackTrace);
            }
        }
        // called when the selected entity changes
        private void SelectEntity()
        {
            List <string>       categories        = new List <string>(indexedBuildables.Keys);
            DrawableComponent   drawable          = dataTracker.Get <DrawableComponent>();
            Entity              selectedEntity    = indexedBuildables[selectedCategory][selectedEntityIndex];
            BuildableComponent  selectedBuildable = selectedEntity.Get <BuildableComponent>();
            DrawableComponent   selectedDrawable  = selectedEntity.Get <DrawableComponent>();
            FoundationComponent foundation        = selectedEntity.Get <FoundationComponent>();

            drawable.Drawables.Clear();

            foreach (KeyValuePair <string, List <GameDrawable> > kvp in selectedDrawable.Drawables)
            {
                foreach (GameDrawable gd in kvp.Value)
                {
                    GameDrawable nd = Serialization.DeepCopy <GameDrawable>(gd);
                    drawable.Add(kvp.Key, nd);
                }
            }
        }
        public override Prototype LoadPrototype(JObject source)
        {
            GameDrawable drawable = JsonConvert.DeserializeObject <GameDrawable>(source.ToString(), new DrawableConverter());

            return(drawable);
        }
        private void Date_TimeChanged(GameDateComponent sender)
        {
            long elapsed = World.Date.MinutesElapsed(lastUpdate);

            lastUpdate = World.Date.Time;

            List <Entity> producers = World.Entities.FindAll(delegate(Entity e) { return(e.HasComponent <ProductionComponent>()); });

            foreach (Entity e in producers)
            {
                ProductionComponent p = e.Get <ProductionComponent>();

                if (string.IsNullOrWhiteSpace(p.Recipe))
                {
                    continue;
                }

                // determines how much work is done
                // TODO: check for divide by zero?
                float workerPercentage = (float)p.Employees.Length / (float)p.MaxEmployees;

                p.WorkDone += (elapsed * workerPercentage);

                Recipe r = (Recipe)World.Prototypes[p.Recipe];
                if (p.WorkDone >= r.Stages[p.CurrentStage].WorkRequired)
                {
                    // TODO: check the inputs and modify or elminate the output based on the amount of inputs present
                    // store output in the inventory
                    Inventory   inventory = e.Get <Inventory>();
                    RecipeStage stage     = r.Stages[p.CurrentStage];

                    foreach (RecipeOutput output in stage.Outputs)
                    {
                        inventory.Add(output.Item, output.AmountProduced);
                        // make sure the newly produced item is marked as output
                        inventory.Items[output.Item].Output = true;
                    }

                    // go to next stage in the recipe
                    p.CurrentStage++;
                    if (p.CurrentStage >= r.Stages.Count)
                    {
                        p.CurrentStage = 0;
                    }

                    // reset the work done
                    p.WorkDone = 0;

                    // update the drawables
                    DrawableComponent drawable = e.Get <DrawableComponent>();
                    stage = r.Stages[p.CurrentStage];

                    // remove
                    foreach (string str in stage.RemoveFromDrawableComponent)
                    {
                        drawable.RemoveByPrototypeID(str);
                    }

                    // add
                    foreach (string str in stage.AddToDrawableComponent)
                    {
                        GameDrawable igd = (GameDrawable)World.Prototypes[str];

                        drawable.Add(igd.Layer, igd);
                    }
                }
            }
        }