[XP] "Inspect" Event Animation

Started by Jaiden, October 01, 2018, 11:14:56 pm

Previous topic - Next topic

Jaiden

October 01, 2018, 11:14:56 pm Last Edit: March 19, 2019, 10:26:18 pm by Jaiden
"Inspect" Event Animation
Authors: Jaiden, KK20
Version: 2.0
Type: Event Feature
Key Term: Environment Add-on



Introduction

Do you want to indicate that an event can be interacted with using a nifty animation? This is the script for you! Similar to how most games have some sort of a "press to interact" indicator, this script displays an animation when the player is facing events you've marked for "inspection".


Features


  • Easy to select which event / event page to display on

  • Animation displayed is customizable

  • Should be reasonably compatible with most scripts




Screenshots

Gif example using a custom animation:
Spoiler: ShowHide



Demo

(N/A)


Script

Spoiler: ShowHide

#==============================================================================
# Inspect Event
# by Jaiden, KK20 (mostly KK20)
# v2.0 March 2019
# - Added the capability to configure the inspect direction
# - KK20 fixed all that logic 'cuz it was bad
# v1.0 September 2018
# - Initial release
#==============================================================================
# Checks if an event is flagged for "inspection" and displays an
# animation above the player indicating it can be interacted with.
# -----
# This script aliases methods in the following classes:
# * Game_Character
# * Game_Event
# * Game_Map
# * Sprite_Character
# It may not be compatible with other scripts that have heavily
# modified these classes
#==============================================================================
# HOW TO USE THIS SCRIPT
# -----
# Create a comment with the text \inspect_event in the page of
# an event that you want to have display an animaton over the player when
# they are facing it. Then, set the animation ID below.
#
# [Multiple Directions]
# You can also set specific directions that will trigger the animation.
# Assuming the player is 0, imagine the directions like this:
#    8
# 4  0  6
#    2
#
# For example, \inspect_event[4,6] will only display the animation when
# the player is facing the event from the left and right.
# Use \inspect_event[0] to only display when the player is on top of the event.
#
# Specifying no direction will show the animation when the player faces
# the event from any direction.
#==============================================================================
module InspectEvent
  #--------------------------------------------------------------------------
  # Animation ID
  #--------------------------------------------------------------------------
  # Set this value to the ID in your animation database that you want
  # to display to indicate that an event can be inspected
  EVENT_ANIMATION_ID = 120
  #--------------------------------------------------------------------------
end
#==============================================================================
# END CONFIGURATION / DOCUMENTATION
#==============================================================================

#==============================================================================
# ** Game_Character
#==============================================================================
class Game_Character
  #--------------------------------------------------------------------------
  # * Public Instance Variables
  # inspect_event - flags an event for interation
  # inspect_dir_flags - Array of directions for inspection
  #   0 = Center (animation displays when player is on the event)
  #   4 = Left
  #   8 = Above
  #   6 = Right
  #   2 = Below
  # animation_loop_id - id for a looped animation
  #--------------------------------------------------------------------------
  attr_accessor    :inspect_event
  attr_accessor    :inspect_dir_flags
  attr_accessor    :animation_loop_id
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  alias inspect_event_initialize initialize
  def initialize
    inspect_event_initialize
    # Flag to determine if an event can be inspected
    @inspect_event = false
    # Array of directions
    @inspect_dir_flags = []
    # ID for looped animations
    @animation_loop_id = 0
  end
end

