Esempio n. 1
0
        public override void Update(double dt)
        {
            foreach (Entity e in brains)
            {
                DrawableComponent drawable = e.Get <DrawableComponent>();
                CitizenComponent  citizen  = e.Get <CitizenComponent>();

                foreach (GameDrawable d in drawable.Get("Text"))
                {
                    d.Visible = true;
                    if (d is DrawableText)
                    {
                        string text = "";

                        Behavior brain = citizen.Brain;
                        while (brain != null)
                        {
                            text += brain.GetType().Name + System.Environment.NewLine;
                            brain = brain.Child;
                        }

                        ((DrawableText)d).Text = text;
                    }
                }
            }
        }
        private void ShowSelection(List <Entity> entities)
        {
            if (_renderer != null)
            {
                _renderer.Shutdown();
            }

            foreach (Button b in _selectionButtons)
            {
                _selectionWindow.Remove(b);
            }

            _selectionButtons.Clear();

            Button btn;

            foreach (Entity e in entities)
            {
                btn = new Button(_selectionWindow.Manager)
                {
                    Height = 20,
                    Width  = _selectionWindow.ClientWidth - 4,
                    Left   = 2,
                    Top    = _selectionButtons.Count * 20 + _selectionButtons.Count * 3
                };
                btn.Init();
                btn.Tag    = e;
                btn.Click += new EventHandler(SelectEntity_Click);

                if (e.HasComponent <CitizenComponent>())
                {
                    CitizenComponent citizen = e.Get <CitizenComponent>();
                    btn.Text = citizen.DisplayName;
                }
                else if (e.HasComponent <BuildableComponent>())
                {
                    BuildableComponent buildable = e.Get <BuildableComponent>();
                    btn.Text = buildable.Name;
                }

                _selectionButtons.Add(btn);
                _selectionWindow.Add(btn);
            }

            _selectionWindow.Show();

            if (_selectionWindow.Left + _selectionWindow.Width < 0)
            {
                _selectionWindow.Center();
                _selectionWindow.Left = _selectionWindow.Manager.GraphicsDevice.Viewport.Width - _selectionWindow.Width;
            }
        }
Esempio n. 3
0
        public override void Update(double dt)
        {
            foreach (Entity e in brains)
            {
                CitizenComponent citizen = e.Get <CitizenComponent>();

                if (citizen.Brain == null)
                {
                    citizen.Brain = new DefaultBehavior();
                    citizen.Brain.Init(World, e);
                }

                citizen.Brain.Update(e, dt);
            }
        }
