On-Click-Movement ( with 3 Scripts and Edits )

Hello,

ThatÂÂÂ ´s my first Topic of a Script...:D

!!!! JUST THE GAME_PLAYER EDIT IS MADE BY ME, THE REST IS FROM PEOPLE OF MY GUILD !!!!


So... Someone has asked for this. Here is it. A Diablo like Movement ( without Pathfinding ). The Only thing you must do, are these 6 Steps. so..go go go...


STEP I
create a new Script above Main and paste this into it:
MOUSE MODULE Spoiler
module Input
VK_LBUTTON = 1
VK_RBUTTON = 2
VK_MBUTTON = 4
SM_SWAPBUTTON = 23
$mousez = Win32API.new 'user32', 'ShowCursor', 'l',''

gsm = Win32API.new 'user32', 'GetSystemMetrics', 'i', 'i'

@@swapped = gsm.call(SM_SWAPBUTTON) != 0
@@key_state = Win32API.new 'user32', 'GetAsyncKeyState',
'i', 'i'
@@cursor_pos = Win32API.new 'user32', 'GetCursorPos',
'p', 'i'

module_function

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(catch_anywhere = false)
x, y = screen_to_client(*mouse_global_pos)
width, height = client_size
if catch_anywhere or (x >= 0 and y >= 0 and x < width and y < height)
if($game_map.display_x == 0 or $game_map.display_x == ($game_map.width-20)*128)
$game_variables[1] = x / 32 +$game_map.display_x / 128
else
$game_variables[1] = (x+16) / 32 +$game_map.display_x / 128
end
$game_variables[2] = y / 32 + $game_map.display_y / 128
x = x-16
y = y-16
end
return x, y
else
return nil
end
end

def mouse_lbutton(ignore_swap = false)
if @@swapped and !ignore_swap
button = @@key_state.call(VK_RBUTTON)
else
button = @@key_state.call(VK_LBUTTON)
end
return(button == -32767 or button == 32768)
end
def mouse_rbutton(ignore_swap = false)
if @@swapped and !ignore_swap
button = @@key_state.call(VK_LBUTTON)
else
button = @@key_state.call(VK_RBUTTON)
end
return(button == -32767 or button == 32768)
end
def mouse_mbuton
button = @@key_state.call(VK_MBUTTON)
return(button == -32767 or button == 32768)
end


$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 screen_to_client(x, y)
return nil unless x and y
pos = [x, y].pack('ll')
if $scr2cli.call(hwnd, pos) != 0
return pos.unpack('ll')
else
return nil
end
end

def 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 client_size
rect = [0, 0, 0, 0].pack('l4')
$client_rect.call(hwnd, rect)
right, bottom = rect.unpack('l4')[2..3]
return right, bottom
end


STEP II
create a new Script BELOW the one above and paste this into it:
MOUSE CURSOR Spoiler
class Maus_Anzeigen
def initialize
$mausz = 0
x, y = Input.mouse_pos
if x and y
$temp = Maus_Auswahl.new(x,y)
end
end
def update
x, y = Input.mouse_pos
if x and y
if $temp
$temp.dispose
$temp = Maus_Auswahl.new(x,y)
else
$temp = Maus_Auswahl.new(x,y)
end
end
end
end

class Maus_Auswahl < Sprite
def initialize(px, py)
super()
@x = px
@y = py
$mausz += 1
if $mausz < 10
if $key.getkey >= 2
#RIGHT CLICK
self.bitmap = RPG::Cache.picture("ZielKreuz2.png")
$game_switches[1] = false
$game_switches[2] = true
$mausz = 1
else
if $key.getkey >= 1
#LEFT CLICK
self.bitmap = RPG::Cache.picture("ZielKreuz2.png")
$game_switches[1] = true
$game_switches[2] = false
$mausz = 1
else
#NO CLICK
self.bitmap = RPG::Cache.picture("ZielKreuz.png")
$game_switches[1] = false
$game_switches[2] = false
$mausz = 1
end
end
end
self.x = @x
self.y = @y
self.z = 65535
self.opacity = 255
self.visible = true
end
def dispose
if self.bitmap != nil
self.bitmap.dispose
end
super
end
end


STEP III
make a new Script BELOW the Second and paste this into it:
KEY_SCRIPT Spoiler
class Key_Abfrage
def initialize
@GetAsyncKeyState = Win32API.new 'user32', 'GetAsyncKeyState', 'l', 'l'
@keybd_event = Win32API.new 'user32', 'keybd_event', %w(l l l l), ''
end
def getkey
for i in 0..2
if @GetAsyncKeyState.call(i) != 0
return i
end
end
return 0
end
def getkey_id(id)
if @GetAsyncKeyState.call(id) != 0
return 1
end
return 0
end
def getkey_var(var)
for i in 0..255
if @GetAsyncKeyState.call(i) != 0
$game_variables[var] = i
end
end
end
def getkey_var_id(var,id)
if @GetAsyncKeyState.call(id) != 0
$game_variables[var] = 1
else
$game_variables[var] = 0
end
end
def setkey_id_down(id)
@keybd_event.call(id,0,0,0)
end
def setkey_var_down(var)
@keybd_event.call($game_variables[var],0,0,0)
end
def setkey_id_up(id)
@keybd_event.call(id,0,2,0)
end
def setkey_var_up(var)
@keybd_event.call($game_variables[var],0,2,0)
end
end



