Class: DXRuby::Tiled::StaggeredLayer

Inherits:
Layer
  • Object
show all
Defined in:
lib/dxruby_tiled/layer_staggered.rb

Instance Attribute Summary

Attributes inherited from Layer

#data, #height, #name, #offset_x, #offset_y, #opacity, #properties, #visible, #width

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layer

#[], #[]=, #include?, #initialize

Constructor Details

This class inherits a constructor from DXRuby::Tiled::Layer

Class Method Details

.pixel_height(map) ⇒ Object



113
114
115
116
117
# File 'lib/dxruby_tiled/layer_staggered.rb', line 113

def self.pixel_height(map)
  return 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



107
108
109
110
111
# File 'lib/dxruby_tiled/layer_staggered.rb', line 107

def self.pixel_width(map)
  return map.stagger_axis_y ?
          map.tile_width * map.width + map.tile_width / 2 :
          map.tile_width * (map.width + 1) / 2
end

Instance Method Details

#at(x, y) ⇒ Object



75
76
77
78
# File 'lib/dxruby_tiled/layer_staggered.rb', line 75

def at(x, y)
  tmp_x, tmp_y = xy_at(x, y)
  return self[tmp_x, tmp_y]
end

#change_at(x, y, value) ⇒ Object



80
81
82
83
# File 'lib/dxruby_tiled/layer_staggered.rb', line 80

def change_at(x, y, value)
  tmp_x, tmp_y = xy_at(x, y)
  self[tmp_x, tmp_y] = value
end

#draw(x, y, target = DXRuby::Window) ⇒ Object



5
6
7
8
9
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
# File 'lib/dxruby_tiled/layer_staggered.rb', line 5

def draw(x, y, target = DXRuby::Window)
  tile_width2, tile_height2 = @map.tile_width / 2, @map.tile_height / 2
  tile_images = @map.tilesets.tile_images
  left, top = xy_at(x - @offset_x, y - @offset_x)
  left -= 1
  top  -= 1
  off_x = x - tile_width2  + @offset_x
  off_y = y - tile_height2 + @offset_y
  alpha = (@opacity * 255).floor
  
  if @map.stagger_axis_y
    x_range = left..(left + target.width  / tile_width2  / 2 + 1).floor
    y_range =  top..(top  + target.height / tile_height2     + 3).floor
    
    y_range.each do |y2|
      x0 = @map.stagger_index_odd ^ y2.even? ? tile_width2 : 0
      x_range.each do |x2|
        image = tile_images[self[x2, y2]]
        target.draw_alpha(x0 + x2 * tile_width2 * 2 - off_x - image.width  / 2,
                               y2 * tile_height2    - off_y - image.height / 2,
                          image, alpha)
      end
    end
  else
    x_range = left..(left + target.width  / tile_width2      + 3).floor
    y_range =  top..(top  + target.height / tile_height2 / 2 + 1).floor
    
    y_range.each do |y2|
      x_range.each do |x2|
        y0 = @map.stagger_index_odd ^ x2.even? ? tile_height2 : 0
        image = tile_images[self[x2, y2]]
        target.draw_alpha(     x2 * tile_width2      - off_x - image.width  / 2,
                          y0 + y2 * tile_height2 * 2 - off_y - image.height / 2,
                          image, alpha)
      end
    end
  end
end

#vertexs(x, y) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dxruby_tiled/layer_staggered.rb', line 85

def vertexs(x, y)
  w, h = @map.tile_width / 2, @map.tile_height / 2
  if @map.stagger_axis_y
    x0 = @map.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 = @map.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(x, y) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dxruby_tiled/layer_staggered.rb', line 44

def xy_at(x, y)
  if @map.stagger_axis_y
    y2 = y * 2 / @map.tile_height
    x -= @map.tile_width / 2 if @map.stagger_index_odd ^ y2.even?
    x2 = x / @map.tile_width
    x0 = x % @map.tile_width
    y0 = y % (@map.tile_height / 2)
    if (y0 < @map.tile_height / 2 - x0 * @map.tile_height / @map.tile_width)
      y2 -= 1
      x2 += @map.stagger_index_odd ^ y2.even? ? -1 : 0
    elsif (y0 < x0 * @map.tile_height / @map.tile_width - @map.tile_height / 2)
      y2 -= 1
      x2 += @map.stagger_index_odd ^ y2.even? ? 0 : 1
    end
  else
    x2 = x * 2 / @map.tile_width
    y -= @map.tile_height / 2 if @map.stagger_index_odd ^ x2.even?
    y2 = y / @map.tile_height
    x0 = x % (@map.tile_width / 2)
    y0 = y % @map.tile_height
    if (y0 < @map.tile_height / 2 - x0 * @map.tile_height / @map.tile_width)
      x2 -= 1
      y2 += @map.stagger_index_odd ^ x2.even? ? -1 : 0
    elsif (y0 > x0 * @map.tile_height / @map.tile_width + @map.tile_height / 2)
      x2 -= 1
      y2 += @map.stagger_index_odd ^ x2.even? ? 0 : 1
    end
  end
  return x2, y2
end