RPG游戏-NPC系统
生活随笔
收集整理的這篇文章主要介紹了
RPG游戏-NPC系统
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.NPC定義
using System; using System.Collections.Generic; using System.Linq; using System.Text; using SkillBridge.Message;namespace Common.Data {public enum NpcType //NPC定義 是功能性的 還是任務性的{None = 0,Function,Task}public enum NpcFunction //點擊NPC會觸發的功能事件{None = 0,InvokeShop,InvokeInsrance}public class NpcDefine{public int ID { get; set; }public string Name { get; set; }public NpcType Type { get; set; }public string Description { get; set; }//基本屬性public NpcFunction Function { get; set; }public int Integer { get; set; }} }2.NPC管理器
實現管理NPC交互功能
using Common.Data; using System.Collections.Generic;namespace Managers {class NpcManager:Singleton<NpcManager>{public delegate bool NpcActionHandler(NpcDefine npc);Dictionary<NpcFunction, NpcActionHandler> eventMap = new Dictionary<NpcFunction, NpcActionHandler>();public NpcDefine GetNpcDefine(int npcID){return DataManager.Instance.Npcs[npcID];}public void RegisterNpcEvent(NpcFunction function,NpcActionHandler action){if (!eventMap.ContainsKey(function)){eventMap[function] = action;}elseeventMap[function] += action;}public bool Interactive(int npcID){if (DataManager.Instance.Npcs.ContainsKey(npcID)){NpcDefine npc = DataManager.Instance.Npcs[npcID];return Interactive(npc);}return false;}public bool Interactive(NpcDefine npc){if (npc.Type == NpcType.Task){return DoTaskInteractive(npc);}else if (npc.Type == NpcType.Function){return DoFunctionInteractive(npc);}return false;}private bool DoTaskInteractive(NpcDefine npc){if(npc.Type!= NpcType.Task){return false;}if (!eventMap.ContainsKey(npc.Task)){return false;}return eventMap[npc.Task](npc);//執行方法管理器中代碼所注冊的事件函數}private bool DoFunctionInteractive(NpcDefine npc){if(npc.Type!= NpcType.Function){return false;}if (!eventMap.ContainsKey(npc.Function)){return false;}return eventMap[npc.Function](npc);}} }3.NPC控制器
主要控制NPC的行為,比如待機行為,與玩家交互時轉向行為,并處理一些交互任務,觸碰NPC產生的效果等
using System.Collections; using System.Collections.Generic; using UnityEngine; using Common.Data; using Managers; using Models;public class NpcController : MonoBehaviour {public int npcID;Animator anim;NpcDefine npc;Color originColor;private bool inInteractive = false;SkinnedMeshRenderer render;void Start(){anim = this.gameObject.GetComponentInChildren<Animator>();npc = NpcManager.Instance.GetNpcDefine(npcID);render = this.gameObject.GetComponentInChildren<SkinnedMeshRenderer>();originColor = render.sharedMaterial.color;npc = NpcManager.Instance.GetNpcDefine(this.npcID);this.StartCoroutine(Actions());}IEnumerator Actions(){while (true){if (inInteractive)yield return new WaitForSeconds(2f);elseyield return new WaitForSeconds(Random.Range(5f,10f));this.Relatex();}}private void Relatex(){anim.SetTrigger("Relax");}void Interactive(){if (this.inInteractive){this.inInteractive = true;StartCoroutine(DoInteractive());}}IEnumerator DoInteractive(){yield return FaceToPlayer();if (NpcManager.Instance.Interactive(npc)){anim.SetTrigger("Talk");}yield return new WaitForSeconds(3f);this.inInteractive = false;}IEnumerator FaceToPlayer(){Vector3 faceTo = (User.Instance.CurrentCharacterObj.transform.position - this.transform.position).normalized;while (Mathf.Abs(Vector3.Angle(this.gameObject.transform.forward, faceTo)) > 5){this.gameObject.transform.forward = Vector3.Lerp(this.gameObject.transform.forward, faceTo, Time.deltaTime * 5f);yield return null;}}// Update is called once per framevoid OnMouseDown(){this.Interactive();}private void OnMouseOver(){Highlight(true);}private void OnMouseEnter(){Highlight(true);}private void OnMouseExit(){Highlight(false);}private void Highlight(bool show){if (show){if (render.sharedMaterial.color != Color.white){render.sharedMaterial.color = Color.white;}}else{if (render.sharedMaterial.color != originColor){render.sharedMaterial.color = originColor;}}} }4.事件方法管理器
namespace FuncManager{ class FuncManager:Singleton<FuncManager>{ public void Init(){NpcManager.Instance.RegisterNpcEvent(NpcFunction.InvokeShop,OnNpcInvodeShop);} private bool OnNpcInvodeShop(NpcDefine npc){ //根據NPC的類型 打開不同的商店UI //比如 發布任務的 打開的是任務商店 普通商人 打開是道具商店等等 return true; } }總結
以上是生活随笔為你收集整理的RPG游戏-NPC系统的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 六月计划#2B(6.10-6.16)
- 下一篇: 从线上慢sql看explain关键字