public override BehaviorStatus Update(Entity self, double dt) { BehaviorStatus status = base.Update(self, dt); switch (status) { case BehaviorStatus.SUCCESS: case BehaviorStatus.FAIL: break; case BehaviorStatus.RUN: Entity target = World.Entities.Find(delegate(Entity e) { return(e.ID == TargetID); }); // if invalid target fail out if (target == null) { return(BehaviorStatus.FAIL); } if (PathRequest != null) { // try to retrieve the path Path p = PathfinderSystem.GetPath(PathRequest.ID); if (p == null) { // idle for a short bit AddChild(new IdleBehavior() { IdleTime = 0.1 }); return(BehaviorStatus.WAIT); } else { GeneratedPath = p; // TODO: check for tPosition = sPosition if (GeneratedPath.Waypoints.Count == 0) { return(BehaviorStatus.FAIL); } return(BehaviorStatus.SUCCESS); } } else { // make sure the target has a position or foundation if (!MoveToNearbyRoad) { // if the target or self has no position fail out if (!target.HasComponent <PositionComponent>() || !self.HasComponent <PositionComponent>()) { return(BehaviorStatus.FAIL); } PositionComponent tPosition = target.Get <PositionComponent>(); PositionComponent sPosition = self.Get <PositionComponent>(); if (tPosition.Index == null || tPosition.Index == Point.Zero) { tPosition.Index = World.Map.GetIndexFromPosition((int)tPosition.X, (int)tPosition.Y); } PathRequest = new PathRequest() { Start = sPosition.Index, End = tPosition.Index }; if (FollowRoadsOnly) { PathRequest.Validation = OnlyRoads; } else { PathRequest.Validation = AnyPathNotBlocked; } PathfinderSystem.RequestPath(PathRequest); } else { // check for a starting position and ensure the target has a foundation if (!self.HasComponent <PositionComponent>() || !target.HasComponent <FoundationComponent>() || !target.HasComponent <PositionComponent>()) { return(BehaviorStatus.FAIL); } PositionComponent tPosition = target.Get <PositionComponent>(); FoundationComponent foundation = target.Get <FoundationComponent>(); // list to hold valid road tiles to move to List <Point> validLandings = World.GetValidExitsFromFoundation(target); if (validLandings.Count == 0) { return(BehaviorStatus.FAIL); } else { // find a multi-path PositionComponent sPosition = self.Get <PositionComponent>(); PathRequest = new PathRequest() { Start = sPosition.Index, Ends = validLandings }; if (FollowRoadsOnly) { PathRequest.Validation = OnlyRoads; } else { PathRequest.Validation = AnyPathNotBlocked; } PathfinderSystem.RequestPath(PathRequest); } } } break; } return(BehaviorStatus.WAIT); }
/// <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); }