Esempio n. 4
0
        public override BehaviorStatus Update(Entity self, double dt)
        {
            CitizenComponent citizen       = self.Get <CitizenComponent>();
            List <Entity>    potentialJobs = World.GetBuildingsWithinWalkableDistance <ProductionComponent>(citizen.HousingID, 20);

            foreach (Entity e in potentialJobs)
            {
                ProductionComponent production = e.Get <ProductionComponent>();

                // make sure there are jobs available here
                if (production.Employees.Length >= production.MaxEmployees)
                {
                    continue;
                }

                // see if the job hires the appropriate gender
                if (production.EmployeeGender != Gender.BOTH && production.EmployeeGender != citizen.Gender)
                {
                    continue;
                }

                // take the job
                // TODO: make sure the employee can path to the job
                if (production.AddEmployee(self))
                {
                    citizen.JobID = e.ID;
                    if (production.MaxHaulers > 0)
                    {
                        citizen.IsHauler = production.AddHauler(self);

                        if (citizen.IsHauler)
                        {
                            self.AddComponent(new Inventory()); // add an inventory to the hauler
                        }
                    }
                    return(BehaviorStatus.SUCCESS);
                }
            }

            return(BehaviorStatus.FAIL);
        }
        public override Dialog GetControl(Control parent)
        {
            Control.Text = "";
            Manager.Add(Control);
            Control.Show();

            // stop listening while redrawing
            tabs.PageChanged -= Tabs_PageChanged;

            foreach (TabPage tab in tabs.TabPages)
            {
                tabs.RemovePage(tab);
            }

            if (Data.HasComponent <BuildableComponent>())
            {
                BuildableComponent buildable = Data.Get <BuildableComponent>();
                Control.Text             = buildable.Name;
                Control.Caption.Text     = "Description";
                Control.Description.Text = buildable.Description;
            }

            if (Data.HasComponent <CitizenComponent>())
            {
                CitizenComponent citizen = Data.Get <CitizenComponent>();
                Control.Text             = citizen.DisplayName;
                Control.Caption.Text     = "Pleb";
                Control.Description.Text = "A lower class citizen of the city.";

                cr = new CitizenRenderer(Data.Get <CitizenComponent>(), World);
                TabPage tab = tabs.AddPage("Info");

                Panel cp = cr.GetControl(tab);
            }

            // Get housing control
            if (Data.HasComponent <HousingComponent>())
            {
                hr = new HousingRenderer(Data.Get <HousingComponent>(), World);
                TabPage tab = tabs.AddPage("Housing");

                Panel gp = hr.GetControl(tab);
            }

            // Get production control
            if (Data.HasComponent <ProductionComponent>())
            {
                pr = new ProductionRenderer(Data.Get <ProductionComponent>(), World);
                TabPage tab = tabs.AddPage("Production");
                Panel   p   = pr.GetControl(tab);
            }

            // Get stockpile
            if (Data.HasComponent <StockpileComponent>())
            {
                sr = new StockpileRenderer(Data.Get <StockpileComponent>(), World);
                TabPage tab = tabs.AddPage("Stockpile");

                Table t = sr.GetControl(tab);
                t.SetPosition(-tabs.ClientMargins.Left, -2);
                t.SetSize(tab.ClientWidth + tabs.ClientMargins.Horizontal, tab.ClientHeight + 2 + tabs.ClientMargins.Bottom);
                t.Parent = tab;
            }

            // Get inventory
            if (Data.HasComponent <Inventory>())
            {
                ir = new InventoryRenderer(Data.Get <Inventory>(), World);
                TabPage tab = tabs.AddPage("Inventory");

                Table it = ir.GetControl(tab);
                it.SetPosition(-tabs.ClientMargins.Left, -2);
                it.SetSize(tab.ClientWidth + tabs.ClientMargins.Horizontal, tab.ClientHeight + 2 + tabs.ClientMargins.Bottom);
                it.Parent = tab;
            }

            Control.Text = string.Format("({0}) {1}", Data.ID, Control.Text);

            if (previousSelectedTab < tabs.TabPages.Length)
            {
                tabs.SelectedIndex = previousSelectedTab;
            }

            tabs.PageChanged += new EventHandler(Tabs_PageChanged);

            return(Control);
        }
