Class: Viewport

Inherits:
Object show all
Extended by:
Publisher
Defined in:
lib/gamebox/core/viewport.rb

Overview

Viewport represents the current “camera” location. Essensially it translates from world to screen coords and from screen coords to world coords.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeViewport

Returns a new instance of Viewport.



19
20
21
22
23
24
# File 'lib/gamebox/core/viewport.rb', line 19

def initialize
  res = config_manager[:screen_resolution]
  @width = res[0]
  @height = res[1]
  reset
end

Instance Attribute Details

#boundaryObject

Returns the value of attribute boundary.



9
10
11
# File 'lib/gamebox/core/viewport.rb', line 9

def boundary
  @boundary
end

#follow_targetObject Also known as: follow_target?

Returns the value of attribute follow_target.



9
10
11
# File 'lib/gamebox/core/viewport.rb', line 9

def follow_target
  @follow_target
end

#heightObject

Returns the value of attribute height.



9
10
11
# File 'lib/gamebox/core/viewport.rb', line 9

def height
  @height
end

#rotationObject

Returns the value of attribute rotation.



9
10
11
# File 'lib/gamebox/core/viewport.rb', line 9

def rotation
  @rotation
end

#speedObject

Returns the value of attribute speed.



12
13
14
# File 'lib/gamebox/core/viewport.rb', line 12

def speed
  @speed
end

#widthObject

Returns the value of attribute width.



9
10
11
# File 'lib/gamebox/core/viewport.rb', line 9

def width
  @width
end

#x_offset(layer = 1) ⇒ Object

Returns the value of attribute x_offset.



9
10
11
# File 'lib/gamebox/core/viewport.rb', line 9

def x_offset
  @x_offset
end

#x_offset_rangeObject

Returns the value of attribute x_offset_range.



9
10
11
# File 'lib/gamebox/core/viewport.rb', line 9

def x_offset_range
  @x_offset_range
end

#y_offset(layer = 1) ⇒ Object

Returns the value of attribute y_offset.



9
10
11
# File 'lib/gamebox/core/viewport.rb', line 9

def y_offset
  @y_offset
end

#y_offset_rangeObject

Returns the value of attribute y_offset_range.



9
10
11
# File 'lib/gamebox/core/viewport.rb', line 9

def y_offset_range
  @y_offset_range
end

Instance Method Details

#boundsObject



99
100
101
102
103
104
# File 'lib/gamebox/core/viewport.rb', line 99

def bounds
  left = -@x_offset
  top = -@y_offset
  # Rect.new left, top, left + @width, top + @height
  Rect.new left, top, @width, @height
end

#debugObject



15
16
17
# File 'lib/gamebox/core/viewport.rb', line 15

def debug
  "xoff:#{@x_offset} yoff:#{@y_offset}"
end

#follow(target, off = [0,0], buff = [0,0]) ⇒ Object

deprecated; use #stay_centered_on



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/gamebox/core/viewport.rb', line 86

def follow(target, off=[0,0], buff=[0,0])
  @follow_target = target
  @follow_offset_x = off[0]
  @follow_offset_y = off[1]
  @buffer_x = buff[0]
  @buffer_y = buff[1]

  @x_offset = @width/2 - @follow_target.x + @follow_offset_x
  @y_offset = @height/2 - @follow_target.y + @follow_offset_y

  fire :scrolled
end

#resetObject



26
27
28
29
30
31
# File 'lib/gamebox/core/viewport.rb', line 26

def reset
  @rotation = 0
  @speed = 1
  @x_offset = 0
  @y_offset = 0
end

#scroll(x_delta, y_delta) ⇒ Object



33
34
35
36
37
38
# File 'lib/gamebox/core/viewport.rb', line 33

def scroll(x_delta,y_delta)
  @x_offset += x_delta
  @y_offset += y_delta

  fire :scrolled
end

#stay_centered_on(target, args = {}) ⇒ Object

Viewport will stay centered on the targets x,y

args hash: :x_offset - keep the viewport centered on x + x_offset :y_offset - keep the viewport centered on y + y_offset :x_chain_length - allow this much x slack when following :y_chain_length - allow this much y slack when following



77
78
79
80
81
82
83
# File 'lib/gamebox/core/viewport.rb', line 77

def stay_centered_on(target, args={})
  offset_x = args[:x_offset] || 0
  offset_y = args[:y_offset] || 0
  x_chain_length = args[:x_chain_length] || 0
  y_chain_length = args[:y_chain_length] || 0
  follow target, [-offset_x, -offset_y], [x_chain_length, y_chain_length]
end

#update(time) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/gamebox/core/viewport.rb', line 62

def update(time)
  if follow_target?
    scrolled = move_towards_target
    clamp_to_boundary
    fire :scrolled if scrolled
  end
end