Heretic's Magic Compass Script [XP]

Heretic's Magic Compass [XP]
Authors: Heretic
Version: 1.2
Type: Misc Add-on



Introduction

This script will allow you to create a Magic Compass that points at either a Map Coordinates or an Event. Its called a Magic Compass because normal compasses only point North. This one points at a location Magically!



Features

    Very easy to Install and Use
    Should have a very high degree of compatability
    Can point the Compass at any set of X and Y coordinates
    Can point the Compass at an Event, even while it is moving around
    Compass won't point at an Event that is Erased or doesn't have an Active Page
    Can point at a set of X and Y coordinates that are off the current Map
    Doesn't glitch when you go to Menu, unlike other Compass Scripts or Event Solutions
    Uses a Background and Needle Image and positions these things for you!
    Configuration Options can be altered during Gameplay
    Limburger Cheese is really stinky but very tasty
    Does anyone read this stuff?
    No dependancies on other Scripts



Screenshots

Content Hidden Spoiler
Image

The compass is in the Bottom Right corner. It updates dynamically as you move about.




Demo

http://downloads.chaos-project.com/here ... ssDemo.exe



Script

Place above Main and below Scene_Debug. Anywhere else should be fine in theory. If there are any problems, try moving this script closer to Main as some scripts replace methods that are aliased in this script.