Esempio n. 6
0
        private void Entities_EntityAdded(object sender, EntityEventArgs e)
        {
            Point index = e.Entity.Get <PositionComponent>().Index;

            // update the game data
            foreach (Component c in e.Entity.Components.Values.ToList())
            {
                switch (c.GetType().Name)
                {
                case "CitizenComponent":
                    CitizenComponent citizen = (CitizenComponent)c;

                    // fill in the data if it doesn't already exist
                    if (string.IsNullOrWhiteSpace(citizen.Name))
                    {
                        // generate name
                        string[] names = { "Steve", "John", "Bill" };
                        citizen.Name = names[Random.Next(0, names.Length)];
                    }

                    if (string.IsNullOrWhiteSpace(citizen.Surname))
                    {
                        // generate family name
                        string[] names = { "Johnson", "Miller", "Smith" };
                        citizen.Surname = names[Random.Next(0, names.Length)];
                    }

                    if (citizen.Gender == Gender.BOTH)
                    {
                        citizen.Gender = (Gender)Random.Next(1, 3);
                    }

                    if (citizen.Age == 0)
                    {
                        // generate age
                        citizen.Age = Random.Next(14, 46);
                    }

                    if (citizen.Money == 0)
                    {
                        citizen.Money = Random.Next(20, 100);
                    }
                    break;

                case "CollapsibleComponent":
                    CollapsibleComponent collapsible = (CollapsibleComponent)c;

                    if (collapsible.Value == 0)
                    {
                        collapsible.Value = CollapsibleComponent.MAX;
                    }
                    break;

                case "CollisionComponent":
                    CollisionComponent collision = (CollisionComponent)c;

                    foreach (LocationValue lv in collision.Plan)
                    {
                        Point p = new Point(index.X + lv.Offset.X, index.Y + lv.Offset.Y);

                        if (Collisions.Map.ContainsKey(p))
                        {
                            Collisions.Map[p] = (PathTypes)lv.Value;
                        }
                        else
                        {
                            Collisions.Map.Add(p, (PathTypes)lv.Value);
                        }
                    }
                    break;

                case "CollisionMapComponent":
                    Collisions = (CollisionMapComponent)c;
                    break;

                case "FoundationComponent":
                    FoundationComponent floor = (FoundationComponent)c;

                    // update the floor planner
                    foreach (LocationValue lv in floor.Plan)
                    {
                        Point update = new Point(index.X + lv.Offset.X, index.Y + lv.Offset.Y);
                        Foundations.SpaceTaken.Add(update, e.Entity.ID);
                    }
                    break;

                case "FoundationPlannerComponent":
                    Foundations = (FoundationPlannerComponent)c;
                    break;

                case "GameDateComponent":
                    Date = (GameDateComponent)c;
                    break;

                case "IsometricMapComponent":
                    Map = e.Entity.Get <IsometricMapComponent>();

                    if (Map.Terrain == null)
                    {
                        Map.CreateMap(Map.SpriteSheetName, Map.TxWidth, Map.TxHeight, Map.PxTileWidth, Map.PxTileHeight);

                        // replace the map
                        e.Entity.RemoveComponent(e.Entity.Get <IsometricMapComponent>());
                        e.Entity.AddComponent(Map);
                    }
                    break;

                case "PositionComponent":
                    PositionComponent position = (PositionComponent)c;

                    if (!String.IsNullOrWhiteSpace(position.GenerateAt))
                    {
                        int xIndex = -1;
                        int yIndex = -1;

                        switch (position.GenerateAt)
                        {
                        // random
                        case "Edge":
                            int side = Random.Next(4);

                            switch (side)
                            {
                            case 0:
                                // northwest
                                xIndex = 0;
                                yIndex = Random.Next(1, Map.TxHeight);
                                break;

                            case 1:
                                // northeast
                                xIndex = Random.Next(1, Map.TxWidth);
                                yIndex = 0;
                                break;

                            case 2:
                                // southeast
                                xIndex = Map.TxWidth - 1;
                                yIndex = Random.Next(1, Map.TxHeight);
                                break;

                            default:
                                // southwest
                                xIndex = Random.Next(1, Map.TxWidth);
                                yIndex = Map.TxHeight - 1;
                                break;
                            }
                            break;

                        case "NoEdge":
                            xIndex = Random.Next(1, Map.TxWidth - 1);
                            yIndex = Random.Next(1, Map.TxHeight - 1);
                            break;

                        default:
                            xIndex = Random.Next(0, Map.TxWidth);
                            yIndex = Random.Next(0, Map.TxHeight);
                            break;
                        }

                        Vector2 pos = Map.GetPositionFromIndex(xIndex, yIndex);

                        position.X          = pos.X;
                        position.Y          = pos.Y;
                        position.Index      = new Point(xIndex, yIndex);
                        position.GenerateAt = String.Empty;
                        index = position.Index;
                    }
                    break;

                case "RoadComponent":
                    // setup the road component
                    RoadComponent road = (RoadComponent)c;
                    road.BuiltAt = index;

                    // update the planner
                    Roads.AddOrUpdate(this, e.Entity, true);
                    break;

                case "RoadPlannerComponent":
                    Roads = (RoadPlannerComponent)c;
                    break;

                case "SpawnerComponent":
                    break;
                }
            }
        }
        public override BehaviorStatus Update(Entity self, double dt)
        {
            BehaviorStatus status = base.Update(self, dt);

            switch (status)
            {
            // child returned a success
            case BehaviorStatus.SUCCESS:
                if (Finished is ExitBuildingBehavior)
                {
                    ExitBuildingBehavior exit = (ExitBuildingBehavior)Finished;

                    // make sure the citizen starts at the right position
                    PositionComponent position = self.Get <PositionComponent>();
                    Vector2           startAt  = World.Map.GetPositionFromIndex(exit.SelectedPath.Start.X, exit.SelectedPath.Start.Y);
                    position.X     = startAt.X;
                    position.Y     = startAt.Y;
                    position.Index = exit.SelectedPath.Start;

                    GoToBehavior g2b = new GoToBehavior()
                    {
                        GeneratedPath = exit.SelectedPath,
                        TargetID      = exit.TargetID
                    };
                    AddChild(g2b);
                }
                break;

            case BehaviorStatus.FAIL:

                break;

            case BehaviorStatus.RUN:
                // look for a secondary behavior to do
                CitizenComponent citizen = self.Get <CitizenComponent>();

                // TODO: need to implement "archetype" behaviors like: child, student, adult, retired etc
                // these top-level behaviors will manage the needs and invoke appropriate sub-behaviors

                // the citizen is homeless, find them a home!
                if (citizen.HousingID == 0 && PreviousBehavior.GetType() != typeof(FindHomeBehavior))
                {
                    AddChild(new FindHomeBehavior());
                }
                else if (citizen.JobID == 0 && citizen.HousingID != 0 && PreviousBehavior.GetType() != typeof(FindJobBehavior))
                {
                    AddChild(new FindJobBehavior());
                }
                else
                {
                    // do the hauling job
                    if (citizen.IsHauler && citizen.JobID != 0)
                    {
                        AddChild(new HaulerBehavior());
                    }
                    // go to work
                    //else if (citizen.JobID != -1 && PreviousBehavior.GetType() == typeof(IdleBehavior) && citizen.InsideID != citizen.JobID)
                    //{
                    //    state.Push(new ExitBuildingBehavior() { ExitID = citizen.InsideID, TargetID = citizen.JobID });
                    //    PreviousBehavior = state.Peek();
                    //}
                    // go home
                    else if (citizen.HousingID != 0 && PreviousBehavior.GetType() == typeof(IdleBehavior) && citizen.InsideID != citizen.HousingID)
                    {
                        AddChild(new ExitBuildingBehavior()
                        {
                            ExitID = citizen.InsideID, TargetID = citizen.HousingID
                        });
                    }
                    else
                    {
                        AddChild(new IdleBehavior());
                    }
                }
                PreviousBehavior = Child;
                break;
            }

            return(BehaviorStatus.RUN);
        }
