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.

[Review]World Map Script

ive done my hardest to playtest all the bugs out. so far it is working amazing for me. but i know there are a few things i did wrong. namely i know i messed up Sprite_Cursor's viewport(i did what i did to remove the extra cursor it seemed to be creating), my method text in Window_Map might also be done wrong, to be honnest im not sure. im sure i made a more errors in my Main_Window_Map and New_Scene_Map :) other then that i know some of my comments need updating i will do that before release(most of the comment errors are due to recomondations that i change the names of classes. anyways here is the code.
Module
Code:
#------------------------------------------------------------------------------

# module Map_Items

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

module Map_Items

  # All icons are to be located in your Graphics/Icon folder

 

  # Icon used for the players current location 

  PLAYER_ICON = "player"  

  

  # Icon used for the cursor 

  CURSOR = "cursor"    

  

  # Cursor offset type: 

  # 1 = Static Choices(cursor can only go where the player can move)

  # 2 = Free Choice(cursor can freely move)

  CURSOR_TYPE = 1  

  

  # Name Styles:

  # 1 = Under the cursor on the town the cursor is on

  # 2 = Help window on top of screen

  # 3 = None

  NAME_STYLE = 2

  

  # Only for type CURSOR_TYPE 2

  # Number of pixels to move per left(down) or right(up) press

  MOVE_PIXEL = 15

  

  # Only for type CURSOR_TYPE 2

  # Number of pixels around the icon you can click

  PIXEL_OFFSET = 20

  

  # Only for type NAME_STYLE 2

  # Opacity of Name Window

  NAME_WINDOW_OPACITY = 255

  

  # Show player icon above town?

  SHOW_PLAYER = true

  

  # Sound effect on move

  MOVE_SE = RPG::AudioFile.new("018-Teleport01", 80)

  

  # Number of the text color you want to use

  # Please note that you only need Window_Base(216 Colors) if you want to use a 

  # non-default color

  TOWN_TEXT_COLOR = 211 

  

  # Turn fog on and off

  FOG_ON = true

  

  # Graphic used for fog, must be in the Graphics/Fogs Folder

  FOG_GRAPHIC = "001-Fog01"

  

  # Fog Opacity

  FOG_OPACITY = 64

  

  # Fog scroll X

  FOG_SX = 1

  

  # Fog Scroll Y

  FOG_SY = 1

  

  # Fog Blending Type(1=Normal, 2=Add, 3=Subtract)

  FOG_BLEND_TYPE = 1 

 

  # Dont touch

  TOWN_COORD = {}  

# Setting Up a New Town

# 1. Assign a starting x coord

# 2. Assign a starting y coord

# 3. Assign what map you want it to go too

# 4. Assign a x coord on the map you will be moving to

# 5. Assign a y coord on the map you will be moving to

# 6. Assign the direction you want the player to face

#     2 = down

#     4 = left

#     6 = right

#     8 = up

# 7. Assign a icon graphic

# 8. Assign a switch for locking and unlocking the town

# 

# [starting_x, starting_y, map_id, map_x, map_y, direction, icon, 

#   switch, name of town]

#

# E.g TOWN_COORD[1] = [200,200,2,2,2,2,"town3", 1, "Lost Island"]

#

# VERY IMPORTANT: Put the towns in the order you want the game to cycle threw

# them. you dont have to unlock them in order, just insert them in the order

# you plan too have them cycle.

  TOWN_COORD[0] = [80,130,1,8,4,2,"town3",1,"Saar",] 

  TOWN_COORD[1] = [150,300,2,6,2,4,"snowtown",2,"Alphian"] 

  TOWN_COORD[2] = [215,390,3,2,8,2,"mountain",3,"Dwarven Mine"]        

  TOWN_COORD[3] = [270,105,4,1,2,2,"castle",4,"Royal Palace"]        

  TOWN_COORD[4] = [425,340,5,12,2,6,"tower",5,"Reeve"]        

  TOWN_COORD[5] = [450,100,6,2,6,2,"ruins",6,"Lost Ruins"]        

  TOWN_COORD[6] = [520,50,7,2,2,8,"forrest",7,"Elven Forrest",] 

end
Sprite_Cursor
Code:
#==============================================================================

# ** Sprite_Cursor

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

#  This sprite is used to display the cursor for Plague_Map.

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

 

class Sprite_Cursor < RPG::Sprite

 def initialize(viewport = Viewport.new(0, 0, 0, 0))

    super(viewport)

    update

  end

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

  # * Dispose

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

  def dispose

    if self.bitmap != nil

      self.bitmap.dispose

    end

    super

  end

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

  # * Frame Update

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

  def update

    super

  end