I wrote a Looping Maps script, which is up over at chaos-project.com, and if you use that Script, place Magic Compass below Looping Maps.
Content Hidden Spoiler
  1. #===============================================================================
  2. #
  3. #           HERETIC'S MAGIC COMPASS [XP]
  4. #           Version 1.2
  5. #           Wednesday, May 6th, 2015
  6. #
  7. #===============================================================================
  8. #
  9. #  -----  Overview  -----
  10. #
  11. #  This script will allow you to create a Magic Compass that points at either
  12. #  a Map Coordinates or an Event.  Its called a Magic Compass because normal
  13. #  compasses only point North.  This one points at a location Magically!
  14. #
  15. #
  16. #  -----  Features  -----
  17. #
  18. #  - Very easy to install and use
  19. #  - Should be highly compatible
  20. #  - No dependancies on other Scripts
  21. #  - Can point the Compass at any set of X and Y coordinates
  22. #  - Can point the Compass at an Event, even while it is moving around
  23. #  - Can point at a set of X and Y coordinates that are off the current Map
  24. #  - Doesn't glitch when you go to Menu, unlike other Compass Scripts
  25. #  - Uses a Background and Needle Image and positions these things for you
  26. #  - Configuration Options can be altered during gameplay
  27. #  - Limburger Cheese is really stinky but tasty
  28. #  - Does anyone read this stuff?
  29. #
  30. #
  31. #  -----  Version History  -----
  32. #
  33. #
  34. #  Version 1.2 - Wednesday, May 6th, 2015
  35. #    - Fixed a bug where Tone did not match Screen Tone on Initialize
  36. #  Version 1.1 - Thursday, April 30th, 2015
  37. #    - Updated to work with Looping Maps
  38. #     * Compass will point at the closest possible distance to a Target
  39. #  Version 1.0 - Friday, December 26th, 2014
  40. #    - Initial Release
  41. #
  42. #  -----  Instructions  -----
  43. #
  44. #  There are a few very simple script calls.  Put these in Event -> Script
  45. #
  46. #  $game_system.enable_compass = true / false
  47. #    * set to true or false to Show or Hide the Compass
  48. #
  49. #  set_compass_map_target(x, y)
  50. #    * Sets the Compass to point at a Map X and Y Location (off map is a-okay)
  51. #
  52. #  set_compass_event_target(target)
  53. #    * Sets the Compass to point at an Event or Event ID (either is fine)
  54. #
  55. #  clear_compass_target
  56. #    * Clears Map Location and Target Event and causes Needle to float to North
  57. #
  58. #
  59. #  -----  Conditional Branch Scripts  -----
  60. #
  61. #  Put these in Event -> Conditional Branch -> Script
  62. #
  63. #  enable_compass?
  64. #    * returns True or False if the Compass is Enabled
  65. #
  66. #  can_point_compass?
  67. #    * returns True or False if the Compass will Point at a Target
  68. #    * returns False if an Event is Erased or doesn't have an Active Page or
  69. #      the Compass doesn't have a Map Target
  70. #
  71. #  valid_compass_target?
  72. #    * NOTE: This must be run on an Event (recommended for Advanced Users)
  73. #    * $game_map.events[@event_id].valid_compass_target?
  74. #    * returns True or False if the Event ID is not Erased and Page is Active
  75. #
  76. #
  77. #  -----  Usage  -----
  78. #
  79. #  Once you have a Compass displayed on your screen, it will remain there
  80. #  until you call "enable_compass = false".
  81. #
  82. #  The Compass Target will be erased when you change maps.  Transfer Player
  83. #  on the same map will not erase the Compass Target however.
  84. #
  85. #  If you set a Compass Target to an Event, the Event can not be erased
  86. #  and must have an active Page.  For example, you have an Event with
  87. #  just one Page, and that Page has a Page Condition that is not met, such
  88. #  as a Game Switch that is not set to On, then the Compass will not
  89. #  point at that Event.
  90. #
  91. #  If an Event is Erased or Page Conditions are not met while your Compass
  92. #  is displaying, the Compass will simply slowly float back to Due North.
  93. #
  94. #
  95. #  -----  Limitations  -----
  96. #
  97. #  The only Limitation I can think of is that this script in its current state
  98. #  probably won't work with Looping Map Scripts.  I may take a crack at
  99. #  changing this if I ever get around to working on a Looping Map Script
  100. #  for RPG Maker XP.
  101. #
  102. #  This has been fixed in Version 1.1 so the compass will consider the size
  103. #  of the map and point the shortest distance to your Compass Target.
  104. #
  105. #
  106. #  -----  Legal  -----
  107. #
  108. #  You may use this script in commercial products without permission, provided
  109. #  that I am credited for the usage of this script.  You may redistribute this
  110. #  script on any websites.  You may translate this script, provided that I am
  111. #  retained as the original author.  You may make reasonable alterations
  112. #  to this script to suit your needs.
  113. #
  114. #  You may NOT sell this script.
  115. #
  116. #  You may NOT claim this script to be your property or intellectual work.
  117. #
  118. #
  119. #  -----  Layman Legal  -----
  120. #
  121. #  This script is supposed to be free.  Just credit me if you use it in
  122. #  any sort of commercial stuff.
  123. #
  124. #
  125. #  -----  Configuration Options  -----
  126. #
  127. #  There are a number of Configuration Options so that you can customize this
  128. #  script to suit your needs.  The Configuration Options establish default
  129. #  values, and can be changed during gameplay.  Recommended for advanced
  130. #  users, but basically, all the Configuration Options are stored in
  131. #  your $game_system.
  132. #
  133. #  COMPASS_X and COMPASS_Y are the On Screen Position for your Compass
  134. #
  135. #  COMPASS_ZOOM will resize an image without the need to edit the images you
  136. #  use in an editor.  I've found that zooming out looks just a bit better
  137. #  than 100% sized images while rotating.
  138. #
  139. #  COMPASS_BACK_OPACITY allows you to make the Compass Background Image
  140. #  slightly transparent so it does not fully obstruct a Player's view
  141. #  of the current Map.
  142. #
  143. #  COMPASS_HUD_Z is used to put the Compass on top of other Pictures or
  144. #  put the Compass below other Pictures.  Up to you.
  145. #
  146. #  COMPASS_SCREEN_TONE is used to match the Tone of the Compass Images
  147. #  to the Screen Tone.  This is useful if you fade to black and still have
  148. #  the Compass displayed during a Map Change.  If you want the compass
  149. #  to display even when the screen changes Tone, then set this value
  150. #  to False.
  151.  
  152.  
  153. # Compass Background Graphic for Player's Compass (Use "" for No Picture)
  154. COMPASS_BACK_GRAPHIC = "Compass Back.png"
  155.  
  156. # Compass Graphic for Player's Compass (Use "" for No Picture)
  157. COMPASS_NEEDLE_GRAPHIC = "Compass Needle.png"
  158.  
  159. # Screen Position of Compass (this will change depening on Picture Size)
  160. COMPASS_X = 525
  161. COMPASS_Y = 365
  162.  
  163. # Adjust Compass Size for both Graphics (Smaller Grpahics rotate a bit better)
  164. COMPASS_ZOOM = 75.0
  165.  
  166. # Makes the Background Image of the Compass Transparent
  167. COMPASS_BACK_OPACITY = 160.0
  168.  
  169. # Compass Hud Z-Index - Use -1 for below Pictures and 100+ for above Pictures
  170. COMPASS_HUD_Z = -1
  171.  
  172. # Make the Compass have a tone Consistent with the Game Screen (true / false)
  173. COMPASS_SCREEN_TONE = true
  174.  
  175. #==============================================================================
  176. # ** Game_System
  177. #==============================================================================
  178. class Game_System
  179.   #--------------------------------------------------------------------------
  180.   # * Public Instance Variables - Game_System
  181.   #--------------------------------------------------------------------------
  182.   attr_reader    :enable_compass           # Display On Screen Compass
  183.   attr_accessor  :compass_back             # Compass Back Graphic Default
  184.   attr_accessor  :compass_needle           # Compass Needle Graphic Default
  185.   attr_accessor  :compass_x                # Compass Screen X Position
  186.   attr_accessor  :compass_y                # Compass Screen Y Position
  187.   attr_accessor  :compass_zoom             # Zoom of Compass Pictures
  188.   attr_accessor  :compass_screen_tone      # Match Compass Tone to Screen Tone
  189.   attr_accessor  :compass_back_opacity     # Opacity of Compass Background
  190.   attr_accessor  :compass_target_x         # Map X Target Location
  191.   attr_accessor  :compass_target_y         # Map Y Target Location
  192.   attr_accessor  :compass_target_event_id  # Map Target Event  
  193.   #--------------------------------------------------------------------------
  194.   # * Game System Initialization - Game_System
  195.   #  - Default Values for when Compass is changed to Enabled
  196.   #  - Adjusting values in Game System will not affect an existing Compass
  197.   #--------------------------------------------------------------------------  
  198.   alias compass_game_system_initialize initialize unless $@
  199.   def initialize
  200.     # Call Original or other Aliases
  201.     compass_game_system_initialize
  202.     # Enable or Disable display of Compass
  203.     @enable_compass = false
  204.     # Compass Image Filenames (from Pictures folder)
  205.     @compass_back = COMPASS_BACK_GRAPHIC
  206.     @compass_needle = COMPASS_NEEDLE_GRAPHIC
  207.     # Default Screen Position of Compass Graphics
  208.     @compass_x = COMPASS_X
  209.     @compass_y = COMPASS_Y
  210.     # Default Zoom Level of Compass Graphics
  211.     @compass_zoom = COMPASS_ZOOM
  212.     # Default Opacity of a Compass Background Picture
  213.     @compass_back_opacity = COMPASS_BACK_OPACITY
  214.     # Default Match the two Compass Pictures Tone to the Screen Tone for Effect
  215.     @compass_screen_tone = COMPASS_SCREEN_TONE
  216.     # Map Coordinates for Compass to point at
  217.     @compass_target_x = nil
  218.     @compass_target_y = nil
  219.     # Event for Compass to point at dynamically
  220.     @compass_target_event_id = nil    
  221.   end
  222.   #--------------------------------------------------------------------------
  223.   # * Enable Compass= (Setter Method) - Game_System
  224.   #  - Enables the Compass and creates if needed
  225.   #  - If called from a Common Event while in a Menu or other Scene, then
  226.   #    the Compass Graphics will be created when you return to the Map.
  227.   #--------------------------------------------------------------------------
  228.   def enable_compass=(value)
  229.     # Check Value
  230.     return unless [true, false].include?(value)
  231.     # If True
  232.     if value == true
  233.       # If the Player's Compass doesn't exist and not a Common Event to change
  234.       if not $game_player.compass
  235.         # Create new Compass Object for Player
  236.         $game_player.compass = Game_Compass.new
  237.         # Position the Compass Object on the Screen
  238.         $game_player.compass.position_compass
  239.       end
  240.     end
  241.     # Store the Value
  242.     @enable_compass = value
  243.   end
  244.   #--------------------------------------------------------------------------
  245.   # * Set Compass Map Target - Game_System
  246.   #  - Sets the Target of the Compass to Map Coordinates, even off map
  247.   #     x : x-coordinate of Map
  248.   #     y : y-coordinate of Map
  249.   #--------------------------------------------------------------------------  
  250.   def set_compass_map_target(x, y)
  251.     # Clears Event Target ID
  252.     @compass_target_event_id = nil
  253.     # Sets a Map Location as a Target
  254.     @compass_target_x = x
  255.     @compass_target_y = y
  256.   end
  257.   #--------------------------------------------------------------------------
  258.   # * Set Compass Event Target - Game_System
  259.   #  - Sets the Target of the Compass to an Event
  260.   #     target : Event or Number
  261.   #--------------------------------------------------------------------------  
  262.   def set_compass_event_target(target)
  263.     # Clears Map Location as a Target
  264.     @compass_target_x = nil
  265.     @compass_target_y = nil
  266.     # If Target is a Character
  267.     if target.is_a?(Game_Event)
  268.       # Just use the Event's ID
  269.       @compass_target_event_id = target.id
  270.     # Elsif the Target is a Number
  271.     elsif target.is_a?(Numeric)
  272.       # Set the Numeric Value of Target
  273.       @compass_target_event_id = target
  274.     end
  275.   end
  276.   #--------------------------------------------------------------------------
  277.   # * Clear Compass Target - Game_System
  278.   #  - Clears the Compass Target
  279.   #--------------------------------------------------------------------------    
  280.   def clear_compass_target
  281.     # Clears Needle Angle of Picture
  282.     $game_player.compass.needle_angle = nil if $game_player.compass
  283.     # Clears Map Location as a Target
  284.     @compass_target_x = nil
  285.     @compass_target_y = nil
  286.     # Clears Event Target ID
  287.     @compass_target_event_id = nil
  288.   end  
  289. end
  290.  
  291. #==============================================================================
  292. # ** Game_Map
  293. #==============================================================================
  294. class Game_Map
  295.   #--------------------------------------------------------------------------
  296.   # * Setup - Game_Map
  297.   #  - Clears Compass Targets on Map Change
  298.   #  - Transferring the Player on the same Map will retain Targets
  299.   #     map_id : map ID
  300.   #--------------------------------------------------------------------------
  301.   alias compass_map_setup setup unless $@
  302.   def setup(map_id)
  303.     # If a Compass is being dislayed and Target is an Event
  304.     if $game_system.enable_compass and $game_system.compass_target_event_id or
  305.        ($game_system.compass_target_x and $game_system.compass_target_y)
  306.       # If Map Change
  307.       if @map and @map_id > 0 and @map_id != map_id
  308.         # Clear Compass Target (Event and Map Location)
  309.         $game_system.clear_compass_target
  310.       end
  311.     end
  312.     # Call Original or other Aliases
  313.     compass_map_setup(map_id)
  314.   end
  315. end
  316.  
  317. #==============================================================================
  318. # ** Compass_Picture
  319. #==============================================================================
  320. class Compass_Picture < Game_Picture
  321.   #--------------------------------------------------------------------------
  322.   # * Public Instance Variables - Compass_Picture
  323.   #--------------------------------------------------------------------------
  324.   attr_accessor :number                   # Picture Number and Z-Index
  325.   attr_accessor :name                     # Picture Filename in Pictures Folder
  326.   attr_accessor :origin                   # 0 Top Left - 1 Center (Default)
  327.   attr_accessor :x                        # x-coordinate
  328.   attr_accessor :y                        # y-coordinate
  329.   attr_accessor :zoom_x                   # x directional zoom rate
  330.   attr_accessor :zoom_y                   # y directional zoom rate
  331.   attr_accessor :opacity                  # opacity level
  332.   attr_accessor :blend_type               # blend method
  333.   attr_accessor :tone                     # color tone
  334.   attr_accessor :angle                    # rotation angle
  335.   attr_accessor :angle_target             # target of angle rotation
  336.   attr_accessor :angle_duration           # duration of angle rotation
  337.   attr_accessor :rotate_speed             # repeating rotation
  338.   #--------------------------------------------------------------------------
  339.   # * Object Initialization - Compass_Picture
  340.   #     number : picture number
  341.   #     type   : background or needle
  342.   #  - Creates the Pictures used for the Compass to add to Spriteset Map
  343.   #  - Uses stored values from Game System to allow change during Gameplay
  344.   #  - Changes to Game System do not affect existing Compass
  345.   #  - Other methods available to Game_Picture are still available
  346.   #--------------------------------------------------------------------------
  347.   def initialize(number, type)
  348.     @number = number
  349.     @name = ""
  350.     @type = type    
  351.     @origin = 1
  352.     @x = 0.0
  353.     @y = 0.0
  354.     @zoom_x = $game_system.compass_zoom
  355.     @zoom_y = $game_system.compass_zoom
  356.     @opacity = (type == :back) ? $game_system.compass_back_opacity : 255.0
  357.     @blend_type = 0
  358.     @duration = 0
  359.     @target_x = @x
  360.     @target_y = @y
  361.     @target_zoom_x = @zoom_x
  362.     @target_zoom_y = @zoom_y
  363.     @target_opacity = @opacity
  364.     # If Config to match Screen Tone    
  365.     if $game_system.compass_screen_tone
  366.       # Set Picture Tone to the Tone of the Screen
  367.       @tone = $game_screen.tone
  368.     else
  369.       # Default Tone
  370.       @tone = Tone.new(0, 0, 0, 0)
  371.     end
  372.     @tone_target = @tone.clone
  373.     @tone_duration = 0
  374.     @angle = 0
  375.     @angle_target = @angle
  376.     @angle_duration = 0
  377.     @rotate_speed = 0
  378.   end
  379.   #--------------------------------------------------------------------------
  380.   # * Frame Update - Compass_Picture
  381.   #  - Replaces Game_Picture Update of Super Class so only updates Rotation
  382.   #  - Updates Transitions for X, Y, Zoom_X, Zoom_Y, Opacity, and Tone
  383.   #  - Updates Transition for Angle
  384.   #  - Similar to Game_Picture Update method but allows Rotation Transition
  385.   #--------------------------------------------------------------------------
  386.   def update
  387.     if @duration >= 1
  388.       d = @duration
  389.       @x = (@x * (d - 1) + @target_x) / d
  390.       @y = (@y * (d - 1) + @target_y) / d
  391.       @zoom_x = (@zoom_x * (d - 1) + @target_zoom_x) / d
  392.       @zoom_y = (@zoom_y * (d - 1) + @target_zoom_y) / d
  393.       @opacity = (@opacity * (d - 1) + @target_opacity) / d
  394.       @duration -= 1
  395.     end
  396.     if @tone_duration >= 1
  397.       d = @tone_duration
  398.       @tone.red = (@tone.red * (d - 1) + @tone_target.red) / d
  399.       @tone.green = (@tone.green * (d - 1) + @tone_target.green) / d
  400.       @tone.blue = (@tone.blue * (d - 1) + @tone_target.blue) / d
  401.       @tone.gray = (@tone.gray * (d - 1) + @tone_target.gray) / d
  402.       @tone_duration -= 1
  403.     end
  404.     if @angle_duration == 0 and @rotate_speed != 0
  405.       @angle += @rotate_speed / 2.0
  406.     elsif @angle_duration >= 1
  407.       d = @angle_duration
  408.       @angle = (@angle * (d - 1) + @angle_target) / d
  409.       @angle_duration -= 1
  410.     end
  411.     if @angle < 0 or @angle > 360
  412.       while @angle < 0 or @angle > 360
  413.         @angle += (@angle < 360 ? 360 : -360)
  414.       end
  415.       @angle %= 360
  416.     end    
  417.   end
  418. end
  419.  
  420. #==============================================================================
  421. # ** Scene_Map
  422. #==============================================================================
  423. class Scene_Map
  424.   #--------------------------------------------------------------------------
  425.   # * Compass Map Spriteset - Scene_Map
  426.   #  - Just returns the Spriteset Map
  427.   #--------------------------------------------------------------------------
  428.   def compass_map_spriteset
  429.     @spriteset
  430.   end
  431. end
  432.  
  433. #==============================================================================
  434. # ** Spriteset_Map
  435. #==============================================================================
  436. class Spriteset_Map
  437.   #--------------------------------------------------------------------------
  438.   # * Public Instance Variables - Spriteset_Map
  439.   #--------------------------------------------------------------------------
  440.   attr_reader    :compass_back_picture    # Compass Picture for Background
  441.   attr_reader    :compass_needle_picture  # Compass Picture for Needle
  442.   attr_reader    :compass_back_sprite     # Picture Sprite for Compass  
  443.   attr_reader    :compass_needle_sprite   # Picture Sprite for Compass
  444.   #--------------------------------------------------------------------------
  445.   # * Add Compass - Spriteset_Map
  446.   #     compass : Game_Compass
  447.   #  - Uses number as a way to control the Z-Index
  448.   #--------------------------------------------------------------------------
  449.   def add_compass
  450.     # If the Compass Background Sprite does not already exist
  451.     if @compass_back_sprite.nil?
  452.       # Set the HUD's Z-Index as number from the Constant in Config
  453.       number = COMPASS_HUD_Z
  454.       # Create a Compass Picture for the Compass Background (type = :back)
  455.       @compass_back_picture = Compass_Picture.new(number, :back)
  456.       # Set the Picture Name from the Compass Object (refer to $game_system)
  457.       @compass_back_picture.name = $game_system.compass_back
  458.       # Shorthand
  459.       b = @compass_back_picture
  460.       # Create the Sprite for the Compass Background Picture
  461.       @compass_back_sprite = Sprite_Picture.new(@viewport2, b)
  462.     end
  463.     # If the Compass Needle Sprite does not already exist
  464.     if @compass_needle_sprite.nil?
  465.       # Set the HUD's Z-Index as number from the Constant in Config
  466.       number = COMPASS_HUD_Z      
  467.       # Create a Compass Picture for the Compass Needle
  468.       @compass_needle_picture = Compass_Picture.new(number, :needle)
  469.       # If the Compass has a stored Needle Angle
  470.       if not $game_player.compass.needle_angle.nil?
  471.         # Set the Needle Angle before setting the Name or creating Sprite
  472.         @compass_needle_picture.angle = $game_player.compass.needle_angle
  473.       end
  474.       # Set the Picture Name from the Compass Object (refer to $game_system)
  475.       @compass_needle_picture.name = $game_system.compass_needle
  476.       # Shorthand
  477.       n = @compass_needle_picture
  478.       # Create the Sprite for the Compass Needle Picture
  479.       @compass_needle_sprite = Sprite_Picture.new(@viewport2, n)
  480.     end
  481.     # Position Compass Internally
  482.     position_compass
  483.   end
  484.   #--------------------------------------------------------------------------
  485.   # * Position Compass - Spriteset_Map
  486.   #  - Positions the Compass Pictures and Sprites internally
  487.   #--------------------------------------------------------------------------  
  488.   def position_compass
  489.     # Shorthand - Same as Position Compass called Internally
  490.     back_picture = @compass_back_picture
  491.     needle_picture = @compass_needle_picture
  492.     back_sprite = @compass_back_sprite
  493.     needle_sprite = @compass_needle_sprite
  494.     # Determine X Value as 1/2 of Larger Sprite
  495.     if back_sprite.bitmap.width > needle_sprite.bitmap.width
  496.       # Use the Centered Bitmap Width of the Background Sprite
  497.       sprite_x = back_sprite.bitmap.width / 2
  498.     else
  499.       # Use the Centered Bitmap Width of the Needle Sprite
  500.       sprite_x = needle_sprite.bitmap.width / 2
  501.     end
  502.     # Determine Y Value as 1/2 of Larger Sprite
  503.     if back_sprite.bitmap.height > needle_sprite.bitmap.height
  504.       # Use the Centered Bitmap Height of the Background Sprite
  505.       sprite_y = back_sprite.bitmap.height / 2
  506.     else
  507.       # Use the Centered Bitmap Height of the Needle Sprite
  508.       sprite_y = needle_sprite.bitmap.height / 2
  509.     end
  510.     # Position the Background Picture
  511.     back_picture.x = $game_player.compass.x + sprite_x
  512.     back_picture.y = $game_player.compass.y + sprite_y
  513.     # Position the Needle Picture
  514.     needle_picture.x = $game_player.compass.x + sprite_x
  515.     needle_picture.y = $game_player.compass.y + sprite_y    
  516.   end
  517.   #--------------------------------------------------------------------------
  518.   # * Compass Sprite Update - Spriteset_Map
  519.   #  - Alias of Main Sprite Update Method
  520.   #  - Checks for Missing Compass Sprites on Scene Change (Menu)
  521.   #--------------------------------------------------------------------------
  522.   alias compass_sprite_update update unless $@
  523.   def update
  524.     # If the Compass Option is Enabled
  525.     if $game_system.enable_compass
  526.       # If the Compass Background or Compass Needle Sprites dont exist
  527.       if @compass_back_sprite.nil? or @compass_back_picture.nil?
  528.         # Create new Compass Object for Player unless it exists
  529.         $game_player.compass = Game_Compass.new unless $game_player.compass
  530.         # Add the Pictures and Sprites to Spriteset Map Object
  531.         add_compass
  532.       end
  533.       # Update the Compass Pictures
  534.       @compass_back_picture.update
  535.       @compass_needle_picture.update
  536.       # Update the Compass Sprites with values from Pictures
  537.       @compass_back_sprite.update
  538.       @compass_needle_sprite.update
  539.     elsif @compass_back_sprite
  540.       # Dispose of the Sprite and set the object to nil
  541.       @compass_back_sprite.dispose
  542.       @compass_back_sprite = nil
  543.       # If Needle Sprite exists
  544.       if @compass_needle_sprite
  545.         # Dispose of Needle Sprite and set the object to nil
  546.         @compass_needle_sprite.dispose
  547.         @compass_needle_sprite = nil
  548.       end
  549.       # Dispose of the Pictures
  550.       @compass_back_picture = nil
  551.       @compass_needle_picture = nil
  552.     end
  553.     # Call Original or other Aliases
  554.     compass_sprite_update    
  555.   end
  556.   #--------------------------------------------------------------------------
  557.   # * Dispose - Spriteset Map
  558.   #  - Disposes of Compass Images and Sprites for their Bitmaps (memory leak)
  559.   #--------------------------------------------------------------------------
  560.   alias compass_spriteset_dispose dispose
  561.   def dispose
  562.     # Dispose of the Sprites and Bitmaps
  563.     @compass_back_sprite.dispose if @compass_back_sprite
  564.     @compass_needle_sprite.dispose if @compass_needle_sprite
  565.     # Set Values to nil
  566.     @compass_back_sprite = nil if @compass_back_sprite
  567.     @compass_needle_sprite = nil if @compass_needle_sprite
  568.     # Call Original or other Aliases
  569.     compass_spriteset_dispose
  570.   end
  571. end
  572.  
  573. #==============================================================================
  574. # ** Game_Character
  575. #==============================================================================
  576. class Game_Character
  577.   #--------------------------------------------------------------------------
  578.   # * Object Initialization - Game_Character
  579.   #  - Add Values before calling Original or other Aliases before Update
  580.   #--------------------------------------------------------------------------
  581.   alias compass_character_initialize initialize unless $@
  582.   def initialize
  583.     # If Player is Initialized by the Character Class (Super)  
  584.     if self.is_a?(Game_Player)
  585.       # Placeholder for Game_Compass Object as a Picture
  586.       @compass = nil
  587.     end
  588.     # Call Original or other Aliases
  589.     compass_character_initialize
  590.   end
  591. end
  592.  
  593. #==============================================================================
  594. # ** Game_Compass
  595. #==============================================================================
  596. class Game_Compass
  597.   #--------------------------------------------------------------------------
  598.   # * Public Instance Variables - Game_Compass
  599.   #--------------------------------------------------------------------------  
  600.   attr_accessor    :x                   # Position of Compass on Screen
  601.   attr_accessor    :y                   # Position of Compass on Screen
  602.   attr_accessor    :needle_angle        # Angle of Compass Needle
  603.   #--------------------------------------------------------------------------
  604.   # * Object Initialization - Game_Compass
  605.   #  - Container in Game_Player for the Pictures
  606.   #--------------------------------------------------------------------------
  607.   def initialize
  608.     # Position of Compass on Screen
  609.     @x = $game_system.compass_x
  610.     @y = $game_system.compass_y
  611.     # Angle of the Compass Needle
  612.     @needle_angle = nil
  613.   end
  614.   #--------------------------------------------------------------------------
  615.   # * Back Sprite - Game_Compass
  616.   #  - Returns the Spriteset Map Compass Background Sprite
  617.   #--------------------------------------------------------------------------  
  618.   def back_sprite
  619.     $scene.compass_map_spriteset.compass_back_sprite
  620.   end
  621.   #--------------------------------------------------------------------------
  622.   # * Needle Sprite - Game_Compass
  623.   #  - Returns the Spriteset Map Compass Background Sprite
  624.   #--------------------------------------------------------------------------  
  625.   def needle_sprite
  626.     $scene.compass_map_spriteset.compass_needle_sprite
  627.   end
  628.   #--------------------------------------------------------------------------
  629.   # * Back Picture - Game_Compass
  630.   #  - Returns the Spriteset Map Compass Background Picture
  631.   #--------------------------------------------------------------------------  
  632.   def back_picture
  633.     $scene.compass_map_spriteset.compass_back_picture
  634.   end
  635.   #--------------------------------------------------------------------------
  636.   # * Needle Picture - Game_Compass
  637.   #  - Returns the Spriteset Map Compass Needle Picture
  638.   #--------------------------------------------------------------------------  
  639.   def needle_picture
  640.     $scene.compass_map_spriteset.compass_needle_picture
  641.   end
  642.   #--------------------------------------------------------------------------
  643.   # * Compass Target Distance - Game_Compass
  644.   #  - Returns Array of Distances between Player and Target Coordinates
  645.   #  - Made into a Method because this is where Looping Map Scripts will
  646.   #    become Incompatible, so one method to fix things hopefully.
  647.   #     target_x : Map Target X Coordinate
  648.   #     target_y : Map Target Y Coordinate  
  649.   #--------------------------------------------------------------------------  
  650.   def compass_target_distance(target_x, target_y)
  651.     # Distance in Real Values between Player and Target
  652.     x_dist = (target_x - $game_player.real_x) * 1.0
  653.     y_dist = (target_y - $game_player.real_y) * 1.0
  654.     # Return the X and Y Distances as an Array
  655.     return [x_dist, y_dist]
  656.   end
  657.   # If Heretic's Loop Maps is installed
  658.   if Game_Map.method_defined?(:map_loop_passable?)  
  659.     #--------------------------------------------------------------------------
  660.     # * Compass Target Distance - Game_Compass (for Loop Maps)
  661.     #  - Required by Trig function to determine Radian Tangent value for Angle
  662.     #  - Returns Array of Distances between Player and Target Coordinates
  663.     #  - Made into a Method because this is where Looping Map Scripts will
  664.     #    become Incompatible, so one method to fix things hopefully.
  665.     #     target_x : Map Target X Coordinate
  666.     #     target_y : Map Target Y Coordinate  
  667.     #--------------------------------------------------------------------------
  668.     alias loop_map_compass_target_distance compass_target_distance unless $@
  669.     def compass_target_distance(target_x, target_y)
  670.       # Get difference in target coordinates from original method      
  671.       dx, dy = loop_map_compass_target_distance(target_x, target_y)
  672.       # If Map Loops Horizontal and Distance is more than Half the Map (128 / 2)
  673.       if $game_map.loop_horizontal? and dx.abs > $game_map.width * 64
  674.         # Adjust X Distance for Horizontal Looping Map
  675.         dx += (dx < 0) ? $game_map.width * 128 : -$game_map.width * 128
  676.       end
  677.       # If Map Loops Vertical and Distance is more than Half the Map (128 / 2)
  678.       if $game_map.loop_vertical? and dy.abs > $game_map.height * 64
  679.         # Adjust X Distance for Vertical Looping Map
  680.         dy += (dy < 0) ? $game_map.height * 128 : -$game_map.height * 128
  681.       end
  682.       # Return Adjusted Difference X and Y values as Array for Looping Maps
  683.       return [dx, dy]
  684.     end
  685.   end # End Optional Alias Definition
  686.   #--------------------------------------------------------------------------
  687.   # * Update (frame) - Game_Compass
  688.   #  - Points the Compass at Target Location or Event
  689.   #--------------------------------------------------------------------------  
  690.   def update
  691.     # If Option to keep the Compass Tone same as the Screen
  692.     if $game_system.compass_screen_tone
  693.       # Set the Tone for the Two Images if visible
  694.       back_picture.tone = $game_screen.tone if back_picture.name != ""
  695.       needle_picture.tone = $game_screen.tone if needle_picture.name != ""
  696.     end
  697.     # Shorthand
  698.     target_id = $game_system.compass_target_event_id
  699.     # If Compass is set to use an Event as a Target and Target is Valid
  700.     if target_id and $game_map.events[target_id] and
  701.        $game_map.events[target_id].valid_compass_target?
  702.       # Get the Event from the Game Map
  703.       target_event = $game_map.events[$game_system.compass_target_event_id]
  704.       # Set Target X and Y
  705.       target_x = target_event.real_x * 1.0
  706.       target_y = target_event.real_y * 1.0
  707.     # If Target Coordinates are a Map Location
  708.     elsif $game_system.compass_target_x and $game_system.compass_target_y
  709.       # Set the Target Coordintates for Real X and Y values
  710.       target_x = $game_system.compass_target_x * 128.0
  711.       target_y = $game_system.compass_target_y * 128.0
  712.     end
  713.     # If Target Coordinates for Compass
  714.     if target_x and target_y
  715.       # X and Y Distance between the Player and Target
  716.       x, y = compass_target_distance(target_x, target_y)
  717.       # Clear Angle Transitions
  718.       needle_picture.angle_target = 0
  719.       needle_picture.angle_duration = 0
  720.       # If Coordinate Match and not Rotating
  721.       if x == 0 and y == 0 and needle_picture.rotate_speed == 0
  722.         # Spin the Needle
  723.         needle_picture.rotate(needle_picture.angle > 180 ? -30 : 30)
  724.       end
  725.       # If Same X Position
  726.       if x == 0 and y != 0
  727.         # Clear repeating Rotation
  728.         needle_picture.rotate(0)
  729.         # Set Needle Picture to Up or Down
  730.         #needle_picture.angle = (target_y < $game_player.real_y ? 0 : 180)
  731.         needle_picture.angle = (y < 0 ? 0 : 180)
  732.       # If Same Y Position
  733.       elsif y == 0 and x != 0
  734.         # Clear repeating Rotation
  735.         needle_picture.rotate(0)
  736.         # Set Needle Picture to Left or Right
  737.         needle_picture.angle = (x < 0 ? 90 : 270)
  738.       # Use Trig to determine Angle of Sprite
  739.       elsif x != 0 and y != 0
  740.         # Clear repeating Rotation
  741.         needle_picture.rotate(0)
  742.         # Trig to determine Angle (-180 used to point with the top of picture)
  743.         needle_picture.angle = (Math::atan2(x,y)*180.0/Math::PI).round - 180
  744.       end
  745.       # Store the Needle Angle for Scene Changes (Menu)
  746.       @needle_angle = needle_picture.angle
  747.     # No Compass Target      
  748.     elsif needle_picture.angle != 0 and needle_picture.angle_duration == 0
  749.       # Clear repeating Rotation
  750.       needle_picture.rotate(0)
  751.       # Set the Needle Angle between 0 and 360
  752.       if needle_picture.angle < 0 or needle_picture.angle > 360
  753.         while needle_picture.angle < 0 or needle_picture.angle > 360
  754.           needle_picture.angle += (needle_picture.angle < 360 ? 360 : -360)
  755.         end
  756.         needle_picture.angle %= 360
  757.       end
  758.       # Angle
  759.       a = needle_picture.angle
  760.       # Use a Transition to move the Needle to 0 at 0.5 Degrees per Frame
  761.       needle_picture.angle_target = (a > 180 ? 360.0 : 0.0)
  762.       needle_picture.angle_duration = (a > 180 ? (a - 180) * 2 : a * 2)
  763.     end
  764.   end
  765.   #--------------------------------------------------------------------------
  766.   # * Position Compass - Game_Compass
  767.   #  - Positions the Compass on the Screen and centers the Compass Images
  768.   #    within this Container
  769.   #--------------------------------------------------------------------------  
  770.   def position_compass
  771.     # Make sure the Sprites for the Compass exist in Spriteset Map
  772.     $scene.compass_map_spriteset.add_compass
  773.     # Determine X Value as 1/2 of Larger Sprite
  774.     if back_sprite.bitmap.width > needle_sprite.bitmap.width
  775.       # Use the Centered Bitmap Width of the Background Sprite
  776.       @sprite_x = back_sprite.bitmap.width / 2
  777.     else
  778.       # Use the Centered Bitmap Width of the Needle Sprite
  779.       @sprite_x = needle_sprite.bitmap.width / 2
  780.     end
  781.     # Determine Y Value as 1/2 of Larger Sprite
  782.     if back_sprite.bitmap.height > needle_sprite.bitmap.height
  783.       # Use the Centered Bitmap Height of the Background Sprite
  784.       @sprite_y = back_sprite.bitmap.height / 2
  785.     else
  786.       # Use the Centered Bitmap Height of the Needle Sprite
  787.       @sprite_y = needle_sprite.bitmap.height / 2
  788.     end
  789.     # Position the Background Picture
  790.     back_picture.x = @x + @sprite_x
  791.     back_picture.y = @y + @sprite_y
  792.     # Position the Needle Picture
  793.     needle_picture.x = @x + @sprite_x
  794.     needle_picture.y = @y + @sprite_y
  795.   end
  796. end
  797.  
  798. #==============================================================================
  799. # ** Game_Player
  800. #==============================================================================
  801. class Game_Player < Game_Character
  802.   #--------------------------------------------------------------------------
  803.   # * Public Instance Variables - Game_Player
  804.   #--------------------------------------------------------------------------
  805.   attr_accessor :compass          # Compass Object for Player
  806.   #--------------------------------------------------------------------------
  807.   # * Update - Game_Player
  808.   #  - Updates the Player's Compass
  809.   #--------------------------------------------------------------------------
  810.   alias player_compass_update update unless $@
  811.   def update
  812.     # Call Original or other Aliases
  813.     player_compass_update
  814.     # If Player has a Compass
  815.     if $game_system.enable_compass and $scene.is_a?(Scene_Map)
  816.       # If the Compass Picture doesn't exist
  817.       if @compass.nil?
  818.         # Create the new Game Compass
  819.         @compass = Game_Compass.new
  820.         # Position the Compass on the Screen
  821.         @compass.position_compass
  822.       end
  823.       # Update the Compass Picture Sprites from within the Object
  824.       @compass.update
  825.     # Compass is Disabled but Compass exists
  826.     elsif @compass
  827.       # Clear the Compass ojbect
  828.       @compass = nil
  829.     end    
  830.   end
  831. end
  832.  
  833. #==============================================================================
  834. # ** Game_Event
  835. #==============================================================================
  836. class Game_Event < Game_Character
  837.   #--------------------------------------------------------------------------
  838.   # * Valid Compass Target? - Game_Event
  839.   #  - Valid Compass Targets should have an Active Page and are not Erased
  840.   #--------------------------------------------------------------------------
  841.   def valid_compass_target?
  842.     return (not @erased and not @page.nil?)
  843.   end
  844. end
  845.  
  846. #==============================================================================
  847. # ** Interpreter
  848. #==============================================================================
  849. class Interpreter
  850.   #--------------------------------------------------------------------------
  851.   # * Set Compass Map Target - Interpreter
  852.   #  - Sets the Target of the Compass to Map Coordinates, even off map
  853.   #     x : x-coordinate of Map
  854.   #     y : y-coordinate of Map
  855.   #--------------------------------------------------------------------------    
  856.   def set_compass_map_target(x, y)
  857.     # Pass the Arguments along to the Game System method
  858.     $game_system.set_compass_map_target(x, y)
  859.   end
  860.   #--------------------------------------------------------------------------
  861.   # * Set Compass Event Target - Interpreter
  862.   #  - Sets the Target of the Compass to an Event
  863.   #     target : Event or Event ID
  864.   #--------------------------------------------------------------------------    
  865.   def set_compass_event_target(target)
  866.     # Pass the Arguments along to the Game System method
  867.     $game_system.set_compass_event_target(target)
  868.   end
  869.   #--------------------------------------------------------------------------
  870.   # * Clear Compass Target - Interpreter
  871.   #  - Clears the Compass Target
  872.   #--------------------------------------------------------------------------    
  873.   def clear_compass_target
  874.     # Call the method in Game System
  875.     $game_system.clear_compass_target
  876.   end
  877.   #--------------------------------------------------------------------------
  878.   # * Enable Compass? - Interpreter (Setter Method)
  879.   #  - Returns True or False from the Game System
  880.   #--------------------------------------------------------------------------
  881.   def enable_compass?
  882.     return ($game_system.enable_compass == true)
  883.   end
  884.   #--------------------------------------------------------------------------
  885.   # * Can Point Compass? - Interpreter
  886.   #  - Checks if the Compass has a Target that will cause it to Point
  887.   #--------------------------------------------------------------------------
  888.   def can_point_compass?
  889.     # Shorthand for Game System
  890.     s = $game_system
  891.     # If Compass has a Target
  892.     if (s.compass_target_x and s.compass_target_y) or
  893.        (s.compass_target_event_id and
  894.         $game_map.events[s.compass_target_event_id] and
  895.         $game_map.events[s.compass_target_event_id].valid_compass_target?)
  896.       # Compass will point at a Target
  897.       return true
  898.     end
  899.     # Default - Needed for Event Conditional Branch -> Script Evaluation
  900.     return false
  901.   end  
  902. end
  903.  
  904. #==============================================================================
  905. # ** Scene_Load
  906. #==============================================================================
  907. class Scene_Load < Scene_File
  908.   #--------------------------------------------------------------------------
  909.   # * Read Save Data - Scene_Load
  910.   #     file : file object for reading (opened)
  911.   #  - Checks for Uninitialized Values that Crash Savegames
  912.   #--------------------------------------------------------------------------
  913.   alias compass_read_save_data read_save_data unless $@
  914.   def read_save_data(file)
  915.     # Call Original or other Aliases
  916.     compass_read_save_data(file)
  917.     # Set Game System Compass to False for later Evaluation in Script Calls
  918.     $game_system.enable_compass = false if $game_system.enable_compass.nil?
  919.     # If Game System Compass Filenames are nil
  920.     if $game_system.compass_back.nil?
  921.       # Initialize from the Constant Value
  922.       $game_system.compass_back = COMPASS_BACK_GRAPHIC
  923.     end
  924.     # If Game System Compass Filenames are nil
  925.     if $game_system.compass_needle.nil?
  926.       # Initialize from the Constant Value
  927.       $game_system.compass_needle = COMPASS_NEEDLE_GRAPHIC
  928.     end
  929.     # If Game System Compass Background Opacity is
  930.     if $game_system.compass_back_opacity.nil?
  931.       # Initialize from the Constant Value
  932.       $game_system.compass_back_opacity = COMPASS_BACK_OPACITY
  933.     end
  934.     # If Game System Compass Screen Tone value not set
  935.     if $game_system.compass_screen_tone.nil?
  936.       # Initialize from the Constant Value
  937.       $game_system.compass_screen_tone = (COMPASS_SCREEN_TONE) ? true : false
  938.     end    
  939.     # If Game System Zoom is nil?
  940.     $game_system.compass_zoom = COMPASS_ZOOM if $game_system.compass_zoom.nil?
  941.     # If Game System Compass X or Y are nil?
  942.     $game_system.compass_x = COMPASS_X if $game_system.compass_x.nil?
  943.     $game_system.compass_y = COMPASS_Y if $game_system.compass_y.nil?
  944.   end
  945. end
  946.    
