Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #===========RGSS+ (v.1.13)==============
- #-------------------------------------------------------------------------------
- #Различные добавки к RGSS.
- #Версия набора: 1.13 от 13.05.2015
- # Array:
- # ~random - возвращает случайный элемент из массива
- # ~size= - строго устанавливает количество элементов
- # Integer:
- # ~sign - возвращает знак для числа (положительное/отрицательное/ноль)
- # ~factorial - возвращает факториал числа
- # ~combine - пересчитывает количество комбинаций от K до текущего
- # Font:
- # ~shadowed? - включены ли наборы теней
- # ~shadowed= - включает/выключает наборы теней
- # ~shadows - возвращает список теней
- # ~shadows= - позволяет задать набор теней в виде хэша в формате:
- # {индекс=> [смещение_по_х, смещение_по_у, цвет]}
- # (индекс определяет порядок отрисовки)
- # Sprite:
- # ~dispose - добавляет удаление битмапа перед удалением спрайта
- # ~text_color - для добавления в спрайт текста с параметрами
- # цвета как в window_base, то есть:
- # ~~normal_color
- # ~~disabled_color
- # ~~system_color
- # ~~crisis_color
- # ~~knockout_color
- # ~break_into_lines - разрезает строку текста на несколько
- # ~symbolize - превращает строку в символ правильно
- # String:
- # ~to_ary - возвращает массив, где каждый элемент - это один символ из строки
- # ~symbolize - правильно конвертирует в символ
- # Дополнено для работы с кириллицей:
- # ~updace - делает буквы ПРОПИСНЫМИ, меняет исходную строку
- # ~downcase - делает буквы строчными, меняет исходную строку
- # RPG:
- # ~confirm - вызывает окно для подтверждения действия
- # ~warn - вызывает окно для показа ошибки/уведомления
- # Bitmap:
- # ~darken - затемняет картинку
- # ~lighten - осветляет картинку
- # Переписано для работы с наборами теней (см. Font):
- # ~draw_text - отображение текста на картинке с набором теней (если включены)
- # Bitmask - полностью скопировано со скрипта от MoonPearl
- # Symbol:
- # ~display - правильно конвертирует в строку
- # Dir:
- # ~files - возвращает массив файлов в папке
- # ~subdirectories - возвращает массив подкаталогов в папке
- # ~has_subdirectories? - проверяет наличие хотя бы одного подкаталога
- # File:
- # ~directory? - проверяет, является ли файл папкой
- # Kernel (неявно):
- # ~with - добавляет аналог метода из VisualBasic и Delphi
- # ~dtime - получает разницу во времени в миллисекундах
- #-------------------------------------------------------------------------------
- ($imported ||= {})[:de79_rgss_plus] = 1.13
- #-------------------------------------------------------------------------------
- # ARRAY
- #-------------------------------------------------------------------------------
- class Array
- def random
- if self.size!=nil
- ret=rand(self.size)
- return self[ret-1]
- end
- return nil
- end
- def size=(value)
- return if value==self.size
- new_arr=[]
- if value<self.size
- for index in 0...value
- new_arr<<self[index]
- end
- self.clear
- for index in 0...new_arr.size
- self.push new_arr[index]
- end
- elsif value>self.size
- for index in self.size...value
- self.push nil
- end
- end
- end
- end
- #-------------------------------------------------------------------------------
- # INTEGER
- #-------------------------------------------------------------------------------
- class Integer
- def sign
- if self>0 then
- return 1
- elsif self<0
- return -1
- else
- return 0
- end
- end
- # Возвращает значение факториала от переменной self
- def factorial
- f = 1
- for i in 1..self
- f *= i
- end
- return f
- end
- # Пересчитывает количество комбинаций от K до текущего
- # - k: любое положительное число, которое меньеше и равно текущему
- def combin(k)
- return self.factorial / (k.factorial * (n - k).factorial)
- end
- end
- #-------------------------------------------------------------------------------
- # FONT
- #-------------------------------------------------------------------------------
- class Font
- alias original_initialize initialize
- def initialize
- original_initialize
- @shadowed=false
- @shadows={}
- end
- def shadowed?
- return @shadowed
- end
- def shadowed=(value)
- @shadowed=value if value.class==TrueClass||FalseClass
- end
- def black_color
- Color.new(0,0,0,128)
- end
- def white_color
- Color.new(255,255,255,128)
- end
- def shadows
- return @shadows
- end
- def shadows=(value)
- @shadows=value if value.class==Hash
- end
- def set_simple_shadow
- shadowed=true
- @shadows={
- 0=>[1,1,black_color]
- }
- end
- def set_simple_stamping
- shadowed=true
- @shadows={
- 0=>[1,1,black_color],
- 1=>[-1,-1,white_color]
- }
- end
- end
- #-------------------------------------------------------------------------------
- # SPRITE
- #-------------------------------------------------------------------------------
- class Sprite
- alias old_dispose dispose
- def dispose
- if self.bitmap!=nil
- self.bitmap.dispose
- end
- old_dispose
- end
- def text_color(n)
- case n
- when 0
- return Color.new(255, 255, 255, 255)
- when 1
- return Color.new(128, 128, 255, 255)
- when 2
- return Color.new(255, 128, 128, 255)
- when 3
- return Color.new(128, 255, 128, 255)
- when 4
- return Color.new(128, 255, 255, 255)
- when 5
- return Color.new(255, 128, 255, 255)
- when 6
- return Color.new(255, 255, 128, 255)
- when 7
- return Color.new(192, 192, 192, 255)
- else
- normal_color
- end
- end
- def normal_color
- return Color.new(255, 255, 255, 255)
- end
- #--------------------------------------------------------------------------
- # * Get Disabled Text Color
- #--------------------------------------------------------------------------
- def disabled_color
- return Color.new(255, 255, 255, 128)
- end
- #--------------------------------------------------------------------------
- # * Get System Text Color
- #--------------------------------------------------------------------------
- def system_color
- return Color.new(192, 224, 255, 255)
- end
- #--------------------------------------------------------------------------
- # * Get Crisis Text Color
- #--------------------------------------------------------------------------
- def crisis_color
- return Color.new(255, 255, 64, 255)
- end
- #--------------------------------------------------------------------------
- # * Get Knockout Text Color
- #--------------------------------------------------------------------------
- def knockout_color
- return Color.new(255, 64, 0)
- end
- end
- #-------------------------------------------------------------------------------
- # STRING
- #-------------------------------------------------------------------------------
- class String
- # Разрезает строку текста на несколько
- # - amount: максимальное количество символов в одной строке
- def break_into_lines(amount)
- if self.size <= amount
- yield self
- else
- i = 0
- until i >= self.size
- n = self[i, amount].reverse.index(" ")
- n = amount - (n.nil? ? 0 : n)
- yield self[i, n]
- i += n
- end
- end
- end
- # Превращает строку в символ правильно
- def symbolize
- return :empty if self.empty?
- return self.downcase.gsub(" ", "_").to_sym
- end
- # Заменяет все строчные буквы прописными
- alias old_upcase upcase
- def upcase
- newstring=self.old_upcase.clone
- newcase=[]
- if RUBY_VERSION.to_i<1.9
- ('А'..'П').each{ |item|
- newcase+= [ item ]
- }
- ('О'..'Я').each{ |item|
- newcase+= [ item ]
- }
- index=0
- ('а'..'п').each{ |item|
- newstring.gsub!(item) {newcase[ index ]}
- index+=1
- }
- ('о'..'я').each{ |item|
- newstring.gsub!(item) {newcase[ index ]}
- index+=1
- }
- else
- ('А'..'Я').each{ |item|
- newcase+= [ item ]
- }
- index=0
- ('а'..'я').each{ |item|
- newstring.gsub!(item) {newcase[ index ]}
- index+=1
- }
- end
- newstring.gsub!('ё') { 'Ё' }
- return newstring
- end
- alias old_upcase! upcase!
- def upcase!
- self.old_upcase!
- newcase=[]
- if RUBY_VERSION.to_i<1.9
- ('А'..'П').each{ |item|
- newcase+= [ item ]
- }
- ('О'..'Я').each{ |item|
- newcase+= [ item ]
- }
- index=0
- ('а'..'п').each{ |item|
- self.gsub!(item) {newcase[ index ]}
- index+=1
- }
- ('о'..'я').each{ |item|
- self.gsub!(item) {newcase[ index ]}
- index+=1
- }
- else
- ('А'..'Я').each{ |item|
- newcase+= [ item ]
- }
- index=0
- ('а'..'я').each{ |item|
- self.gsub!(item) {newcase[ index ]}
- index+=1
- }
- end
- self.gsub!('ё') { 'Ё' }
- return self
- end
- # Заменяет прописные буквы строчными
- alias old_downcase downcase
- def downcase
- newstring=self.old_downcase.clone
- newcase=[]
- if RUBY_VERSION.to_i<1.9
- ('а'..'п').each{ |item|
- newcase+= [ item ]
- }
- ('о'..'я').each{ |item|
- newcase+= [ item ]
- }
- index=0
- ('А'..'П').each{ |item|
- newstring.gsub!(item) {newcase[ index ]}
- index+=1
- }
- ('О'..'Я').each{ |item|
- newstring.gsub!(item) {newcase[ index ]}
- index+=1
- }
- else
- ('а'..'я').each{ |item|
- newcase+= [ item ]
- }
- index=0
- ('А'..'Я').each{ |item|
- newstring.gsub!(item) {newcase[ index ]}
- index+=1
- }
- end
- newstring.gsub!('Ё') { 'ё' }
- return newstring
- #end
- end
- alias old_downcase! downcase!
- def downcase!
- self.old_downcase!
- if RUBY_VERSION.to_i<1.9
- ('а'..'п').each{ |item|
- newcase+= [ item ]
- }
- ('о'..'я').each{ |item|
- newcase+= [ item ]
- }
- index=0
- ('А'..'П').each{ |item|
- self.gsub!(item) {newcase[ index ]}
- index+=1
- }
- ('О'..'Я').each{ |item|
- self.gsub!(item) {newcase[ index ]}
- index+=1
- }
- else
- ('а'..'я').each{ |item|
- newcase+= [ item ]
- }
- index=0
- ('А'..'Я').each{ |item|
- self.gsub!(item) {newcase[ index ]}
- index+=1
- }
- end
- self.gsub!('Ё') { 'ё' }
- return self
- end
- def to_ary
- ary=[]
- while ((c = self.slice(/./m)) != nil)
- ary+=[c]
- end
- return ary
- end
- end
- #-------------------------------------------------------------------------------
- # MODULE RPG
- #-------------------------------------------------------------------------------
- module RPG
- def self.warn(message)
- $game_system.se_play($data_system.decision_se)
- Graphics.freeze
- window = Window_Base.new(32, 192, 576, 96)
- window.contents = Bitmap.new(window.width - 32, window.height - 32)
- window.contents.draw_text(0, 0, window.width - 32, 32, message, 1)
- window.contents.draw_text(0, 32, 544, 32, "OK", 1)
- window.cursor_rect.set(208, 32, 128, 32)
- window.active = true
- window.z = 5000
- Graphics.transition
- loop do
- Input.update
- Graphics.update
- window.update
- if Input.trigger?(Input::C) or Input.trigger?(Input::B)
- $game_system.se_play($data_system.cancel_se)
- break
- end
- end
- Graphics.freeze
- window.dispose
- Graphics.transition
- return
- end
- def self.confirm(message)
- $game_system.se_play($data_system.buzzer_se)
- Graphics.freeze
- window = Window_Base.new(32, 192, 576, 96)
- window.contents = Bitmap.new(window.width - 32, window.height - 32)
- window.contents.draw_text(0, 0, window.width - 32, 32, message, 1)
- window.contents.draw_text(0, 32, 272, 32, "OK", 1)
- window.contents.draw_text(272, 32, 272, 32, "Cancel", 1)
- window.cursor_rect.set(72, 32, 128, 32)
- window.active = true
- window.z = 5000
- Graphics.transition
- index = 0
- result = nil
- while result.nil?
- Input.update
- Graphics.update
- window.update
- if Input.repeat?(Input::LEFT) or Input.repeat?(Input::RIGHT)
- $game_system.se_play($data_system.cursor_se)
- index += 1
- index %= 2
- window.cursor_rect.set(72 + 272 * index, 32, 128, 32)
- end
- if Input.trigger?(Input::C)
- $game_system.se_play($data_system.decision_se)
- result = index == 0
- end
- if Input.trigger?(Input::B)
- $game_system.se_play($data_system.cancel_se)
- result = false
- end
- end
- Graphics.freeze
- window.dispose
- Graphics.transition
- return result
- end
- end
- #-------------------------------------------------------------------------------
- # BITMAP
- #-------------------------------------------------------------------------------
- class Bitmap
- attr_reader :filename
- alias old_initialize initialize
- def initialize(arg1=32,arg2=32)
- old_initialize(arg1,arg2) if arg1.is_a?(Integer)
- if arg1.is_a? String
- @filename=arg1
- old_initialize @filename
- end
- end
- def darken(factor)
- factor = 1 - factor
- for y in 0...self.height
- for x in 0...self.width
- color = self.get_pixel(x, y)
- next if color.alpha == 0
- self.set_pixel(x, y, Color.new(color.red * factor, color.green * factor,
- color.blue * factor, color.alpha) )
- end
- end
- GC.start
- end
- def lighten(factor)
- p = 255 * factor
- factor = 1 - factor
- for y in 0...self.height
- for x in 0...self.width
- color = self.get_pixel(x, y)
- next if color.alpha == 0
- self.set_pixel(x, y, Color.new(color.red * factor + p, color.green * factor + p,
- color.blue * factor + p, color.alpha) )
- end
- end
- GC.start
- end
- alias old_draw_text draw_text
- def draw_text(*args)
- old_color=self.font.color.clone
- if args.size<=3
- draw_shadows_rect(args[0],args[1], (args[2].is_a? NilClass) ? 0 : args[2]) if self.font.shadowed?
- self.font.color=old_color
- old_draw_text(args[0],args[1],(args[2].is_a? NilClass) ? 0 : args[2])
- elsif args.size<=6
- draw_shadows_normal(args[0],args[1],args[2],args[3],args[4],(args[5].is_a? NilClass) ? 0 : args[5]) if self.font.shadowed?
- self.font.color=old_color
- old_draw_text(args[0],args[1],args[2],args[3],args[4],(args[5].is_a? NilClass) ? 0 : args[5])
- end
- end
- def draw_shadows_rect(rect,str,align=0)
- arr=self.font.shadows.keys
- arr.sort!
- index=arr.first
- loop do
- if self.font.shadows.has_key? index
- self.font.color=self.font.shadows[index][2]
- old_draw_text(rect.x+self.font.shadows[index][0],rect.y+self.font.shadows[index][1],
- rect.width,rect.height,str,align)
- end
- index+=1
- break if index>=arr.last
- end
- end
- def draw_shadows_normal(x,y,w,h,str,align=0)
- arr=self.font.shadows.keys
- arr.sort!
- index=arr.first
- loop do
- if self.font.shadows.has_key? index
- self.font.color=self.font.shadows[index][2]
- old_draw_text(x+self.font.shadows[index][0],y+self.font.shadows[index][1],
- w,h,str,align)
- end
- index+=1
- break if index>=arr.last
- end
- end
- end
- #-------------------------------------------------------------------------------
- # BITMASK
- #-------------------------------------------------------------------------------
- class Bitmask
- def initialize(size = 1)
- @data = [0]
- resize(size)
- end
- def [](i)
- if i >= @size
- raise RangeError, "Bit ##{i} not defined (max = #{@size - 1}) in Bitmask#[]"
- end
- return @data[i >> 5][i & 31] == 1
- end
- def []=(i, b)
- case b
- when TrueClass, FalseClass
- if self[i] == b
- return nil
- else
- @data[i >> 5] ^= 2 ** i
- return b
- end
- else
- raise TypeError, "TrueClass or FalseClass expected as parameters in Bitmask#[]="
- end
- end
- def resize(size)
- while (size >> 5) >= @data.size
- @data << 0
- end
- @size = size
- end
- def to_s
- str = String.new
- for i in 0...@size
- if i & 7 == 0
- str << " "
- end
- str << @data[i >> 5][i & 31].to_s
- end
- return str
- end
- end
- #-------------------------------------------------------------------------------
- # SYMBOL
- #-------------------------------------------------------------------------------
- class Symbol
- # Правильно превращает символ в строку текста
- def display
- str = ""
- name = self.to_s
- name.split("_").each { |s| str += s.capitalize + " " }
- str = str.rstrip
- return str
- end
- end
- #-------------------------------------------------------------------------------
- # DIR
- #-------------------------------------------------------------------------------
- class Dir
- # Возвращает все файлы, в названии которых имеются заданные значения,
- # в заданном каталоге как массив
- # - каталог: путь к папке, в которой будет произведен поиск
- # (примеры: "C:\Temp" или "Graphics\Titles")
- # - фильтр(-ы): значения, по которым нужно искать
- # (примеры: "_blue", "icon", ".png", [".txt",".html"] )
- def self.files(directory = self.pwd, *filters)
- ary = []
- for filename in self.entries(directory)
- next if File.directory?(filename) or filename == "." or filename == ".."
- if filters.empty? or filters.include?(File.extname(filename))
- ary << filename
- end
- end
- return ary
- end
- # Возвращает все каталоги, находящиеся в заданном, как массив
- def self.subdirectories(directory = self.pwd)
- ary = []
- for filename in self.entries(directory)
- if File.directory?(filename)
- ary << filename
- end
- end
- return ary
- end
- def self.has_subdirectories?(directory = self.pwd)
- ary = []
- for filename in self.entries(directory)
- if File.directory?(filename)
- ary << filename
- end
- end
- return true if ary.size>0
- return false
- end
- end
- #-------------------------------------------------------------------------------
- # FILE
- #-------------------------------------------------------------------------------
- class File
- def self.directory?(filename)
- return false if filename == "."
- return true if self.extname(filename) == ""
- return false
- end
- end
- #-------------------------------------------------------------------------------
- # CACHE
- #-------------------------------------------------------------------------------
- module RPG
- module Cache
- def self.face(filename, hue)
- self.load_bitmap("Graphics/Faces/", filename, hue)
- end
- def self.state(filename)
- self.load_bitmap("Graphics/States/", filename)
- end
- def self.logo(filename)
- self.load_bitmap("Graphics/Logos/", filename)
- end
- end
- end
- #-------------------------------------------------------------------------------
- # KERNEL
- #-------------------------------------------------------------------------------
- def with(instance, &block)
- instance.instance_eval(&block)
- instance
- end
- # use with t1=Time.now before check and t2=Time.now after
- def dtime(start, finish)
- (finish - start) * 1000.0
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement