Chaos Project

RPG Maker => RPG Maker Scripts => Script Requests => Topic started by: MarkHest on June 17, 2012, 09:17:46 pm

Title: Shaking Enemy Battlers
Post by: MarkHest on June 17, 2012, 09:17:46 pm
So i was wondering, can anyone make a script that makes an enemy shake a bit whenever he/she/it is hit in battle?

Remember the way that bosses shake when you defeat them in Lexima Legends? That is practically the effect i want but i only want the enemy to shake over 10 or 20 frames with a slightly smaller magnitude whenever they are hit.
Title: Re: Shaking Enemy Battlers
Post by: KK20 on June 18, 2012, 02:16:53 am
Okay, I think I got the basic idea down. Two versions here for you to test out:

One that moves +1, 0, -1, 0, etc.
Spoiler: ShowHide
SHAKE_MAGNITUDE = 5

class Sprite_Battler < RPG::Sprite
 
  alias init_before_setting_counter initialize
  def initialize(viewport, battler = nil)
    init_before_setting_counter(viewport, battler)
    @shake_frame_count = 0
  end
 
  alias call_update_before_shaking_sprite update
  def update
    # Check if damage being done and damage pop is true
    unless @shake_frame_count > 0 or @battler.is_a?(Game_Actor)
      if @battler.damage.is_a?(Numeric) and @battler.damage > 0 and @battler.damage_pop
        @shake_frame_count = 20
      end
    end
    # Call update alias
    call_update_before_shaking_sprite
    # Shaking the sprite
    if @shake_frame_count > 0
      move_amount = 0
      if @shake_frame_count % 4 == 0
        move_amount = SHAKE_MAGNITUDE
      elsif (@shake_frame_count+2) % 4 == 0
        move_amount = -SHAKE_MAGNITUDE
      end
      self.x = @battler.screen_x + move_amount
      @shake_frame_count -= 1
    end
  end
 
end


One that moves +1, +2, +1, 0, -1, -2, -1, 0, etc.
Spoiler: ShowHide
SHAKE_MAGNITUDE = 5

class Sprite_Battler < RPG::Sprite
 
  alias init_before_setting_counter initialize
  def initialize(viewport, battler = nil)
    init_before_setting_counter(viewport, battler)
    @shake_frame_count = 0
  end
 
  alias call_update_before_shaking_sprite update
  def update
    # Check if damage being done and damage pop is true
    unless @shake_frame_count > 0 or @battler.is_a?(Game_Actor)
      if @battler.damage.is_a?(Numeric) and @battler.damage > 0 and @battler.damage_pop
        @shake_frame_count = 20
      end
    end
    # Call update alias
    call_update_before_shaking_sprite
    # Shaking the sprite
    if @shake_frame_count > 0
      move_amount = 0
      if (@shake_frame_count+1) % 4 == 0
        if (@shake_frame_count+1) / 4 % 2 == 1
          move_amount = SHAKE_MAGNITUDE * 2
        else
          move_amount = -SHAKE_MAGNITUDE * 2
        end
      elsif [0,2].include?((@shake_frame_count+2) % 4)
        if (@shake_frame_count+2) / 4 % 2 == 1
          move_amount = SHAKE_MAGNITUDE
        else
          move_amount = -SHAKE_MAGNITUDE
        end
      end
      self.x = @battler.screen_x + move_amount
      @shake_frame_count -= 1
    end
  end
 
end

I did the calculations based on 20 frames of motion--I'm pretty sure that changing this value in the script will cause errors.
Title: Re: Shaking Enemy Battlers
Post by: Blizzard on June 18, 2012, 03:48:54 am
This here would be some equivalent code that I use, based on KK20's code.

SHAKE_MAGNITUDE = 4

class Sprite_Battler < RPG::Sprite
 
 alias init_before_setting_counter initialize
 def initialize(viewport, battler = nil)
   init_before_setting_counter(viewport, battler)
   @shake_frame_count = 0
 end
 
 alias call_update_before_shaking_sprite update
 def update
   # Check if damage being done and damage pop is true
   unless @shake_frame_count > 0 or @battler.is_a?(Game_Actor)
     if @battler.damage.is_a?(Numeric) and @battler.damage > 0 and @battler.damage_pop
       @shake_frame_count = 20
     end
   end
   # Call update alias
   call_update_before_shaking_sprite
   # Shaking the sprite
   if @shake_frame_count > 0
     move_amount = 0
     # This is basically the only relevant piece of code here
     self.x = @battler.screen_x - SHAKE_MAGNITUDE + (Graphics.frame_count % 2) * SHAKE_MAGNITUDE * 2
     @shake_frame_count -= 1
   end
 end
 
end


Mess around with this if you want it to look different. It depends on what you want.
Title: Re: Shaking Enemy Battlers
Post by: MarkHest on June 18, 2012, 08:10:51 am
It works perfectly as long as i have four members in my party, but when i have less members than four (1,2 or 3) i get this error at the start of battle:

At line 15 where this is:
if @battler.damage.is_a?(Numeric) and @battler.damage > 0 and @battler.damage_pop



NoMethodError
undefined method 'damage' for nil:Nilclass



I only get this error when i have less than four members in my party, otherwise it's exactly what i want :)
Title: Re: Shaking Enemy Battlers
Post by: Blizzard on June 18, 2012, 08:25:13 am
Change it to this:

if @battler != nil and @battler.damage.is_a?(Numeric) and @battler.damage > 0 and @battler.damage_pop
Title: Re: Shaking Enemy Battlers
Post by: MarkHest on June 18, 2012, 09:12:03 am
That did the trick perfectly! Thanks you two :)