Expand to see the code.



Instructions

This script is quite easy to use.

Once installed somewhere above main, you can mess with the Options if you'd like.

To display the Compass, run a Script $game_system.enable_compass = true
To hide the Compass, run a Script $game_system.enable_compass = false

NOTE: I ran into some issues with the game hanging because of an Interpreter Bug. You can use Zeriab's command_355 fix to take care of the issue, or put another line in your script that says "return true". The command_355 fix is really the way to go.

Now that your Compass is shown, it needs a Target to point at! Just three important script calls.
- set_compass_map_target(x, y) # Use for a Map Location (offscreen is fine)
- set_compass_event_target(event_id) # Use to point the Compass at an existing Event
- clear_compass_target() # Clears Compass Targets

There are more commands and features available, described in documentation.



Compatibility

May not be compatible with any Looping Map Scripts at this time.


Credits and Thanks

I'd like to thank the Meteor that wiped out the Dinosaurs 65 million years ago.
Without that Meteor, we wouldn't be here today and I might be all scaley and have dry skin.



Author's Notes

The point of Scripts is to make things easy for others. It is possible to create a Compass purely out of Events, however, Event solutions have drawbacks as well as they may require more work than is necessary to pull off. Just try the Demo. It's small and will show you why this script is superior.

Edit: Redacted. This script was updated to version 1.1 for compatability with my Looping Maps script.
Wow, I'm surprised this hasn't gotten any comments yet. I tried the demo, and everything worked great. I was able to port the single script into my project with no issues, even when using pixel-movement scripts. I think this is a great way to help guide the player, and I could see this being used in a couple of different ways. One thing I noticed, which is more or less just a minor inconvenience, is that when opening and closing the menu, the compass background becomes completely opaque and loses it's transparency for a few frames. It's not as noticeable in the demo, but when I switched the background to a custom graphic, it becomes more distracting when that piece of the HUD is illuminated for a moment after closing the menu. I wasn't sure if it's possible to correct the problem, but other than that this is a great script, kudos on making everything so easy to use.
This is actually a really cool idea. Nice one.
Thanks for that!

