Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Interactive mouse system changes

Heya, Wyatt here (again).

I'm using this script:

Code:
#===============================================================================
# ** Interactive Mouse System
# : Lets you use mouse functions
#   : x,y
#   : tile_x, tile_y
# : Lets you click on events.
# : Lets you use pathfidning with mouse.
# : Displays different Icons over events if desired. Look at demo.
#-------------------------------------------------------------------------------
# Author    Mr.Mo
# Version   1.0
# Date      10-20-06
# Thanks to Astro_mech for methods tile_x and tile_y.
#=====================Instructions==============================================
# Using this is very very simple. To start it you need to add $mouse = IMS.new
# to a script. I added in Main, have a look. Its best to add there because I know
# error isn't going to start.
#
# In an event add this comment;
# IMS | Start | Key
#
# I will explain:
#
# IMS is there to tell me if the event uses the mouse.
#
# Start is there to tell me if the event starts by clicking on it. If you don't
# want the event to start by clicking on it, then do not add Start.
#
# Key can be anythin you put in Mouse_Icon. What do I mean by that?
# Take a look at this:
# MOUSE_ICON["NPC"] = "Arrow-Event"
# If we wanted to use the icon "Arrow-Event" for a mouse over event, we would do;
# IMS | Start | NPC
# This lets you add all the icon kinds you want below.
# MOUSE_ICON["Key"] = "Icon_Name"
#
#===========Controls
# Left Click is interactivity
# Right Click is action cancel
#===============================================================================
SDK.log("IMS", "Mr.Mo", "1.0", " 13-04-06")
#-------------------------------------------------------------------------------
# Begin SDK Enabled Check
#-------------------------------------------------------------------------------
#p "Mouse Input Module Missing! (class Game_Mouse)" if not SDK.enabled?('MouseInput')
if SDK.enabled?('MouseInput') and SDK.enabled?('IMS')
#--------------------------------------------------------------------------
# * Hides the Mouse
#--------------------------------------------------------------------------
cursor = Win32API.new("user32", "ShowCursor", "i", "i" )
cursor.call(0) # invisible
#cursor.call(1) # visible
#--------------------------------------------------------------------------
class IMS
  #--------------------------------------------------------------------------
  # * Constants - MAKE YOUR EDITS HERE
  #--------------------------------------------------------------------------
  MOUSE_ICON = {}
  MOUSE_ICON["Default"] = "Arrow"         #The default mouse icon.
  #Setting up mouse Icons are easy
  MOUSE_ICON["NPC1"] = "Arrow-Event"
  MOUSE_ICON["Item"] = "Arrow-Event2"
  MOUSE_ICON["Event"] = "Arrow-Event2"
  MOUSE_ICON["FISH"] = "fish"
  MOUSE_ICON["Fish"] = "fish"
  MOUSE_ICON["Attack"] = "001-weapon01"
  MOUSE_ICON["Digsite"] = "Skull"
  MOUSE_ICON["Q"] = "049-skill06"
  MOUSE_ICON["Left"] = "Left_Arrow"
  MOUSE_ICON["Right"] = "Right_Arrow"
  MOUSE_ICON["Down"] = "Down_Arrow"
  MOUSE_ICON["Up"] = "Up_Arrow"
  MOUSE_ICON["Item"] = "032-item01"
  MOUSE_ICON["Talk"] = "npc"
  CURSOR = Win32API.new("user32", "ShowCursor", "i", "i" )
  #--------------------------------------------------------------------------
  SINGLE_CLICK = true   # Make this false if you want Double-Click to activate events.
                        # One click to go to the event, the other click to activate.
  #--------------------------------------------------------------------------
  # * Attributes
  #--------------------------------------------------------------------------
  attr_accessor :icon
  attr_accessor :x
  attr_accessor :y
  #--------------------------------------------------------------------------
  # * Starts up the class, and creates the cursor icon
  #--------------------------------------------------------------------------
  def initialize
    @x = 0
    @y = 0
    @sprite = Sprite.new
    @sprite.z = 10000#5000
    @draw = false
    @events = {}
    reset
  end
  #--------------------------------------------------------------------------
  # * Single Click
  #--------------------------------------------------------------------------
  def SINGLE_CLICK
    return SINGLE_CLICK
  end
  #--------------------------------------------------------------------------
  # * Reset
  #--------------------------------------------------------------------------
  def reset
    @icon = RPG::Cache.icon(MOUSE_ICON["Default"])
    @icon_name = MOUSE_ICON["Default"]
    @sprite.bitmap.dispose if @sprite.bitmap != nil and @sprite.bitmap.disposed?
    @sprite.bitmap = @icon
    @draw = false
    # Updates the co-ordinates of the icon
    @x, @y = Mouse.mouse_pos
    @sprite.x = @x
    @sprite.y = @y
    @sprite.z = 100000#5000
    @sprite.visible = true
  end
  #--------------------------------------------------------------------------
  # * Update
  #--------------------------------------------------------------------------
  def update
    # Updates the co-ordinates of the icon
    @x, @y = Mouse.mouse_pos
    return if @x == nil or @y == nil
    #Get Client Size
    width,height = Mouse.client_size
    @sprite.x = @x
    @sprite.y = @y
    #Check if needs to restart
    (@draw = (@x < 0 or @y < 0 or @x > width or @y > height)) if !@draw
    #Reset if need to reset
    reset if @draw and @x > 1 and @y > 1 and @x < width and @y < height
    #Show mouse if need to
    if (@x < 0 or @y < 0 or @x > width or @y > height) and @visible
      CURSOR.call(1)
      @visible = false
    elsif (@x > 0 or @y > 0 or @x < width or @y < height) and !@visible
      CURSOR.call(0)
      @visible = true
    end
    #Return if Scene is not Scene_Map
    return if !$scene.is_a?(Scene_Map)
    #Check if the mouse is clicked
    mouse_interactive if Input.trigger?(Input::MOUSE_PRIMARY) and !$game_temp.message_window_showing
    $game_player.clear_path if Input.trigger?(Input::MOUSE_SECONDARY)
    #Check for mouse over
    check_icon_to_icon
  end
  #--------------------------------------------------------------------------
  # * Change Icon according to the Icon List
  #--------------------------------------------------------------------------
  def check_icon_to_icon
    object = get_object
    if object[0]
      event = @events[object[1].id]
      return if event == nil
      list = event.list
      return if list == nil
      #Check what the list is
      for key in MOUSE_ICON.keys
        next if !list.include?(key)
        next if @icon_name == MOUSE_ICON[key]
        @icon = RPG::Cache.icon(MOUSE_ICON[key])
        @icon_name = MOUSE_ICON[key]
        @sprite.bitmap.dispose if @sprite.bitmap != nil or @sprite.bitmap.disposed?
        @sprite.bitmap = @icon
      end
    elsif @icon_name != MOUSE_ICON["Default"]
      @icon = RPG::Cache.icon(MOUSE_ICON["Default"])
      @icon_name = MOUSE_ICON["Default"]
      @sprite.bitmap.dispose if @sprite.bitmap != nil or @sprite.bitmap.disposed?
      @sprite.bitmap = @icon
    end
  end
  #--------------------------------------------------------------------------
  # * Interactive System
  #--------------------------------------------------------------------------
  def mouse_interactive
    #Check if there is something on that pos.
    object = get_object
    if object[0]
      event = @events[object[1].id]
      return if event == nil
      list = event.list
      return if list == nil
      #Get the correct pos
      pos = get_pos(object[1], list)
      #return if there is no pos
      return if pos == nil and list.include?("Start") and !in_range?($game_player, object[1], 1)
      #Move to pos
      return $game_player.find_path(pos[0], pos[1], object[1]) if list.include?("Start") and !in_range?($game_player, object[1], 1)
      $game_player.turn_to(object[1]) if !in_direction?($game_player,object[1])
      #Activate event if facing event and next to event.
      return if !in_direction?($game_player,object[1]) or !in_range?($game_player, object[1], 1)
      #start event
      object[1].start if list.include?("Start")
      return
    end
    #If there is nothing than move
    return if !$game_map.passable?(tile_x, tile_y, 0, $game_player)
    $game_player.find_path(tile_x, tile_y)
  end
  #--------------------------------------------------------------------------
  # * Get Pos
  #--------------------------------------------------------------------------
  def get_pos(object, list)
    # First check event for specific directions
    if list.include?("Down") and $game_map.passable?(tile_x, tile_y+1, 0, object)
      return [tile_x,tile_y+1]
    elsif list.include?("Up") and $game_map.passable?(tile_x, tile_y-1, 0, object)
      return [tile_x,tile_y-1]
    elsif list.include?("Left") and $game_map.passable?(tile_x-1, tile_y, 0, object)
      return [tile_x-1,tile_y]
    elsif list.include?("Right") and $game_map.passable?(tile_x+1, tile_y, 0, object)
      return [tile_x+1,tile_y]
    end
    # Next try to go in direction of the event.
    case object.direction
    when 2
      return [tile_x,tile_y+1] if $game_map.passable?(tile_x, tile_y+1, 0, object)
    when 4
      return [tile_x-1,tile_y] if $game_map.passable?(tile_x-1, tile_y, 0, object)
    when 6
      return [tile_x+1,tile_y] if $game_map.passable?(tile_x+1, tile_y, 0, object)
    when 8
      return [tile_x,tile_y-1] if $game_map.passable?(tile_x, tile_y-1, 0, object)
    else
      # If none of it works
      return [tile_x-1,tile_y] if $game_map.passable?(tile_x-1, tile_y, 0, object)
      return [tile_x,tile_y-1] if $game_map.passable?(tile_x, tile_y-1, 0, object)
      return [tile_x+1,tile_y] if $game_map.passable?(tile_x+1, tile_y, 0, object)
      return [tile_x,tile_y+1] if $game_map.passable?(tile_x, tile_y+1, 0, object)
    end
    return nil
  end
  #--------------------------------------------------------------------------
  # * Get Object
  #--------------------------------------------------------------------------
  def get_object
    for event in $game_map.events.values
      return [true,event] if event.x == tile_x and event.y == tile_y
    end
    return [false,nil]
  end
  #--------------------------------------------------------------------------
  # * In Range?(Element, Object, Range) - Near Fantastica
  #--------------------------------------------------------------------------
  def in_range?(element, object, range)
    x = (element.x - object.x) * (element.x - object.x)
    y = (element.y - object.y) * (element.y - object.y)
    r = x + y
    return true if r <= (range * range)
    return false
  end
  #--------------------------------------------------------------------------
  # * In Direction?(Element, Object) - Near Fantastica
  #--------------------------------------------------------------------------
  def in_direction?(element, object)
    return true if element.direction == 2 and object.y >= element.y and object.x == element.x
    return true if element.direction == 4 and object.x <= element.x and object.y == element.y
    return true if element.direction == 6 and object.x >= element.x and object.y == element.y
    return true if element.direction == 8 and object.y <= element.y and object.x == element.x
    return false
  end
  #--------------------------------------------------------------------------
  # * Returns the current x-coordinate of the tile the mouse is over on the map
  #--------------------------------------------------------------------------
  def tile_x
    return ((($game_map.display_x.to_f/4.0).floor + @x.to_f)/32.0).floor
  end
  #--------------------------------------------------------------------------
  # * Returns the current y-coordinate of the tile the mouse is over on the map
  #--------------------------------------------------------------------------
  def tile_y
    return ((($game_map.display_y.to_f/4.0).floor + @y.to_f)/32.0).floor
  end
  #--------------------------------------------------------------------------
  # * Makes the mouse sprite disappear
  #--------------------------------------------------------------------------
  def invisible
    @sprite.opacity = 0
  end
  #--------------------------------------------------------------------------
  # * Makes the mouse sprite visible
  #--------------------------------------------------------------------------
  def visible
    @sprite.opacity = 255
  end
  #--------------------------------------------------------------------------
  # * Disposes the sprite
  #--------------------------------------------------------------------------
  def dispose
    @sprite.dispose
  end
  #--------------------------------------------------------------------------
  # * IMS Refresh(Event, List, Characterset Name
  #--------------------------------------------------------------------------
  def refresh(event, list)
    #Delete the event from the list
    @events.delete(event.id)
    #Skip the event if its invisible or doesn't contain a list
    return if list == nil
    parameter = comment_include(event, "IMS")
    #Skip if the paramete is NIL
    return if parameter == nil
    #Add the event
    @events[event.id] = IMS_Event.new(event.id)
    @events[event.id].list = parameter
  end
  #--------------------------------------------------------------------------
  # * Get Comments
  #--------------------------------------------------------------------------
  def comment_include(*args)
    list = *args[0].list
    trigger = *args[1]
    return nil if list == nil
    return nil unless list.is_a?(Array)
    for item in list
      next if item.code != 108
      par = item.parameters[0].split(' | ')
      return item.parameters[0] if par[0] == trigger
    end
    return nil
  end
  #--------------------------------------------------------------------------
  # * Mouse Over?(object)
  #--------------------------------------------------------------------------
  def over?(object)
    if $mouse.x > object.x and $mouse.x < object.x + object.width and
      $mouse.y > object.y and $mouse.y < object.y + object.height
      return true
    end
    return false
  end
