Esempio n. 1
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 FindPathBehavior)
                {
                    if (status == BehaviorStatus.SUCCESS)
                    {
                        FollowPath(((FindPathBehavior)Finished).GeneratedPath, 1.5f);
                    }
                    else
                    {
                        // TODO: quit job if not able to find a path to it
                        return(BehaviorStatus.FAIL);
                    }
                }
                else if (Finished is FollowPathBehavior)
                {
                    if (status == BehaviorStatus.SUCCESS)
                    {
                        // Fadeout
                        AddChild(new FadeBehavior()
                        {
                            FadeIn = false
                        });
                        self.Get <CitizenComponent>().InsideID = TargetID;
                    }
                    else
                    {
                        // failed to follow path to home
                        // TOOD: possibly fail out to decide to do something else?
                        return(BehaviorStatus.WAIT);
                    }
                }
                else
                {
                    return(status);
                }
                break;

            case BehaviorStatus.RUN:
                if (GeneratedPath == null)
                {
                    FindPathBehavior fpb = new FindPathBehavior()
                    {
                        MoveToNearbyRoad = true,
                        TargetID         = this.TargetID,
                        FollowRoadsOnly  = FollowRoadsOnly
                    };
                    AddChild(fpb);
                }
                else
                {
                    FollowPath(GeneratedPath, 1.5f);
                }
                break;
            }

            return(BehaviorStatus.WAIT);
        }
Esempio n. 2
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 FindPathBehavior)
                {
                    _validExits.RemoveAt(0);

                    if (status == BehaviorStatus.SUCCESS)
                    {
                        // add the generated path
                        _createdPaths.Add(((FindPathBehavior)Finished).GeneratedPath);
                    }

                    if (_validExits.Count > 0)
                    {
                        self.Get <PositionComponent>().Index = _validExits[0];
                        FindPathBehavior fpb = new FindPathBehavior()
                        {
                            TargetID         = this.TargetID,
                            MoveToNearbyRoad = true
                        };
                        AddChild(fpb);
                    }
                    else
                    {
                        // paths have finished generating, pick the best one
                        Path selected = null;

                        foreach (Path p in _createdPaths)
                        {
                            if (selected == null)
                            {
                                selected = p;
                                continue;
                            }

                            // TODO: select the one with the least "length" not the least count
                            if (p.Length < selected.Length)
                            {
                                selected = p;
                            }
                        }

                        if (selected == null)
                        {
                            return(BehaviorStatus.FAIL);
                        }
                        else
                        {
                            SelectedPath = selected;
                            self.Get <PositionComponent>().Index = SelectedPath.Start;
                            return(BehaviorStatus.SUCCESS);
                        }
                    }
                }
                break;

            case BehaviorStatus.RUN:
                // get any valid exits
                if (_validExits.Count == 0)
                {
                    Entity target = World.Entities.Get(TargetID);
                    Entity exit   = World.Entities.Get(ExitID);

                    // if invalid target fail out
                    if (target == null || exit == null)
                    {
                        return(BehaviorStatus.FAIL);
                    }

                    // if the target or self has no position fail out
                    if (!target.HasComponent <PositionComponent>() || !exit.HasComponent <PositionComponent>())
                    {
                        return(BehaviorStatus.FAIL);
                    }

                    _validExits = World.GetValidExitsFromFoundation(exit);

                    if (_validExits.Count == 0)
                    {
                        return(BehaviorStatus.FAIL);
                    }
                    else
                    {
                        // set the position of self to the first point while it tests the path
                        self.Get <PositionComponent>().Index = _validExits[0];
                        FindPathBehavior fpb = new FindPathBehavior()
                        {
                            TargetID         = this.TargetID,
                            MoveToNearbyRoad = true
                        };
                        AddChild(fpb);
                    }
                }
                break;
            }

            return(BehaviorStatus.WAIT);
        }