👨💻个人主页:@元宇宙-秩沅
👨💻 hallo 欢迎 点赞👍 收藏⭐ 留言📝 加关注✅!
👨💻 本文由 秩沅 原创
👨💻 收录于专栏:unityUI专题篇
⭐通用API实现抽象行为封装【五】⭐
文章目录
- ⭐通用API实现抽象行为封装【五】⭐
- 🎶前言
- 🎶(==A==)常用关键API
- 🎶(==B==)需求分析
- 🎶(==C==)行为实现——武器拾取变成技能拾取
- 🎶(==D==)行为实现——四个属性道具一个脚本
- 🎶(==E==)行为实现——摧毁箱子爆出道具
- 总结:
🎶前言
🅰️
🎶(A)常用关键API
🎶(B)需求分析
🎶(C)行为实现——武器拾取变成技能拾取
😶🌫️:步骤
- 1.添加道具预制体(随机生成武器)
- 2.封装道具基类
- 3.封装武器道具逻辑
1.封装道具基类
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 道具基类,提取道具相同点
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class propsFather : MonoBehaviour
{
public float RotateSpeed = 100;
public GameObject Effect; //吃掉道具的特效
private void Update()
{
transform.Rotate(Vector3.up * RotateSpeed * Time.deltaTime);
}
public virtual void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
GainEffect();
}
}
//生成特效
public void GainEffect()
{
GameObject effect = Instantiate(Effect, transform.position, transform.rotation); //实例化吃掉特效
AudioSource effectAudio = effect.GetComponent<AudioSource>();
effectAudio.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
effectAudio.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
gameObject.SetActive(false); //吃掉后让其先失活
Destroy(effect, 3); //特效持续三秒后消失
}
}
封装武器道具逻辑
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 武器道具的逻辑封装
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class props_Weapon : propsFather
{
public GameObject [] weapons;
private int index;
private void Awake()
{
weapons = Resources.LoadAll<GameObject>(@"Prefabs/Weapon") ; //加载武器资源
}
public override void OnTriggerEnter(Collider other)
{
base.OnTriggerEnter(other);
if (other.CompareTag("Player"))
{
if (weapons != null)
{
//随机武器生成
index = Random.Range(0, weapons.Length);
GameObject NewWeapon = Instantiate(weapons[index]);
//标识武器的主人
Weapon weaponScript = NewWeapon.GetComponent<Weapon>();
weaponScript.Tank = other.gameObject;
//拾取武器
MainTank tankScript = other.gameObject.GetComponent<MainTank>();
tankScript.ChangeWeapon(NewWeapon);
}
}
}
}
🎶(D)行为实现——四个属性道具一个脚本
巧妙地使用了枚举,使得四个脚本变成一个脚本共同使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 加血道具逻辑封装
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public enum Type_Atrribute //枚举类型的创建相当于将四个属性的脚本直接变成一个脚本
{
blood,
attack,
defence,
speed
}
public class props_Attribute : propsFather
{
public Type_Atrribute atrribute = Type_Atrribute.blood ;
public int atk = 20;
public int def = 20;
public int Spe = 20;
public int blo = 20;
public override void OnTriggerEnter(Collider other)
{
base.OnTriggerEnter(other);
if (other.CompareTag("Player"))
{
MainTank tankScript = other.gameObject.GetComponent<MainTank>();
switch (atrribute)
{
case Type_Atrribute.blood:
tankScript.nowBlood += blo;
if (tankScript.nowBlood > tankScript.maxBlood)
tankScript.nowBlood = tankScript.maxBlood;
break;
case Type_Atrribute.attack:
tankScript.attack += atk;
break;
case Type_Atrribute.defence:
tankScript.defence += def;
break;
case Type_Atrribute.speed:
tankScript.moveSpeed += Spe;
break;
default:
break;
}
}
}
}
🎶(E)行为实现——摧毁箱子爆出道具
箱子类相关封装
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//-------------------------------------
//—————————————————————————————————————
//___________项目: ______________
//___________功能: 箱子销毁相关逻辑封装
//___________创建者:秩沅_______________
//_____________________________________
//-------------------------------------
public class BoxTrigger : MonoBehaviour
{
public int defence = 50; //箱子的防御力
public GameObject effect; //箱子销毁特效
private GameObject[] props;
private int sufferAttack;
private int PropChance;
private void Start()
{
PropChance = Random.Range(1, 101);
if (PropChance <= 70)
{
props = Resources.LoadAll<GameObject>(@"Prefabs/Props");
}
}
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("bullet"))
{
BulletMove BulletSript = other.gameObject.GetComponent<BulletMove>();
//如果子弹是玩家打来的则获取子弹的攻击力
if(BulletSript.Tank.tag == "Player")
{
TankFather tankScript = BulletSript.Tank.GetComponent<TankFather>();
sufferAttack = tankScript.attack;
defence -= sufferAttack; //防御力减少
sufferAttack = 0; //子弹的伤害是一次性的所以伤害归0
if (defence <= 0) RandomMakeProp();
}
}
}
//随机生成道具
public void RandomMakeProp()
{
int index;
if (props != null) //70%概率生成道具
{
index = Random.Range(0, props.Length);
GameObject eff = Instantiate(effect, transform.position , transform.rotation);
//音效同步
AudioSource audioS = eff.GetComponent<AudioSource>();
audioS.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
audioS.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
Destroy(eff, 3); //延时销毁特效
GameObject prop = Instantiate(props[index], transform.position +new Vector3(0, 2, 0), transform.rotation);
Destroy(gameObject);
}
else //30%的概率直接销毁
{
GameObject eff = Instantiate(effect, transform.position , transform.rotation);
//音效同步
AudioSource audioS = eff.GetComponent<AudioSource>();
audioS.enabled = MusicContol.SingleMusicContol.nowMusicData.soundSwitch;
audioS.volume = MusicContol.SingleMusicContol.nowMusicData.soundRoll;
Destroy(eff, 3); //延时销毁特效
Destroy(gameObject);
}
}
}
总结:
- 枚举在面向对象中的妙用
⭐相关文章⭐
⭐【2023unity游戏制作-mango的冒险】-6.关卡设计
⭐【2023unity游戏制作-mango的冒险】-5.攻击系统的简单实现
⭐【2023unity游戏制作-mango的冒险】-4.场景二的镜头和法球特效跟随
⭐【2023unity游戏制作-mango的冒险】-3.基础动作和动画API实现
⭐【2023unity游戏制作-mango的冒险】-2.始画面API制作
⭐【2023unity游戏制作-mango的冒险】-1.场景搭建
⭐“狂飙”游戏制作—游戏分类图鉴(网易游学)
⭐本站最全-unity常用API大全(万字详解),不信你不收藏
你们的点赞👍 收藏⭐ 留言📝 关注✅是我持续创作,输出优质内容的最大动力!