查看: 2666|回复: 4

[RMXP发布] 数组方法新增 & 战斗胜利时的窗口美化

[复制链接]

矿工

梦石
0
星屑
134
在线时间
898 小时
注册时间
2012-10-5
回帖
1303
发表于 2013-12-20 23:11:01 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 876加几 于 2013-12-20 23:51 编辑

我为了将[7, 7, 7, 7, 6]这类的数组消灭掉全部相同的,费了我很大的功夫。
于是,我好不容易才写好了Array方法新增,从此消灭重复单元不再费劲了。
脚本:
[pre lang="ruby" line="1"]=begin
===============================================================================
  ■ 使用方法:
-------------------------------------------------------------------------------
  ● number 则返回某个单元名称重复的数量。
    例:[7, 7, 7, 7, 6, 6, 5].number(7)      => 4
  ● aftftn 也是返回重复的数量,但与 number 有所不同。
    例:[7, 7, 7, 7, 6, 6, 5].aftftn(4)      => 2
    也就是说就是数组第几个单元的名称重复的数量。
  ● delete_same 则删除单元里指定名称重复的部分,具有破坏性。
    例:a = [7, 7, 7, 7, 6, 6, 5]
        a.delete_same(7)
        p a                                  => [7, 6, 6, 5]
  ● same? 则返回是否是重复的单元。
    例:a = [7, 7, 7, 7, 6, 6, 5]
        p a.same?(4)                         => false
        p a.same?(3)                         => true
  ● das 和 das! 都是删除数组中所有重复的单元,但是 das 只会返回删除后的数组,das!
     具有破坏性。
    例:a = [7, 7, 7, 7, 6, 6, 5]
        p a.das                              => [7, 6, 5]
        p a                                  => [7, 7, 7, 7, 6, 6, 5]   # 没有变化
        a.das!
        p a                                  => [7, 6, 5]
        # 之后没法再消了,总是返回原来的数组。
  ● aftft 和 index 都是寻找第一个出现的单元,但有所不同。
    例:a = [7, 7, 7, 7, 6, 6, 5]
        p a.aftft(2)                         => 0
    等同于
        p a.index(a[2])                      => 0
  ● dasn 和 dasn! 都是先删除重复的单元再计算出重复单元的数量,但是 dasn 只会返
     回改变后的数组,dasn! 具有破坏性!
    例:a = [7, 7, 7, 7, 6, 6, 5]
        p a.dasn                             => [4, 2, 1]
        p a                                  => [7, 7, 7, 7, 6, 6, 5]   # 没有变化
        a.dasn!
        p a                                  => [4, 2, 1]
        a.dasn!
        p a                                  => [1, 1, 1]
        a.dasn!
        p a                                  => [3]
        a.dasn!
        p a                                  => [1]
        # 之后永远就会出现 [1] 了。
  ● size 可以自行更改了,请看例子:
    例:a = [7, 7, 7, 7, 6, 6, 5]
        p a.size                             => 7
        a.size = 4
        p a                                  => [7, 7, 7, 7]
        p a.size                             => 4
        a.size **= 2
        p a                                  => [7, 7, 7, 7, nil, nil, nil, nil,
                                                 nil, nil, nil, nil, nil, nil,
                                                 nil, nil]
        # size 可以做任何运算。
  ● fill 就是规定长度往后面 push 相应的字串,具有破坏性。
    例:a = [7, 7, 7, 7, 6, 6, 5]
        a.fill(3)
        p a                                  => [7, 7, 7, 7, 6, 6, 5, nil, nil,
                                                 nil]
        a.fill(2, "我们")                    => [7, 7, 7, 7, 6, 6, 5, nil, nil,
                                                 nil, "我们", "我们"]