end
#============================================================================
# * IMS_Event
#============================================================================
class IMS_Event
  attr_accessor :id
  attr_accessor :list
  #--------------------------------------------------------------------------
  # * Initialize
  #--------------------------------------------------------------------------
  def initialize(id)
    @id = id
    @list = nil
  end
end
#============================================================================
# *  Game Event  - Near Fantastica
#============================================================================
class Game_Event < Game_Character
  #--------------------------------------------------------------------------
  # * Alias
  #--------------------------------------------------------------------------
  alias mrmo_ims_game_event_refresh_set_page refresh_set_page
  #--------------------------------------------------------------------------
  # * Event Name
  #--------------------------------------------------------------------------
  def name
    return @event.name
  end
  #--------------------------------------------------------------------------
  # * Event ID
  #--------------------------------------------------------------------------
  def id
    return @id
  end
  #--------------------------------------------------------------------------
  # * Refreshes Page
  #--------------------------------------------------------------------------
  def refresh_set_page
    mrmo_ims_game_event_refresh_set_page
    $mouse.refresh(self, @list)
  end
end
#============================================================================
# * Scene Map
#============================================================================
class Scene_Map
  #--------------------------------------------------------------------------
  alias mrmo_mouse_scene_map_update update
  alias mrmo_mouse_scene_map_main_dispose main_dispose
  alias mrmo_mouse_scene_map_main_draw main_window
  #--------------------------------------------------------------------------
  # * Main Draw
  #--------------------------------------------------------------------------
  def main_draw
    $mouse.reset
    mrmo_mouse_scene_map_main_draw
  end
  #--------------------------------------------------------------------------
  # * Update
  #--------------------------------------------------------------------------
  def update
    $mouse.update
    #If a message is showing close it.
    if $game_temp.message_window_showing and Input::trigger?(Input::MOUSE_PRIMARY)
      if $game_temp.choice_max > 0
        $game_system.se_play($data_system.decision_se)
        $game_temp.choice_proc.call(@message_window.index)
      end
      @message_window.terminate_message
    end
    mrmo_mouse_scene_map_update
  end
  #--------------------------------------------------------------------------
  # * Main Dispose
  #--------------------------------------------------------------------------
  def main_dispose
    $mouse.reset
    mrmo_mouse_scene_map_main_dispose
  end
