Class: Yuki::Tilemap::MapData

Inherits:
Object
  • Object
show all
Defined in:
scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb

Overview

Class containing the map Data and its resources

Constant Summary collapse

POSITION_LOADERS =

List of method that help to load the position

{
  north: :load_position_north,
  south: :load_position_south,
  east: :load_position_east,
  west: :load_position_west,
  self: :load_position_self
}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map, map_id) ⇒ MapData

Create a new MapData

Parameters:



43
44
45
46
47
48
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 43

def initialize(map, map_id)
  @data = map.data
  @map = map
  @map_id = map_id
  @rect = Rect.new(0, 0, 32, 32)
end

Class Attribute Details

.tileset_chunksHash{filename => Array<Texture>} (readonly)

Get tileset chunks

Returns:

  • (Hash{filename => Array<Texture>})


254
255
256
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 254

def tileset_chunks
  @tileset_chunks
end

Instance Attribute Details

#mapRPG::Map (readonly)

Get access to the original map data

Returns:



15
16
17
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 15

def map
  @map
end

#map_idInteger (readonly)

Get the map id

Returns:



18
19
20
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 18

def map_id
  @map_id
end

#offset_xInteger (readonly)

Get the map offset_x

Returns:



27
28
29
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 27

def offset_x
  @offset_x
end

#offset_yInteger (readonly)

Get the map offset_y

Returns:



30
31
32
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 30

def offset_y
  @offset_y
end

#sideSymbol (readonly)

Get the side of the map

Returns:

  • (Symbol)


36
37
38
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 36

def side
  @side
end

#tileset_nameString (readonly)

Get the tileset filename (to prevent unwanted dispose in the future)

Returns:



33
34
35
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 33

def tileset_name
  @tileset_name
end

#x_rangeRange (readonly)

Get the map X coordinate range

Returns:

  • (Range)


21
22
23
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 21

def x_range
  @x_range
end

#y_rangeRange (readonly)

Get the map Y coordinate range

Returns:

  • (Range)


24
25
26
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 24

def y_range
  @y_range
end

Instance Method Details

#[](x, y, z)

Get a tile from the map

Parameters:



64
65
66
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 64

def [](x, y, z)
  @data[x + @offset_x, y + @offset_y, z]
end

#assign_tile_to_sprite(sprite, tile_id)

Set tile sprite to sprite

Parameters:

  • sprite (Sprite)
  • tile_id (Integer)

    ID of the tile the sprite wants



71
72
73
74
75
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 71

def assign_tile_to_sprite(sprite, tile_id)
  tile_id -= 384
  sprite.bitmap = @tilesets[tile_id / 256]
  sprite.src_rect.set(tile_id % 8 * 32, (tile_id % 256) / 8 * 32, 32, 32)
end

#draw(x, y, tx, ty, tz, layer)

Draw the tile on the right layer

Parameters:

  • x (Integer)

    real world x of the top left tile

  • y (Integer)

    real world y of the top left tile

  • tx (Integer)

    x index of the tile to draw from top left tile (0)

  • ty (Integer)

    y index of the tile to draw from top left tile (0)

  • tz (Integer)

    z index of the tile to draw

  • layer (Array<Array<SpriteMap>>)

    layers of the tilemap .dig(priority, ty)



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 84

def draw(x, y, tx, ty, tz, layer)
  tile_id = self[x + tx, y + ty, tz]
  return unless tile_id && tile_id != 0

  priority = @priorities[tile_id] || 0
  if tile_id < 384 # Autotile
    tileset = @autotiles[tile_id / 48 - 1]
    tileset && layer.dig(priority, ty).set(tx, tileset, @rect.set((tile_id % 48) * 32, @autotile_counter[tile_id / 48] * 32))
  else
    tile_id -= 384
    tileset = @tilesets[tile_id / 256]
    tileset && layer.dig(priority, ty).set(tx, tileset, @rect.set(tile_id % 8 * 32, (tile_id % 256) / 8 * 32))
  end
end

#draw_map(x, y, rx, ry, layers)

Draw the visible part of the map

Parameters:

  • x (Integer)

    real world x of the top left tile

  • y (Integer)

    real world y of the top left tile

  • rx (Integer)

    real world x of the bottom right tile

  • ry (Integer)

    real world y of the bottom right tile

  • layers (Array<Array<Array<SpriteMap>>>)

    layers of the tilemap .dig(tz, priority, ty)



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 105

def draw_map(x, y, rx, ry, layers)
  lx = x_range.min
  mx = x_range.max
  ly = y_range.min
  my = y_range.max

  bx = lx > x ? lx : x
  ex = mx > rx ? rx : mx
  by = ly > y ? ly : y
  ey = my > ry ? ry : my
  return unless bx <= ex && by <= ey

  bx.upto(ex) do |ax|
    by.upto(ey) do |ay|
      layers.each_with_index do |layer, tz|
        draw(x, y, ax - x, ay - y, tz, layer)
      end
    end
  end
end

#load_position(map, side, offset)

Sets the position of the map in the 2D Space

Parameters:

  • map (RPG::Map)

    current map

  • side (Symbol)

    which side the map is (:north, :south, :east, :west)

  • offset (Integer)

    offset relative to the side of the map in the positive perpendicular position



54
55
56
57
58
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 54

def load_position(map, side, offset)
  maker_offset = MapLinker::DELTA_MAKER
  send(POSITION_LOADERS[side], map, offset, maker_offset)
  @side = side
end

#load_tileset

Load the tileset



127
128
129
130
131
132
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 127

def load_tileset
  # @type [RPG::Tileset]
  @tileset = $data_tilesets[@map.tileset_id]
  @priorities = @tileset.priorities
  load_tileset_graphics
end

#update_counters

Update the autotiles counter (for tilemap)



135
136
137
138
139
140
141
142
143
# File 'scripts/01450 Systems/00003 Map Engine/00002 Logic/00300 Tilemap/00010 MapData.rb', line 135

def update_counters
  @autotiles.each_with_index do |autotile, index|
    next unless autotile
    next if autotile.height <= 32

    frame_count = autotile.height / 32
    @autotile_counter[index + 1] = (@autotile_counter[index + 1] + 1) % frame_count
  end
end