end

 
Window_Map
Code:
#==============================================================================

# ** Window_Plague_Map

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

#  This window displays the name box for Plague_Map

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

 

class Window_Map < Window_Base

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

  # * Object Initialization

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

  def initialize

    super(0, 0, 640, 64)

    self.contents = Bitmap.new(width - 32, height - 32)

    self.z = 2010

    @text = ""

    refresh

  end

  def text=(text1)

    @text = text1

    refresh

  end  

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

  # * Refresh

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

  def refresh

    self.contents.clear

    cx = contents.text_size(@text).width

    self.contents.font.color = normal_color

    self.contents.draw_text((320-(cx/1.5)), 0, cx, 32, @text)

  end

end

 
Main_Window_Map
Code:
#==============================================================================

# ** Plague_Map Script

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

#  Script by            :   Plague180

#  Last Update          :   March 29, 2010

#  Version              :   1.0

#  Contact Information  :   plague180([url=http://www.hbgames.org]http://www.hbgames.org[/url])

#  Contact Information  :   [email=plague180@yahoo.com]plague180@yahoo.com[/email]

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

# * Version History :

# 29MAR10 - Version 1.0 Testing Release

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

 

class Main_Window_Map < Window_Base

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

  # * Object Initialization

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

  def initialize(index=$game_map.map_id-1)

    super(0, 0, 640, 480)

    self.contents = Bitmap.new(640-32, 480-32)

    self.z = 2010

    self.opacity = 0

    @pindex = index 

    @cursor = Sprite_Cursor.new()

    @cursor.bitmap = RPG::Cache.icon(Map_Items::CURSOR)

    @cursor.z = 2020 

    if Map_Items::CURSOR_TYPE == 2

     @cursor.x = Map_Items::TOWN_COORD[@pindex][0] + RPG::Cache.icon(Map_Items::TOWN_COORD[@pindex][6]).width/4

     @cursor.y = Map_Items::TOWN_COORD[@pindex][1] + RPG::Cache.icon(Map_Items::TOWN_COORD[@pindex][6]).height

     @cursor.z = 2020

    end  

    refresh

  end

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

  # * Return value of @pindex

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

  def pindex

    return @pindex

  end  

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

  # * Move Player To New Map

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

  def move(index=0)

      $game_system.se_play(Map_Items::MOVE_SE)

      if Map_Items::CURSOR_TYPE == 1

        index = @pindex

      end

      Audio.bgm_fade(800)

      Audio.bgs_fade(800)

      Audio.me_fade(800)

      $game_temp.player_new_map_id = Map_Items::TOWN_COORD[index][2]

      $game_temp.player_new_x = Map_Items::TOWN_COORD[index][3]

      $game_temp.player_new_y = Map_Items::TOWN_COORD[index][4]

      $game_temp.player_new_direction = Map_Items::TOWN_COORD[index][5]

      $game_temp.player_transferring = true

      $game_map.autoplay

      $scene = Scene_Map.new

  end 

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

  # * Check if can move(and do it if you can)

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

  def move_if_can

    for i in 0...Map_Items::TOWN_COORD.size

      if @cursor.x > Map_Items::TOWN_COORD[i][0]-Map_Items::PIXEL_OFFSET && @cursor.x < Map_Items::TOWN_COORD[i][0] + RPG::Cache.icon(Map_Items::TOWN_COORD[i][6]).width + Map_Items::PIXEL_OFFSET

        if @cursor.y > Map_Items::TOWN_COORD[i][1]-Map_Items::PIXEL_OFFSET && @cursor.y < Map_Items::TOWN_COORD[i][1] + RPG::Cache.icon(Map_Items::TOWN_COORD[i][6]).height + Map_Items::PIXEL_OFFSET

          move(i)

        end

      end 

    end  

  end  

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

  # * Check if you can click on town, if so show name

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

  def check_name

      for i in 0...Map_Items::TOWN_COORD.size

      if @cursor.x > Map_Items::TOWN_COORD[i][0]-Map_Items::PIXEL_OFFSET && @cursor.x < Map_Items::TOWN_COORD[i][0] + RPG::Cache.icon(Map_Items::TOWN_COORD[i][6]).width + Map_Items::PIXEL_OFFSET

        if @cursor.y > Map_Items::TOWN_COORD[i][1]-Map_Items::PIXEL_OFFSET && @cursor.y < Map_Items::TOWN_COORD[i][1] + RPG::Cache.icon(Map_Items::TOWN_COORD[i][6]).height + Map_Items::PIXEL_OFFSET

          self.contents.clear

          refresh 

          self.contents.font.size = 18

          self.contents.font.color = text_color(Map_Items::TOWN_TEXT_COLOR)

          cx = contents.text_size(Map_Items::TOWN_COORD[i][8]).width

          if $game_switches[Map_Items::TOWN_COORD[i][7]] == true #unlocked?

            self.contents.draw_text(@cursor.x-cx/2,@cursor.y,cx,32,Map_Items::TOWN_COORD[i][8])

          end   

        end

      end 

    end 

  end  

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

  # * Check if you can click on town, if so show name window

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

  def check_name_window

      for i in 0...Map_Items::TOWN_COORD.size

      if @cursor.x > Map_Items::TOWN_COORD[i][0]-Map_Items::PIXEL_OFFSET && @cursor.x < Map_Items::TOWN_COORD[i][0] + RPG::Cache.icon(Map_Items::TOWN_COORD[i][6]).width + Map_Items::PIXEL_OFFSET

        if @cursor.y > Map_Items::TOWN_COORD[i][1]-Map_Items::PIXEL_OFFSET && @cursor.y < Map_Items::TOWN_COORD[i][1] + RPG::Cache.icon(Map_Items::TOWN_COORD[i][6]).height + Map_Items::PIXEL_OFFSET

          if $game_switches[Map_Items::TOWN_COORD[i][7]] == true #unlocked?

            return Map_Items::TOWN_COORD[i][8]

          end

        end  

      end 

    end 

    return ""

  end 

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

  # * Move Cursor Right 

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

  def move_cursor_right_one

     @cursor.x += Map_Items::MOVE_PIXEL

     refresh

  end

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

  # * Move Cursor Left 

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

  def move_cursor_left_one

     @cursor.x -= Map_Items::MOVE_PIXEL

     refresh

  end

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

  # * Move Cursor Up 

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

  def move_cursor_up_one

     @cursor.y -= Map_Items::MOVE_PIXEL

     refresh

  end

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

  # * Move Cursor Down

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

  def move_cursor_down_one

     @cursor.y += Map_Items::MOVE_PIXEL

     refresh

  end

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

  # * Move Cursor Right

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

  def move_cursor_right

      @pindex += 1

      if @pindex > Map_Items::TOWN_COORD.size-1

        @pindex = 0

      end

      if  $game_switches[Map_Items::TOWN_COORD[@pindex][7]] == false

        @pindex += 1

      end 

     refresh

   end 

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

  # * Move Cursor Left

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

  def move_cursor_left

      @pindex -= 1

      if @pindex < 0

        @pindex = Map_Items::TOWN_COORD.size-1

      end 

      if  $game_switches[Map_Items::TOWN_COORD[@pindex][7]] == false

        @pindex -= 1

      end

      refresh

  end  

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

  # * Move Cursor Up

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

  def move_cursor_up

      @pindex += 1

      if @pindex > Map_Items::TOWN_COORD.size-1

        @pindex = 0

      end

      if  $game_switches[Map_Items::TOWN_COORD[@pindex][7]] == false

        @pindex += 1

      end 

     refresh

  end

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

  # * Move Cursor Down

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

  def move_cursor_down

      @pindex -= 1

      if @pindex < 0

        @pindex = Map_Items::TOWN_COORD.size-1

      end 

      if  $game_switches[Map_Items::TOWN_COORD[@pindex][7]] == false

        @pindex -= 1

      end

      refresh

  end

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

  # * Draw Player

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

  def draw_player

    bitmap = RPG::Cache.icon(Map_Items::PLAYER_ICON) 

    @player_offset_x = Map_Items::TOWN_COORD[$game_map.map_id-1][0]+((RPG::Cache.icon(Map_Items::TOWN_COORD[$game_map.map_id-1][6]).width/2)-(bitmap.width/2))

    @player_offset_y = Map_Items::TOWN_COORD[$game_map.map_id-1][1]-bitmap.height

    self.contents.blt(@player_offset_x,@player_offset_y, bitmap, Rect.new(0,0,RPG::Cache.icon(Map_Items::PLAYER_ICON).height,RPG::Cache.icon(Map_Items::PLAYER_ICON).width))

  end 

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

  # * Draw Towns

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

  def draw_towns

    for i in 0...Map_Items::TOWN_COORD.size

      if $game_switches[Map_Items::TOWN_COORD[i][7]] == true #unlocked?

        bitmap = RPG::Cache.icon(Map_Items::TOWN_COORD[i][6]) 

        self.contents.blt(Map_Items::TOWN_COORD[i][0], Map_Items::TOWN_COORD[i][1], bitmap, Rect.new(0,0,bitmap.width,bitmap.height))

      end 

    end

  end 

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

  # * Draw Cursor

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

  def draw_cursor

     if Map_Items::CURSOR_TYPE == 1

        bitmap = @cursor.bitmap

        if $game_switches[Map_Items::TOWN_COORD[@pindex][7]] == true#unlocked

          if Map_Items::TOWN_COORD[@pindex][6] != nil#if custom town graphic

            x = Map_Items::TOWN_COORD[@pindex][0]+(RPG::Cache.icon(Map_Items::TOWN_COORD[@pindex][6]).width/2)-(bitmap.width/2)

            y = Map_Items::TOWN_COORD[@pindex][1]+(RPG::Cache.icon(Map_Items::TOWN_COORD[@pindex][6]).height+3)

          end

          if Map_Items::TOWN_COORD[@pindex][6] != nil#checks for custom icon

            self.contents.blt(x, y, bitmap, Rect.new(0,0,bitmap.height,bitmap.width))

          else #not custom

            self.contents.blt(Map_Items::TOWN_COORD[@pindex][0], Map_Items::TOWN_COORD[@pindex][1]+bitmap.width, bitmap, Rect.new(0,0,bitmap.height,bitmap.width))

          end 

          if Map_Items::NAME_STYLE == 1 # draw text under cursor

            self.contents.font.size = 18

               cx = contents.text_size(Map_Items::TOWN_COORD[@pindex][8]).width

               x = Map_Items::TOWN_COORD[@pindex][0]+(RPG::Cache.icon(Map_Items::TOWN_COORD[@pindex][6]).width/2)-cx/2

               self.contents.font.color = text_color(Map_Items::TOWN_TEXT_COLOR)

               self.contents.draw_text(x,y,cx,32,Map_Items::TOWN_COORD[@pindex][8])

          end 

        end

      end 

      if Map_Items::CURSOR_TYPE == 2

         bitmap = @cursor.bitmap

         self.contents.blt(@cursor.x, @cursor.y, bitmap, Rect.new(0,0,bitmap.height,bitmap.width))

      end  

    end 

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

  # * Refresh

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

  def check_cursor_bounds

     if @cursor.x < 0

       @cursor.x = 0

     end

     if @cursor.x > 640 - RPG::Cache.icon(Map_Items::CURSOR).width - 32

       @cursor.x = 640 - RPG::Cache.icon(Map_Items::CURSOR).width - 32

     end  

     if @cursor.y < 0

       @cursor.y = 0

     end

     if @cursor.y > 480 - RPG::Cache.icon(Map_Items::CURSOR).height - 32

       @cursor.y = 480 - RPG::Cache.icon(Map_Items::CURSOR).height - 32

     end 

  end  

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

  # * Refresh

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

  def refresh

    self.contents.clear

    check_cursor_bounds

    #draws player

    if Map_Items::SHOW_PLAYER == true

      draw_player

    end 

    #draws all towns

    draw_towns

    #draw cursor

    draw_cursor

  end

