Class: ViewportWidget

Inherits:
Object
  • Object
show all
Defined in:
lib/delve/widgets/viewport.rb

Instance Method Summary collapse

Constructor Details

#initialize(x, y, width, height, world) ⇒ ViewportWidget

Returns a new instance of ViewportWidget.



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/delve/widgets/viewport.rb', line 2

def initialize(x, y, width, height, world)
  raise 'Cannot initialize text widget when x is nil' unless x
  raise 'Cannot initialize text widget when y is nil' unless y
  raise 'Cannot initialize text widget when width is nil' unless width
  raise 'Cannot initialize text widget when height is nil' unless height
  raise 'Cannot initialize text widget when text is nil' unless world

  @x = x
  @y = y
  @width = width
  @height = height
  @world = world
  @rx = 0
  @ry = 0
end

Instance Method Details

#draw(display) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/delve/widgets/viewport.rb', line 41

def draw(display)
  raise 'Cannot draw text when display is nil' unless display

  dx = 0
  (@x..(@x+@width-1)).each do |x|
    dy = 0
    (@y..(@y+@height-1)).each do |y|
      tile = @world.at(@rx+dx, @ry+dy)
      tile = { :char => '?', :color => :red } unless tile

      if tile.respond_to?(:has?)
        symbol = tile.get(:symbol).symbol
        symbol[:walkable] = tile.get(:walkable).walkable
        tile = symbol
      end

      display.draw x, y, tile[:char], tile[:color]
      dy += 1
    end
    dx += 1
  end
end

#focus(x, y) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/delve/widgets/viewport.rb', line 26

def focus(x, y)
  raise 'Cannot focus viewport out of world' if x < 0 || y < 0 || x >= @world.width || y >= @world.height

  @rx = x - (@width/2).floor
  @ry = y - (@height/2).floor

  @rx = 0 if @rx < 0
  @ry = 0 if @ry < 0
  
  far_east = @world.width - @width
  far_south = @world.height - @height
  @rx = far_east if @rx > far_east
  @ry = far_south if @ry > far_south
end

#heightObject



22
23
24
# File 'lib/delve/widgets/viewport.rb', line 22

def height
  @height
end

#top_leftObject



64
65
66
# File 'lib/delve/widgets/viewport.rb', line 64

def top_left
  { x: @rx, y: @ry }
end

#widthObject



18
19
20
# File 'lib/delve/widgets/viewport.rb', line 18

def width
  @width
end