Esempio n. 8
0
        public override BehaviorStatus Update(Entity self, double dt)
        {
            BehaviorStatus status = base.Update(self, dt);

            switch (status)
            {
            case BehaviorStatus.SUCCESS:
            case BehaviorStatus.FAIL:
                if (Finished is GoToBehavior)
                {
                    Entity house = World.Entities.Get(HousingID);

                    // take ourselves out of the tennant list
                    house.Get <HousingComponent>().RemoveProspect(self);

                    if (status == BehaviorStatus.FAIL)
                    {
                        return(BehaviorStatus.FAIL);
                    }
                    else
                    {
                        house.Get <HousingComponent>().AddTennant(self);
                        self.Get <CitizenComponent>().HousingID = HousingID;
                        AddChild(new FadeBehavior()
                        {
                            FadeIn = false
                        });
                    }
                }
                else
                {
                    return(status);
                }
                break;

            case BehaviorStatus.RUN:
                List <Entity> houses     = World.Entities.FindAll(delegate(Entity e) { return(e.HasComponent <HousingComponent>()); });
                CitizenComponent citizen = self.Get <CitizenComponent>();

                foreach (Entity housingEntity in houses)
                {
                    HousingComponent house = housingEntity.Get <HousingComponent>();

                    if (house.SpaceAvailable <= 0)
                    {
                        continue;
                    }

                    // TODO: check for appropriate level of housing (rich, middle class, poor)
                    if (house.Rent <= citizen.Money)
                    {
                        HousingID = housingEntity.ID;
                        house.AddProspect(self);

                        // TODO: enter sub behavior to move into the home (find a path there and then move there)
                        AddChild(new GoToBehavior()
                        {
                            TargetID = HousingID, FollowRoadsOnly = false
                        });
                        return(BehaviorStatus.WAIT);
                    }
                }

                // if no home was found enter failure state
                return(BehaviorStatus.FAIL);
            }

            return(BehaviorStatus.WAIT);
        }