坦克大战

1.初始化和实例化以及位置的产生。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapCreation : MonoBehaviour
{

//用来装饰初始化地图所需物体的数组。
//0.老家 1.墙 2.障碍 3.出生效果 4.河流 5.草 6.空气墙
public GameObject[] item;

//已经有东西的位置列表
private List<Vector3> itemPositionList = new List<Vector3>();



private void Awake()
{
    InitMap();
}
private void InitMap()
{
    //实例化老家
    CreateItem(item[0], new Vector3(0, -8, 0), Quaternion.identity);
    //用墙把老家围起来
    CreateItem(item[1], new Vector3(-1, -8, 0), Quaternion.identity);
    CreateItem(item[1], new Vector3(1, -8, 0), Quaternion.identity);
    for (int i = -1; i < 2; i++)
    {
        CreateItem(item[1], new Vector3(i, -7, 0), Quaternion.identity);
    }
    //实例化外围墙
    for (int i = -10; i < 11; i++) //上
    {
        CreateItem(item[6], new Vector3(i, 9, 0), Quaternion.identity);
    }
    for (int i = -11; i < 12; i++)  // 下
    {
        CreateItem(item[6], new Vector3(i, -9, 0), Quaternion.identity);
    }
    for (int i = -8; i < 9; i++)  //右
    {
        CreateItem(item[6], new Vector3(-11, i, 0), Quaternion.identity);
    }
    for (int i = -8; i < 9; i++)  //左
    {
        CreateItem(item[6], new Vector3(11, i, 0), Quaternion.identity);
    }

    //初始化玩家
    GameObject go = Instantiate(item[3], new Vector3(-2, -8, 0), Quaternion.identity);
    go.GetComponent<Born>().createPlayer = true;

    //产生敌人
    CreateItem(item[3], new Vector3(-10, 8, 0), Quaternion.identity);
    CreateItem(item[3], new Vector3(0, 8, 0), Quaternion.identity);
    CreateItem(item[3], new Vector3(10, 8, 0), Quaternion.identity);

    InvokeRepeating("CreateEnemy", 4, 5);

    //实例化地图
    for (int i = 0; i < 60; i++)
    {
        CreateItem(item[1], CreateRandomPosition(), Quaternion.identity);
    }
    for (int i = 0; i < 20; i++)
    {
        CreateItem(item[2], CreateRandomPosition(), Quaternion.identity);
    }
    for (int i = 0; i < 20; i++)
    {
        CreateItem(item[4], CreateRandomPosition(), Quaternion.identity);
    }
    for (int i = 0; i < 20; i++)
    {
        CreateItem(item[5], CreateRandomPosition(), Quaternion.identity);
    }
}

private void CreateItem(GameObject createCameObject,Vector3 createPosition,Quaternion createRotation)
{
    GameObject itemGO = Instantiate(createCameObject,createPosition,createRotation);
    itemGO.transform.SetParent(gameObject.transform);
    itemPositionList.Add(createPosition);

}
//产生随机位置的方法
private Vector3 CreateRandomPosition()
{
    //不生成X=-10,10的两列,y=-8.8正两行的位置
    while (true)
    {
        Vector3 createPosition = new Vector3(Random.Range(-9, 10), Random.Range(-7, 8), 0);
        if (!HasThePosition(createPosition))
        {
            return createPosition;
        }
        
    }
}

//用来判断位置列表中是否有这个位置

private bool HasThePosition(Vector3 createPos)
{
    for (int i = 0; i < itemPositionList.Count; i++)
    {
        if (createPos == itemPositionList[i])
        {
            return true;
        }
    }
    return false;
}


//产生敌人的方法
private void CreateEnemy()
{
    int num = Random.Range(0, 3);
    Vector3 EnemyPos = new Vector3();
    if (num == 0)
    {
        EnemyPos = new Vector3(-10, 8, 0);
    }
    else if (num == 1)
    {
        EnemyPos = new Vector3(0, 8, 0);
    }
    else
    {
        EnemyPos = new Vector3(10, 8, 0);
    }
    CreateItem(item[3], EnemyPos, Quaternion.identity);
}

}
2.坦克的移动,死亡,爆炸,攻击,子弹的cd,无敌状态的方法。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
//属性值
public float moveSpeed = 3;
private Vector3 bullectEulerAngles;
private float timeVal;
private float defendTimeVal=3;
private bool isDefended=true;

//引用
private SpriteRenderer sr;
public Sprite[] tankSprite;//上 右 下 左
public GameObject bullectPrefab;
public GameObject explosionPrefab;
public GameObject defendEffectPrefab;
public AudioSource moveAudio;
public AudioClip[] tankAudio;

private void Awake()
{
    sr = GetComponent<SpriteRenderer>();
}
// Start is called before the first frame update
void Start()
{
    
}

// Update is called once per frame
void Update()
{
    //是否处于无敌状态
    if (isDefended)
    {
        defendEffectPrefab.SetActive(true);
        defendTimeVal -= Time.deltaTime;
        if (defendTimeVal <= 0)
        {
            isDefended = false;
            defendEffectPrefab.SetActive(false);
        }
    }

    
}

