dp-s_doc/Start/Example/11.md

84 lines
2.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 通关时间播报 (贡献者: 邪神)
> 首先我们先在dp_s文件夹中建立一个项目文件夹 MyProject 方便管理
![Alt text](../../image/6.png)
> 然后我们建立一个新文件 通关时间播报.nut 用于编写我们的代码
> 然后我们在通关时间播报.nut中写入以下代码
```
//贡献者: 邪神
ClearTimeMsg <- "玩家[%s]通关<%s>[%s]耗时:[%s]";
//难度对应难度名称
dungeon_diff_Map <- {
"0": "普通级",
"1": "冒险级",
"2": "王者级",
"3": "地狱级",
"4": "英雄级"
};
//通关时间回调
Cb_CParty_SetBestClearTime_Enter_Func.ClearTime <- function(args) {
local PartyObj = Party(args[0]);
local dungeon_diff = args[2];
local clearTime = args[3];
local Bfobj = PartyObj.GetBattleField();
local DgnObj = Bfobj.GetDgn();
local Dungeon_Name = DgnObj.GetName();
local diff_name = dungeon_diff_Map[(dungeon_diff).tostring()];
local MemberNames = [];
for (local i = 0; i < 4; ++i) {
local SUser = PartyObj.GetUser(i);
if (SUser) {
local name = SUser.GetCharacName();
MemberNames.push(name);
}
}
local joinedNames = join(MemberNames, ", ");
local time = formatMilliseconds(clearTime);
World.SendNotiPacketMessage(format(ClearTimeMsg, joinedNames, diff_name, Dungeon_Name, time), 0);
}
function join(array, delimiter) {
local result = "";
for (local i = 0; i < array.len(); ++i) {
if (i > 0) {
result += delimiter;
}
result += array[i];
}
return result;
}
//毫秒转换
function formatMilliseconds(ms) {
local str = "";
local minutes = ms / 60000;
local seconds = (ms % 60000) / 1000;
local milliseconds = (ms % 1000) / 10;
if (minutes > 0) {
str = minutes + "分" +
(seconds < 10 ? "0" : "") + seconds + "秒" +
(milliseconds < 10 ? "0" : "") + milliseconds;
} else {
str = seconds + "秒" +
(milliseconds < 10 ? "0" : "") + milliseconds;
}
return str;
}
```
> 最后我们回到dp_s文件夹中打开Main.nut 加载我们刚才编写的逻辑
```
sq_RunScript("MyProject/通关时间播报.nut");
```
> 至此一个简单的通关时间播报的逻辑就写完了