コード例 #1
0
    public ITargettable GetTargetFromSide(float deg)
    {
        Vector3 direction = Quaternion.AngleAxis(deg, Vector3.up) * transform.forward;

        if (direction.y < 0)
        {
            direction.y = 0;
        }

        RaycastHit[] hits = Physics.SphereCastAll(target.position + direction * lockWidthOfSight, lockWidthOfSight, direction, lockSight, ~LayerMask.GetMask(sightIgnoredLayers));

        if (hits != null && hits.Length > 0)
        {
            foreach (var hit in hits)
            {
                ITargettable targettable = hit.collider.GetComponent <ITargettable>();
                if (targettable != null)
                {
                    targettable.OnTargetFocused();
                    return(targettable);
                }
            }
        }
        return(null);
    }
コード例 #2
0
    public static ITargettable FindClosestTargetInList(List <ITargettable> objects, GameObject agent)
    {
        ITargettable closest = null;

        if (objects.Count > 0)
        {
            float dist = 5;//starting distance to search through
            while (closest == null && dist < maxDistance)
            {
                // if (dist >= 100) {Debug.Log(dist);}
                foreach (ITargettable b in objects)
                {
                    //Debug.Log(b.TargetBy);
                    if (b.GetOwner() == null || b.GetOwner() == agent)
                    {
                        float thisDist = GetDist(b.gameObj, agent);
                        if (thisDist < dist)
                        {
                            closest = b;
                        }
                    }
                }
                dist += 10;
            }
        }
        return(closest);
    }
コード例 #3
0
    public override float GetTargetScore(ITargettable other)
    {
        float value = 0f;

        hatedTypes.TryGetValue(((Animal)other).type, out value);
        return(base.GetTargetScore(other) * hatedTypes[((Animal)other).type] * 10f);
    }
コード例 #4
0
    /// <summary>
    /// Lock camera on a single target. Disable all controls.
    /// </summary>
    public void Lock()
    {
        ITargettable target = GetTargetCameraBased();

        if (target != null)
        {
            (states["Lock"] as CameraLockState).focusedTarget = target;
            Switch("Lock");
        }
    }
コード例 #5
0
 protected void Die()
 {
     AnimalMap.animalMap.animals.Remove(this);
     if (herd != null)
     {
         herd.RemoveMember(this);
     }
     target = null;
     Destroy(gameObject);
 }
コード例 #6
0
    public ITargettable GetBestTarget()
    {
        IListConverter <ITargettable> allTargets = GetAllPotentialTargets();

        ITargettable currentBest = null;
        float        bestValue   = 0f;

        for (int i = 0; i < allTargets.Count; i++)
        {
            if (IsTargettable(allTargets[i]))
            {
                float value = GetTargetScore(allTargets[i]);
                if (value > bestValue)
                {
                    currentBest = allTargets[i];
                    bestValue   = value;
                }
            }
        }

        return(currentBest);

        //List<Carrot> carrots = AnimalMap.animalMap.carrots;

        /*IListConverter<ITargettable> potentialFood = null;
         * if (!target.predator)
         * {
         *  potentialFood = new ListConverter<Carrot, ITargettable>(AnimalMap.animalMap.carrots);
         * }
         * else
         * {
         *  potentialFood = new ListConverter<Animal, ITargettable>(AnimalMap.animalMap.animals);
         * }
         *
         *
         * ITargettable closestCarrot = null;
         * float shortestDistance = float.MaxValue;
         *
         * for (int i = 0; i < potentialFood.Count; i++)
         * {
         *  if (!potentialFood[i].CanBeTargetted(target))
         *  {
         *      continue;
         *  }
         *
         *  float distance = (potentialFood[i].GetPosition() - target.transform.position).sqrMagnitude;
         *  if (distance < shortestDistance)
         *  {
         *      shortestDistance = distance;
         *      closestCarrot = potentialFood[i];
         *  }
         * }
         *
         * return closestCarrot;*/
    }
コード例 #7
0
 public bool CanEat(Animal target, ITargettable victim)
 {
     if (target.predator)
     {
         return(victim is Animal);
     }
     else
     {
         return(victim is Carrot);
     }
 }
コード例 #8
0
    public override bool IsTargettable(ITargettable other)
    {
        Animal animal = other as Animal;

        if (animal == null || animal.type == AnimalTypes.Bunny)
        {
            return(base.IsTargettable(other));
        }
        else
        {
            return(false);
        }
    }
コード例 #9
0
    public bool CanBreed(ITargettable other)
    {
        Animal animal = other as Animal;

        if (animal != null)
        {
            return(animal.type == target.type);
        }
        else
        {
            return(false);
        }
    }