end #class
New_Scene_Map
Code:
#==============================================================================

# ** Plague_Scene_Map Script

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

#  Script by            :   Plague180

#  Last Update          :   March 29, 2010

#  Version              :   1.0

#  Contact Information  :   plague180([url=http://www.hbgames.org]http://www.hbgames.org[/url])

#  Contact Information  :   [email=plague180@yahoo.com]plague180@yahoo.com[/email]

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

# e.g $scene = New_Scene_Map.new("World_Map.bmp")

class New_Scene_Map 

   def initialize(name)

    @name = name.to_s

    @map = Sprite.new

    @map.bitmap = RPG::Cache.picture(@name)

    @map.z = 2000

    if Map_Items::FOG_ON == true

      @fog = Plane.new

      @fog.bitmap = RPG::Cache.fog(Map_Items::FOG_GRAPHIC,0)

      @fog.opacity = Map_Items::FOG_OPACITY

      @fog.blend_type = Map_Items::FOG_BLEND_TYPE

      @fog.z = 2005

    end

  end

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

  # * Main Processing

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

  def main

    # Make Plague Map window

    @plague_map = Main_Window_Map.new()

    # Make name window

    if Map_Items::NAME_STYLE == 2

      @name_window = Window_Map.new

      @name_window.opacity = Map_Items::NAME_WINDOW_OPACITY

      @name_window.text = Map_Items::TOWN_COORD[@plague_map.pindex][8]

    end

    # Execute transition

    Graphics.transition

    # Main loop

    loop do

      # Update game screen

      Graphics.update

      # Update input information

      Input.update

      # Frame update

      update

      # Abort loop if screen is changed

      if $scene != self

        break

      end

    end

    # Prepare for transition

    Graphics.freeze

    # Dispose of windows

    @plague_map.dispose

    @map.dispose

    if Map_Items::FOG_ON == true

      @fog.dispose 

    end

    if Map_Items::NAME_STYLE == 2

      @name_window.dispose

    end

  end

  def update

    # Update windows

    @plague_map.update

    @map.update

    if Map_Items::FOG_ON == true

      @fog.ox += Map_Items::FOG_SX

      @fog.oy -= Map_Items::FOG_SY 

    end

    if Map_Items::NAME_STYLE == 1

      @plague_map.check_name

    end  

    if Map_Items::NAME_STYLE == 2

      @name_window.text = Map_Items::TOWN_COORD[@plague_map.pindex][8]

      @name_window.update

      if Map_Items::CURSOR_TYPE == 2

        @name_window.text = @plague_map.check_name_window

        @name_window.update

      end  

    end

    # If B button was pressed

    if Input.trigger?(Input::B)

      # Return to map

      $scene = Scene_Map.new

      return

    end

    if Input.trigger?(Input::RIGHT)

      # Move the cursor to the right

      if Map_Items::CURSOR_TYPE == 1

        @plague_map.move_cursor_right

      end  

      if Map_Items::CURSOR_TYPE == 2

        @plague_map.move_cursor_right_one

      end 

      return

    end

    # If L button was pressed

    if Input.trigger?(Input::LEFT)

      # Move the cursor to the left

      if Map_Items::CURSOR_TYPE == 1

        @plague_map.move_cursor_left

      end

      if Map_Items::CURSOR_TYPE == 2

        @plague_map.move_cursor_left_one

      end  

      return

    end

    # If Up button was pressed

    if Input.trigger?(Input::UP)

      # Move the cursor to the left

      if Map_Items::CURSOR_TYPE == 1

        @plague_map.move_cursor_up

      end

      if Map_Items::CURSOR_TYPE == 2

        @plague_map.move_cursor_up_one

      end  

      return

    end

    # If Down button was pressed

    if Input.trigger?(Input::DOWN)

      # Move the cursor to the left

      if Map_Items::CURSOR_TYPE == 1

        @plague_map.move_cursor_down

      end

      if Map_Items::CURSOR_TYPE == 2

        @plague_map.move_cursor_down_one

      end  

      return

    end

    if Input.trigger?(Input::C)

        # Switch to item screen

        if Map_Items::CURSOR_TYPE == 1

          @plague_map.move

        end  

        if Map_Items::CURSOR_TYPE == 2

         @plague_map.move_if_can

        end  

    end  

  end