private void FixedUpdate()
{
    if (PlayerManager.Instance.isDefeat)
    {
        return;
    }
    Move();
    //攻击的CD
    if (timeVal >= 0.4f)
    {
        Attack();
    }

    else
    {
        timeVal += Time.fixedDeltaTime;
    }

}
 
//坦克的攻击方法
private void Attack()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        //子弹产生的角度:当前坦克的角度+子弹应该旋转的角度
        Instantiate(bullectPrefab, transform.position, Quaternion.Euler(transform.eulerAngles + bullectEulerAngles));
        timeVal = 0;
    }
}



//坦克的移动方法
private void Move()
{
    float v = Input.GetAxisRaw("Vertical");
    transform.Translate(Vector3.up * v * moveSpeed * Time.fixedDeltaTime, Space.World);
    if (v < 0)
    {
        sr.sprite = tankSprite[2];
        bullectEulerAngles = new Vector3(0, 0, -180);
    }

    else if (v > 0)
    {
        sr.sprite = tankSprite[0];
       bullectEulerAngles = new Vector3(0, 0, 0);
    }

    if (Mathf.Abs(v) > 0.05f)
    {
        moveAudio.clip = tankAudio[1];
        
        if (!moveAudio.isPlaying)
        {
            moveAudio.Play();
        }
    }

    if (v != 0)
    {
        return;
    }

    float h = Input.GetAxisRaw("Horizontal");
    transform.Translate(Vector3.right * h * moveSpeed * Time.fixedDeltaTime, Space.World);
    if (h < 0)
    {
        sr.sprite = tankSprite[3];
        bullectEulerAngles = new Vector3(0, 0, 90);
    }

    else if (h > 0)
    {
        sr.sprite = tankSprite[1];
        bullectEulerAngles = new Vector3(0, 0, -90);
    }

    if (Mathf.Abs(h) > 0.05f)
    {
        moveAudio.clip = tankAudio[1];

        if (!moveAudio.isPlaying)
        {
            moveAudio.Play();
        }
    }
    else
    {
        moveAudio.clip = tankAudio[0];

        if (!moveAudio.isPlaying)
        {
            moveAudio.Play();
        }
    }
}

//坦克的死亡方法
private void Die()
    {

    if (isDefended)
    {
        return;
    }

    PlayerManager .Instance.isDead = true;

    //产生爆炸特效
    Instantiate(explosionPrefab, transform.position, transform.rotation);
    //死亡
    Destroy(gameObject);
}

}

敌方坦克的攻击时间间隔,攻击,移动,死亡,产生爆炸特效的方法。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour
{
//属性值
public float moveSpeed = 3;
private Vector3 bullectEulerAngles;
private float v=-1;
private float h;

//引用
private SpriteRenderer sr;
public Sprite[] tankSprite;//上 右 下 左
public GameObject bullectPrefab;
public GameObject explosionPrefab;


//计时器
private float timeVal;
private float timeValChangeDirection=0;

private void Awake()
{
    sr = GetComponent<SpriteRenderer>();
}
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
   
    //攻击的时间间隔
    if (timeVal >= 3)
    {
        Attack();
    }

    else
    {
        timeVal += Time.deltaTime;
    }

}

private void FixedUpdate()
{
    Move();


}

//坦克的攻击方法
private void Attack()
{
   
    
        //子弹产生的角度:当前坦克的角度+子弹应该旋转的角度
        Instantiate(bullectPrefab, transform.position, Quaternion.Euler(transform.eulerAngles + bullectEulerAngles));
        timeVal = 0;
    
}



//坦克的移动方法
private void Move()
{
    if (timeValChangeDirection>=4)
    {
        int num = Random.Range(0, 8);
        if (num > 5)
        {
            v = -1;
            h = 0;

        }
        else if (num==0)
        {
            v = 1;
            h = 0;
        }
        else if (num > 0 && num <= 2)
        {
            h = -1;
            v = 0;
        }
        else if (num > 2 && num <= 4)
        {
            h = 1;
            v = 0;
        }

        timeValChangeDirection = 0;
    }
    else
    {
        timeValChangeDirection += Time.fixedDeltaTime;
    }
 


    transform.Translate(Vector3.up * v * moveSpeed * Time.fixedDeltaTime, Space.World);
    if (v < 0)
    {
        sr.sprite = tankSprite[2];
        bullectEulerAngles = new Vector3(0, 0, -180);
    }

    else if (v > 0)
    {
        sr.sprite = tankSprite[0];
        bullectEulerAngles = new Vector3(0, 0, 0);
    }

    if (v != 0)
    {
        return;
    }

 
    transform.Translate(Vector3.right * h * moveSpeed * Time.fixedDeltaTime, Space.World);
    if (h < 0)
    {
        sr.sprite = tankSprite[3];
        bullectEulerAngles = new Vector3(0, 0, 90);
    }

    else if (h > 0)
    {
        sr.sprite = tankSprite[1];
        bullectEulerAngles = new Vector3(0, 0, -90);
    }
}

//坦克的死亡方法
private void Die()
{
    PlayerManager.Instance.playerScore++; 


    //产生爆炸特效
    Instantiate(explosionPrefab, transform.position, transform.rotation);
    //死亡
    Destroy(gameObject);
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.tag=="Enemy")
    {
        timeValChangeDirection = 4;
    }
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值