コード例 #10
0
    public virtual float GetTargetScore(ITargettable other)
    {
        float dist = (other.GetPosition() - target.transform.position).magnitude;

        if (dist > 0)
        {
            return(((maxRange - dist) / maxRange) * GetFearInfluence(other));
        }
        else
        {
            return(GetFearInfluence(other));
        }
    }
コード例 #11
0
 protected override BehaviourState RunStart()
 {
     food = GetBestTarget();
     if (food == null)
     {
         return(BehaviourState.Running);
     }
     else
     {
         target.target = food;
         return(BehaviourState.Completed);
     }
 }
コード例 #12
0
    private float GetFearInfluence(ITargettable other)
    {
        // Based upon the proximity of this target to things we fear, pick another target.
        // fear should be overridden by desperation?
        // how should this be represented?
        // 1f = no fear modifier.
        float fear = 1f - AnimalMap.animalMap.GetFearOfPosition(target.type, other.GetPosition());

        if (fear < GetDesperation())
        {
            return(1f);
        }
        else
        {
            return(fear);
        }
    }
コード例 #13
0
 protected override BehaviourState RunUpdate()
 {
     if (food != null)
     {
         return(BehaviourState.Completed);
     }
     else
     {
         food = GetClosestSameSpecies();
         if (food != null)
         {
             target.target = food;
             return(BehaviourState.Completed);
         }
         else
         {
             return(BehaviourState.Running);
         }
     }
 }
コード例 #14
0
    protected override GameObject FindClosestObjectOfLayer(Creature agent)
    {
        GameObject   target  = null;
        ITargettable itarget = null;

        if (ActionLayer == 6)
        {
            //need to use this thing to search for closest + check ownership of item. its kinda messy but works
            itarget = Tools.FindClosestTargetInList(Tools.ConvertToITargettable(manager.spawner.ActiveBushes), agent.gameObject);
            if (itarget != null)
            {
                target = itarget.gameObj;
            }
        }
        if (ActionLayer == 8)
        {
            itarget = Tools.FindClosestTargetInList(Tools.ConvertToITargettable(manager.spawner.ActiveMushrooms), agent.gameObject);
            if (itarget != null)
            {
                target = itarget.gameObj;
            }
        }
        if (ActionLayer == 11)
        {
            target = Tools.FindClosestObjectInList(manager.spawner.ActiveEnemies, agent.gameObject);
        }
        if (ActionLayer == 12)
        {
            target = Tools.FindClosestObjectInList(manager.spawner.ActiveBuddies, agent.gameObject);
        }
        if (ActionLayer == 13) //player
        {
            target = player;
        }
        if (ActionLayer == 14) //cow
        {
            target = cow;
        }
        return(target);
    }
コード例 #15
0
    protected override GameObject FindClosestObjectOfLayer(Creature agent)
    {
        GameObject   target  = null;
        ITargettable itarget = null;

        if (ActionLayer == 7)
        {
            itarget = Tools.FindClosestTargetInList(Tools.ConvertToITargettable(manager.spawner.ActiveBerries), agent.gameObject);
            if (itarget != null)
            {
                target = itarget.gameObj;
            }
        }
        if (ActionLayer == 9)
        {
            itarget = Tools.FindClosestTargetInList(Tools.ConvertToITargettable(manager.spawner.ActiveFungus), agent.gameObject);
            if (itarget != null)
            {
                target = itarget.gameObj;
            }
        }
        if (ActionLayer == 10)
        {
            itarget = Tools.FindClosestTargetInList(Tools.ConvertToITargettable(manager.spawner.ActiveBerryPoop), agent.gameObject);
            if (itarget != null)
            {
                target = itarget.gameObj;
            }
        }
        if (ActionLayer == 16)
        {
            itarget = Tools.FindClosestTargetInList(Tools.ConvertToITargettable(manager.spawner.ActiveFungusPoop), agent.gameObject);
            if (itarget != null)
            {
                target = itarget.gameObj;
            }
        }
        return(target);
    }
コード例 #16
0
ファイル: Player.cs プロジェクト: Buya-Games/MimicA-GOAP
    //triggered when pressing space bar (via PlayerControl)
    public void Interact()
    {
        if (HeldItem != null)
        {
            ThrowItem();
        }
        else
        {
            Collider[] nearbyObjects = Physics.OverlapSphere(transform.position, 2, interactLM);//check surroundings for stuff
            if (nearbyObjects.Length > 0)
            {
                GameObject closest = Tools.FindClosestColliderInGroup(nearbyObjects, transform.position);
                if (closest != null)
                {
                    Target = closest;

                    ITargettable oldTarget = Target.GetComponent <ITargettable>();
                    if (oldTarget != null)
                    {
                        oldTarget.NotTargeted();
                    }
                    //SetTarget(closest);
                }
                //Target = nearbyObjects[0].gameObject;//i only use the first object of an array which I believe is random, but that's fine I think?
                MeleeAttack();
            }
            else
            {
                if (bobHeight < 1.3f)
                {
                    Swing(2);//2 will show a red trail streak to indicate pw0ness
                }
                else
                {
                    Swing();
                }
            }
        }
    }
