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; } }
public void AddOrUpdate(GameWorld world, Entity e, bool recursion = false) { RoadComponent road = e.Get <RoadComponent>(); string value = "dirt-road-" + GetRoadValue(world.Map, road.BuiltAt); // set the value of built into the roadPlanner if (!Built.ContainsKey(road.BuiltAt)) { Built.Add(road.BuiltAt, value); } else { Built[road.BuiltAt] = value; } if (road.Updateable) { DrawableComponent drawable = e.Get <DrawableComponent>(); foreach (GameDrawable sprite in drawable.Drawables["Foundation"].ToList()) { if (sprite.PrototypeID.Contains("road")) { // remove old road drawables drawable.Drawables["Foundation"].Remove(sprite); } } road.RoadType = Built[road.BuiltAt]; drawable.Add("Foundation", (GameDrawable)world.Prototypes[road.RoadType]); } if (recursion) { UpdateNeighbors(world, e); } }
// 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); } } }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { base.Initialize(); diagnostics = new DiagnosticInfo("System Performance"); // Add the content to the textures singleton Textures.Instance.Graphics = GraphicsDevice; Textures.Instance.Content = Content; world = new GameWorld(); world.Systems.Add(world.Input); world.Systems.Add(new CameraSystem()); world.Systems.Add(new InspectionSystem()); world.Systems.Add(new ControlSystem()); world.Systems.Add(new DateTimeSystem()); world.Systems.Add(new BehaviorSystem()); world.Systems.Add(new ImmigrationSystem()); world.Systems.Add(new ProductionSystem()); world.Systems.Add(new CityInformationSystem()); world.Systems.Add(new HousingUpgradeSystem()); world.Systems.Add(new OverlaySystem() { Graphics = GraphicsDevice }); world.Systems.Add(new CollapsibleSystem()); world.Systems.Add(new IsometricMapSystem() { Graphics = GraphicsDevice }); world.Renderer = new DefaultRenderSystem() { Graphics = GraphicsDevice, ClearColor = Color.Black }; world.Systems.Add(world.Renderer); world.UI = new Manager(this, "Pixel") { AutoCreateRenderTarget = false, AutoUnfocus = true, TargetFrames = 60 }; world.UI.Initialize(); world.UI.RenderTarget = world.UI.CreateRenderTarget(); Window w = new TomShane.Neoforce.Controls.Window(world.UI) { Text = "My Quick Test Window" }; w.Init(); w.Center(); // Load the scenario // TODO: put this in a method somewhere string s = File.ReadAllText("Content/Data/Scenarios/alpha.json"); Scenario scenario = JsonConvert.DeserializeObject <Scenario>(s); // Load textures from config Textures.Instance.LoadFromJson(scenario.Textures, true); // Load the drawables world.Prototypes.LoadFromFile( new DrawablesLoader(), scenario.Drawables, true); world.City = scenario.City; // Load in entities world.Prototypes.LoadFromFile( new CustomEntityLoader() { Converter = new CustomComponentConverter() }, scenario.Entities, true); // load scenario data world.Prototypes.LoadFromFile( new DataLoader <Item>(), scenario.Items, true); world.Prototypes.LoadFromFile( new DataLoader <Recipe>(), scenario.Recipes, true); //GameData.Instance.LoadItemsFromJson(scenario.Items, true); //GameData.Instance.LoadRecipesFromJson(scenario.Recipes, true); CustomEntityLoader cel = new CustomEntityLoader() { Library = world.Prototypes, Converter = new CustomComponentConverter() }; foreach (JObject o in scenario.DefaultEntities) { Entity e = (Entity)cel.LoadPrototype(o); world.Entities.Add(e); } // start up the pathfinder thread PathfinderSystem pfs = new PathfinderSystem() { Map = world.Map, Collisions = world.Collisions }; pathThread = new Thread(new ThreadStart(pfs.Run)); pathThread.Start(); pfs = new PathfinderSystem() { Map = world.Map, Collisions = world.Collisions }; pathThread2 = new Thread(new ThreadStart(pfs.Run)); //pathThread2.Start(); // TODO: create a settings file to read any key bindings from inputControlEntity = new Entity(); inputControlEntity.AddComponent(new PositionComponent()); inputControlEntity.AddComponent(new CameraController()); world.Entities.Add(inputControlEntity); DrawableComponent diagDrawable = new DrawableComponent(); diagDrawable.Add("Text", new DrawableText() { Text = "", Color = Color.White, Visible = true, Layer = "Text", Static = true }); diagnosticEntity = new Entity(); diagnosticEntity.AddComponent(new PositionComponent() { X = 0, Y = 50f }); diagnosticEntity.AddComponent(diagDrawable); world.Entities.Add(diagnosticEntity); }
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); } } } }