RPG-Maker Quartier

Hier dreht sich alles um die RPG-Maker-Reihe von ASCII/Enterbrain. Der RPG-Maker ist ein Tool, mit dem du dir dein eigenes kleines Rollenspiel erstellen kannst. Du findest hier alles, was du dazu brauchst. Aber natürlich umfasst die Community noch mehr!
Aktuelle Zeit: Fr Sep 20, 2019 4:03

Alle Zeiten sind UTC + 1 Stunde



Mitglieder in diesem Forum: 0 Mitglieder und 3 Gäste



Ein neues Thema erstellen Auf das Thema antworten  [ 4 Beiträge ] 
Autor Nachricht
Offline
Dieb
Dieb
Benutzeravatar
Beiträge: 8611
Alter: 30
BeitragVerfasst: Mo Mär 19, 2012 14:53 
Yo.

Code:
#==============================================================================
#
# Mouse Script 1.0 created by: cybersam
# edited by: MagicMagor
#
#==============================================================================
#
# (got rid of cybersams explanation here, because it isn't true for this module)
# MagicMagor
#==============================================================================

#==============================================================================
# I have edited cybersams script, to fit my needs and my personal style of
# coding. I have also extended the script and got rid of several bugs.
# If you use this, give credit to cybersam and me.
#------------------------------------------------------------------------------
# MagicMagor
#==============================================================================

module Mouse

MOUSE_LEFT = 0x01 # left mouse button
MOUSE_RIGHT = 0x02 # right mouse button

attr_reader :MOUSE_LEFT, :MOUSE_RIGHT

# Flags

INIT_CURSOR = 0x01
INIT_SAVE = 0x02
INIT_TYPE = 0x03

CURSOR_PIC = 0x01
CURSOR_ROOT = 0x02
CURSOR_BITMAP = 0x03

attr_reader :INIT_CURSOR, :INIT_SAVE, :INIT_TYPE,
:CURSOR_PIC, :CURSOR_ROOT, :CURSOR_BITMAP