コード例 #17
0
    public ITargettable GetClosestSameSpecies()
    {
        List <Animal> potentialMates = AnimalMap.animalMap.animals;

        ITargettable closestBreeder   = null;
        float        shortestDistance = float.MaxValue;

        for (int i = 0; i < potentialMates.Count; i++)
        {
            if (potentialMates[i].type == target.type && potentialMates[i] != target)
            {
                continue;
            }

            float distance = (potentialMates[i].GetPosition() - target.transform.position).sqrMagnitude;
            if (distance < shortestDistance)
            {
                shortestDistance = distance;
                closestBreeder   = potentialMates[i];
            }
        }

        return(closestBreeder);
    }
コード例 #18
0
ファイル: Creature.cs プロジェクト: Buya-Games/MimicA-GOAP
    //used to target (set "ownership") of items so multiple agents don't go for same thing
    public void SetTarget(GameObject targetedObj)
    {
        if (!enemy)
        {
            if (Target != null)
            {
                ITargettable oldTarget = Target.GetComponent <ITargettable>();
                if (oldTarget != null)
                {
                    oldTarget.NotTargeted();
                }
            }
        }

        Target = targetedObj;
        if (!enemy)
        {
            ITargettable newTarget = Target.GetComponent <ITargettable>();
            if (newTarget != null)
            {
                newTarget.Targeted(gameObject);
            }
        }
    }
コード例 #19
0
    public void Update()
    {
        //if is too far away return in default state
        if (Vector3.Distance(focusedTarget.Transform.position, Transform.position) > LockMaxDistanceFromTarget)
        {
            focusedTarget.OnTargetLosed();
            ActionCamera.Unlock();
            return;
        }

        Vector3 newFor = (focusedTarget.Transform.position - Transform.position).normalized;

        Transform.rotation = Quaternion.LookRotation(Vector3.Lerp(Transform.forward, newFor, Time.deltaTime * 8f));


        Vector3 position = Transform.rotation * new Vector3(0, LockHeight, LockDistance) + Target.position;

        if (Repositioning.active)
        {
            RaycastHit hit;
            if (Physics.Linecast(Target.position, position, out hit, ~(LayerMask.GetMask(Repositioning.ignoredLayers))))
            {
                newDistance = -hit.distance;
                Camera.main.nearClipPlane = Repositioning.onRepositionNearClipPlane;
            }
            else
            {
                newDistance = Mathf.Lerp(newDistance, LockDistance, LockLerpSpeed * Time.deltaTime);
                Camera.main.nearClipPlane = Repositioning.onNormalNearClipPlane;
            }
        }
        position           = Transform.rotation * new Vector3(0, LockHeight, newDistance) + Target.position;
        position.y         = LockHeight;
        Transform.position = Vector3.Lerp(Transform.position, position, Time.deltaTime * 10f);

        if (!ActionCamera.ManageInputFromOtherScript)
        {
            //must be removed, use ActionCamera methods for switching states.
            if (Input.GetKeyDown(KeyCode.JoystickButton0))
            {
                focusedTarget.OnTargetReleased();
                ActionCamera.Unlock();
            }
        }

        if (Input.GetKeyDown(KeyCode.Z))
        {
            ITargettable t = ActionCamera.GetTargetFromSide(-30);
            if (t != null)
            {
                focusedTarget = t;
            }
        }
        else if (Input.GetKeyDown(KeyCode.X))
        {
            ITargettable t = ActionCamera.GetTargetFromSide(30);
            if (t != null)
            {
                focusedTarget = t;
            }
        }
    }
コード例 #20
0
 public void Exit()
 {
     focusedTarget = null;
 }
コード例 #21
0
 public virtual bool IsTargettable(ITargettable other)
 {
     return(other.CanBeTargetted(target));
 }
コード例 #22
0
 public override bool IsTargettable(ITargettable other)
 {
     return(base.IsTargettable(other) && hatedTypes.ContainsKey(((Animal)other).type) && hatedTypes[((Animal)other).type] > 0);
 }
コード例 #23
0
 protected override void Awake()
 {
     base.Awake();
     target  = GameObject.FindGameObjectWithTag("Player").GetComponent <ITargettable>();
     enemies = new List <Enemy>();
 }