Exemplo n.º 1
0
    public GameObject GetTarget()
    {
        _possibleTargets = new List <Killable> ();

        foreach (Killable enemyObj in _enemyInRadius)
        {
            RaycastHit hit;

            if (Physics.Raycast(t.position, enemyObj.transform.position - t.position, out hit, 10000))
            {
                k = hit.transform.GetComponent <Killable> ();

                if (k != null && k == enemyObj)
                {
                    if (FactionRelationshipManager.IsHostile(turret._faction, enemyObj._faction))
                    {
                        _possibleTargets.Add(enemyObj);
                    }
                }
                //		print (hit.transform.name);
            }

            //	Debug.DrawRay (t.position , enemyObj.transform.position - t.position,Color.yellow);
        }

        if (_possibleTargets.Count > 0)
        {
            return(_possibleTargets[Random.Range(0, _possibleTargets.Count)].gameObject);
        }
        //_enemyInRadius = new List<Killable> ();
        return(null);
    }
Exemplo n.º 2
0
    void OnTriggerEnter(Collider collider)
    {
        if (_timeCount < .04f)
        {
            return;
        }

        Killable killable = collider.GetComponent <Killable> ();

        if (killable != null)
        {
            // If friendly fire, ignore
            if (!FactionRelationshipManager.IsHostile(_Owner._faction, killable._faction))
            {
                return;
            }
            //Create a hit spark particle and activate it
            HitSparks hs = ObjectPool.instance.GetHitSpark();
            if (hs != null)
            {
                hs.transform.position = t.position;
                hs.Live();
                //Rotate spark
                hs.transform.LookAt(_initPos);
            }

            //Cause damage
            killable.OnHit(_damage, _Owner);

            Destroy();
        }
    }
Exemplo n.º 3
0
    // Update is called once per frame
    void Update()
    {
        _enemyInRadius = GameObject.FindObjectsOfType <OffScreenTracking> ();

        for (int i = 0; i < _enemyInRadius.Length; i++)
        {
            // If no more indicator to accomodate
            if (i > _indicators.Length - 1)
            {
                return;
            }

            if (FactionRelationshipManager.IsHostile(Faction.PLAYER, _enemyInRadius[i]._faction))
            {
                _indicators [i].Track(_enemyInRadius [i].transform);
            }
        }
    }
Exemplo n.º 4
0
    private void GetNearbyTarget()
    {
        // init target list
        possibleTargets = new List <Killable>();

        GameObject[] gos = GameObject.FindGameObjectsWithTag("Fighter");

        foreach (GameObject go in gos)
        {
            Killable killable = go.GetComponent <Killable> ();
            float    distance = Vector3.Distance(killable.transform.position, transform.position);
            if (killable != null && distance < 1000)
            {
                if (FactionRelationshipManager.IsHostile(killable._faction, _faction))
                {
                    possibleTargets.Add(killable);
                }
            }
        }

        //If no possible targets found, go to battle center
        if (possibleTargets.Count == 0)
        {
            if (currentWaypoint == null)
            {
                //targetTransform = battleCenter.transform;
                targetNavWaypoint = battleCenter.navWaypoints;
                GetNextTarget();
            }
            return;
        }

        // Randomize the targets
        target = possibleTargets[Random.Range(0, possibleTargets.Count - 1)];

        if (target != null)
        {
            targetTransform   = target.transform;
            targetNavWaypoint = target.navWaypoints;

            // set first target
            GetNextTarget();
        }
    }
Exemplo n.º 5
0
    public void OnHit(float damage, Killable attacker)
    {
        // If friendly fire, ignore
        if (!FactionRelationshipManager.IsHostile(_faction, attacker._faction))
        {
            return;
        }

        //In case resistance value is invalid
        if (_resistance > 100 || _resistance < 0)
        {
            print("Resistance value invalid!");
            _resistance = 0;
        }

        // Decrease health
        _hitPoints -= (damage - (damage * _resistance) / 100);

        if (_hitPoints <= 0)
        {
            Killed();

            if (attacker._faction == Faction.PLAYER)
            {
                PlayerScore.instance.OnKilledEnemy();
            }

            _hitPoints = 100;
        }
        else
        {
            if (attacker._faction == Faction.PLAYER)
            {
                PlayerScore.instance.OnHitEnemy();
            }
        }
        if (OnHitEvent != null)
        {
            OnHitEvent();
        }

        OnHitAdditional();
    }
Exemplo n.º 6
0
    // Update is called once per frame
    void FixedUpdate()
    {
        // If enemy, show targeting
        if (ai != null)
        {
            if (!FactionRelationshipManager.IsHostile(ai._faction, Faction.PLAYER))
            {
                t.localScale = Vector3.zero;
                return;
            }
        }

        t.LookAt(transform.position + cameraT.rotation * Vector3.forward,
                 cameraT.rotation * Vector3.up);
//		print (Vector3.Distance(t.position,cameraT.position));

        _distance = Vector3.Distance(t.position, cameraT.position);

        if (special)
        {
            if (_distance < 3000f)
            {
                t.localScale = Vector3.zero;
            }
            else
            {
                t.localScale = Vector3.one;
            }
        }

        //if (_distance > 110f) {

        //	_scale = _distance / testNum;
        //t.localScale = new Vector3 (_scale, _scale, _scale);

        //} //else {
        //_scale = 1;
        //t.localScale = new Vector3 (_scale, _scale, _scale);
        //}
    }
