TheoAllen

Grid System v0.1

Nov 1st, 2014
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.19 KB | None | 0 0
  1. module Grid
  2.   MaxRow = 3
  3.   MaxCol = 4
  4.  
  5.   PosDebug = true # Display dummy box for positioning
  6.   Party = [
  7.     [302,169],[356,169],[414,169],[471,169],
  8.     [304,222],[360,222],[422,222],[482,222],
  9.     [306,270],[364,270],[430,270],[493,270],
  10.   ]
  11.  
  12.   # Troop grid coordinate should be reversed
  13.   Troop = [
  14.     [244,169],[188,169],[132,169],[ 77,169],
  15.     [242,222],[184,222],[126,222],[ 66,222],
  16.     [240,270],[180,270],[114,270],[ 55,270],
  17.   ]
  18.  
  19. end
  20.  
  21. class FieldEffect
  22.   attr_reader :grid_index
  23.   attr_reader :turn
  24.  
  25.   def initialize(state_id, turn, grid_index)
  26.     @state_id = state
  27.     @turn = turn
  28.     @grid_index
  29.   end
  30.  
  31.   def state
  32.     $data_states[@state_id]
  33.   end
  34.  
  35.   def update_turn
  36.     @turn -= 1
  37.   end
  38.  
  39. end
  40.  
  41. class << Grid
  42.  
  43.   def clear
  44.     @field_effect = []
  45.   end
  46.  
  47.   def update_turn
  48.     @field_effect.delete_if {|f| f.update_turn; f.turn == 0}
  49.   end
  50.  
  51.   def horz_line(index, all = false)
  52.     result = [index]
  53.     sel = index
  54.     min = Grid::MaxCol * (index / Grid::MaxCol)
  55.     max = min + Grid::MaxCol - 1
  56.     loop do
  57.       sel += 1
  58.       break if out_of_bound?(sel, min, max)
  59.       result << sel
  60.     end
  61.     if all
  62.       sel = index
  63.       loop do
  64.         sel -= 1
  65.         break if out_of_bound?(sel, min, max)
  66.         result << sel
  67.       end
  68.     end
  69.     return result.compact
  70.   end
  71.  
  72.   def vert_line(index)
  73.     result = [index]
  74.     sel1 = sel2 = index
  75.     boundary = (Grid::MaxRow * Grid::MaxCol) - 1
  76.     Grid::MaxRow.times do
  77.       sel1 += Grid::MaxCol
  78.       sel2 -= Grid::MaxCol
  79.       result << sel1
  80.       result << sel2
  81.     end
  82.     return result.compact.delete_if {|c| out_of_bound?(c, 0, boundary)}
  83.   end
  84.  
  85.   def round_area(index)
  86.     result = [1,2,3,4,6,7,8,9].collect {|dir| neighbor(index, dir)} + [index]
  87.     result.compact
  88.   end
  89.  
  90.   def cross_area(index)
  91.     result = [1,3,7,9].collect {|dir| neighbor(index, dir)} + [index]
  92.     result.compact
  93.   end
  94.  
  95.   def plus_area(index)
  96.     result = [2,4,6,8].collect {|dir| neighbor(index, dir)} + [index]
  97.     result.compact
  98.   end
  99.  
  100.   def all_area
  101.     Array.new(MaxRow * MaxCol) {|i| i}
  102.   end
  103.   #----------------------------------------------------------------------------
  104.   # * Get neighbor grid
  105.   # 2 = DOWNWARD
  106.   # 4 = FORWARD
  107.   # 6 = BACKWARD
  108.   # 8 = UPWARD
  109.   #----------------------------------------------------------------------------
  110.   def neighbor(index, dir)
  111.     coordinate = index
  112.     coordinate = point(index) unless index.is_a?(Array)
  113.     case dir
  114.     when 2; coordinate[1] += 1  # DOWN
  115.     when 4; coordinate[0] -= 1  # FORWARD
  116.     when 6; coordinate[0] += 1  # BACKWARD
  117.     when 8; coordinate[1] -= 1  # UP
  118.      
  119.     when 1
  120.       coordinate[0] -= 1
  121.       coordinate[1] += 1
  122.     when 3
  123.       coordinate[0] += 1
  124.       coordinate[1] += 1
  125.     when 7
  126.       coordinate[0] -= 1
  127.       coordinate[1] -= 1
  128.     when 9
  129.       coordinate[0] += 1
  130.       coordinate[1] -= 1
  131.     end
  132.     return cell(*coordinate)
  133.   end
  134.   #----------------------------------------------------------------------------
  135.   # * Get cell index
  136.   # - Column equal as X axis
  137.   # - Row equal as Y axis
  138.   #----------------------------------------------------------------------------
  139.   def cell(col, row)
  140.     return nil if out_of_bound?(row, 0, Grid::MaxRow - 1)
  141.     return nil if out_of_bound?(col, 0, Grid::MaxCol - 1)
  142.     return (Grid::MaxCol * row) + col
  143.   end
  144.  
  145.   def point(index)
  146.     return [index % Grid::MaxCol, index / Grid::MaxCol]
  147.   end
  148.  
  149.   def out_of_bound?(value, min, max)
  150.     return value > max || value < min
  151.   end
  152.  
  153.   def x(index)
  154.     point(index)[0]
  155.   end
  156.  
  157.   def y(index)
  158.     point(index)[1]
  159.   end
  160.  
  161.   def max_index
  162.     Grid::MaxRow * Grid::MaxCol
  163.   end
  164.  
  165. end
  166.  
  167. class Game_Battler
  168.   attr_accessor :grid_pos
  169.  
  170.   alias tsbs_grid_init initialize
  171.   def initialize
  172.     @grid_pos = 0
  173.     tsbs_grid_init
  174.   end
  175.  
  176.   def original_x
  177.     actor? ? Grid::Party[@grid_pos][0] : Grid::Troop[@grid_pos][0]
  178.   end
  179.  
  180.   def original_y
  181.     actor? ? Grid::Party[@grid_pos][1] : Grid::Troop[@grid_pos][1]
  182.   end
  183.  
  184. end
  185.  
  186. class Game_Actor
  187.  
  188.   def original_x
  189.     super
  190.   end
  191.  
  192.   def original_y
  193.     super
  194.   end
  195.  
  196. end
  197.  
  198. class Game_Enemy
  199.   def init_grid_position
  200.     self.x = @ori_x = original_x
  201.     self.y = @ori_y = original_y
  202.   end
  203. end
  204.  
  205. class Game_Party
  206.  
  207.   alias tsbs_grid_battle_start on_battle_start
  208.   def on_battle_start
  209.     members.each {|m| m.grid_pos = rand(Grid.max_index)
  210.       m.init_oripost
  211.     }
  212.     tsbs_grid_battle_start
  213.   end
  214.  
  215. end
  216.  
  217. class Game_Troop
  218.   def setup(troop_id)
  219.     clear
  220.     @troop_id = troop_id
  221.     @enemies = []
  222.     troop.members.each do |member|
  223.       next unless $data_enemies[member.enemy_id]
  224.       enemy = Game_Enemy.new(@enemies.size, member.enemy_id)
  225.       enemy.hide if member.hidden
  226.       enemy.grid_pos = rand(Grid::MaxCol * Grid::MaxRow)
  227.       enemy.init_grid_position
  228.       @enemies.push(enemy)
  229.     end
  230.     init_screen_tone
  231.     make_unique_names
  232.   end
  233. end
  234.  
  235. class Sprite_TestGrid < Sprite
  236.   def initialize(viewport, array, index)
  237.     super(viewport)
  238.     @index = index
  239.     self.bitmap = Bitmap.new(32,32)
  240.     self.ox = width/2
  241.     self.oy = height
  242.     self.x = array[index][0]
  243.     self.y = array[index][1]
  244.     self.z = 4
  245.     bitmap.entire_fill(Color.new(0,0,0,150))
  246.     bitmap.font.size = 24
  247.     bitmap.font.bold = false
  248.     draw_text(bitmap.rect, index, 1)
  249.   end
  250. end
  251.  
  252. class Spriteset_Battle
  253.  
  254.   alias tsbs_grid_init initialize
  255.   def initialize
  256.     tsbs_grid_init
  257.     @party_grid = []
  258.     @troop_grid = []
  259.     if Grid::PosDebug
  260.       maxtimes = Grid.max_index
  261.       maxtimes.times do |i|
  262.         @party_grid.push(Sprite_TestGrid.new(@viewport1,Grid::Party,i))
  263.       end
  264.       maxtimes.times do |i|
  265.         @troop_grid.push(Sprite_TestGrid.new(@viewport1,Grid::Troop,i))
  266.       end
  267.     end
  268.   end
  269.  
  270.   alias tsbs_grid_dispose dispose
  271.   def dispose
  272.     tsbs_grid_dispose
  273.     (@party_grid + @troop_grid).each {|grid| grid.dispose}
  274.   end
  275.  
  276. end
  277.  
  278. class Window_ActorCommand
  279.  
  280.   alias tsbs_grid_grd_command add_guard_command
  281.   def add_guard_command
  282.     tsbs_grid_grd_command
  283.     add_command('Move',:move)
  284.   end
  285.  
  286. end
  287.  
  288. class Scene_Battle
  289.  
  290.  
  291. end
  292.  
  293. p Grid.neighbor(5,2)
Advertisement
Add Comment
Please, Sign In to add comment