===============================================================================
=end
class Array
  def number(nth_name)
    if self.index(nth_name) == nil
      return 0
    end
    num = 1
    for i in 0...self.size
      if self == nth_name and self.index(nth_name) != i
        num += 1
      end
    end
    return num
  end
  def aftftn(index)
    num = 1
    aftft = 0
    for i in 0...self.size
      if self == self[index]
        aftft = i
        break
      end
    end
    for j in 0...self.size
      if self[j] == self[aftft] and aftft != j
        num += 1
      end
    end
    return num
  end
  def delete_same(name)
    if self.number(name) == 0
      return self
    end
    for i in 1..self.number(name)
      for d in 0...self.size
        if self[d] == name and self.index(name) != d
          self.delete_at(d)
          break
        end
      end
    end
    return self
  end
  def same?(index)
    return (self.aftft(index) != index)
  end
  def das # (delete_all_same)
    ary = self.dup
    for j in 0...self.size
      ary.delete_same(self[j])
    end
    return ary
  end
  def das!
    ary = self.dup
    for j in 0...ary.size
      self.delete_same(ary[j])
    end
    return self
  end
  def aftft(index)
    num = nil
    for j in 0...self.size
      if self[index] == self[j]
        num = j
        break
      end
    end
    return num
  end
  def dasn # (delete_all_same_number)
    pushing = []
    ary = self.dup
    a = self.das
    for i in 0...a.size
      pushing = ary.number(a)
    end
    return pushing
  end
  def dasn!
    ary = self.dup
    a = self.das
    self.clear
    for i in 0...a.size
      self = ary.number(a)
    end
    return self
  end
  def size=(size)
    if self.size == size
      return
    end
    if self.size > size
      s = self.size
      for i in 0..self.size-size
        self.delete_at(s-i)
      end
      return
    end
    for i in self.size...size
      self = nil
    end
  end
  def fill(size, text = nil)
    size.times do
      self.push(text)
    end
  end
end
[/pre]有了这个,还可以美化一下战斗结束提示窗口。
[pre lang="ruby" line="1"]#==============================================================================
# ■ Window_BattleResult
#------------------------------------------------------------------------------
#  战斗结束时、显示获得的 EXP 及金钱的窗口。
#==============================================================================

class Window_BattleResult < Window_Base
  #--------------------------------------------------------------------------
  # ● 初始化对像
  #     exp       : EXP
  #     gold      : 金钱
  #     treasures : 宝物
  #--------------------------------------------------------------------------
  def initialize(exp, gold, treasures)
    @exp = exp
    @gold = gold
    @treasures = treasures.das
    @treasures_num = treasures.dasn
    super(160, 0, 320, @treasures.size * 32 + 64)
    self.contents = Bitmap.new(width - 32, height - 32)
    self.y = 160 - height / 2
    self.back_opacity = 160
    self.visible = false
    refresh
  end
  #--------------------------------------------------------------------------
  # ● 刷新
  #--------------------------------------------------------------------------
  def refresh
    self.contents.clear
    x = 4
    self.contents.font.color = normal_color
    cx = contents.text_size(@exp.to_s).width
    self.contents.draw_text(x, 0, cx, 32, @exp.to_s)
    x += cx + 4
    self.contents.font.color = system_color
    cx = contents.text_size("EXP").width
    self.contents.draw_text(x, 0, 64, 32, "EXP")
    x += cx + 16
    self.contents.font.color = normal_color
    cx = contents.text_size(@gold.to_s).width
    self.contents.draw_text(x, 0, cx, 32, @gold.to_s)
    x += cx + 4
    self.contents.font.color = system_color
    self.contents.draw_text(x, 0, 128, 32, $data_system.words.gold)
    y = 32
    for i in 0...@treasures.size
      item = @treasures
      draw_item_name(item, 4, y)
      self.contents.draw_text(320 - 32 - 48, y, 48, 32, @treasures_num.to_s, 2)
      self.contents.draw_text(320 - 32 - 96, y, 48, 32, "×", 1)
      y += 32
    end
  end
end
[/pre]

点评

我想说的晴兰已经说了……  发表于 2013-12-22 11:49
@晴兰 RMXP没有此方法。  发表于 2013-12-21 08:22
消灭重复其实有uniq。。。  发表于 2013-12-21 01:25

评分

参与人数 1星屑 +275 收起 理由
怪蜀黍 + 275 部分有用

查看全部评分

