日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

RPG游戏-NPC系统

發布時間:2023/12/10 windows 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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系统的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。