protected override void OnActionUpdate(FixPoint delta_time)
        {
            int current_target_id = (int)(m_context.GetData(BTContextKey.CurrentTargetID));

            if (current_target_id <= 0)
            {
                return;
            }
            Entity current_target = GetLogicWorld().GetEntityManager().GetObject(current_target_id);

            if (current_target == null)
            {
                return;
            }
            Entity owner_entity = GetOwnerEntity();

            if (owner_entity == null)
            {
                return;
            }
            PositionComponent  position_cmp        = owner_entity.GetComponent(PositionComponent.ID) as PositionComponent;
            LocomotorComponent locomotor_cmp       = owner_entity.GetComponent(LocomotorComponent.ID) as LocomotorComponent;
            PositionComponent  target_position_cmp = current_target.GetComponent(PositionComponent.ID) as PositionComponent;

            Vector3FP direction = target_position_cmp.CurrentPosition - position_cmp.CurrentPosition;
            FixPoint  distance  = direction.FastLength() - target_position_cmp.Radius - position_cmp.Radius; //ZZWTODO 多处距离计算

            if (distance <= m_range)
            {
                return;
            }

            PathFindingComponent pathfinding_component = owner_entity.GetComponent(PathFindingComponent.ID) as PathFindingComponent;

            if (pathfinding_component != null)
            {
                if (pathfinding_component.FindPath(target_position_cmp.CurrentPosition))
                {
                    locomotor_cmp.GetMovementProvider().FinishMovementWhenTargetInRange(target_position_cmp, m_range);
                }
            }
            else
            {
                List <Vector3FP> path = new List <Vector3FP>();
                path.Add(position_cmp.CurrentPosition);
                path.Add(target_position_cmp.CurrentPosition);
                locomotor_cmp.MoveAlongPath(path, false);
                locomotor_cmp.GetMovementProvider().FinishMovementWhenTargetInRange(target_position_cmp, m_range);
            }
        }
