Boa Tarde Aldeia
Este é um script do Mendesx que já tem um tempinho que foi criado. Já foi postado em outras comunidades, mas agora eu queria botar aqui, porque acho que ele pode vir a ser útil em alguns jogos. Enfim, é um script que permite que o personagem se mova pelo mapa clicando com o mouse, como em MMORPGs, por exemplo.
créditos a:
Cybersam, pelo módulo que permite o clique do mouse, a Near Fantastica, pelo módulo que permite ter a posição do mouse e também pelo script de path finding, e ao Mendesx, por "juntar" os três scripts e adicionar alguns códigos, para permitir o movimento.
O efeito não é perceptível por imagens.
O script:
- Código:
#================================================================
# Movimento pelo Mouse
# Autor: Mendesx
#================================================================
# Este script permite movimentar o herói pelo clique do mouse.
# Créditos:
# - Cybersam e Near Fantastica
#================================================================
#======================================
# ■ Mouse Input - Near Fantastica
#======================================
module Mouse
@position
GSM = Win32API.new('user32', 'GetSystemMetrics', 'i', 'i')
Cursor_Pos= Win32API.new('user32', 'GetCursorPos', 'p', 'i')
Scr2cli = Win32API.new('user32', 'ScreenToClient', %w(l p), 'i')
Client_rect = Win32API.new('user32', 'GetClientRect', %w(l p), 'i')
Readini = Win32API.new('kernel32', 'GetPrivateProfileStringA', %w(p p p p l p), 'l')
Findwindow = Win32API.new('user32', 'FindWindowA', %w(p p), 'l')
def Mouse.grid
return nil if @pos == nil
x = @pos[0] / 32
y = @pos[1] / 32
x = x * 32
y = y * 32
return [x, y]
end
def Mouse.position
return @pos == nil ? [0, 0] : @pos
end
def Mouse.global_pos
pos = [0, 0].pack('ll')
if Cursor_Pos.call(pos) != 0
return pos.unpack('ll')
else
return nil
end
end
def Mouse.pos
x, y = Mouse.screen_to_client(*Mouse.global_pos)
begin
return x, y
rescue
return -1, -1
end
end
def Mouse.update
@pos = Mouse.pos
end
def Mouse.screen_to_client(x, y)
return nil unless x and y
pos = [x, y].pack('ll')
if Scr2cli.call(Mouse.hwnd, pos) != 0
return pos.unpack('ll')
else
return nil
end
end
def Mouse.hwnd
game_name = "\0" * 256
Readini.call('Game','Title','',game_name,255,".\\Game.ini")
game_name.delete!("\0")
return Findwindow.call('RGSS Player',game_name)
end
def Mouse.client_size
rect = [0, 0, 0, 0].pack('l4')
Client_rect.call(Mouse.hwnd, rect)
right, bottom = rect.unpack('l4')[2..3]
return right, bottom
end
end
#==============================================================================
# Keyboard Input Module - Cybersam
#==============================================================================
module Input
@keys = []
@pressed = []
Mouse_Left = 1
Mouse_Right = 2
Mouse_Middle = 4
USED_KEYS = [Mouse_Left, Mouse_Right, Mouse_Middle]
module_function
def triggered?(key)
Win32API.new("user32","GetAsyncKeyState",['i'],'i').call(key) & 0x01 == 1
end
def check(key)
Win32API.new("user32","GetAsyncKeyState",['i'],'i').call(key) & 0x01 == 1
end
def pressed?(key)
return true unless Win32API.new("user32","GetKeyState",['i'],'i').call(key).between?(0, 1)
return false
end
def mouse_update
@used_i = []
for i in USED_KEYS
x = check(i)
if x == true
@used_i.push(i)
end
end
end
end
#==============================================================================
# Path Finding
#==============================================================================
# Near Fantastica
#===========================================================
class Game_Character
#--------------------------------------------------------------------------
alias nf_pf_game_character_initialize initialize
alias nf_pf_game_character_update update
#--------------------------------------------------------------------------
attr_accessor :map
attr_accessor :runpath
#--------------------------------------------------------------------------
def initialize
nf_pf_game_character_initialize
@map = nil
@runpath = false
end
#--------------------------------------------------------------------------
def update
run_path if @runpath == true
nf_pf_game_character_update
end
#--------------------------------------------------------------------------
def run_path
return if moving?
step = @map[@x,@y]
if step == 1
@map = nil
@runpath = false
return
end
dir = rand(2)
case dir
when 0
move_right if @map[@x+1,@y] == step - 1 and step != 0
move_down if @map[@x,@y+1] == step - 1 and step != 0
move_left if @map[@x-1,@y] == step -1 and step != 0
move_up if @map[@x,@y-1] == step - 1 and step != 0
when 1
move_up if @map[@x,@y-1] == step - 1 and step != 0
move_left if @map[@x-1,@y] == step -1 and step != 0
move_down if @map[@x,@y+1] == step - 1 and step != 0
move_right if @map[@x+1,@y] == step - 1 and step != 0
end
end
#--------------------------------------------------------------------------
def find_path(x,y)
sx, sy = @x, @y
result = setup_map(sx,sy,x,y)
@runpath = result[0]
@map = result[1]
@map[sx,sy] = result[2] if result[2] != nil
end
#--------------------------------------------------------------------------
def clear_path
@map = nil
@runpath = false
end
#--------------------------------------------------------------------------
def setup_map(sx,sy,ex,ey)
map = Table.new($game_map.width, $game_map.height)
map[ex,ey] = 1
old_positions = []
new_positions = []
old_positions.push([ex, ey])
depth = 2
depth.upto(100){|step|
loop do
break if old_positions[0] == nil
x,y = old_positions.shift
return [true, map, step] if x == sx and y+1 == sy
if $game_player.passable?(x, y, 2) and map[x,y + 1] == 0
map[x,y + 1] = step
new_positions.push([x,y + 1])
end
return [true, map, step] if x-1 == sx and y == sy
if $game_player.passable?(x, y, 4) and map[x - 1,y] == 0
map[x - 1,y] = step
new_positions.push([x - 1,y])
end
return [true, map, step] if x+1 == sx and y == sy
if $game_player.passable?(x, y, 6) and map[x + 1,y] == 0
map[x + 1,y] = step
new_positions.push([x + 1,y])
end
return [true, map, step] if x == sx and y-1 == sy
if $game_player.passable?(x, y, 8) and map[x,y - 1] == 0
map[x,y - 1] = step
new_positions.push([x,y - 1])
end
end
old_positions = new_positions
new_positions = []
}
return [false, nil, nil]
end
end
class Game_Map
#--------------------------------------------------------------------------
alias pf_game_map_setup setup
#--------------------------------------------------------------------------
def setup(map_id)
pf_game_map_setup(map_id)
$game_player.clear_path
end
end
class Game_Player
def update_player_movement
$game_player.clear_path if Input.dir4 != 0
# Move player in the direction the directional button is being pressed
case Input.dir4
when 2
move_down
when 4
move_left
when 6
move_right
when 8
move_up
end
end
end
class Interpreter
#--------------------------------------------------------------------------
def event
return $game_map.events[@event_id]
end
end
class Game_Player < Game_Character
alias move update
def update
move
mx = (Mouse.pos[0] + $game_map.display_x / 4) / 32
my = (Mouse.pos[1] + $game_map.display_y / 4) / 32
unless moving? or $game_system.map_interpreter.running? or
@move_route_forcing or $game_temp.message_window_showing
if Input.triggered?(Input::Mouse_Left)
find_path(mx, my)
end
end
end
end
Mendex