@getCursorPos = Win32API.new('user32', 'GetCursorPos', 'p', 'i')
@getWindowRect = Win32API.new('user32', 'GetWindowRect', ['p', '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', 'FindWindow', %w(p p), 'l')
@getKeyState = Win32API.new("user32","GetAsyncKeyState",['i'],'i')

#---------------------------------------------------------------------------
# Initialize the mouse-system, with given arguments. Is automaticly called
# if needed.
#---------------------------------------------------------------------------
def Mouse.initSystem(*args)
@init = true
@handle = self.getHandle()
@cursor = Sprite.new()
if args.length >= 1
flags = args[0]
@showCursor = (flags & INIT_CURSOR)
@saveToGame = (flags & INIT_SAVE)
withType = (flags & INIT_TYPE)
if (@showCursor != 0)
@showCursor = true
else
@showCursor = false
end
if (@saveToGame != 0)
@saveToGame = true
else
@saveToGame = false
end
@cursor.visible = @showCursor
@originX = 0
@originY = 0
if (@saveToGame)
@gameX = args[-2]
@gameY = args[-1]
end
if (@showCursor)
if (withType)
self.setCursor(args[1], args[2])
else
self.setCursor(args[1])
end
end
end
return nil
end

#---------------------------------------------------------------------------
# Checks if key was pressed.
#---------------------------------------------------------------------------
def Mouse.pressed?(key)
return ((@getKeyState.call(key) & 0x01) == 1)
end

#---------------------------------------------------------------------------
# Returns true if saving mouse positions to game-variables is enabled
#---------------------------------------------------------------------------
def Mouse.saveToGame?()
if !(@init)
self.initSystem(nil)
end
return @saveToGame
end

#---------------------------------------------------------------------------
# Enables saving the mouse positions to game-variables
#---------------------------------------------------------------------------
def Mouse.enableSaveToGame(game_x = nil, game_y = nil )
if !(@init)
self.initSystem(nil)
end
@saveToGame = true
if (game_x != nil)
@gameX = game_x
end
if (game_y != nil)
@gameY = game_y
end
return nil
end

#---------------------------------------------------------------------------
# Disables saving the mouse positions to game-variables
#---------------------------------------------------------------------------
def Mouse.disableSaveToGame()
if !(@init)
self.initSystem(nil)
end
@saveToGame = false
return nil
end

#---------------------------------------------------------------------------
# Sets the variable-ID in which the X position is saved
#---------------------------------------------------------------------------
def Mouse.setGameX(game_x)
if !(@init)
self.initSystem(nil)
end
@gameX = game_x
return nil
end

#---------------------------------------------------------------------------
# Sets the variable-ID in which the Y position is saved
#---------------------------------------------------------------------------
def Mouse.setGameY(game_y)
if !(@init)
self.initSystem(nil)
end
@gameY = game_y
return nil
end

#---------------------------------------------------------------------------
# Returns the variable-ID in which the X position is saved
#---------------------------------------------------------------------------
def Mouse.gameX
return @gameX
end

#---------------------------------------------------------------------------
# Returns the variable-ID in which the Y position is saved
#---------------------------------------------------------------------------
def Mouse.gameY
return @gameY
end

#---------------------------------------------------------------------------
# Returns X-Value of mouse positions
#---------------------------------------------------------------------------
def Mouse.x()
if !(@init)
self.initSystem(nil)
end
x, y = Mouse.mousePos()
return x
end

#---------------------------------------------------------------------------
# Returns Y-Value of mouse positions
#---------------------------------------------------------------------------
def Mouse.y()
if !(@init)
self.initSystem(nil)
end
x, y = Mouse.mousePos()
return y
end

#---------------------------------------------------------------------------
# Returns TRUE when the mouse-cursor is shown.
#---------------------------------------------------------------------------
def Mouse.shown?()
if !(@init)
self.initSystem(nil)
end
return @showCursor
end

#---------------------------------------------------------------------------
# Enables showing of the mouse-cursor.
#---------------------------------------------------------------------------
def Mouse.enableShow()
if !(@init)
self.initSystem(nil)
end
@showCursor = true
@cursor.visible = true
return nil
end

#---------------------------------------------------------------------------
# Disable showing of the mouse-cursor.
#---------------------------------------------------------------------------
def Mouse.disableShow()
if !(@init)
self.initSystem(nil)
end
@showCursor = false
@cursor.visible = false
return nil
end

#---------------------------------------------------------------------------
# Sets a new bitmap for the cursor.
#---------------------------------------------------------------------------

def Mouse.setCursor(filename, type = CURSOR_PIC, originX = 0, originY = 0)
if !(@init)
self.initSystem(nil)
end
@originX = originX
@originY = originY
case type
when CURSOR_PIC
@cursor.bitmap = RPG::Cache.picture(filename)
@cursor.z = 999
return nil
when CURSOR_ROOT
@cursor.bitmap = Bitmap.new(filename)
@cursor.z = 999
return nil
when CURSOR_BITMAP
@cursor.bitmap = filename
@cursor.z = 999
return nil
end
end

#---------------------------------------------------------------------------
# Sets the x-coordinate of the cursor-picture origin.
#---------------------------------------------------------------------------
def Mouse.setOriginX(originX)
if !(@init)
self.initSystem(nil)
end
@originX = originX
return nil
end

#---------------------------------------------------------------------------
# Sets the y-coordinate of the cursor-picture origin.
#---------------------------------------------------------------------------
def Mouse.setOriginY(originY)
if !(@init)
self.initSystem(nil)
end
@originY = originY
return nil
end

#---------------------------------------------------------------------------
# Updates the position of the mouse-cursor on screen.
#---------------------------------------------------------------------------
def Mouse.update()
if !(@init)
self.initSystem(nil)
end
if (@showCursor)
if Mouse.x() != nil
@cursor.x = Mouse.x() - @originX
end
if Mouse.y() != nil
@cursor.y = Mouse.y() - @originY
end
end
if ((@saveToGame) && (@gameX != nil) && (@gameY != nil))
$game_variables[@gameX] = Mouse.x()
$game_variables[@gameY] = Mouse.y()
end
return nil
end

#================================================= ==============================
# Private methods start here
# Do not call them directly.
# Edit them only if you know what you are doing.
#================================================= ==============================

#---------------------------------------------------------------------------
# Calculates mouse position inside the programms window.
#---------------------------------------------------------------------------
def Mouse.mousePos(catch_anywhere = false)
x, y = Mouse.screenToClient(*Mouse.getPos)
width, height = Mouse.clientSize()
if ((x == nil) || (y == nil))
return nil
end
if ((catch_anywhere) || ((x >= 0) && (y >= 0) && (x < width) && (y < height)))
return x, y
else
return nil
end
end

#---------------------------------------------------------------------------
# Gets mouse position from OS.
#---------------------------------------------------------------------------
def Mouse.getPos()
if !(@init)
Mouse.initSystem(nil)
end
pos = [0, 0].pack('ll')
if @getCursorPos.call(pos) != 0
return pos.unpack('ll')
else
return nil
end
end

#---------------------------------------------------------------------------
# Transforms screen-coordinates in client-coordinates.
#---------------------------------------------------------------------------
def Mouse.screenToClient(x, y)
if !(@init)
self.initSystem(nil)
end
return nil unless x and y
pos = [x, y].pack('ll')
if @scr2cli.call(@handle, pos) != 0
return pos.unpack('ll')
else
return nil
end
end

#---------------------------------------------------------------------------
# Gets the client size from OS.
#---------------------------------------------------------------------------
def Mouse.clientSize()
if !(@init)
self.initSystem(nil)
end
rect = [0, 0, 0, 0].pack('l4')
@client_rect.call(@handle, rect)
right, bottom = rect.unpack('l4')[2..3]
return right, bottom
end

#---------------------------------------------------------------------------
# Gets the windows handle from OS.
#---------------------------------------------------------------------------
def Mouse.getHandle()
if !(@init)
self.initSystem(nil)
end
gameName = "\0" * 256
@readini.call('Game', 'Title', '', gameName, 255, ".\\Game.ini")
gameName.delete!("\0")
if ($DEBUG)
# Only one RGSS Player should be open
result = @findwindow.call('RGSS Player', 0)
else
result = @findwindow.call('RGSS Player', gameName)
end
return result
end

end


Ich habe Schwierigkeiten mit der Funktion m.pressed?(key). Wenn ich sie in einer Conditional Branch oder per if (m.pressed?(m::MOUSE_LEFT)) aufrufe, wird der entsprechende Code danach nur sehr sehr selten ausgeführt. Beispiel:

Ich habe ein PP, das diesen Code ständig ausführt:
Code:
if (Mouse.pressed?(Mouse::MOUSE_LEFT))
print "Yo."
end
m.update()


Erst wenn ich auf die linke Maustaste hämmere wie bekloppt, kommt irgendwann die "Yo."-Nachricht. Das ist extrem ärgerlich und ich weiß nicht, woran das liegen könnte. Weiß da einer Rat?

Danke schonmal.

_________________
Was mach ich hier überhaupt


Nach oben
 Profil  
Mit Zitat antworten  
Offline
Official Oldschool
Official Oldschool
Benutzeravatar
Beiträge: 8917
Alter: 30
Wohnort: BRD, Thüringen
BeitragVerfasst: Mo Mär 19, 2012 18:44 
Das Problem hängt damit zusammen, dass der parallele Prozess nur alle 2 Frames aufgerufen wird. Du hast also eine 50%ige Chance den Mausklick zu verpassen.

Ich hab hier noch eine bearbeitete Version des Mausskripts rumliegen:
Code:
#==============================================================================
#
# Mouse Script v1                                       created by: cybersam
#                                                       edited by: MagicMagor
#                                                                  KD
#
#==============================================================================
# Hier handelt es sich um das Mouse-Script von Cybersam und MagicMagor
#
#------------------------------------------------------------------------------
module Mouse
 
  MOUSE_LEFT    = 0x01        # left mouse button
  MOUSE_RIGHT   = 0x02        # right mouse button
  MOUSE_MIDDLE  = 0x04        # middle mouse button
 
  MSB = 1<<15
 
  @getCursorPos = Win32API.new('user32', 'GetCursorPos', 'p', 'i')
  @getWindowRect = Win32API.new('user32', 'GetWindowRect', ['p', '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', 'FindWindow', %w(p p), 'l')
  @getKeyState = Win32API.new("user32","GetAsyncKeyState",['i'],'i')
 
  class Cursor < Sprite
    ORIGIN_X = 0
    ORIGIN_Y = 0
    CURSOR_BITMAP = "cursor"
    def initialize
      @cursor_viewport = Viewport.new(0,0,640, 480)
      @cursor_viewport.z = 9999
      super(@cursor_viewport)
      self.bitmap = RPG::Cache.picture(CURSOR_BITMAP)
      self.visible = false
      self.ox = ORIGIN_X
      self.oy = ORIGIN_Y
    end
   
    def show
      self.visible = true
    end
   
    def hide
      self.visible = false
    end
   
    def set(x,y)
      self.x, self.y = x,y
    end
   
  end
 
  class MouseKey
   
    def initialize
      inf = 1.0/0.0
      @released_since = -inf # time of last release
      @pressed_since = -inf # time of last press down
      @hold = false # current status
      @double_click_intervall = inf # duration between the last two releases
    end
   
    def last_release
      @released_since
    end
   
    def last_press
      @pressed_since
    end
   
    def down?
      @hold
    end
   
    def clicked?
      @released_since == Mouse.update_counter
    end
   
    def clicked_since? time, intervall=40
      @released_since >= Mouse.update_counter-time && @released_since - @pressed_since <= intervall
    end
   
    def pressed?
      @hold
    end
   
    def pressed_since? time
      @hold && @released_since < @pressed_since && @pressed_since >= Mouse.update_counter - time
    end
   
    def double_clicked? time=0, intervall=10
      clicked_since?(time) && @double_click_intervall <= intervall
    end
   
    def update counter, status
      return if status == @hold # nothing changes
      @hold = status
      if status
        @pressed_since = counter
      else
        @double_click_intervall = counter - @released_since
        @released_since = counter
      end
    end
   
  end
 
  class << self
    attr_reader :cursor, :x, :y, :left, :middle, :right, :update_counter
    #---------------------------------------------------------------------------
    # Initialisierung der Maus. Wird zu Spielbeginn aufgerufen
    #---------------------------------------------------------------------------
    def init
      @handle = getHandle()
      @cursor = Cursor.new()
      @update_counter = 0
      @left = MouseKey.new
      @middle = MouseKey.new
      @right = MouseKey.new
      @moving = false
      @x = 0
      @y = 0
    end
   
    def update
      @update_counter += 1
      update_pos
      update_buttons
    end
   
    def clicked?
      left.clicked? || middle.clicked? || right.clicked?
    end
   
    def logical_x
      @x + @cursor.ox
    end
   
    def logical_y
      @y + @cursor.oy
    end
   
    def moved?
      @moving
    end
   
    private
   
    def update_pos
      x,y = screenToClient(*get_pos)
      @moving = (x != @x) || (y != @y)
      @cursor.set(x,y)
      @x = x
      @y = y
    end
   
    def update_buttons
      left.update @update_counter, key_down?(MOUSE_LEFT)
      middle.update @update_counter, key_down?(MOUSE_MIDDLE)
      right.update @update_counter, key_down?(MOUSE_RIGHT)
    end
   
    #---------------------------------------------------------------------------
    # Checks if key is pressed down.
    #--------------------------------------------------------------------------- 
    def key_down?(key)
      (@getKeyState.call(key) & MSB) == MSB
    end
    #---------------------------------------------------------------------------
    # Gets mouse position from OS.
    #---------------------------------------------------------------------------
    def get_pos
      pos = [0, 0].pack('ll')
      if @getCursorPos.call(pos) != 0
        return pos.unpack('ll')
      else
        [0,0]
      end
    end
    #---------------------------------------------------------------------------
    # Gets the client size from OS.
    #---------------------------------------------------------------------------   
    def clientSize()
      rect = [0, 0, 0, 0].pack('l4')
      @client_rect.call(@handle, rect)
      right, bottom = rect.unpack('l4')[2..3]
      return right, bottom
    end
    #---------------------------------------------------------------------------
    # Transforms screen-coordinates in client-coordinates.
    #---------------------------------------------------------------------------   
    def screenToClient(x, y)
      pos = [x, y].pack('ll')
      if @scr2cli.call(@handle, pos) != 0
        return pos.unpack('ll')
      else
        [0,0]
      end
    end
    #---------------------------------------------------------------------------
    # Gets the windows handle from OS.
    #---------------------------------------------------------------------------   
    def getHandle()
      gameName = "\0" * 256
      @readini.call('Game', 'Title', '', gameName, 255, ".\\Game.ini")
      gameName.delete!("\0")
      if ($DEBUG)
        # Only one RGSS Player should be open
        result = @findwindow.call('RGSS Player', 0)
      else
        result = @findwindow.call('RGSS Player', gameName)
      end
      return result
    end
   
  end
 
end

class << Input
    alias update_mouse update
   
    def update
        update_mouse
        Mouse.update
    end
end

Mouse.init
Mouse.cursor.show


Achtung, dass Script funktioniert an manchen Stellen etwas anders, kann aber auch angepasst werden. Der Mauscursor wird in pictures/cursor.png abgespeichert. Auf die Mauskoordinaten greifst du mit Mouse.x und Mouse.y zu. Die Tastenabfragen funktionieren so:
Mouse.left.clicked? gibt true zurück wenn die linke Maustaste gerade gedrückt wurde.
Mouse.left.clicked_since?(2) gibt true zurück, wenn die linke Maustaste in den letzten zweiFrames einmal gedrückt wurde. Das solltest du für parallele Prozesse verwenden.
Mouse.left.double_clicked? gibt es auch noch und prüft auf doppeltes Klicken der Maustaste.
Analog gibt es auch Mouse.middle.clicked? etc.

_________________


Nach oben
 Profil ICQ  
Mit Zitat antworten  
Offline
Dieb
Dieb
Benutzeravatar
Beiträge: 8611
Alter: 30
BeitragVerfasst: Di Mär 20, 2012 15:50 
Danke, das klappt schonmal sehr geil.

Andere Frage:
Ich mőchte zwischen zwei Punkten auf dem Bildschirm den Winkel herausfinden. Also den Winkel vom Held zum Cursor der Maus.
Ich subtrahieren also zuerst die X- und Y-Koordinaten voneinander und errechne dann mit Hilfe des Tangens den Winkel. Wie stelle ich das am Geschicktesten an?

Danke schonmal.

_________________
Was mach ich hier überhaupt


Nach oben
 Profil  
Mit Zitat antworten  
Offline
Schweizer Reiter
Schweizer Reiter
Benutzeravatar
Beiträge: 379
BeitragVerfasst: Di Mär 20, 2012 17:49 
Tipp: Benutze für Winkelberechnungen niemald den Tangens, weil der ist für 0 und 90° nicht definiert.

Nimm lieber den Cosinus, der ist recht einfach anzuwenden.
Hier mal eine Funktion, die ich aus meiner Klasse Game_Character entnommen habe.

Code:
  def get_winkel(x, y)
    return 0 if x == @x and y == @y
    if x <= @x
      # linke Seite
      return Math.acos((@y - y) / dist_to(x, y))
    else
      # rechte seite
      return -Math.acos((@y - y) / dist_to(x, y))
    end
  end

Edit: 2.Funktion vergessen. Für die Hypothenuse bzw. die Entfernung
Code:
  def dist_to(x, y)
    abs_sx = (x - @x).abs
    abs_sy = (y - @y).abs
    return Math.sqrt(abs_sx * abs_sx + abs_sy * abs_sy)
  end

_________________


Nach oben
 Profil  
Mit Zitat antworten  
Beiträge der letzten Zeit anzeigen:  Sortiere nach  
Ein neues Thema erstellen Auf das Thema antworten  [ 4 Beiträge ] 

Alle Zeiten sind UTC + 1 Stunde


Du darfst keine neuen Themen in diesem Forum erstellen.
Du darfst keine Antworten zu Themen in diesem Forum erstellen.
Du darfst deine Beiträge in diesem Forum nicht ändern.
Du darfst deine Beiträge in diesem Forum nicht löschen.
Du darfst keine Dateianhänge in diesem Forum erstellen.

Suche nach:
Gehe zu:  
cron
Powered by phpBB® Forum Software © phpBB Group
Deutsche Übersetzung durch phpBB.de