I actually just updated the script in my Collection for compatability with a bunch of new scripts, namely Looping Maps. Thus, version 1.1 is now compatible with Looping Maps.

Oh yeah, VEHICLES has been released from the Screenshot. Its up over at chaos-project.com, but I'll post my updates here soon as well.

---

Okay, now that I have some time, I did try to duplicate the Opacity Bug, but was unable to do so. Do you have any other scripts that modify pictures, or have you tried doing the same thing in another project?
Minor bump.

Found the mentioned bug and fixed it. Initialize did not set the Tone to the value of Screen Tone when the option to match screen tone was applied.
Hey Heretic, I had another question regarding this script. When you transfer maps, the script resets the needle position back to north. Is there a way to avoid that? If possible, I'd like the needle to remain in the same position, until a new coordinate is specified.

I have a parallel process event set the coordinates each time the player enters a new map, which means there's a frame where the needle goes back to north before the new coordinate can be assigned, just a minor gripe. Thanks for the help!
It might be possible to fix this during that single frame by calling $game_player.compass.update as soon as you set the Compass Target... I'll have to take a look when I get a bit more time...

---

Ok, I spent a few minutes checking it out. I think there may be another script, or even the default system, that is causing the delay. I tried it on the collection and couldn't reproduce the bug, but I do believe that it may exist. The above solution is a possible fix, but I'll have to consider other ways to make sure the compass updates as soon as the player is transferred, and once the new coordinates are assigned.

Question: are you using a Fade when you Transfer the Player?
It looks as if adding $game_player.compass.update after setting the coordinates did solve my problem, now it updates smoothly when transitioning from map to map. Thanks again for the help, I appreciate it. I hope you come check out my project when I release a demo so you can see your hard work put to good use.

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