呃,发糖贴好冷清呀!
梦石
0
星屑
50
在线时间
466 小时
注册时间
2013-2-8
回帖
420
发表于 2013-12-21 00:15:42 来自手机 | 显示全部楼层
我好像记得本来就有压缩的方法吧
回复

使用道具 举报

梦石
0
星屑
2826
在线时间
2634 小时
注册时间
2013-1-16
回帖
5453

贵宾

发表于 2013-12-21 00:48:20 | 显示全部楼层
话说class Array里def number写成def amount 会更好…

点评

RMVA里面叫count。。不过这个也是因人而异的  发表于 2013-12-21 01:24
(Created by @喵kano)


施工现场:hotege.github.io
回复

使用道具 举报

梦石
0
星屑
122
在线时间
552 小时
注册时间
2012-8-18
回帖
1392
发表于 2013-12-22 09:56:02 | 显示全部楼层
本帖最后由 无脑之人 于 2013-12-22 13:37 编辑

骚年Array有uniq……就算RMXP没有用| []也是可以的
另外你怒黑fill方法,这个方法在Array里面可是有定义的哦


uniq
uniq!

    uniq会删除数组中的重复元素后生成新数组并返回它。剩下的元素会向前移动。uniq!具有破环性,若进行了删除则返回self,若没有删除则返回nil。

    使用Object#eql?来判定元素是否重复。

    例:

    p [1, 1, 1].uniq         #=> [1]
    p [1, 4, 1].uniq         #=> [1, 4]
    p [1, 3, 2, 2, 3].uniq   #=> [1, 3, 2]

fill(val)
fill(val, start[, length])
fill(val, start..end)
fill {|index| ... } ((<ruby 1.7 特性>))
fill(start[, length]) {|index| ... } ((<ruby 1.7 特性>))
fill(start..end) {|index| ... } ((<ruby 1.7 特性>))

    将val赋值给数组中被指定的范围内的所有元素。在第二种形式中,若省略length则意味着将指定范围扩大到数组尾部。若指定范围最终超出原数组的长度时,将自动加长原数组,并以val来对新增元素进行初始化。

    该方法并不是使用val的拷贝,而是用val本身进行赋值(trap::Array)。

    ruby 1.7 特性:

    若没有指定val,而是代之以块时,则以块的计算值进行赋值。对每个元素进行赋值时都会重新计算块,所以被赋值的元素都是某对象的拷贝。传给块的形参的值,是从start开始的索引。

    ary = []
    p ary.fill(1..2) {|i| i}        # => [nil, 1, 2]
    p ary.fill(0,3) {|i| i}         # => [0, 1, 2]
    p ary.fill { "foo" }            # => ["foo", "foo", "foo"]
    p ary.collect {|v| v.id }       # => [537770124, 537770112, 537770100]

    ruby 1.8 特性:在1.8.0版本中有bug,块的形参会出问题。

    ary = []
    p ary.fill(1..2) {|i| i}        # => [2, 4, 6]  <- bug
    p ary.fill(0,3) {|i| i}         # => [1, 3, 5]  <- bug
    p ary.fill { "foo" }            # => ["foo", "foo", "foo"]
    p ary.collect {|v| v.id }       # => [537770124, 537770112, 537770100]


参考文档:http://www.kuqin.com/rubycndocum ... s_object_array.html

点评

真的没有~~~  发表于 2013-12-22 13:03
我要填坑!我要背单词!我要学日语!我要每天锻炼!
好吧呵呵= =
回复

使用道具 举报

○赛

梦石
0
星屑
1254
在线时间
1276 小时
注册时间
2013-1-22
回帖
2078

贵宾

发表于 2013-12-27 10:15:12 | 显示全部楼层
@876加几 @无脑之人 @晴兰
经过我的测试,uniq!是有效的,有图为证: 搜狗截图 2年01月01日0155_6.png 搜狗截图 2年01月01日0155_7.png 搜狗截图 2年01月01日0155_9.png

点评

请不要莫名其妙的@人- -  发表于 2013-12-27 14:03
当然有- -  发表于 2013-12-27 12:20

评分

参与人数 1星屑 +25 收起 理由
怪蜀黍 + 25 测试奖

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部