| 赞 | 0 |
| VIP | |
| 好人卡 | |
| 积分 | 1 |
| 经验 | |
| 最后登录 | 2018-1-20 |
| 在线时间 | 704 小时 |
- 梦石
- 0
- 星屑
- 52
- 在线时间
- 704 小时
- 注册时间
- 2011-12-27
- 回帖
- 858
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有账号?注册会员
×
本帖最后由 zeldafd 于 2015-4-27 12:28 编辑
Utility Game Libraries
說明:
此為本人最近開發遊戲時使用的常用腳本庫
在此開帖是為了整合和分享個人的部分腳本
所有的腳本都已經有足夠的注解, 理論上即使不會腳本也能夠看得懂吧
在未來有更多數量的腳本後, 應該會把其製作成完整的工程發佈
希望各位能夠多多支持
* 可能有些腳本在遠古時代已有人發佈而小弟不知情, 這裡的腳本都是自製的(除了Base64加密)
更新日誌:
2015/4/26 - 新增 日誌記錄庫 , Base64加密庫 , 幾何運算庫
2015/4/27 - 修改 日誌記錄庫 : 自動轉換參數message為字串, 現在可自由放入可字串化的類型資料
腳本列表:
[fold=日誌記錄庫]
[pre lang="ruby"]
#===============================================================================
# Utility Game Libraries: 日誌記錄庫
#-------------------------------------------------------------------------------
# 作者: RedSuns S.H.Chan (zeldafd@66rpg)
# 版本: 1.1
#-------------------------------------------------------------------------------
# 說明:
# 本腳本是用於在遊戲不同的腳本或是事件內進行日誌記錄並輸出到控制台
# 以統一的方式的輸出日誌
# 並提供了print_log方法輸出至於遊戲根目錄的日誌文件檔案
#===============================================================================
#===============================================================================
# 全局變量: $UGE_log_stack
#-------------------------------------------------------------------------------
# 類型: Array
#-------------------------------------------------------------------------------
# 說明:
# 作為日誌記錄的堆疊
#===============================================================================
$UGE_log_stack = []
#===============================================================================
# 函數: log(mixed, string)
#-------------------------------------------------------------------------------
# 說明:
# 寫入DEBUG級別的日誌記錄及輸出到控制台
#-------------------------------------------------------------------------------
# 參數:
# mixed (string): 寫入日誌的訊息或數據
# tag (string): 寫入日誌時的標籤
#-------------------------------------------------------------------------------
# 返回: void
#===============================================================================
def log(message, tag="UGE Logger")
msg = "| " + Time.new.strftime("%Y-%m-%d %H:%M:%S") + " | DEBUG | " + tag + " | " + message.to_s + "\n"
print msg
$UGE_log_stack.push(msg)
end
#===============================================================================
# 函數: log_warning(mixed, string)
#-------------------------------------------------------------------------------
# 說明:
# 寫入WARNING級別的日誌記錄及輸出到控制台
#-------------------------------------------------------------------------------
# 參數:
# message (mixed): 寫入日誌的訊息或數據
# tag (string): 寫入日誌時的標籤
#-------------------------------------------------------------------------------
# 返回: void
#===============================================================================
def log_warning(message, tag="UGE Logger")
msg = "| " + Time.new.strftime("%Y-%m-%d %H:%M:%S") + " | WARNING | " + tag + " | " + message.to_s + "\n"
print msg
$UGE_log_stack.push(msg)
end
#===============================================================================
# 函數: log_error(mixed, string)
#-------------------------------------------------------------------------------
# 說明:
# 寫入ERROR級別的日誌記錄及輸出到控制台
#-------------------------------------------------------------------------------
# 參數:
# message (mixed): 寫入日誌的訊息或數據
# tag (string): 寫入日誌時的標籤
#-------------------------------------------------------------------------------
# 返回: void
#===============================================================================
def log_error(message, tag="UGE Logger")
msg = "| " + Time.new.strftime("%Y-%m-%d %H:%M:%S") + " | ERROR | " + tag + " | " + message.to_s + "\n"
print msg
$UGE_log_stack.push(msg)
end
#===============================================================================
# 函數: print_log(string)
#-------------------------------------------------------------------------------
# 說明:
# 輸出日誌堆疊中的所有記錄至filename指定的文件
#-------------------------------------------------------------------------------
# 參數:
# filename (string): 寫入到的文件
#-------------------------------------------------------------------------------
# 返回: void
#===============================================================================
def print_log(filename="runtime.log")
open(filename, "w") { |f|
$UGE_log_stack.each { |line|
f << line
}
}
end
[/pre][/fold]
[fold=角色幾何運算庫]
[pre lang="ruby"]
#===============================================================================
# Utility Game Libraries: 角色幾何運算庫
#-------------------------------------------------------------------------------
# 作者: RedSuns S.H.Chan (zeldafd@66rpg)
# 版本: 1.0
#-------------------------------------------------------------------------------
# 說明:
# 本腳本是用於在遊戲角色的角度或距離計算
# 另外提供了兩個於Game_Interpreter, 即事件解釋器用, 可於事件的腳本之中使用的方法
#===============================================================================
#===============================================================================
# 函數: distance_with_coordinate(double, double, double, double)
#-------------------------------------------------------------------------------
# 說明:
# 計算兩點之間的距離
#-------------------------------------------------------------------------------
# 參數:
# x1 (double): 第一個X坐標
# y1 (double): 第一個Y坐標
# x2 (double): 第二個X坐標
# y2 (double): 第二個Y坐標
#-------------------------------------------------------------------------------
# 返回: double
#===============================================================================
def distance_with_coordinate(x1, y1, x2, y2)
result = ((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))
if result < 0
result *= -1
end
return Math.sqrt(result)
end
#===============================================================================
# 函數: angle_with_coordinate(double, double, double, double)
#-------------------------------------------------------------------------------
# 說明:
# 計算由(x1, y1)測得(x2, y2)的角度
#-------------------------------------------------------------------------------
# 參數:
# x1 (double): 第一個X坐標
# y1 (double): 第一個Y坐標
# x2 (double): 第二個X坐標
# y2 (double): 第二個Y坐標
#-------------------------------------------------------------------------------
# 返回: double
#===============================================================================
def angle_with_coordinate(x1, y1, x2, y2)
x = x1 - x2
y = y1 - y2
result = (Math.atan2(y, x) / Math::PI * 180)
result += 90
if result < 0
result = 360 + result
end
return result
end
#===============================================================================
# 函數: distance_of(Game_CharacterBase, Game_CharacterBase)
#-------------------------------------------------------------------------------
# 說明:
# 計算由character1測得character2的距離
# character1及character2為Game_CharacterBase的實例
#-------------------------------------------------------------------------------
# 參數:
# character1 (Game_CharacterBase): 第一個角色
# character2 (Game_CharacterBase): 第二個角色
#-------------------------------------------------------------------------------
# 返回: double
#===============================================================================
def distance_of(character1, character2)
return distance_with_coordinate(character1.real_x, character1.real_y, character2.real_x, character2.real_y)
end
#===============================================================================
# 函數: angle_of(Game_CharacterBase, Game_CharacterBase)
#-------------------------------------------------------------------------------
# 說明:
# 計算由character1測得character2的角度
# character1及character2為Game_CharacterBase的實例
#-------------------------------------------------------------------------------
# 參數:
# character1 (Game_CharacterBase): 第一個角色
# character2 (Game_CharacterBase): 第二個角色
#-------------------------------------------------------------------------------
# 返回: double
#===============================================================================
def angle_of(character1, character2)
return angle_with_coordinate(character1.real_x, character1.real_y, character2.real_x, character2.real_y)
end
#===============================================================================
# 函數: distance_between_event_and_player(int)
#-------------------------------------------------------------------------------
# 說明:
# 計算玩家角色及指定事件ID的實例的距離
#-------------------------------------------------------------------------------
# 參數:
# event_id (int): 指定的事件ID
#-------------------------------------------------------------------------------
# 返回: double
#===============================================================================
def distance_between_event_and_player(event_id)
return distance_of($game_player, $game_map.events[event_id])
end
#===============================================================================
# 函數: angle_from_event_to_player(int)
#-------------------------------------------------------------------------------
# 說明:
# 計算玩家角色及指定事件ID的實例的角度
#-------------------------------------------------------------------------------
# 參數:
# event_id (int): 指定的事件ID
#-------------------------------------------------------------------------------
# 返回: double
#===============================================================================
def angle_from_event_to_player(event_id)
return angle_of($game_player, $game_map.events[event_id])
end
#===============================================================================
# 類別: Game_Interpreter
#-------------------------------------------------------------------------------
# 說明:
# 為事件解釋器加入幾何計算函數, 方便進行範圍的計算
#===============================================================================
class Game_Interpreter
#===============================================================================
# 函數: distance_with_player(void)
#-------------------------------------------------------------------------------
# 說明:
# 自動取得當前事件與玩家角色的距離
#-------------------------------------------------------------------------------
# 返回: double
#===============================================================================
def distance_with_player
return distance_between_event_and_player(self.event_id)
end
#===============================================================================
# 函數: angle_to_player(void)
#-------------------------------------------------------------------------------
# 說明:
# 自動取得當前事件與玩家角色的角度
#-------------------------------------------------------------------------------
# 返回: double
#===============================================================================
def angle_to_player
return angle_from_event_to_player(self.event_id)
end
end
[/pre][/fold]
[fold=Base64加密庫]
[pre lang="ruby"]
#===============================================================================
# Utility Game Libraries: Base64加密庫
#-------------------------------------------------------------------------------
# 作者: RedSuns S.H.Chan (zeldafd@66rpg)
# 版本: 1.0
#-------------------------------------------------------------------------------
# 說明:
# 本腳本是取自Ruby的核心庫, 以函數方式實現出來
# 腳本的函數定義不是本人原創, 我只是進行了整合
# 可以進行對遊戲數據的加密處理
#===============================================================================
#===============================================================================
# 函數: base64_encode(bin)
#-------------------------------------------------------------------------------
# 說明:
# 把指定的數據進行BASE64加密
#-------------------------------------------------------------------------------
# 參數:
# bin (mixed): 需要進行編碼的數據
#-------------------------------------------------------------------------------
# 返回: string
#===============================================================================
def base64_encode(bin)
[bin].pack("m")
end
#===============================================================================
# 函數: base64_decode(str)
#-------------------------------------------------------------------------------
# 說明:
# 把指定的數據進行BASE64解密
#-------------------------------------------------------------------------------
# 參數:
# str (string): 需要進行解碼的數據
#-------------------------------------------------------------------------------
# 返回: mixed
#===============================================================================
def base64_decode(str)
str.unpack("m").first
end
#===============================================================================
# 函數: base64_strict_encode(bin)
#-------------------------------------------------------------------------------
# 說明:
# 把指定的數據進行BASE64加密
#-------------------------------------------------------------------------------
# 參數:
# bin (mixed): 需要進行編碼的數據
#-------------------------------------------------------------------------------
# 返回: string
#===============================================================================
def base64_strict_encode(bin)
[bin].pack("m0")
end
#===============================================================================
# 函數: base64_strict_decode(str)
#-------------------------------------------------------------------------------
# 說明:
# 把指定的數據進行BASE64解密
#-------------------------------------------------------------------------------
# 參數:
# str (string): 需要進行解碼的數據
#-------------------------------------------------------------------------------
# 返回: mixed
#===============================================================================
def base64_strict_decode(str)
str.unpack("m0").first
end
#===============================================================================
# 函數: base64_urlsafe_encode(bin)
#-------------------------------------------------------------------------------
# 說明:
# 把指定的數據進行BASE64保持超連結的加密
#-------------------------------------------------------------------------------
# 參數:
# bin (mixed): 需要進行編碼的數據
#-------------------------------------------------------------------------------
# 返回: string
#===============================================================================
def base64_urlsafe_encode(bin)
base64_strict_encode(bin).tr("+/", "-_")
end
#===============================================================================
# 函數: base64_urlsafe_decode(str)
#-------------------------------------------------------------------------------
# 說明:
# 把指定的數據進行BASE64保持超連結的解密
#-------------------------------------------------------------------------------
# 參數:
# str (string): 需要進行解碼的數據
#-------------------------------------------------------------------------------
# 返回: mixed
#===============================================================================
def base64_urlsafe_decode(str)
base64_strict_decode(str.tr("-_", "+/"))
end
[/pre][/fold] |
评分
-
| 参与人数 1 | 星屑 +60 |
收起
理由
|
晴兰
| + 60 |
Math.hypot和Math.atan2以及弧角变换. |
查看全部评分
|