查看: 2457|回复: 1

[RMXP发布] 初学RGSS,实现简单的推箱子功能。

[复制链接]
梦石
0
星屑
50
在线时间
14 小时
注册时间
2010-7-3
回帖
0
发表于 2013-12-21 12:53:42 | 显示全部楼层 |阅读模式

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

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

×
本帖最后由 jiaboxuan 于 2013-12-21 23:15 编辑

因为是刚刚开始接触RGSS,所以不知道我这样实现有什么弊端,期望有经验的朋友们来帮忙指正。
[pre lang="ruby" line="1" file="Game_Character_Box"]
#==============================================================================
# ■ Game_Character_Box (自定义)
#------------------------------------------------------------------------------
# □ 扩展Game_Character
#==============================================================================

class Game_Character
  #--------------------------------------------------------------------------
  # ● 定义实例变量
  #--------------------------------------------------------------------------
  attr_accessor   :destination            # 目的地
  attr_accessor   :is_arrive              # 是否到达
  #--------------------------------------------------------------------------
  # ● 初始化对像
  #--------------------------------------------------------------------------
  def initialize_box
    @destination = false
    @is_arrive = false   
    $pass_num = 0
  end
end
[/pre]
[pre lang="ruby" line="1" file="Interpreter_Box"]
#==============================================================================
# ■ Interpreter_Box (自定义)
#------------------------------------------------------------------------------
# □ 扩展Interpreter
#==============================================================================
class Interpreter
  #--------------------------------------------------------------------------
  # ● 自定义脚本_设定目的地
  #--------------------------------------------------------------------------
  def command_set_destination   
    unless get_character(0).destination
      get_character(0).destination = true
      $pass_num += 1
    end  
  end  
  #--------------------------------------------------------------------------
  # ● 自定义脚本_推箱子
  #--------------------------------------------------------------------------
  def command_pushbox
   
    row = 0
    col = 0
   
    # 判断人物朝向
    case $game_player.direction
    when 2
      row += 1
    when 4
      col -= 1
    when 6
      col += 1
    when 8
      row -= 1
    end   
   
    # 箱子
    box = get_character(0)

    # 地图上的这个位置不能通行的情况下
    unless $game_map.passable?(box.x+col, box.y+row, 0)
      return
    end   
   
    # 遍历所有事件
    for event in $game_map.events.values  
      # 首先排除自己
      if event.id != box.id
        # 1.判断箱子后方是否存在将要触发的事件
        if event.x == box.x+col && event.y == box.y+row
          # 2.判断是否可以通过
          if event.through
            break
          else            
            return
          end
        end        
      else
          next
      end
    end
   
    # 如果方法可以执行到这里,说明触发了移动事件   
    box.moveto(box.x+col, box.y+row)   
    # 移动之后暂时不清楚是否到达目的,所以将标志位统一设置成false
    box.is_arrive = false

    # 更新人物位置
    case $game_player.direction
    when 2
      $game_player.move_down
    when 4
      $game_player.move_left
    when 6
      $game_player.move_right      
    when 8
      $game_player.move_up      
    end   

    # 遍历所有事件,判断移动后的箱子是否到达目的地
    for event in $game_map.events.values   
      # 首先排除自己
      if event.id != box.id
        # 1.判断箱子所在位置是否存在事件
        if event.x == box.x && event.y == box.y
            # 2.判断该事件的目的地信息
            if event.destination
              box.is_arrive = true
              break
            else
              return
            end            
        end
      end  
    end
   
    # 如果方法可以执行到这里,说明这次移动触发了到达目的地事件   
    num = 0
    # 遍历所有事件,统计到达目的地的箱子数量
    for event in $game_map.events.values
      if event.is_arrive
        num +=1
      end
    end
   
    # 判断到达目的地的箱子数量和通关所需的数量是否一致   
    if num != 0 && num == $pass_num
      print "通关(打印信息会暂停动画效果,并非BUG.)"
    end
   
  end  
end
[/pre]


最后在 Game_Character 1 中 initialize 方法中调用 initialize_box 方法

在地图编辑时
1.如果需要设置箱子,
  编辑事件-->选项-->固定朝向
                -->事件开始条件-->与主角接触
                -->执行内如-->插入-->脚本-->command_pushbox
2.如果需要设置目的地,
  编辑事件-->选项-->允许穿透
                -->事件开始条件-->自动执行
                -->执行内容-->插入-->脚本-->command_set_destination
                -->执行内容-->插入-->操作独立开关-->A:ON
                -->新建事件页-->事件出现条件-->独立开关-->A 为 ON

Project1.rar

244.44 KB, 下载次数: 69

推箱子

评分

参与人数 1星屑 +300 收起 理由
怪蜀黍 + 300 发布奖

查看全部评分

梦石
0
星屑
9292
在线时间
2504 小时
注册时间
2011-5-20
回帖
14521

开拓者

发表于 2013-12-28 19:18:05 | 显示全部楼层
转换地图的话目测要出问题···具体情况不明···你可以看看我去年发的那个···可以改变量和开关
[img]http://service.t.sina.com.cn/widget/qmd/5339802982/c02e16bd/7.png
回复

使用道具 举报

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

本版积分规则

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

在本版发帖返回顶部