码迷,mamicode.com
首页 > 编程语言 > 详细

如何制作塔防游戏 代码,操作,说明——Unity 5.3.5f1

时间:2017-08-26 23:27:15      阅读:299      评论:0      收藏:0      [点我收藏+]

标签:规则   reading   string   cti   ide   read   攻击   debug   学生   

 

       我以前使用过unity但是第一次写这么全面的塔防小游戏。我以后会陆续的将我跟过的一些项目的心得经验与体会发表出来希望各位能人能够给出评价,我在此感激各位的批评与赞扬。另外我只是一个学生学艺不精,粗制滥造还请看不过去的大神放过................0.0................................

 首先是路径的管理设置

using UnityEngine;
using System.Collections;

namespace RayGame{
    
    public class PathManager : MonoBehaviour {

        //路点数组
        public GameObject[] Path1;
        public GameObject[] Path2;

        /// <summary>
        /// 返回路点
        /// </summary>
        /// <returns>The node.</returns>
        /// <param name="pathId">路径索引.</param>
        /// <param name="nodeIndex">路点索引</param>
        public GameObject getNode(int pathId,int nodeIndex){
            if(pathId == 1){
                //超出路径数组范围,返回 Null
                if (nodeIndex >= Path1.Length) {
                    return null;
                } else {
                    //返回相应路点游戏对象
                    return Path1 [nodeIndex];
                }
            }else{
                if (nodeIndex >= Path2.Length) {
                    return null;
                } else {
                    return Path2 [nodeIndex];
                }
            }
        }
    }

}

然后是游戏玩家管理以及设置

// Authors:Liu Yong
// Email: <raygenes@gmail.com>
// QQ:1931195500

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
namespace RayGame{
	
	public class GameManager : MonoBehaviour {
		Animator canvasAnim;
		//最大生命值
		public int totalLife = 20;
		//最大金钱值
		public int totalGold = 200;
		//当前生命值
		int curLife = 0;
		//当前金币
		int curGold = 0;

		UIManager uiMgr;

		public int[] towerPrices;
		void Awake(){
			uiMgr = GameObject.Find ("UIManager").GetComponent<UIManager> ();
			canvasAnim = uiMgr.GetComponent<Animator> ();
		}

		void Start(){
			DataInit ();
			UIInit ();
		}

		//数据初始化
		void DataInit(){
			curLife = totalLife;
			curGold = totalGold;
		}

		//UI界面初始化
		void UIInit(){
			uiMgr.SetHeartValue (curLife);
			uiMgr.SetGoldValue (curGold);
		}

		//添加金钱
		public void addGold(int num){
			curGold += num;
			uiMgr.SetGoldValue (curGold);
		}

		public void useGold(int num){
			curGold -= num;
			uiMgr.SetGoldValue (curGold);
		}

		//增加生命值
		public void addLife(int num){
			curLife += num;
			uiMgr.SetHeartValue (curLife);
		}

		public void useLife(int num){
			curLife -= num;
			uiMgr.SetHeartValue (curLife);
		}

		public bool HasEnoughLife(int num){
			return curLife - num >= 0;
		}

		public bool HasEnoughGold(int num){
			return curGold - num >= 0;
		}	
		public void ShowButton(){
			//触发动画切换
			canvasAnim.SetTrigger ("ShowStartButton");
		}

		public void EnterGame(){
			SceneManager.LoadScene ("Game2");
		}

	}

}

  怪物的管理及设置

// Authors:Liu Yong
// Email: <raygenes@gmail.com>
// QQ:1931195500

using UnityEngine;
using System.Collections;
using System.Threading;
namespace RayGame{
    
    public class MonsterManager : MonoBehaviour {

        //怪物资源路径
        string monPath = "Prefabs/Monsters/Mon1";
        //刷怪点
        public Transform spawnPoint;
        //刷新时间
        //public float spawnInterval =3f;

        void Start()
        {
            for (int i=0; i<= 5; i++)
            {  
                Spawn();
            }
            InvokeRepeating ("Start2",35,20);
        }


        void Start2(){
            for (int i=0; i<= 5; i++)
            {  
                Spawn();
            }

        }
        void Spawn(){
            
            //随机数生成,半闭半开区间
            //加载ra物资源,并实例化游戏对象
                GameObject mon = (GameObject)Instantiate (Resources.Load (monPath), spawnPoint.position,
                                    Quaternion.identity);
                //关联怪物移动组件
                mon.AddComponent<MonsterMove> ();
                mon.AddComponent<MonsterHealth> ();
                mon.AddComponent<Rigidbody2D> ();
                BoxCollider2D collider = mon.AddComponent<BoxCollider2D> ();
                collider.isTrigger = true;
                   }
    }

}