end
#==============================================================================
# ** Game_Character (part 1)
#------------------------------------------------------------------------------
#  This class deals with characters. It's used as a superclass for the
#  Game_Player and Game_Event classes.
#==============================================================================
class Game_Character
  #--------------------------------------------------------------------------
  # * Turn Towards B
  #--------------------------------------------------------------------------
  def turn_to(b)
    # Get difference in player coordinates
    sx = @x - b.x
    sy = @y - b.y
    # If coordinates are equal
    if sx == 0 and sy == 0
      return
    end
    # If horizontal distance is longer
    if sx.abs > sy.abs
      # Turn to the right or left towards player
      sx > 0 ? turn_left : turn_right
    # If vertical distance is longer
    else
      # Turn up or down towards player
      sy > 0 ? turn_up : turn_down
    end
  end
  #--------------------------------------------------------------------------
  # * Run Path
  #--------------------------------------------------------------------------
  def run_path
    return if moving?
    step = @map[@x,@y]
    if step == 1
      @map = nil
      @runpath = false
      turn_to(@object) if @object != nil and in_range?(self, @object, 1)
      @object.start if @object != nil and in_range?(self, @object, 1) and $mouse.SINGLE_CLICK
      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
  #--------------------------------------------------------------------------
  # * In Range?(Element, Object, Range) - Near Fantastica
  #--------------------------------------------------------------------------
  def in_range?(element, object, range)
    x = (element.x - object.x) * (element.x - object.x)
    y = (element.y - object.y) * (element.y - object.y)
    r = x + y
    return true if r <= (range * range)
    return false
  end
  #--------------------------------------------------------------------------
  # * Find Path
  #--------------------------------------------------------------------------
  def find_path(x,y,object=nil)
    sx, sy = @x, @y
    @object = object
    result = setup_map(sx,sy,x,y)
    @runpath = result[0]
    @map = result[1]
    @map[sx,sy] = result[2] if result[2] != nil
  end
end
#-------------------------------------------------------------------------------
# End SDK Enabled Check
#-------------------------------------------------------------------------------
end

By Mr.Mo, called the Interactive Mouse System.

Trouble is, with this script player_touch events are pretty much useless, because if you click an event you don't move there.

What I need is that if you click an event, if it is player_touch you still move there, and then once on the event it activates.

Also, the main problem, I need it so that if the message window is showing you can't walk. Because at the moment, when a message shows, you can just walk away. The way I think this could be done, is that when an event is activated, there is a call script. Then when you want the player to move again, you just do another call script. I'll look and see if this is part of the system already, but I don't think it is.

Also, if possible, it would be useful to have it so that if you click an event with IMS in the comment you move there, or as close to there as possible. But this isn't really required too much, just something that would be useful.

Notes: commercial online game, will credit but cannot offer any money unfortunately.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top