Exemple #2
0
        bool HandleEntityMove(EntityMoveCommand cmd)
        {
            Entity entity = m_logic_world.GetEntityManager().GetObject(cmd.m_entity_id);

            if (entity == null)
            {
                return(false);
            }
            LocomotorComponent locomotor_component = entity.GetComponent(LocomotorComponent.ID) as LocomotorComponent;

            if (locomotor_component == null)
            {
                return(false);
            }
            if (cmd.m_move_type != EntityMoveCommand.StopMoving)
            {
                TargetingComponent targeting_component = entity.GetComponent(TargetingComponent.ID) as TargetingComponent;
                if (targeting_component != null)
                {
                    targeting_component.StopTargeting();
                }
            }
            if (cmd.m_move_type == EntityMoveCommand.DestinationType)
            {
                PathFindingComponent pathfinding_component = entity.GetComponent(PathFindingComponent.ID) as PathFindingComponent;
                if (pathfinding_component != null)
                {
                    return(pathfinding_component.FindPath(cmd.m_vector));
                }
                else
                {
                    PositionComponent position_component = entity.GetComponent(PositionComponent.ID) as PositionComponent;
                    List <Vector3FP>  path = new List <Vector3FP>();
                    path.Add(position_component.CurrentPosition);
                    path.Add(cmd.m_vector);
                    locomotor_component.MoveAlongPath(path, true);
                }
            }
            else if (cmd.m_move_type == EntityMoveCommand.DirectionType)
            {
                locomotor_component.MoveByDirection(cmd.m_vector);
            }
            else if (cmd.m_move_type == EntityMoveCommand.StopMoving)
            {
                locomotor_component.StopMoving(true);
            }
            return(true);
        }
        public void OnTaskService(FixPoint delta_time)
        {
            PositionComponent     position_cmp  = ParentObject.GetComponent(PositionComponent.ID) as PositionComponent;
            LocomotorComponent    locomotor_cmp = ParentObject.GetComponent(LocomotorComponent.ID) as LocomotorComponent;
            SkillManagerComponent skill_cmp     = ParentObject.GetComponent(SkillManagerComponent.ID) as SkillManagerComponent;
            Skill skill = skill_cmp.GetSkill(m_skill_index);

            if (skill == null)
            {
                StopTargeting();
                return;
            }

            if (!skill.IsReady() && locomotor_cmp != null && locomotor_cmp.IsMoving)
            {
                FixPoint delay = skill.GetNextReadyTime();
                if (delay == FixPoint.Zero)
                {
                    delay = FixPoint.PrecisionFP;
                }
                ScheduleTargeting(delay);
                return;
            }

            bool              move_required       = false;
            FixPoint          max_range           = skill.GetDefinitionComponent().MaxRange;
            PositionComponent target_position_cmp = m_current_target.GetComponent(PositionComponent.ID) as PositionComponent;
            Vector3FP         direction           = target_position_cmp.CurrentPosition - position_cmp.CurrentPosition;

            if (max_range > 0)
            {
                FixPoint distance = direction.FastLength() - target_position_cmp.Radius - position_cmp.Radius;  //ZZWTODO 多处距离计算
                if (distance > max_range)
                {
                    move_required = true;
                    if (locomotor_cmp == null)
                    {
                        //ZZWTODO
                        ScheduleTargeting(TARGETING_UPDATE_MAX_FREQUENCY);
                    }
                    else if (!locomotor_cmp.IsEnable())
                    {
                        ScheduleTargeting(TARGETING_UPDATE_MIN_FREQUENCY);
                    }
                    else
                    {
                        FixPoint delay = (distance - max_range) / locomotor_cmp.MaxSpeed;
                        if (delay > TARGETING_UPDATE_MAX_FREQUENCY)
                        {
                            delay = TARGETING_UPDATE_MAX_FREQUENCY;
                        }
                        else if (delay < TARGETING_UPDATE_MIN_FREQUENCY)
                        {
                            delay = TARGETING_UPDATE_MIN_FREQUENCY;
                        }
                        PathFindingComponent pathfinding_component = ParentObject.GetComponent(PathFindingComponent.ID) as PathFindingComponent;
                        if (pathfinding_component != null)
                        {
                            if (pathfinding_component.FindPath(target_position_cmp.CurrentPosition))
                            {
                                locomotor_cmp.GetMovementProvider().FinishMovementWhenTargetInRange(target_position_cmp, max_range);
                            }
                        }
                        else
                        {
                            List <Vector3FP> path = new List <Vector3FP>();
                            path.Add(position_cmp.CurrentPosition);
                            path.Add(target_position_cmp.CurrentPosition);
                            locomotor_cmp.MoveAlongPath(path, false);
                            locomotor_cmp.GetMovementProvider().FinishMovementWhenTargetInRange(target_position_cmp, max_range);
                        }
                        ScheduleTargeting(delay);
                    }
                }
            }
            if (!move_required)
            {
                if (skill.CanActivate(false))
                {
                    position_cmp.SetFacing(direction);
                    skill.GetDefinitionComponent().ExternalID = m_current_target.ID;
                    skill.Activate(GetCurrentTime());
                    if (m_attack_once)
                    {
                        StopTargeting();
                        return;
                    }
                    FixPoint delay = skill.GetDefinitionComponent().CooldownTime + FixPoint.PrecisionFP;
                    if (delay > TARGETING_UPDATE_MAX_FREQUENCY)
                    {
                        delay = TARGETING_UPDATE_MAX_FREQUENCY;
                    }
                    ScheduleTargeting(delay);
                }
                else
                {
                    FixPoint delay = skill.GetNextReadyTime();
                    if (delay < TARGETING_UPDATE_MIN_FREQUENCY)
                    {
                        delay = TARGETING_UPDATE_MIN_FREQUENCY;
                    }
                    ScheduleTargeting(delay);
                }
            }
        }