#===============================================================
# ■ 即时遇敌,
#---------------------------------------------------------------
# 碰到什么怪物,周围是什么怪物战斗中就是什么怪物
#===============================================================
=begin
使用说明
在地图上放置事件,事件的第一个指令用注释
在里面填写 敌人: ID (注意是半角冒号),决定触碰到此事件时所战斗的敌人
第二行可以填写 视野: 数字 ,决定本事件追逐主角的视野范围(默认为8)
触碰后就会触发战斗,敌人队伍依照主角身边的敌人事件设定生成
如果主角身边有5个敌人事件,则敌人队伍有五个
战斗胜利、失败、逃跑后的公共事件,先设定好,把ID填写在脚本当中
($win_common_event_id 这几行)
数据库里面的敌人队伍可以照常使用,没有影响。
作为敌人的事件也可以照常放置其他的指令,执行完后才会执行战斗
=end
$win_common_event_id = 1 # 战胜时的公共事件
$lose_common_event_id = 2 # 战败时的公共事件
$escape_common_event_id = 3 # 逃跑时的公共事件
class Game_Character < Game_CharacterBase
def move_type_toward_player
# 计算与主角的距离
sx = (@x - $game_player.x).abs
sy = (@y - $game_player.y).abs
# 如果纵横共计离开 20 个元件
if sx + sy >= approach_range
# 随机
move_random
return
end
# 随机 0~5 的分支
case rand(6)
when 0..3 # 接近主角
move_toward_player
when 4 # 随机
move_random
when 5 # 前进一步
move_forward
end
end
def approach_range
return 8
end
end
class Game_Event < Game_Character
#--------------------------------------------------------------------------
# ● 初始化对像
# map_id : 地图 ID
# event : 事件 (RPG::Event)
#--------------------------------------------------------------------------
alias old_initialize initialize
def initialize(map_id, event)
old_initialize(map_id, event)
name = event.name
@enemy_id = 0
@approach_range = 8
setup_as_enemy
end
#--------------------------------------------------------------------------
# ● 返回视野距离
#--------------------------------------------------------------------------
def approach_range
return @approach_range
end
#--------------------------------------------------------------------------
# ● 判断事件是否存在
#--------------------------------------------------------------------------
def exist?
return !@erased
end
#--------------------------------------------------------------------------
# ● 将事件设为敌人
#--------------------------------------------------------------------------
def setup_as_enemy
enemy_comment = @list[0]
scope_comment = @list[1] if @list[1] && @list[1].code == 408
return if enemy_comment.code != 108
# 读取敌人 ID
if enemy_comment.parameters[0] =~ /(?:敌人):(?:\s|)(\d+)/
@enemy_id = $1.to_i
else
return
end
# 读取敌人视野距离
if scope_comment.parameters[0] =~ /(?:视野):(?:\s|)(\d+)/
@approach_range = $1.to_i
end
command = RPG::EventCommand.new(355, 0, ["$instant_encounter_battle=true"])
@list.push(command) # 执行脚本
command = RPG::EventCommand.new(301, 0, [0, 1, true, true])
@list.push(command) # 战斗处理
command = RPG::EventCommand.new(601, 0)
@list.push(command) # 战胜时的场合
command = RPG::EventCommand.new(117, 1, [$win_common_event_id])
@list.push(command) # 呼叫公共事件
command = RPG::EventCommand.new(602, 0)
@list.push(command) # 逃跑时的场合
command = RPG::EventCommand.new(117, 1, [$escape_common_event_id])
@list.push(command) # 呼叫公共事件
command = RPG::EventCommand.new(603, 0)
@list.push(command) # 战败时的场合
command = RPG::EventCommand.new(117, 1, [$lose_common_event_id])
@list.push(command) # 呼叫公共事件
command = RPG::EventCommand.new(604, 0)
@list.push(command) # 战斗处理结束
command = RPG::EventCommand.new
@list.push(command)
end
#--------------------------------------------------------------------------
# ● 判断事件是否为敌人
#--------------------------------------------------------------------------
def isEnemy?
return @enemy_id > 0
end
attr_reader :enemy_id # 指定的敌人ID
end
class Game_Map
ENCOUNTER_RANGE = 3 #这个代表遇到敌人的范围大小
#--------------------------------------------------------------------------
# ●获取指定范围内的事件
# x : X 坐标
# y : Y 坐标
# l : 范围
#--------------------------------------------------------------------------
def scope_event
scope_event = []
x = $game_player.x
y = $game_player.y
for event in $game_map.events.values
next unless event.isEnemy?
if (event.x - x).abs + (event.y - y).abs <=ENCOUNTER_RANGE and event.exist?
scope_event.push(event.id)
break if scope_event.size >= 8
end
end
return scope_event
end
end
module BattleManager
#--------------------------------------------------------------------------
# ● 处理战斗结束
#--------------------------------------------------------------------------
class << self
alias instant_encounter_setup setup
def setup(troop_id, can_escape = true, can_lose = false)
if $instant_encounter_battle
$data_troops[1].members = []
scope_event = $game_map.scope_event
size = scope_event.size
for i in 1..size
temp = RPG::Troop::Member.new
temp.enemy_id = $game_map.events[scope_event[i-1]].enemy_id
if size > 4
temp.x = Graphics.width * (i - 0.5) / size
temp.y = 288 / 2*(i%2+1)
temp.y += 100*((i+1)%2)
else
temp.x = Graphics.width * (i - 0.5) / size
temp.y = 288
end
$data_troops[1].members.push(temp)
$instant_encounter_battle = false
end
end
instant_encounter_setup(troop_id, can_escape, can_lose)
end
alias instant_encounter_battle_end battle_end
def battle_end(result)
if result == 0
# 战斗结束后清除事件(未完成)
scope_event = $game_map.scope_event
$game_troop.members.size.times{ |i|
$game_map.events[scope_event[i]].erase
}
end
instant_encounter_battle_end(result)
end
end
end