コード例 #1
0
ファイル: Battle.cs プロジェクト: 16110506066/OnlineTankWar
    //生成
    public void StartTwoCampBattle(int n1, int n2)
    {
        Transform sp      = GameObject.Find("SwopPoints").transform;
        Transform spCamp1 = sp.GetChild(0);
        Transform spCamp2 = sp.GetChild(1);

        if (spCamp1.childCount < n1 || spCamp2.childCount < n2)
        {
            Debug.LogError("出生点数量不够");
            return;
        }
        if (tankPerfabs.Length < 2)
        {
            Debug.LogError("坦克预设数量不够");
            return;
        }
        ClearBattle();
        battleTanks = new BattleTank[n1 + n2];
        for (int i = 0; i < n1; i++)
        {
            generateTank(1, i, spCamp1, i);
        }
        for (int i = 0; i < n2; i++)
        {
            generateTank(2, i, spCamp2, n1 + i);
        }
        tankcontrol tankCmp = battleTanks[0].tank;

        tankCmp.ctrlType = tankcontrol.CtrlType.player;
        CameraFollow cf     = Camera.main.gameObject.GetComponent <CameraFollow>();
        GameObject   target = tankCmp.gameObject;

        cf.SetTarget(target);
    }
コード例 #2
0
    public void NetBeAttacked(float att, GameObject attackTank)
    {
        Debug.LogError("123213");
        if (Hp <= 0)
        {
            return;
        }
        if (Hp > 0)
        {
            Hp -= att;
        }
        if (Hp <= 0)
        {
            ctrlType = CtrlType.none;
            GameObject destoryObj = (GameObject)Instantiate(destoryEffect);
            destoryObj.transform.SetParent(transform, false);
            destoryObj.transform.localPosition = Vector3.zero;

            //播放击杀提示
            if (attackTank != null)
            {
                tankcontrol tankcmp = attackTank.GetComponent <tankcontrol>();
                if (tankcmp != null && tankcmp.ctrlType == CtrlType.player)
                {
                    tankcmp.startDrawKill();
                }
            }
        }
    }
コード例 #3
0
 //击中目标
 public void BeAttacked(float att, GameObject attackTank)
 {
     if (Hp <= 0)
     {
         return;
     }
     Hp -= att;
     if (ai != null)
     {
         ai.OnAttecked(attackTank);
     }
     if (Hp <= 0)
     {
         Battle.instance.IsWin(attackTank);
         Debug.Log(attackTank);
         GameObject destoryObj = (GameObject)Instantiate(destoryEffect);
         destoryObj.transform.SetParent(transform, false);
         destoryObj.transform.localPosition = Vector3.zero;
         ctrlType = CtrlType.none;
         if (attackTank != null)
         {
             tankcontrol Tankcmp = attackTank.GetComponent <tankcontrol>();
             if (Tankcmp != null && Tankcmp.ctrlType == CtrlType.player)
             {
                 Tankcmp.startDrawKill();
             }
         }
     }
 }
コード例 #4
0
    private void OnCollisionEnter(Collision collision)
    {
        tankcontrol tank = collision.gameObject.GetComponent <tankcontrol>();

        if (collision.gameObject == tank)
        {
            return;
        }
        GameObject  explodeObj = (GameObject)Instantiate(explode, transform.position, transform.rotation);
        AudioSource audio      = explodeObj.AddComponent <AudioSource>();

        audio.spatialBlend = 1;
        audio.PlayOneShot(explodeClip);
        Destroy(gameObject);
        if (tank != null)
        {
            float att = GetAttc();
            tank.BeAttacked(att, attactTank);
        }
        tankcontrol tankcmp = collision.gameObject.GetComponent <tankcontrol>();

        if (tankcmp != null && attactTank.name == GameMgr.instance.id)
        {
            float att = GetAttc();
            tankcmp.SendHit(tankcmp.name, att);
        }
    }
コード例 #5
0
ファイル: Battle.cs プロジェクト: 16110506066/OnlineTankWar
    //生成场景
    private void generateTank(int camp, int num, Transform spCamp, int index)
    {
        //throw new NotImplementedException();
        Transform   trans   = spCamp.GetChild(num);
        Vector3     pos     = trans.position;
        Quaternion  rot     = trans.rotation;
        GameObject  perfab  = tankPerfabs[camp - 1];
        GameObject  tankObj = (GameObject)Instantiate(perfab, pos, rot);
        tankcontrol tankCmp = tankObj.GetComponent <tankcontrol>();

        battleTanks[index]      = new BattleTank();
        battleTanks[index].tank = tankCmp;
        battleTanks[index].camp = camp;
    }
コード例 #6
0
    //有目标,判断目标丢失
    void HasTarget()
    {
        tankcontrol targetTank = target.GetComponent <tankcontrol>();
        Vector3     pos        = transform.position;
        Vector3     targetPos  = target.transform.position;

        if (targetTank.ctrlType == tankcontrol.CtrlType.none)
        {
            Debug.Log("目标死亡,丢失目标");
            target = null;
        }
        else if (Vector3.Distance(pos, targetPos) > sightDistance)
        {
            Debug.Log("距离过远,丢失目标");
            target = null;
        }
    }
コード例 #7
0
    //没有目标的情况,搜索视野中的坦克
    private void Notarget()
    {
        //throw new NotImplementedException();
        float minHp = float.MaxValue;

        GameObject[] targets = GameObject.FindGameObjectsWithTag("Tankk");
        for (int i = 0; i < targets.Length; i++)
        {
            tankcontrol tank = targets[i].GetComponent <tankcontrol>();
            if (tank == null)
            {
                continue;
            }

            if (targets[i] == gameObject)
            {
                continue;
            }
            if (Battle.instance.IsSameCamp(targets[i], gameObject))
            {
                continue;
            }
            if (tank.ctrlType == tankcontrol.CtrlType.none)
            {
                continue;
            }
            Vector3 pos       = transform.position;
            Vector3 targetpos = targets[i].transform.position;
            if (Vector3.Distance(pos, targetpos) > sightDistance)
            {
                continue;
            }
            if (minHp > tank.Hp)
            {
                target = tank.gameObject;
                minHp  = tank.Hp;
            }
        }
        if (target != null)
        {
            Debug.Log("获取目标" + target.name);
        }
    }