Module: Ruby2D::Camera

Defined in:
lib/ruby2d/camera.rb,
lib/ruby2d/camera/line.rb,
lib/ruby2d/camera/quad.rb,
lib/ruby2d/camera/text.rb,
lib/ruby2d/camera/circle.rb,
lib/ruby2d/camera/sprite.rb,
lib/ruby2d/camera/square.rb,
lib/ruby2d/camera/triangle.rb,
lib/ruby2d/camera/rectangle.rb

Defined Under Namespace

Classes: Circle, Line, Quad, Rectangle, Sprite, Square, Text, Triangle

Class Method Summary collapse

Class Method Details

.<<(item) ⇒ Object

Adding objects so they are tracked by the Camera



49
50
51
52
53
54
# File 'lib/ruby2d/camera.rb', line 49

def self.<<(item)
  unless objects.include?(item)
    objects.push(item)
    self._sort_by_z
  end
end

._redraw(auto_purge: false) ⇒ Object

Redraw all objects that are tracked by the Camera



62
63
64
65
# File 'lib/ruby2d/camera.rb', line 62

def self._redraw(auto_purge: false)
  self._resolve_z_sorting
  objects.each(&:_draw)
end

._resolve_z_sortingObject



26
27
28
29
# File 'lib/ruby2d/camera.rb', line 26

def self._resolve_z_sorting
  objects.sort_by!(&:z) if @sort_by_z
  @sort_by_z = false
end

._sort_by_zObject



22
23
24
# File 'lib/ruby2d/camera.rb', line 22

def self._sort_by_z
  @sort_by_z = true
end

._x(x) ⇒ Object

Variables changing Camera properties



68
69
70
# File 'lib/ruby2d/camera.rb', line 68

def self._x(x)
  @x += x
end

._y(y) ⇒ Object



72
73
74
# File 'lib/ruby2d/camera.rb', line 72

def self._y(y)
  @y += y
end

.angleObject



100
101
102
# File 'lib/ruby2d/camera.rb', line 100

def self.angle
  @angle ||= 0
end

.angle=(angle) ⇒ Object



104
105
106
107
# File 'lib/ruby2d/camera.rb', line 104

def self.angle=(angle)
  angle %= 360
  @angle = angle
end

.coordinate_to_screenspace(x, y) ⇒ Object

Convert worldspace camera coordinates into screenspace ones



120
121
122
123
124
# File 'lib/ruby2d/camera.rb', line 120

def self.coordinate_to_screenspace(x, y)
  angle = Camera.angle * (Math::PI / 180)
  [(((x - Camera.x) * Math.cos(angle)) - ((y - Camera.y) * Math.sin(angle))) * Camera.zoom + (Window.width * 0.5),
   (((x - Camera.x) * Math.sin(angle)) + ((y - Camera.y) * Math.cos(angle))) * Camera.zoom + (Window.height * 0.5)]
end

.coordinate_to_worldspace(x, y) ⇒ Object

Convert screenspace coordinates into worldspace camera ones



110
111
112
113
114
115
116
117
# File 'lib/ruby2d/camera.rb', line 110

def self.coordinate_to_worldspace(x, y)
  angle = Camera.angle * (Math::PI / 180)
  half_width = Window.width * 0.5
  half_height = Window.height * 0.5

  [(((x - half_width) / zoom) * Math.cos(-angle)) - (((y - half_height) / zoom) * Math.sin(-angle)) + self.x,
   (((x - half_width) / zoom) * Math.sin(-angle)) + (((y - half_height) / zoom) * Math.cos(-angle)) + self.y]
end

.debug_xObject



31
32
33
# File 'lib/ruby2d/camera.rb', line 31

def self.debug_x
  @debug_x ||= 0
end

.debug_x=(debug_x) ⇒ Object



35
36
37
# File 'lib/ruby2d/camera.rb', line 35

def self.debug_x=(debug_x)
  @debug_x = debug_x
end

.debug_yObject



39
40
41
# File 'lib/ruby2d/camera.rb', line 39

def self.debug_y
  @debug_y ||= 0
end

.debug_y=(debug_y) ⇒ Object



43
44
45
# File 'lib/ruby2d/camera.rb', line 43

def self.debug_y=(debug_y)
  @debug_y = debug_y
end

.remove(item) ⇒ Object



56
57
58
# File 'lib/ruby2d/camera.rb', line 56

def self.remove(item)
  objects.delete(item) if objects.include?(item)
end

.xObject



76
77
78
# File 'lib/ruby2d/camera.rb', line 76

def self.x
  @x ||= 0
end

.x=(x) ⇒ Object



80
81
82
# File 'lib/ruby2d/camera.rb', line 80

def self.x=(x)
  @x = x
end

.yObject



84
85
86
# File 'lib/ruby2d/camera.rb', line 84

def self.y
  @y ||= 0
end

.y=(y) ⇒ Object



88
89
90
# File 'lib/ruby2d/camera.rb', line 88

def self.y=(y)
  @y = y
end

.zoomObject



92
93
94
# File 'lib/ruby2d/camera.rb', line 92

def self.zoom
  @zoom ||= 1.0
end

.zoom=(zoom) ⇒ Object



96
97
98
# File 'lib/ruby2d/camera.rb', line 96

def self.zoom=(zoom)
  @zoom = zoom
end