Exemplo n.º 7
0
    void Update()
    {
        if (Disable)
        {
            flight.enabled = false;
            return;
        }


        if (!flight)
        {
            return;
        }


        if (CenterOfBattle)
        {
            // CenterOfBattle is exist , BattlePosition = CenterOfBattle position
            //in case of able you to changing the battle area
            BattlePosition = CenterOfBattle.gameObject.transform.position;
        }

        // calculate target behavior.
        TargetBehaviorCal();

        // AI state machine
        switch (AIstate)
        {
        case AIState.Patrol:        // AI will free flying and looking for a target.

            // Find only gameobject by Tag within TargetTag[]

            // get all objects as same tag as TargetTag[i]
            objs = new List <Killable>();

            GameObject[] gos = GameObject.FindGameObjectsWithTag("Fighter");

            foreach (GameObject go in gos)
            {
                Killable killable = go.GetComponent <Killable> ();
                if (killable != null)
                {
                    if (FactionRelationshipManager.IsHostile(killable._faction, plane_ai._faction))
                    {
                        objs.Add(killable);
                    }
                }
            }


            float distance = int.MaxValue;
            for (int i = 0; i < objs.Count; i++)
            {
                if (objs [i])
                {
                    // make it delay by TimeToLock
                    if (timetolockcount + TimeToLock < Time.time)
                    {
                        float dis = Vector3.Distance(objs [i].transform.position, transform.position);
                        // selected closer target
                        if (DistanceLock > dis)
                        {
                            if (!Target)
                            {
                                // i random a bit because i want this AI look hesitate. so you can remove "Random.Range (0, 100) > 80" if you want this AI select every target.
                                if (distance > dis && Random.Range(0, 100) > 80)
                                {
                                    distance            = dis;
                                    Target              = objs [i].gameObject;
                                    flight.FollowTarget = true;
                                    // go to Attacking state
                                    AIstate = AIState.Attacking;
                                    // save current time
                                    timestatetemp = Time.time;
                                    // random weapon from the flight.WeaponControl.WeaponLists
                                    WeaponSelected = Random.Range(0, flight.WeaponControl.WeaponLists.Length);
                                }
                            }
                        }
                    }
                    // shooting..
                    //
                    shootTarget(objs [i].transform.position);
                }
            }


            break;

        case AIState.Idle:        // Free state
            // free fly and checking the AI is in of Battle Area
            //	if (Vector3.Distance (flight.PositionTarget, this.transform.position) <= FlyDistance) {
            // go back to Patrol state
            AIstate       = AIState.Patrol;
            timestatetemp = Time.time;
            //}

            break;

        case AIState.Attacking:        // Attacking state
            if (Target)
            {
                // if target is exist , Position target = target position
                flight.PositionTarget = Target.transform.position;
                // shoot the target..
                if (!shootTarget(flight.PositionTarget))
                {
                    if (attacking)
                    {
                        // if in attacking but cannot shoot anymore turn back in 5 sec
                        if (Time.time > timestatetemp + 5)
                        {
                            turnPosition();
                        }
                    }
                    else
                    {
                        // if no attacking turn back in 7 sec
                        if (Time.time > timestatetemp + 7)
                        {
                            turnPosition();
                        }
                    }
                }
            }
            else
            {
                // if target is Destroyed go back to Patrol state
                AIstate = AIState.Patrol;
                // save current time
                timestatetemp = Time.time;
            }
            // if going out of the Battle Area. Go back to the center (BattlePosition)
            if (Vector3.Distance(BattlePosition, this.transform.position) > FlyDistance)
            {
                gotoCenter();
            }
            break;

        case AIState.TurnPosition:        // Turn back state
            if (Time.time > timestatetemp + 7)
            {
                //try to turn to attacking again after 7 sec in case of Target still alive but you just turning out of shooting range
                timestatetemp = Time.time;
                AIstate       = AIState.Attacking;
            }
            // if going out of the Battle Area. Go back to the center (BattlePosition)
            if (Vector3.Distance(BattlePosition, this.transform.position) > FlyDistance)
            {
                gotoCenter();
            }

            // current target height Y
            float height = flight.PositionTarget.y;
            if (targetHavior == TargetBehavior.Static)              // if static target
            {
                directionTurn.y = 0;
                // calculate Position of bullet Drop and speed. try to make it accuracy
                flight.PositionTarget   += (this.transform.forward + directionTurn) * flight.Speed;
                flight.PositionTarget.y  = height;
                flight.PositionTarget.y += flight.Speed / 2;
            }
            else
            {
                // calculate Position of bullet Drop and speed. try to make it accuracy
                flight.PositionTarget   += (this.transform.forward + directionTurn) * flight.Speed;
                flight.PositionTarget.y  = height;
                flight.PositionTarget.y += flight.Speed / 2;
                //flight.PositionTarget = BattlePosition + new Vector3 (Random.Range (-FlyDistance, FlyDistance), Random.Range (0, FlyDistance / 2), Random.Range (-FlyDistance, FlyDistance));
            }
            break;
        }
    }