end

 

thanks for any advice, and i wont cry over harse advice, i know im still learning, will upload demo if any wants to see it in action without making all their own graphics.
 
guess thats a no...np anyways i put it in the play test version of my demo and none of the errors that were mentioned were related to this script. :) if u dont mind though i do have one more question. how would you guys go about making it so you can go to different sub sections of each town? the way i would go is simply open up a window with the choices(market, home, post office, eta) but that wont please most people who use this script and i want it to be really useful(to everyone), not another useless script from me. so the next way i would do it is change the background and draw the new coords of all the places you can go....but its not that simple. i cant just reuse the scene...it will only draw the normal stuff...and if i simply make a new scene i will need a new window class too(because thats where ive been putting all my methods that do math and check whats unlocked) i dont think users want alot of code to copy from demo to their project(tho this could be a useless worry, you tell me). plus i would have to do stuff like this:
Code:
#i know this is dumb code, but something like this

if Map_Items::Towns[@pindex.upper_level].switch == true && Sub_Map::Towns[@pindex].switch == true 

#draw sub_items(another loop lol)

 
or at least i think i would...check to see if the town you choose is valid(even tho it should be from current code) then check whats unlocked in that town to travel too...idk seems like a pain. im more then willing to do it. just want to do it the easy way not the hard way first. cause adding sephs much better data structure took hours cause i had to find and replace all my code. anyways thanks for any advice you can give :)



edit: i have it working!!!! just one little thing that annoys me to fix.
 

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