STEP IV
to make the Mouse moving. go to Main and paste this below
line 14 -- Graphics.freeze
Quote:
$key = Key_Abfrage.new


Go to Scene_Title and paste the following under
$game_player = Game_Player.new
Quote:
$maus = Maus_Anzeigen.new


Now go to Scene_Map and search for this Lines:
Quote:
if $scene != self
break
end
end

under the second End, paste this:
Quote:
$maus.update


in def update after "loop do" paste this:
Quote:
$maus.update



STEP V
To make the Player move, youÂÂÂ ´ll need a Common Event, or a Script that sets the variables 3 to heroÂÂÂ ´s Tile_X and the Variable 4 to heroÂÂÂ ´s Tile_Y.


STEP VI
now the last part. YouÂÂÂ ´ve made all the edits and the Common Event. Now we can make the Move-Code.
Go to Game_Player and serach for this lines:
Quote:
case Input.dir4
when 2
move_down
when 4
move_left
when 6
move_right
when 8
move_up
end
end


Replace them all ( with the TWO Ends ) with that:
Quote:
if $game_switches[1] == true
if $game_variables[1] > $game_variables[3]
if $game_variables[2] > $game_variables[4]
move_down
move_right
else
if $game_variables[2] == $game_variables[4]
move_right
else
move_up
move_right
end
end
else
if $game_variables[1] < $game_variables[3]
if $game_variables[2] > $game_variables[4]
move_down
move_left
else
if $game_variables[2] == $game_variables[4]
move_left
else
move_up
move_left
end
end
else
if $game_variables[1] == $game_variables[3]
if $game_variables[2] > $game_variables[4]
move_down
else
if $game_variables[2] < $game_variables[4]
move_up
else
#nix
end
end
else
#nix
end
end
end
else
#nix
end
end



STEP VII


IMPORT THE TWO PICTURES FROM THE ATTACHMENT TO YOUR PICTURES FOLDER....


YOU`VE DONE. NOW TEST YOUR MOVEMENT. YOU CAN MOVE WHILE PRESSING THE LEFT MOUSEBUTTON.

Ps.: 8-Directions.



###########################################################################################
Credits to:

--==Sir Lord Biowulve==--
Geo
Light Dark Side

###########################################################################################







end---------
I'm getting arror on line 30 Mouse Module.

"undefined method '>=" for nil:NilClass"

it's this one:
if catch_anywhere or (x >= 0 and y >= 0 and x < width and y < height)
Expand to see the code.
hmmm...
3 reasons...

1. Are you using SDK? The Script is at the moment not SDK comatible
2. YouÂÂÂ ´re using RGSS100J.dll
3. YouÂÂÂ ´re using a MAIN Script from another RGSS.dll as the original...


For solving Problems:

I packed a Demo-Project and edited it to the Attachments of the Start-Post.
I am not using SDK, and I pasted the scripts into a new project with version 1.02. but that does't matter since the demo is working great. I like it very much and will try implementing it to f0tz!'s pixelmovement :)
It´s working fine with the pixelmovement from f0tz!'s

I know it...without a doubt... because i tested it ^^ We are in the same German Forum..

The only thing you must Change if you use the pixel-movement is in game_Player. All Move-Command...

example:

move_down

to

move_down(pixels)


that´s all
ah well, forget it
Please speak english on these forums, as this is a english speaking forum. A few words (in what I assume to be german) in a post isn't a horrible thing, but put something in it the rest of us can read. Otherwise we start to end up with a chain of post that are in a language only 5% of the people can understand, making it practically spam.

Thank you.
mh.. quite a nice script, although it seems to be a bit too complicated (e.g. using a common event.. instead of this and the game variables for the coordinates you could also use $game_player.x/y, cant you?)
but it works.. and thats the most important thing.. ^^

btw.. normally you neednt add the pixels to the move commands while using the pixelmovement script.. 32Px is the standard setting.. ;)

-f0tz!

[i]edit: @Nova-MK-VI: imagine everybody would do this.. then this forum wouldnt make any sense anymore.. (aber ich hab die selbe erfahrung gemacht^^ )
Quite right...I appreciate you doing this. Now as f0tz! has said, why use any type of events with scripts? Events + script = meh :)

If you could just sum it up in a script, that would be better imo. Also...pixel movement...if possible :p. I'm attempting to make an isometric game so I will need all courses of collision covered, in a more realistic setting if you catch my drift.

~ Wings
http://www.calamity.cba.pl/files/myszopiksel.rar
I'v cut out the pathfinding nad the caterpillar. unfortunatelly there's this bug when going iso (the "jumping" of the sprite). f0tz!, if You could piont which line exactly xcauses this bug, I'd appreciate it :)
this problem will be fixed in the new version.. ^^
but I think you should wait anyway until this script is optimized a bit.. ;)

-f0tz!
Where is The second end?
how bout making a demo? It would be realy helpfull :p
I'd be glad to get the new version since I get the same error as Arevulopapo ( Mouse Module line 38 )
if catch_anywhere or (x >= 0 and y >= 0 and x < width and y < height)
me to have this Provlem I under stand The error

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=3839