本帖最后由 咸鱼羊 于 2021-7-24 12:07 编辑
改用了NPC的任务,编号参考与IQuest相关方法
//制作目的是对话的停顿感
var Thread = Java.type("java.lang.Thread");
//普通对话
/**
* String message 单条聊天信息
* IPlayer player 收信息的玩家
* Int waitTime 发送该条信息后距离下一条的间隔时间(ms)
*/
function Dialogue(message,player,waitTime){
this.type = 1;
this.message = message;
this.player = player;
this.waitTime = waitTime;
this.play=function(){
this.player.message(this.message);
Thread.sleep(this.waitTime);
}
}
//对话选项(未完成品,会导致卡死,请勿调用)
//ps:如果有大佬能帮忙修修就好了
/**
* String question 发出选项的主对话
* String[] message 每一个选项对应的文本
* DialogueBus[] correspondingBus 每一个选项所对应的对话总线
* IPlayer player 接收信息的玩家
*/
function DialogueOptions(question,messages,correspondingBus,player){
this.type = 2;
this.question = question;
this.messages = messages;
this.correspondingBus = correspondingBus;
this.player = player;
this.play=function(){
this.player.message(this.question);
for(var k in this.messages){
this.player.message(this.messages[k]);
}
this.player.getTempdata().put("useingDialogue",this);
this.player.getTempdata().put("useingDialogueType",2);
}
this.choose=function(num){
this.correspondingBus[num-1].play();
}
}
//对话任务
/**
* boolean reusable 是否可以重复使用(填写ture/false)
* Int task 任务编号
* IPlayer player 接收任务的玩家(关于玩家的实现请自行编写,仅需求有获取任务的方法[getTasks])
* Int waitTime 接收任务后距离下一条总线任务执行的时间
*/
function DialogueTask(reusable,task,player,waitTime){
this.type = 3;
this.reusable = reusable;
this.task = task;
this.player = player;
this.waitTime = waitTime;
this.play=function(){
if((!(this.reusable)&&this.player.getStoreddata().get(("hadDoing"+this.task))!=true)||(this.reusable)){
this.player.startQuest();
this.player.getStoreddata().put(("hadDoing"+this.task),true)
Thread.sleep(this.waitTime);
}
}
}
/**
* IPlayer 接收对话的玩家
* 此放在对话总线的末尾(用于判断是否正在进行某个对话/选项)
*/
function DialogueEnd(player){
this.type = 4;
this.player = player;
this.play=function(){
this.player.getTempdata().put("useingDialogue",null);
this.player.getTempdata().put("useingDialogueType",4);
}
}
//对话总线
/**
* Object[] dialogues 对话/对话任务,或者是其他的有play方法的对象
* IPlayer player 使用总线的玩家
*/
function DialogueBus(dialogues,player){
this.type = 5;
this.dialogues = dialogues;
this.player = player;
this.play=function(){
this.player.getTempdata().put("useingDialogue",this);
this.player.getTempdata().put("useingDialogueType",5);
for(var i in this.dialogues){
this.dialogues[i].play();
}
}
this.add=function(dialogue){
this.dialogues.add(dialogue);
}
}
//判断字符串是否为正整数
function isInteger(str) {
return (!isNaN(str) && (/^\d+$/.test(str)));
}
//聊天栏事件
//用于判断是否选择选项,以及删除纯数字信息
function chat(event){
if(event.player.getTempdata().get("useingDialogueType") == 2){
if(isInteger(event.message)&&parseInt(event.message)>0&&parseInt(event.message)<=event.player.getTempdata().get("useingDialogue").dialogues.length){
event.player.getTempdata().get("useingDialogue").choose(parseInt(event.message));
event.message = null;
}
}
}
//使用案例
function interact(e){
var dia = new Dialogue("a test text",e.player,1000);
var diaEnd = new DialogueEnd(e.player);
var aBusOnlyWithDia = new DialogueBus([dia,dia,diaEnd],e.player);
var diaOp = new DialogueOptions("选项的主文本",["选项1 test1","选项2 test2"],[aBusOnlyWithDia,aBusOnlyWithDia],e.player);
var aBusWithDiaAndOp = new DialogueBus([dia,diaOp],e.player);
aBusWithDiaAndOp.play();
e.player.message("执行完上述代码(仅仅是执行了第一个总线,没有执行选项的总线)");
}[i]
|