Class: DXRuby::Tiled::IsometricLayer

Inherits:
Layer
  • Object
show all
Defined in:
lib/dxruby_tiled/layer_isometric.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



63
64
65
# File 'lib/dxruby_tiled/layer_isometric.rb', line 63

def self.pixel_height(map)
  return map.tile_height * (map.width + map.height) / 2
end

.pixel_width(map) ⇒ Object



59
60
61
# File 'lib/dxruby_tiled/layer_isometric.rb', line 59

def self.pixel_width(map)
  return map.tile_width  * (map.width + map.height) / 2
end

Instance Method Details

#at(x, y) ⇒ Object



37
38
39
40
# File 'lib/dxruby_tiled/layer_isometric.rb', line 37

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



42
43
44
45
# File 'lib/dxruby_tiled/layer_isometric.rb', line 42

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dxruby_tiled/layer_isometric.rb', line 7

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
  x_range = 0..(target.width  / tile_width2 / 2 + 1).floor
  y_range = 0..(target.height / tile_height2    + 3).floor
  off_x = x - @map.pixel_width / 2 + @offset_x
  off_y = y - tile_height2 + @offset_y
  alpha = (@opacity * 255).to_i
  
  y_range.each do |yy|
    x_range.each do |xx|
      x2 = left + xx + yy / 2
      y2 = top  - xx + (yy + 1) / 2
      image = tile_images[self[x2, y2]]
      target.draw_alpha(x2 * tile_width2  - y2 * tile_width2  - off_x - image.width  / 2,
                        y2 * tile_height2 + x2 * tile_height2 - off_y - image.height / 2,
                        image, alpha)
    end
  end
end

#vertexs(x, y) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/dxruby_tiled/layer_isometric.rb', line 47

def vertexs(x, y)
  x0 = @map.pixel_width / 2
  w, h = @map.tile_width / 2, @map.tile_height / 2
  return [
    [ x0 + x * w - y * w    , y * h + x * h         ],
    [ x0 + x * w - y * w - w, y * h + x * h + h     ],
    [ x0 + x * w - y * w    , y * h + x * h + h * 2 ],
    [ x0 + x * w - y * w + w, y * h + x * h + h     ]
  ]
end

#xy_at(tmp_x, y) ⇒ Object



31
32
33
34
35
# File 'lib/dxruby_tiled/layer_isometric.rb', line 31

def xy_at(tmp_x, y)
  x = tmp_x - @map.pixel_width / 2
  return (1.0 * x / @map.tile_width + 1.0 * y / @map.tile_height).floor,
          (1.0 * y / @map.tile_height - 1.0 * x / @map.tile_width).floor
end