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.

Swimming Script!

Swimming
Version: 1.8

Introduction

It seems to me that most swimming is done through event systems or Seph's vehicle script which is fine if you want to hop on a penguin to get across a river but we all know swimming isn't the same as riding a bike. I saw a lot of requests for a swimming script so I thought I'd make one. This is my first script that does something more interesting than displaying a window XD.

The script will cause the following when the player approaches water:
  1. The player will change to a diving graphic (optional)
  2. The player will jump into a body of water (at least 2 tiles wide)
  3. A sound is played (optional and customizable)
  4. The player will change to a swimming graphic
  5. The speed will change
  6. The player will jump out of water onto land that is at least 2 tiles wide and walk onto land that is 1 tile wide
  7. Swimming can be but doesn't have to be, controled by a switch
  8. If swimming is not available (switch is off), water will be impassable OR the player can drown

Updates
  • fixed the bug where the player jumps over 1 tile streams when DROWNING is true
  • fixed the water passability problem
  • I think i fixed that _swim_swim bug
  • Added more swim availability conditions!
  • Fixed the problem that disabling swimming didn't really do anything XD
  • Removed custom dashing and sneaking animation suffixes. They don't work. Sorry!
  • Fixed bug where player will jump right non water tiles if there is only one and water behind it
  • Added leveling up; you can swim faster if you swim more
  • Changed in water dashing speed and availability option
  • Added diving graphic change option
  • Added 'treading water' effect
  • Added Touch Swim
  • Added dashing and sneaking ability
  • Fixed the bug where the character gets trapped on an impassable tile after getting out of water

Screenshots
Before

Diving

In the water


Templates

Both from Mack's Male




Script

Code:
 

#==============================================================================#

#  Swimming!  v 1.8                                                          #

#  By: ToriVerly @ rmxp.org                                                    #

#==============================================================================#

#  Intructions:                                                                #

#------------------------------------------------------------------------------#