#==============================================================================
# ** Game_Event
#==============================================================================
class Game_Event
  alias inspect_event_refresh refresh
  def refresh
    prev_page = @page
    # Call original
    inspect_event_refresh
    return if prev_page == @page
    # Initialize flags
    @inspect_event = false
    # Initialize direction array
    @inspect_dir_flags = []
    # Check each list item for a comment
    return if @list.nil?
    @list.each do |cmd|
      # Check if it's a comment
      if cmd.code == 108
        # Set comment
        comment = cmd.parameters[0]
        comment_check = comment.scan(/^\\inspect_event/).flatten[0]
        # Check if the comment contains the string "\inspect_event"
        if comment_check == "\\inspect_event"
          # Flag it
          @inspect_event = true
          # Set directional array
          @inspect_dir_flags = comment.scan(/(\d)+/).flatten
          # Convert to ints
          @inspect_dir_flags.map!{|c| c.to_i}
          break
        end
      end
    end
  end
  #--------------------------------------------------------------------------
  # * Check if player is adjacent to and facing an event
  # directions = an array containing the different directions to register
  # the animation by returning true:
  #    8
  # 4  0  6
  #    2
  #--------------------------------------------------------------------------
  def player_facing?(directions = @inspect_dir_flags)
    # Exit if we're not checking directions
    return false if directions.nil?
    # Initialize variable
    check = false
    # Test each direction
    case $game_player.direction
    when 2 # Facing down
      if directions.include?(8) || directions == [] # Player is above event
        check = (self.y == $game_player.y + 1 && self.x == $game_player.x)
      else
        check = false
      end
    when 4 # Facing left
      if directions.include?(6) || directions == [] # Player is right of event
        check = (self.x == $game_player.x - 1 && self.y == $game_player.y)
      else
        check = false
      end
    when 6 # Facing right
      if directions.include?(4) || directions == [] # Player is left of event
        check = (self.x == $game_player.x + 1 && self.y == $game_player.y)
      else
        check = false
      end
    when 8 # Facing up
      if directions.include?(2) || directions == [] # Player is below event
        check = (self.y == $game_player.y - 1 && self.x == $game_player.x)
      else
        check = false
      end
    end
    # Check for 0 (player on event)
    if directions.include?(0)
      check = (self.y == $game_player.y && self.x == $game_player.x)
    end
    return check
  end
end
#==============================================================================
# ** Game_Map
#==============================================================================
class Game_Map
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  alias inspect_event_map_update update
  def update
    # Call original
    inspect_event_map_update
    # Call animation method
    inspect_event_anim
  end
  #--------------------------------------------------------------------------
  # * Inspect Event Animation
  #--------------------------------------------------------------------------
  def inspect_event_anim
    if $game_player.moving?
      $game_player.animation_loop_id = 0
      return
    end
   
    @events.values.each do |event|
      next unless event.inspect_event
      next if (event.x - $game_player.x).abs + (event.y - $game_player.y).abs > 1
      if event.player_facing?
        $game_player.animation_loop_id = InspectEvent::EVENT_ANIMATION_ID
        return
      end
    end
    $game_player.animation_loop_id = 0

  end
end

#==============================================================================
# ** Sprite_Character
#==============================================================================
class Sprite_Character
  #--------------------------------------------------------------------------
  # * Frame Update
  #--------------------------------------------------------------------------
  alias inspect_event_character_update update
  def update
    # Call original
    inspect_event_character_update
    # Set loop animation (not if message windows are showing)
    if $game_temp.message_window_showing == true
      loop_animation(nil)
    elsif @character.animation_loop_id != 0
      anim = $data_animations[@character.animation_loop_id]
      loop_animation(anim)
    else
      loop_animation(nil)
    end
  end
end



Instructions

Instructions are in the script, but it is as simple as putting a comment with "\inspect_event" in the event page you want the animation to display on. Note that the default animation used, "EM Exclamation" plays a sound every time it is looped. I suggest creating a new, silent animation and setting its ID in the configuration.


Compatibility

This script aliases the Game_Map, Game_Event, Game_Character and Sprite_Character classes. This will not be compatible with any scripts that heavily re-write those classes.


Credits and Thanks


  • KK20 for helping with 90% of this

  • Everyone else in the Discord server for helping out and/or dealing with my ramblings




Author's Notes

This is my first script. I figured I'd make a contribution back to Chaos Project since this community has been so helpful in my game's development. Hopefully this script treats someone else just as well!

Credit for this script isn't required, but greatly appreciated, with the exception of redistribution. If you post this script elsewhere, please link back to this original post.

Jaiden

Updated to v1.1; fixed a bug where the map wasn't refreshing correctly and the animation wouldn't always display.

Blizzard

Now you're a real scripter. First you had to fix your own broken code, it's kind of a ritual. >_>
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Jaiden

Updated to v2.0
Now you can specify what directions the animation displays! Also, KK20 fixed all the bugs and bad logic :)