BomberMan

Unity3D BomberMan

1.Done_BoardManager

using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 地图管理器,负责墙体的生成和销毁,道具的放置等
/// </summary>
public class Done_BoardManager : MonoBehaviour
{
    /// <summary>
    /// 用于管理可炸毁墙体数量
    /// </summary>
    public class Count
    {
        public int minimum;
        public int maximum;

        public Count(int min, int max)
        {
            minimum = min;
            maximum = max;
        }
    }

    //声明各种地图内的预制体                               
    public GameObject wallTile;

    public GameObject metalTile;

    //敌人预制体
    public GameObject wormPrefab;
    //public GameObject doorTile;
    //public GameObject badgeTile;

    // 数量参数,地图的行列数以及墙体的数量范围,敌人的数量
    public int columns { get; private set; }
    public int rows { get; private set; }
    public Count wallCount = new Count(30, 40);
    public Count wormCount = new Count(3, 5);

    // 管理用参数
    public List<Done_Wall> wallList = new List<Done_Wall>(); // Wall类集合
    public List<Done_Metal> metalList = new List<Done_Metal>(); // Metal类集合

    private List<Vector3> gridPositions = new List<Vector3>(); // 可用的格子的集合
    //public Door door;                                           // 门
    //public Badge badge;                                         // 徽章

    private Transform boardHolder; // 墙体的父Transform

    void InitialiseList()
    {
        gridPositions.Clear();
        for (int x = 0; x < columns; x++)
        {
            for (int y = 0; y < rows; y++)
            {
                // 为主角留一个出生位置
                // □□■■■■
                // □■■■■■
                // ■■■■■■
                if ((x == 0 & y == rows - 1) | (x == 0 & y == rows - 2) | (x == 1 & y == rows - 1))
                {
                    continue;
                }

                // 找出所有的可用的格子存入gridPositions
                //在炸弹人游戏中,偶数行和偶数列的格子是可以用于可炸毁墙体的生成和怪物以及人的行走的。其与墙体均不可走。
                if (x % 2 == 0 || y % 2 == 0)
                {
                    gridPositions.Add(new Vector3(x, y, 0f));
                }
            }
        }
    }

    /// <summary>
    /// 给游戏创建Metal外墙(边界)和内墙
    /// 在炸弹人游戏中,除整个地图被外墙包围以外,地图中x,y均为奇数的格子也会生成不可炸毁的墙体
    /// </summary>
    void BoardSetup()
    {
        columns = 18;
        rows = 7;
        metalList.Clear();
        boardHolder = new GameObject("Board").transform;

        for (int x = -1; x < columns + 1; x++)
        {
            for (int y = -1; y < rows + 1; y++)
            {
                GameObject toInstantiate = null;
                if ((x % 2 == 1 & y % 2 == 1) | x == -1 | x == columns | y == -1 | y == rows)
                {
                    toInstantiate = metalTile;
                }

                if (toInstantiate)
                {
                    GameObject instance = Instantiate(toInstantiate, new Vector3(x, y, 0f), Quaternion.identity);
                    instance.transform.SetParent(boardHolder);
                    metalList.Add(instance.GetComponent<Done_Metal>());
                }
            }
        }
    }

    /// <summary>
    /// 从我们的gridPositions集合中返回一个随机位置
    /// </summary>
    /// <returns></returns>
    public Vector3 RandomPosition()
    {
        if (gridPositions.Count == 0)
        {
            Debug.Log("可用的格子不够了");
        }

        int randomIndex = Random.Range(0, gridPositions.Count);
        Vector3 randomPosition = gridPositions[randomIndex];
        gridPositions.RemoveAt(randomIndex);
        return randomPosition;
    }


    /// <summary>
    ///  创建minimum到maximum数之间个数的可炸毁墙体wall
    ///  wall的生成范围在GridPositions列表中
    /// </summary>
    /// <param name="minimum"></param>
    /// <param name="maximum"></param>
    void LayoutWallAtRandom(int minimum, int maximum)
    {
        int objectCount = Random.Range(minimum, maximum + 1);
        Debug.Log(objectCount);

        for (int i = 0; i < objectCount; i++)
        {
            Vector3 randomPosition = RandomPosition();

            GameObject obj = Instantiate(wallTile, randomPosition, Quaternion.identity);

            obj.transform.SetParent(boardHolder);

            wallList.Add(obj.GetComponent<Done_Wall>());
        }
    }

    /// <summary>
    ///  创建minimum到maximum数之间个数的敌人
    ///  敌人的生成范围在GridPositions列表中
    /// </summary>
    /// <param name="minimum"></param>
    /// <param name="maximum"></param>
    void LayoutWormAtRandom(int minimum, int maximum)
    {
        wallList.Clear();

        int objectCount = Random.Range(minimum, maximum + 1);
        Debug.Log(objectCount);

        for (int i = 0; i < objectCount; i++)
        {
            Vector3 randomPosition = RandomPosition();

            GameObject obj = Instantiate(wormPrefab, randomPosition, Quaternion.identity);
        }
    }