=begin

 Paste this script above Main and below everything else.

 For each character you will have swimming, make a swimming sprite that has the

 same name as the character but ending with "_swim" and another with "_dive" if

 DIVE_GRAPHIC is true.

    Example: "001-Fighter01_swim.png"

 

 If TREAD_ANI = true, the character will be animated while in water when they are

 not moving.  Hence a treading water effect.  Set it to false if you don't want 

 the effect.

 

 Set the WATER constant to the terrain tag ID of your water tiles or whatever tile

 you want to swim through.  When you place non water tiles over water tiles (in 

 a higher layer), the non water tiles will need to have a terrain tag that is

 different than WATER and not 0 or else the characters is swim through it.

 

    IMPORTANT--->make sure your water tile is passable.

 

 If you want the ability to swim to depend on a switch, set SWIM_SWITCH to the ID

 of the game switch you're using. If you don't want to use a switch, set it to nil.

 Similarily, set SWIM_ITEM, SWIM_ARMOR or SWIM_WEAPON to the ID of the item, armor

 or weapon required for swimming and nil if there is none.  You can even set more

 than one condition!

 

 The SWIM_SE will play every time you jump into water.  If you don't want a sound,

 set DIVE_SOUND_OFF to true.

 

 The SNEAK_KEY and DASH_KEY functions can be set for Mr.Mo's ABS or an input

 letters script.  If you don't have such an input system but have another dashing

 and/or sneaking system/script, change them to Input::YourKey.

     Example: Input::X

              Input::Y

 If you don't have dashing or sneaking at all, set them to nil.

 WATER_DASHING is self explanitory.  If you want to dash in water, set it to true.

 If DROWNING is on, the player will have about three seconds to get out of water

 before they die (if swimming isn't available). If it is off, water will just be

 impassable.

 Enjoy!

=end

#------------------------------------------------------------------------------#

WATER = 1

SWIM_SWITCH = 1

SWIM_ITEM = nil

SWIM_ARMOR = nil

SWIM_WEAPON = nil

SNEAK_KEY = nil #Input::Letterres["Z"] for Mr.Mo's ABS or input letters script

DASH_KEY = nil #Input::Letters["X"] for Mr.Mo's ABS or input letters script

SWIM_SE = "022-Dive02"

DROWN_SE = "021-Dive01"

DROWNING = true

WATER_DASHING = false

DIVE_SOUND_OFF = false

DIVE_GRAPHIC = true

TREAD_ANI = true

#------------------------------------------------------------------------------#

 

#==============================================================================#

# Game_Player                                                                  #

#------------------------------------------------------------------------------#

# Modifies the Game_Player class initialization and updating                   #

#==============================================================================#

class Game_Player < Game_Character

  attr_reader   :swim

  attr_reader   :swimming?

  attr_reader   :swim_count

  alias swim_init initialize

  def initialize

    @swim = false

    @drown_count = 0

    @swim_count = 0

    swim_init

  end

  alias swim_update update

  def update

 

    # Checks if swimming is triggered

    if DROWNING == true

    return jump_in if facing?(WATER, 'any', 1) and !@swim and moving?

    # Drowns if it is not available

    drown if !swim_available? and on?(WATER)

    elsif DROWNING == false

    return jump_in if facing?(WATER, 'any', 1) and !@swim and moving? and swim_available?

    end

    

    # Jumps out of water at shore

    jump_forward if !on?(WATER) and !facing?(WATER, 'any', 1) and !facing?(WATER, 'any', 2) and @swim and moving?

    # Returns original settings when out of water 

    revert if @swim and !on?(WATER)

    

    # Refreshes swimming state

     swim_refresh if swimming?

  swim_update

end

 

    # Makes water impassable when swimming isn't available

  alias mrmo_swim_game_player_passable passable?

  def passable?(x, y, d)

    # Get new coordinates

    new_x = x + (d == 6 ? 1 : d == 4 ? -1 : 0)

    new_y = y + (d == 2 ? 1 : d == 8 ? -1 : 0)

    # Check if it water tag

    return false if $game_map.terrain_tag(new_x,new_y) == WATER and !swim_available? and DROWNING == false

    # Old Method

    mrmo_swim_game_player_passable(x,y,d)

  end

#------------------------------------------------------------------------------#

# Custom Methods                                                               #

#------------------------------------------------------------------------------#

  # Checks swimming availability

  def swim_available?

     if SWIM_SWITCH != nil

      return true if $game_switches[SWIM_SWITCH]

      return false if !$game_switches[SWIM_SWITCH]

     end

     if SWIM_ITEM != nil

       return true if $game_party.item_number(SWIM_ITEM) != 0

       return false if $game_party.item_number(SWIM_ITEM) == 0

     end

     if SWIM_ARMOR != nil

       return true if $game_party.actors[0].armor1_id == SWIM_ARMOR

       return true if $game_party.actors[0].armor2_id == SWIM_ARMOR

       return true if $game_party.actors[0].armor3_id == SWIM_ARMOR

       return true if $game_party.actors[0].armor4_id == SWIM_ARMOR

       return false

     end

     if SWIM_WEAPON != nil

       return true if $game_party.actors[0].weapon_id == SWIM_WEAPON

       return false

     end

     return true

   end

    

  # Jumps in the water if swimming is triggered

  def jump_in

    @swim = true

    unless DIVE_SOUND_OFF

     @play_sound = true

    end

   if DIVE_GRAPHIC == true

    @character_name = $game_party.actors[0].character_name

    @character_name = $game_party.actors[0].character_name + "_dive"

   end

   jump_forward if facing?(WATER, 'any', 1)

  end

  

  # Swimming setup

  def swim_refresh

      get_speed if moving?

      if !moving?

        @character_name = $game_party.actors[0].character_name

        @character_name = $game_party.actors[0].character_name + "_swim"

      end

      if @play_sound and !moving?

         Audio.se_play("Audio/SE/" + SWIM_SE , 80, 100)

         @play_sound = false

      end

         @swim = true

      if TREAD_ANI == true

         @step_anime = true

       end

     end

     

  # Drowning

  def drown

    @move_speed = 0.1

    if @drown_count <= 120

      #jump_in if !@swim

      @drown_count += 1

        if @drown_count %40 == 0

          Audio.se_play("Audio/SE/" + DROWN_SE, 80, 100)

        end

      elsif @drown_count >= 120

      @character_name = ""

      @drown_count = 0

      Audio.se_play("Audio/SE/" + SWIM_SE, 80, 100)

     $scene = Scene_Gameover.new

    end

  end

  

  # Reverts original settings when out of water

  def revert

      @character_name = $game_party.actors[0].character_name

      @swim = false

      @drown_count = 0

        unless dashing? or sneaking?

         @move_speed = 4

         @move_frequency = 6

        end

       if TREAD_ANI == true

       @step_anime = false

     end

   end

   

  # Determines Speed (Swim Leveling)

  def get_speed

    # Gets Swim Count

      @swim_count += 0.05

    case @swim_count

    when 0.05

      @swim_speed = 1

      @move_frequency = 1

    when 100

      @swim_speed =  2

      @move_frequency = 1

    when 250

      @swim_speed = 3

      @move_frequency = 1

    when 750

      @swim_speed = 4

      @move_frequency = 1

    when 2000

      @swim_speed = 5

      @move_frequency = 1

    end

    @move_speed = @swim_speed

      if WATER_DASHING == true

          if DASH_KEY != nil and Input.press?(DASH_KEY) and !sneaking?

           @move_speed = @swim_speed + 1

           @move_frequency = 6

          end

          if SNEAK_KEY != nil and Input.press?(SNEAK_KEY) and !dashing?

           @move_speed = @swim_speed -1

           @move_frequency = 2

         end

       end

     end

  

# Jumps forward

  def jump_forward

  case @direction

      when 2

        jump(0, 1)

      when 4

        jump(-1, 0)

      when 6

        jump(1, 0)

      when 8

        jump(0, -1)

      end

    end

  # Jumps backward

  def jump_backward

    case @direction

      when 2

        jump(0, -1)

      when 4

        jump(1, 0)

      when 6

        jump(-1, 0)

      when 8

        jump(0, 1)

      end

    end

 

  # Checks if dashing

  def dashing?

    return true if DASH_KEY != nil and Input.press?(DASH_KEY)

    return false if SNEAK_KEY != nil and Input.press?(SNEAK_KEY)

  end

  # Checks if sneaking

  def sneaking?

    return true if SNEAK_KEY != nil and Input.press?(SNEAK_KEY)

    return false if DASH_KEY != nil and Input.press?(DASH_KEY)

  end

  # Checks if swimming

  def swimming?

    return true if on?(WATER) and @swim

  end

  # Checks if player is on a terrain tag

  def on?(tag)

    return true if $game_map.terrain_tag($game_player.x, $game_player.y) == tag

  end

  # Checks if player is facing a terrain tag

  def facing?(tag, dir, dist)

    case dir

     when 2

       if $game_player.direction == 2

        tag_x = $game_player.x

        tag_y = $game_player.y + dist

      end

     when 4

       if $game_player.direction == 4

        tag_x = $game_player.x - dist

        tag_y = $game_player.y

       end

     when 6

       if $game_player.direction == 6

        tag_x = $game_player.x + dist

        tag_y = $game_player.y

       end

     when 8

       if $game_player.direction == 8

        tag_x = $game_player.x

        tag_y = $game_player.y - dist

      end

     when 'any'

       if $game_player.direction == 2

        tag_x = $game_player.x

        tag_y = $game_player.y + dist

      end

       if $game_player.direction == 4

        tag_x = $game_player.x - dist

        tag_y = $game_player.y

      end

       if $game_player.direction == 6

        tag_x = $game_player.x + dist

        tag_y = $game_player.y

      end

      if $game_player.direction == 8

        tag_x = $game_player.x

        tag_y = $game_player.y - dist

      end

    end

   return false if tag_x == nil or tag_y == nil

   return true if $game_map.terrain_tag(tag_x, tag_y) == tag

 end

end

#------------------------------------------------------------------------------#

# By ToriVerly

# Thanks to Mr.Mo for help with my passability issues and to Chaosg1 for my intro

# into scripting :)

#------------------------------------------------------------------------------#

 

Instructions


Read the script. I'm getting tired of updating this part each time XD

Compatibility

Compatable with SDK...well I'm not sure about the new one as of yet.
Not compatable with scripts that will change your graphic (ie sneaking/dashing scripts or fukyama's caterpiller) although these scripts can be easily made compatable if you ask somebody.
Credits and Thanks

Major thanks to Chaosg1 whom without this wouldn't be possible :#
Thanks to Mr.Mo for helping me with passability.
 
id appreciate the screenshots if your willing to put them up just to see what it looks like, it actually sounds like a really cool idea. maybe you can do a diving script as well (would probably be something really simple actually)
 
uzumakishade, i think to successfully do a diving system is to make a "DEEP" tile on the water, and when the player presses a button in front of it, the system will ask if he wants to dive, then transfers him into another "UNDERWATER" map.
Or, use ccoa's Tileset swap to swap tilesets to underwater, no?

This is a nice idea Tory!
 
@Diving idea

You could make the water tile have priority 5 and put player on top on but turn player on top off when you press a button...but then going under stuff would get comlicated...anyway, I'm going to bask in my own personal glory lite for a while...and do some h/w...before I try another script

@~Atlaswing~
Thanks :)
 
This is so awesome!
I guess this will go into my game...
EDIT: It works with all the scripts I have, and the SDK.
 
@Kiniko...for you mushroom...I can try...if only I can remember how it was before the button input... lol i thought it would be nice so sue me.

EDIT:: Okay it's done. My first post is updated.

@Mr. Mo---thanks a lot!
 
Lol, I in the credits! But now...You were the one who created this script not me... I just helped! All the credit is yours! Good job!
 
great one!!

still.. it seems that
Dash system in the Mr.mo's ABS doesn't work with this.:-/

I had to delete the part where it changes the speed..
however, i'd like to slow the speed in the water :p

could you do so that it works fine with the dash ?
(I think it's because the speed out of water is set parallel with the game)


//oh, I almost forgot.

I wanted to make a difference between walking in a shalow water and swimming in a deep one
please do it if you can :p
 
I wanted to make a difference between walking in a shalow water and swimming in a deep one please do it if you can

That is easy, in the shallow water, simply make the tile passable and turn on the brush flag, it should look like you are wading, I do this in my game already.

EDIT: As well, can't you disable dash/sneak simply with a script command? I'll ask MrMo about it, I'm pretty sure that it can be.
 
For shallow water, make passable water with the bush tag and remove the terrain tag.

For the speed changes...I'll get on that...but probably not a lot of progress today. I didn't do any h/w on friday...or thursday...XD

EDIT:: Mkay it wasn't that hard and only took an hour! XD lol it'll work with or without Mr.Mo's now.
 

sixdd

Sponsor

I have found a bug, if you are in the water(swimming) and you come to, for example, a bridge, instead of being stopped at the bridge tile you just keep swimming over the bridge. You can also press "a" while on the bridge and it will allow you to jump and such.
 
I don't think I can do anything about it if your terrain tag is 0. The terrain tag of the water overrides the terrain tag 0 when they occupy the same square. Set your bridge's terrain tag to something that's not the terrain tag of water and not 0.
 
You should have the swimming "Level Up" Depending on how long you are in the water. Say for instance, you start out swimming really slow, but because you can swim, you get to special places that normally you couldn't... It starts out as speed of 1 when you hit 50 steps it goes up to speed 2, when you swim for 100 steps it levels up to speed 3, when you swim for 250 more steps it levels up to speed 4, when the player reaches 500 steps while swimming the speed is increased to 5, and finally at 1000 steps you increase it to speed of 6. As well you should allow the swimming to be activated if Item_ID### Is in inventory, would be like having the Zora Flippers in LoZ:LttP.
 

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