Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Grid
- MaxRow = 3
- MaxCol = 4
- PosDebug = true # Display dummy box for positioning
- Party = [
- [302,169],[356,169],[414,169],[471,169],
- [304,222],[360,222],[422,222],[482,222],
- [306,270],[364,270],[430,270],[493,270],
- ]
- # Troop grid coordinate should be reversed
- Troop = [
- [244,169],[188,169],[132,169],[ 77,169],
- [242,222],[184,222],[126,222],[ 66,222],
- [240,270],[180,270],[114,270],[ 55,270],
- ]
- end
- class FieldEffect
- attr_reader :grid_index
- attr_reader :turn
- def initialize(state_id, turn, grid_index)
- @state_id = state
- @turn = turn
- @grid_index
- end
- def state
- $data_states[@state_id]
- end
- def update_turn
- @turn -= 1
- end
- end
- class << Grid
- def clear
- @field_effect = []
- end
- def update_turn
- @field_effect.delete_if {|f| f.update_turn; f.turn == 0}
- end
- def horz_line(index, all = false)
- result = [index]
- sel = index
- min = Grid::MaxCol * (index / Grid::MaxCol)
- max = min + Grid::MaxCol - 1
- loop do
- sel += 1
- break if out_of_bound?(sel, min, max)
- result << sel
- end
- if all
- sel = index
- loop do
- sel -= 1
- break if out_of_bound?(sel, min, max)
- result << sel
- end
- end
- return result.compact
- end
- def vert_line(index)
- result = [index]
- sel1 = sel2 = index
- boundary = (Grid::MaxRow * Grid::MaxCol) - 1
- Grid::MaxRow.times do
- sel1 += Grid::MaxCol
- sel2 -= Grid::MaxCol
- result << sel1
- result << sel2
- end
- return result.compact.delete_if {|c| out_of_bound?(c, 0, boundary)}
- end
- def round_area(index)
- result = [1,2,3,4,6,7,8,9].collect {|dir| neighbor(index, dir)} + [index]
- result.compact
- end
- def cross_area(index)
- result = [1,3,7,9].collect {|dir| neighbor(index, dir)} + [index]
- result.compact
- end
- def plus_area(index)
- result = [2,4,6,8].collect {|dir| neighbor(index, dir)} + [index]
- result.compact
- end
- def all_area
- Array.new(MaxRow * MaxCol) {|i| i}
- end
- #----------------------------------------------------------------------------
- # * Get neighbor grid
- # 2 = DOWNWARD
- # 4 = FORWARD
- # 6 = BACKWARD
- # 8 = UPWARD
- #----------------------------------------------------------------------------
- def neighbor(index, dir)
- coordinate = index
- coordinate = point(index) unless index.is_a?(Array)
- case dir
- when 2; coordinate[1] += 1 # DOWN
- when 4; coordinate[0] -= 1 # FORWARD
- when 6; coordinate[0] += 1 # BACKWARD
- when 8; coordinate[1] -= 1 # UP
- when 1
- coordinate[0] -= 1
- coordinate[1] += 1
- when 3
- coordinate[0] += 1
- coordinate[1] += 1
- when 7
- coordinate[0] -= 1
- coordinate[1] -= 1
- when 9
- coordinate[0] += 1
- coordinate[1] -= 1
- end
- return cell(*coordinate)
- end
- #----------------------------------------------------------------------------
- # * Get cell index
- # - Column equal as X axis
- # - Row equal as Y axis
- #----------------------------------------------------------------------------
- def cell(col, row)
- return nil if out_of_bound?(row, 0, Grid::MaxRow - 1)
- return nil if out_of_bound?(col, 0, Grid::MaxCol - 1)
- return (Grid::MaxCol * row) + col
- end
- def point(index)
- return [index % Grid::MaxCol, index / Grid::MaxCol]
- end
- def out_of_bound?(value, min, max)
- return value > max || value < min
- end
- def x(index)
- point(index)[0]
- end
- def y(index)
- point(index)[1]
- end
- def max_index
- Grid::MaxRow * Grid::MaxCol
- end
- end
- class Game_Battler
- attr_accessor :grid_pos
- alias tsbs_grid_init initialize
- def initialize
- @grid_pos = 0
- tsbs_grid_init
- end
- def original_x
- actor? ? Grid::Party[@grid_pos][0] : Grid::Troop[@grid_pos][0]
- end
- def original_y
- actor? ? Grid::Party[@grid_pos][1] : Grid::Troop[@grid_pos][1]
- end
- end
- class Game_Actor
- def original_x
- super
- end
- def original_y
- super
- end
- end
- class Game_Enemy
- def init_grid_position
- self.x = @ori_x = original_x
- self.y = @ori_y = original_y
- end
- end
- class Game_Party
- alias tsbs_grid_battle_start on_battle_start
- def on_battle_start
- members.each {|m| m.grid_pos = rand(Grid.max_index)
- m.init_oripost
- }
- tsbs_grid_battle_start
- end
- end
- class Game_Troop
- def setup(troop_id)
- clear
- @troop_id = troop_id
- @enemies = []
- troop.members.each do |member|
- next unless $data_enemies[member.enemy_id]
- enemy = Game_Enemy.new(@enemies.size, member.enemy_id)
- enemy.hide if member.hidden
- enemy.grid_pos = rand(Grid::MaxCol * Grid::MaxRow)
- enemy.init_grid_position
- @enemies.push(enemy)
- end
- init_screen_tone
- make_unique_names
- end
- end
- class Sprite_TestGrid < Sprite
- def initialize(viewport, array, index)
- super(viewport)
- @index = index
- self.bitmap = Bitmap.new(32,32)
- self.ox = width/2
- self.oy = height
- self.x = array[index][0]
- self.y = array[index][1]
- self.z = 4
- bitmap.entire_fill(Color.new(0,0,0,150))
- bitmap.font.size = 24
- bitmap.font.bold = false
- draw_text(bitmap.rect, index, 1)
- end
- end
- class Spriteset_Battle
- alias tsbs_grid_init initialize
- def initialize
- tsbs_grid_init
- @party_grid = []
- @troop_grid = []
- if Grid::PosDebug
- maxtimes = Grid.max_index
- maxtimes.times do |i|
- @party_grid.push(Sprite_TestGrid.new(@viewport1,Grid::Party,i))
- end
- maxtimes.times do |i|
- @troop_grid.push(Sprite_TestGrid.new(@viewport1,Grid::Troop,i))
- end
- end
- end
- alias tsbs_grid_dispose dispose
- def dispose
- tsbs_grid_dispose
- (@party_grid + @troop_grid).each {|grid| grid.dispose}
- end
- end
- class Window_ActorCommand
- alias tsbs_grid_grd_command add_guard_command
- def add_guard_command
- tsbs_grid_grd_command
- add_command('Move',:move)
- end
- end
- class Scene_Battle
- end
- p Grid.neighbor(5,2)
Advertisement
Add Comment
Please, Sign In to add comment