    // Use this for initialization
    /// <summary>
    /// 建立场景
    /// </summary>
    void Start()
    {
        BoardSetup();

        InitialiseList();

        LayoutWallAtRandom(wallCount.minimum, wallCount.maximum);

        //敌人生成
        LayoutWormAtRandom(wormCount.minimum, wormCount.maximum);
    }

    // Update is called once per frame
    void Update()
    {
    }
}

2.Done_Bomb

using UnityEngine;

/// <summary>
/// 火花生成
/// </summary>
public class Done_Bomb : MonoBehaviour
{
    //火花承载变量
    public GameObject ExplosionRrefab;


    void OnDestroy()
    {
        //在炸弹的位置上生成火花
        Instantiate(ExplosionRrefab, transform.position, Quaternion.identity);
    }
}

3.Done_BombDrop

using UnityEngine;

/// <summary>
/// 炸弹生成脚本
/// </summary>
public class Done_BombDrop : MonoBehaviour
{
    //申明炸弹
    public GameObject BombPrefab;

    void Update()
    {
        //检测空格键,生成炸弹
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //获取炸弹人当前坐标,将坐标整数化,在此位置上生成炸弹
            Vector2 pos = transform.position;
            //Vector2 pos = new Vector2(Mathf.Floor(transform.position.x), Mathf.Floor(transform.position.y));
            //Mathf.Round(pos.x);
            //Mathf.Round(pos.y);
            Instantiate(BombPrefab, pos, Quaternion.identity);
        }
    }
}

4.Done_DestroyAfter

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 炸弹消失脚本
/// </summary>
public class Done_DestroyAfter : MonoBehaviour {


    //设置炸弹存在时间
    public float time = 3f;
	// Use this for initialization
	void Start () {
        //销毁炸弹
        Destroy(gameObject,time);
	}
	
	
}

5.Done_Explosion

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 火花管理
/// </summary>
public class Done_Explosion : MonoBehaviour {

    private void OnTriggerEnter2D(Collider2D coll)
    {
        //如果碰到的物体不是静态属性,则被消除
        if (!coll.gameObject.isStatic) {
            Destroy(coll.gameObject);
        }
    }
}

6.Done_Metal

using UnityEngine;

public class Done_Metal : MonoBehaviour
{
    // Use this for initialization
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
    }
}

7.Done_Move

using UnityEngine;

/// <summary>
/// 玩家移动脚本
/// </summary>
public class Done_Move : MonoBehaviour
{
    //设置运动速度
    public float speed = 16f;

    /// <summary>
    /// 实时获取键盘输入
    /// </summary>
    private void FixedUpdate()
    {
        //getAxisRaw只能返回-1,0,1三个值
        //得到水平或垂直命令
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        //访问炸弹人2D刚体,通过获取的值进行运动
        GetComponent<Rigidbody2D>().velocity = new Vector2(h, v) * speed;

        //播放对应动画
        GetComponent<Animator>().SetInteger("x", (int) h);
        GetComponent<Animator>().SetInteger("y", (int) v);
    }


    /// <summary>
    /// 玩家与敌人相撞消失
    /// </summary>
    /// <param name="co"></param>
    private void OnCollisionEnter2D(Collision2D co)
    {
        if (co.gameObject.name == "Done_worm(Clone)")
        {
            Destroy(gameObject);
        }
    }
}

8.Done_Wall

using UnityEngine;

public class Done_Wall : MonoBehaviour
{
}

9.Done_worm

using UnityEngine;

/// <summary>
/// 怪物运动
/// </summary>
public class Done_worm : MonoBehaviour
{
    //怪物运动速度
    public float Speed = 2f;

    /// <summary>
    /// 给怪物设定一个随机的运动方向
    /// </summary>
    /// <returns></returns>
    Vector2 randir()
    {
        //设置随机数为-1,0,1
        int r = Random.Range(-1, 2);

        //三目运算符(条件?结果1:结果2),给怪物一个运动发方向
        return (Random.value < 0.5) ? new Vector2(r, 0) : new Vector2(0, r);
    }

    /// <summary>
    /// 检测运动方向
    /// </summary>
    /// <param name="dir"></param>
    /// <returns></returns>
    bool isVaildDir(Vector2 dir)
    {
        //获取怪物此时的位置
        Vector2 pos = transform.position;
        //从怪物当前位置发射一条射线,如果碰到物体则怪物无法运动,反之可以
        RaycastHit2D hit = Physics2D.Linecast(pos + dir, pos);
        return (hit.collider.gameObject == gameObject);
    }

    /// <summary>
    /// 怪物运动,调用动画
    /// </summary>
    void ChangeDir()
    {
        //获取随机的二维向量
        Vector2 dir = randir();
        {
            //检测是否能运动
            if (isVaildDir(dir))
            {
                GetComponent<Rigidbody2D>().velocity = dir * Speed;
                GetComponent<Animator>().SetInteger("x", (int) dir.x);
                GetComponent<Animator>().SetInteger("y", (int) dir.y);
            }
        }
    }

    /// <summary>
    /// 实时获取运动方向
    /// </summary>
    void Start()
    {
        //每隔0.5秒重复调用怪物运动函数,让怪物可以实时获取新的运动方向
        InvokeRepeating("ChangeDir", 0, 0.5f);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值