效果演示:
思路分析:
reworld有3种类型的脚本,适用于不同场景
服务器脚本:处理游戏内所有内容·(UI除外)
客户端脚本:处理游戏内UI。也可以处理游戏内容,但是属于本地内容改变,多人游戏其他玩家看不到
通用模块脚本:定义通用数据和通用方法
服务器脚本和客户端脚本属于2个脚本环境,互相是不能调用对方定义的数据和方法的。这个时候就需要通用模块脚本,使用通用模块脚本写的代码服务器脚本和客户端脚本都能使用。它们使用的同一份代码,但只是运行前,运行后相当于2个脚本环境各自new了一个对象,在服务器脚本修改属性,客户端脚本是不会改变的,并不能使用通用模块脚本来充当服务器脚本和客户端脚本的桥梁。通用模块脚本的用处就是定义游戏数据和通用的方法。
下面是官方通用模块脚本使用例子
通用模块脚本:
mod = {} mod.a = "hello" mod.b = "world" function mod.ab() c = RWObject.Create("Part") c.Name = mod.a..mod.b end return mod
服务器脚本:
a = RWrequire(WorkSpace["通用模块脚本"]) print(a.a) print(a.b) a.ab()
游戏内有2个NPC角色分别是战士导师和法师导师,NPC有好感度不同的好感度会触发不同的动作和不同的对话,靠近NPC会弹出对话UI,为演示功能好感度大于40就不会弹出对话。点击对话结束增加好感度,对话未结束离开NPC减少好感度。
案例主要演示通用模块脚本定义数据的NPC数据和定义方法的NPC方法,NPC数据:NPC角色动作和对话数据。NPC方法:获取NPC角色数据的方法。使用通用模块脚本定义,通过引用服务器脚本和客户端脚本都能直接调用。
功能搭建:
1、NPC好感度
按照下面图片创建通用模块脚本和文件夹
NPC方法
--NPC方法 local this={} this.parent=script.Parent --增加NPC好感度,返回NPC好感度 --参数 1.NPC角色 2.好感度 function this.addFavor(npc,favor) npc.好感度.Value=npc.好感度.Value+favor return npc.好感度.Value end --获取对话 --参数 1.NPC角色 function this.getTalk(npc) local data=RWrequire(npc.NPC数据) local favor=npc.好感度.Value local len=#data.talks for i=1,len,1 do if data.talks[i].favor >= favor then return data.talks[i].talk end end end --获取动作 --参数 1.NPC角色 function this.getAction(npc) local data=RWrequire(npc.NPC数据) local favor=npc.好感度.Value local len=#data.actions for i=1,len,1 do if data.actions[i].favor >= favor then return data.actions[i].action end end end return this
2、按照下面图片先创建战士导师角色,完成后复制一个修改为法师导师角色
战士导师NPC数据
--NPC数据 战士 local this={} this.parent=script.Parent --动作 this.actions={{favor=0,action='yaotou'},{favor=10,action='showmuscle1'},{favor=30,action='showmuscle2'}} --对话 this.talks={{favor=0,talk={'你好,我是'..script.Parent.Name,'新来的冒险者,欢迎来到重启世界'}}, {favor=10,talk={'很高兴再次见到你冒险者','你现在太弱了,还不能转职'}}, {favor=30,talk={'好久不见,冒险者','你经历了什么,变得如此强大','来转职战士吧,一拳一个脆皮法师'}}} return this
法师导师NPC数据
--NPC数据 法师 local this={} this.parent=script.Parent --动作 this.actions={{favor=0,action='yaotou'},{favor=10,action='youtaishou'},{favor=30,action='wave'}} --对话 this.talks={{favor=0,talk={'你好,我是'..script.Parent.Name,'新来的冒险者,欢迎来到重启世界'}}, {favor=10,talk={'很高兴再次见到你冒险者','你现在太弱了,还不能转职'}}, {favor=30,talk={'好久不见,冒险者','你经历了什么,变得如此强大','想成为法师吗,魔法令人着迷'}}} return this
3、按照下面图片创建界面,创建完成取消界面显示
talkUI_C
--对话界面 local this={} this.parent=script.Parent this.conversation=nil this.conversationIndex=1 this.npcMethod=RWrequire(WorkSpace["NPC方法"]) this.npc=nil function this.main() this.parent.GuiMouseLeftUp:Connect(this.guiMouseLeftUp) MessageEvent.ClientEventCallBack("showTalkUI"):Connect(this.showUIECB) MessageEvent.ClientEventCallBack("hideTalkUI"):Connect(this.hideUIECB) end --界面点击 function this.guiMouseLeftUp(x,y) if this.conversationIndex<#this.conversation then this.conversationIndex=this.conversationIndex+1 this.showConversation() elseif this.conversationIndex==#this.conversation then this.parent.IsVisable =false this.npcMethod.addFavor(this.npc,15) end end --显示界面 function this.showUIECB(npcName) this.parent.IsVisable =true this.npc=WorkSpace.npc_F[npcName] this.conversation=this.npcMethod.getTalk(this.npc) this.conversationIndex=1 this.showConversation() end --隐藏界面 function this.hideUIECB() this.parent.IsVisable =false end --显示对话 function this.showConversation() local con=this.conversation[this.conversationIndex] this.parent.文本控件.Text=con end this.main()
4、在玩家初始化角色初始化脚本下创建Player_S服务器脚本
--玩家 local this={} this.parent=script.Parent this.npcArr=nil this.interactNPC=nil this.npcMethod=RWrequire(WorkSpace["NPC方法"]) function this.main() this.npcArr=WorkSpace.npc_F:GetAllChild() GameRun.Update:Connect(this.Update) end --帧频函数 function this.Update(delta) local len=#this.npcArr local distance = nil local minDistance = 1.5 local npc=nil for i=1,len,1 do distance = Vector3.Distance(this.parent.Position,this.npcArr[i].Position) if distance < minDistance and this.npcMethod.addFavor(this.npcArr[i],0)<40 then minDistance=distance npc=this.npcArr[i] end end if this.interactNPC==nil and npc~= nil then print('接触NPC:',tostring(npc.Name)) npc.action.LocalClipId =this.npcMethod.getAction(npc) npc.action:PlayAnimation() MessageEvent:FireClient(this.parent.PlayerId,"showTalkUI",npc.Name) elseif this.interactNPC~=nil and npc== nil then print('离开NPC:',tostring(this.interactNPC.Name)) this.npcMethod.addFavor(this.interactNPC,-5) MessageEvent:FireClient(this.parent.PlayerId,"hideTalkUI") end this.interactNPC=npc end this.main()
本帖最后由 土豆虎 于 2020-05-13 14:03 编辑
请登录后评论~