攻击塔的管理及设置

// Authors:Liu Yong
// Email: <raygenes@gmail.com>
// QQ:1931195500

using UnityEngine;
using System.Collections;

namespace RayGame{
	
	public class TowerManager : MonoBehaviour {

		//造塔点
		public GameObject[] TowerPoints;

		//建造过程中的图片
		public Sprite[] TowerBuilding;

		//血条资源路径
		string barPath = "Prefabs/UI/ProgressBar";

		//造塔时间
		public float buildingTime = 1f;

		//当前的造塔点
		GameObject curTowerPoint = null;
		//当前塔的类型
		int curTowerId;

		void Awake(){
			//遍历 所有造塔点
			for (int i = 0; i < TowerPoints.Length; i++) {
				GameObject tp = TowerPoints [i];
				//关联碰撞盒
				tp.AddComponent<CircleCollider2D> ();
				//关联脚本
				tp.AddComponent<TowerPoint> ();
			}
		}

		public void ShowBuilding(GameObject _towerPoint,int towerId){
			//记住造塔点
			curTowerPoint = _towerPoint;
			//记住造塔类型
			curTowerId = towerId;
			//获取渲染组件
			SpriteRenderer spRender = _towerPoint.GetComponent<SpriteRenderer> ();
			//更改精灵图片
			spRender.sprite = TowerBuilding [towerId - 1];
			//显示建造过程
			ShowProgress (_towerPoint);
		}

		void ShowProgress(GameObject towerPoint){
			GameObject barObj = (GameObject)Instantiate (Resources.Load (barPath),
				Vector3.zero,Quaternion.identity);
			//ProgressBar组件
			ProgressBar bar = barObj.GetComponent<ProgressBar> ();
			//挂载血条
			bar.SetBarParent (towerPoint.transform);
			//激活进度条
			bar.SetActive (true,buildingTime,gameObject);
		}

		public void ProgressEnd(){
			Debug.Log ("进度条结束");
			BuildTower (curTowerPoint,curTowerId,1);
		}

		/// <summary>
		/// 造塔
		/// </summary>
		/// <param name="towerPoint">造塔点</param>
		/// <param name="type">塔类型</param>
		/// <param name="level">塔的等级</param>
		public void BuildTower(GameObject towerPoint,int type,int level){
			//按照规则拼接路径
			string towerPath = "Prefabs/Towers/Tower"+type+"/Tower"+type+"_"+level;
			Debug.Log ("show path "+towerPath);
			//实例化塔对象
			GameObject tower = (GameObject)Instantiate (Resources.Load (towerPath),
				towerPoint.transform.position, Quaternion.identity);
			Tower tw = tower.AddComponent<Tower> ();
			tw.SetTowerType (type);
			tw.TowerInit ();

			//删除造塔点
			Destroy (towerPoint);
		}

	}

}

  UImanger的管理及设置

// Authors:Liu Yong
// Email: <raygenes@gmail.com>
// QQ:1931195500

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

namespace RayGame{
	
	public class UIManager : MonoBehaviour {

		string panelPath = "Prefabs/UI/TowerPanel";

		//指向当前打开的面板
		GameObject curPanel = null;

		//指向点击的当前的造塔点
		public GameObject curTowerPoint = null;

		public Text heartValue;
		public Text goldValue;
		public Text waveValue;


		/// <summary>
		/// 在指定位置显示面板
		/// </summary>
		/// <param name="pos">指定的位置</param>
		public void ShowTowerPanel(Vector3 pos,GameObject towerPoint){
			if(curPanel != null){
				Destroy (curPanel);
			}
			curPanel = (GameObject)Instantiate (Resources.Load (panelPath),
				pos,Quaternion.identity);

			//记录当前点击的造塔点
			curTowerPoint = towerPoint;
		}

		public void CloseTowerPanel(){
			Destroy (curPanel);
		}

		public void SetHeartValue(int val){
			heartValue.text = val.ToString ();
		}

		public void SetGoldValue(int val){
			goldValue.text = val.ToString ();
		}

		public void SetWaveValue(int val){
			waveValue.text = val.ToString ();
		}
	}

}

  另外其实还需要大量的动态图片,路径点设置,路径选择。各种绑定的工作没有办法在这上面演示。想要完整的文件私下qq邮箱我1170826169@qq.com

 

以上地图所具备的组件已经都完成了。接下来就是塔防,买塔,怪物移动,怪物受到攻击以及打死怪赚钱等等行为的代码

技术分享

 

如何制作塔防游戏 代码,操作,说明——Unity 5.3.5f1

标签:规则   reading   string   cti   ide   read   攻击   debug   学生   

原文地址:http://www.cnblogs.com/r-00/p/7436586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!