Class: DXRuby::Tiled::StaggeredLayer

Inherits:
TileLayer show all
Defined in:
lib/dxruby_tiled/layer_staggered.rb

Instance Attribute Summary

Attributes inherited from TileLayer

#height, #startx, #starty, #width

Attributes inherited from Layer

#name, #offset_x, #offset_y, #opacity, #properties, #visible, #z_index

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TileLayer

#[], #[]=, #at, #change_at, #include?, #tile_at

Methods inherited from Layer

create

Constructor Details

#initialize(data, map) ⇒ StaggeredLayer

Returns a new instance of StaggeredLayer.



4
5
6
7
8
# File 'lib/dxruby_tiled/layer_staggered.rb', line 4

def initialize(data, map)
  super
  @stagger_axis_y    = map.stagger_axis_y
  @stagger_index_odd = map.stagger_index_odd
end

Class Method Details

.pixel_height(map) ⇒ Object



117
118
119
120
121
# File 'lib/dxruby_tiled/layer_staggered.rb', line 117

def self.pixel_height(map)
  map.stagger_axis_y ?

    map.tile_height * (map.height + 1) / 2 :
    map.tile_height * map.height + map.tile_height / 2
end

.pixel_width(map) ⇒ Object



111
112
113
114
115
# File 'lib/dxruby_tiled/layer_staggered.rb', line 111

def self.pixel_width(map)
  map.stagger_axis_y ?

    map.tile_width * map.width + map.tile_width / 2 :
    map.tile_width * (map.width + 1) / 2
end

Instance Method Details

#render(pos_x, pos_y, target = DXRuby::Window, z = @z_index, offset_x = 0, offset_y = 0, opacity = 1.0) ⇒ Object Also known as: draw



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dxruby_tiled/layer_staggered.rb', line 10

def render(pos_x, pos_y, target = DXRuby::Window, z = @z_index, offset_x = 0, offset_y = 0, opacity = 1.0)
  super
  
  pos_x, pos_y = 0, 0 if @fixed
  tile_width2  = @tile_width / 2
  tile_height2 = @tile_height / 2
  off_x = offset_x + @offset_x - pos_x
  off_y = offset_y + @offset_y - pos_y
  x1, y1 = xy_at(@tile_width  - @tilesets.tile_right  - off_x,
                 @tile_height - @tilesets.tile_bottom - off_y)
  x2, y2 = xy_at(@render_target.width  - @tilesets.tile_left - off_x - 1,
                 @render_target.height - @tilesets.tile_top  - off_y - 1)
  x_range = ((x1 - 1)..(x2 + 1))
  y_range = ((y1 - 1)..(y2 + 1))
  
  if @stagger_axis_y
    y_range.public_send(@renderorder_y ? :each : :reverse_each) do |y|
      x0 = @stagger_index_odd ^ y.even? ? tile_width2 : 0
      x_range.public_send(@renderorder_x ? :each : :reverse_each) do |x|
        @tilesets[self[x, y]].render(
          x * @tile_width   + off_x + x0,
          y *  tile_height2 + off_y,
          @render_target)
      end
    end
  else
    y_range.public_send(@renderorder_y ? :each : :reverse_each) do |y|
      x_range.public_send(@renderorder_x ? :each : :reverse_each) do |x|
        next if @stagger_index_odd ^ x.even?
        @tilesets[self[x, y]].render(
          x *  tile_width2 + off_x,
          y * @tile_height + off_y,
          @render_target)
      end
      x_range.public_send(@renderorder_x ? :each : :reverse_each) do |x|
        next unless @stagger_index_odd ^ x.even?
        @tilesets[self[x, y]].render(
          x *  tile_width2 + off_x,
          y * @tile_height + off_y + tile_height2,
          @render_target)
      end
    end
  end
  
  target.draw_alpha(0, 0, @render_target, @opacity * 255, z + @z_index)
end

#vertexs(x, y) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dxruby_tiled/layer_staggered.rb', line 89

def vertexs(x, y)
  w, h = @tile_width / 2, @tile_height / 2
  if @stagger_axis_y
    x0 = @stagger_index_odd ^ y.even? ? w : 0
    return [
      [ x0 + x * w * 2 + w    , y * h         ],
      [ x0 + x * w * 2        , y * h + h     ],
      [ x0 + x * w * 2 + w    , y * h + h * 2 ],
      [ x0 + x * w * 2 + w * 2, y * h + h     ]
    ]
  else
    y0 = @stagger_index_odd ^ x.even? ? h : 0
    return [
      [ x * w + w    , y0 + y * h * 2         ],
      [ x * w        , y0 + y * h * 2 + h     ],
      [ x * w + w    , y0 + y * h * 2 + h * 2 ],
      [ x * w + w * 2, y0 + y * h * 2 + h     ]
    ]
  end
end

#xy_at(pos_x, pos_y) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dxruby_tiled/layer_staggered.rb', line 58

def xy_at(pos_x, pos_y)
  if @stagger_axis_y
    y = (pos_y * 2 / @tile_height).floor
    pos_x -= @tile_width / 2 if @stagger_index_odd ^ y.even?
    x = (pos_x / @tile_width).floor
    dx = pos_x % @tile_width
    dy = pos_y % (@tile_height / 2)
    if (dy < @tile_height / 2 - dx * @tile_height / @tile_width)
      y -= 1
      x += @stagger_index_odd ^ y.even? ? -1 : 0
    elsif (dy < dx * @tile_height / @tile_width - @tile_height / 2)
      y -= 1
      x += @stagger_index_odd ^ y.even? ? 0 : 1
    end
  else
    x = (pos_x * 2 / @tile_width).floor
    pos_y -= @tile_height / 2 if @stagger_index_odd ^ x.even?
    y = (pos_y / @tile_height).floor
    dx = pos_x % (@tile_width / 2)
    dy = pos_y % @tile_height
    if (dy < @tile_height / 2 - dx * @tile_height / @tile_width)
      x -= 1
      y += @stagger_index_odd ^ x.even? ? -1 : 0
    elsif (dy > dx * @tile_height / @tile_width + @tile_height / 2)
      x -= 1
      y += @stagger_index_odd ^ x.even? ? 0 : 1
    end
  end
  return x, y
end