Shaking Enemy Battlers

Started by MarkHest, June 17, 2012, 09:17:46 pm

Previous topic - Next topic

MarkHest

June 17, 2012, 09:17:46 pm Last Edit: June 17, 2012, 09:19:51 pm by MarkHest
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.
   

KK20

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.

Other Projects
RPG Maker XP Ace  Upgrade RMXP to RMVXA performance!
XPA Tilemap  Tilemap rewrite with many features, including custom resolution!

Nintendo Switch Friend Code: 8310-1917-5318
Discord: KK20 Tyler#8901

Join the CP Discord Server!

Blizzard

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.
Check out our game brands:

Daygames
Game Night Games
Chugnar Games

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.

MarkHest

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 :)
   

Blizzard

Change it to this:

if @battler != nil and @battler.damage.is_a?(Numeric) and @battler.damage > 0 and @battler.damage_pop
Check out our game brands:

Daygames
Game Night Games
Chugnar Games

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.

MarkHest

That did the trick perfectly! Thanks you two :)