Class: SpriteKit::Camera

Inherits:
Object
  • Object
show all
Defined in:
mygame/lib/sprite_kit/camera.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x: 0, y: 0, target_x: 0, target_y: 0, target_scale: 2, scale: 2, w: nil, h: nil, offset_x: nil, offset_y: nil, path: nil) ⇒ Camera

Returns a new instance of Camera.



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

def initialize(
  x: 0, y: 0,
  target_x: 0, target_y: 0,
  target_scale: 2, scale: 2,
  w: nil, h: nil,
  offset_x: nil, offset_y: nil,
  path: nil
)
  @x = x
  @y = y
  @target_x = target_x
  @target_y = target_y
  @target_scale = target_scale
  @scale = scale

  @w = w
  @h = h
  @offset_x = offset_x
  @offset_y = offset_y
  @path = path
end

Instance Attribute Details

#hObject



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

def h
  @h || viewport_h
end

#offset_xObject



75
76
77
78
79
80
81
82
83
84
85
86
# File 'mygame/lib/sprite_kit/camera.rb', line 75

def offset_x
  return @offset_x if @offset_x

  # old offset_x functionality when defining an @w
  return (viewport_w - @w / 2) if @w

  if Grid.origin_center?
    0
  else
    Grid.allscreen_x
  end
end

#offset_yObject



88
89
90
91
92
93
94
95
96
97
98
99
# File 'mygame/lib/sprite_kit/camera.rb', line 88

def offset_y
  return @offset_x if @offset_x

  # old offset_y functionality when defining an @h
  return (viewport_h - @h / 2) if @h

  if Grid.origin_center?
    0
  else
    Grid.allscreen_y
  end
end

#pathObject

SCREEN_WIDTH = 1280 SCREEN_HEIGHT = 720



6
7
8
# File 'mygame/lib/sprite_kit/camera.rb', line 6

def path
  @path
end

#scaleObject

SCREEN_WIDTH = 1280 SCREEN_HEIGHT = 720



6
7
8
# File 'mygame/lib/sprite_kit/camera.rb', line 6

def scale
  @scale
end

#target_scaleObject

SCREEN_WIDTH = 1280 SCREEN_HEIGHT = 720



6
7
8
# File 'mygame/lib/sprite_kit/camera.rb', line 6

def target_scale
  @target_scale
end

#target_xObject

SCREEN_WIDTH = 1280 SCREEN_HEIGHT = 720



6
7
8
# File 'mygame/lib/sprite_kit/camera.rb', line 6

def target_x
  @target_x
end

#target_yObject

SCREEN_WIDTH = 1280 SCREEN_HEIGHT = 720



6
7
8
# File 'mygame/lib/sprite_kit/camera.rb', line 6

def target_y
  @target_y
end

#wObject



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

def w
  @w || viewport_w
end

#xObject

SCREEN_WIDTH = 1280 SCREEN_HEIGHT = 720



6
7
8
# File 'mygame/lib/sprite_kit/camera.rb', line 6

def x
  @x
end

#yObject

SCREEN_WIDTH = 1280 SCREEN_HEIGHT = 720



6
7
8
# File 'mygame/lib/sprite_kit/camera.rb', line 6

def y
  @y
end

Class Method Details

.to_screen_space(camera, rect) ⇒ Object

Parameters:



172
173
174
# File 'mygame/lib/sprite_kit/camera.rb', line 172

def self.to_screen_space(camera, rect)
  to_screen_space!(camera, rect.merge({}))
end

.to_screen_space!(camera, rect) ⇒ Object

Parameters:



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'mygame/lib/sprite_kit/camera.rb', line 178

def self.to_screen_space!(camera, rect)
  if rect.x
    rect.x = rect.x * camera.scale - camera.x * camera.scale + (camera.half_width)
  end

  if rect.y
    rect.y = rect.y * camera.scale - camera.y * camera.scale + (camera.half_height)
  end

  if rect.w
    rect.w = rect.w * camera.scale
  end

  if rect.h
    rect.h = rect.h * camera.scale
  end

  rect
end

.to_world_space(camera, rect) ⇒ Object

Parameters:



138
139
140
# File 'mygame/lib/sprite_kit/camera.rb', line 138

def self.to_world_space(camera, rect)
  to_world_space!(camera, rect.merge({}))
end

.to_world_space!(camera, rect) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'mygame/lib/sprite_kit/camera.rb', line 142

def self.to_world_space!(camera, rect)
  if rect.x
    rect.x = (rect.x - (camera.half_width) + camera.x * camera.scale - camera.offset_x) / camera.scale
  end

  if rect.y
    rect.y = (rect.y - (camera.half_height) + camera.y * camera.scale - camera.offset_y) / camera.scale
  end

  if rect.w
    rect.w = rect.w / camera.scale
  end

  if rect.h
    rect.h = rect.h / camera.scale
  end

  rect
end

Instance Method Details

#find_all_intersect_viewport(rects) ⇒ Object



210
211
212
# File 'mygame/lib/sprite_kit/camera.rb', line 210

def find_all_intersect_viewport(rects)
  Geometry.find_all_intersect_rect(viewport_world, rects)
end

#half_heightObject



45
46
47
48
49
# File 'mygame/lib/sprite_kit/camera.rb', line 45

def half_height
  return @h.fdiv(2).ceil if @h

  viewport_h_half
end

#half_widthObject



39
40
41
42
43
# File 'mygame/lib/sprite_kit/camera.rb', line 39

def half_width
  return @w.fdiv(2).ceil if @w

  viewport_w_half
end

#intersect_viewport?(rect) ⇒ Boolean

Returns:

  • (Boolean)


214
215
216
# File 'mygame/lib/sprite_kit/camera.rb', line 214

def intersect_viewport?(rect)
  viewport_world.intersect_rect?(rect)
end

#resetObject



222
223
224
225
226
227
# File 'mygame/lib/sprite_kit/camera.rb', line 222

def reset
  @target_x = 0
  @target_y = 0
  @x = 0
  @y = 0
end

#to_aObject



218
219
220
# File 'mygame/lib/sprite_kit/camera.rb', line 218

def to_a
  viewport.merge!({})
end

#to_screen_space(rect) ⇒ Object



198
199
200
# File 'mygame/lib/sprite_kit/camera.rb', line 198

def to_screen_space(rect)
  self.class.to_screen_space(self, rect)
end

#to_screen_space!(rect) ⇒ Object



202
203
204
# File 'mygame/lib/sprite_kit/camera.rb', line 202

def to_screen_space!(rect)
  self.class.to_screen_space!(self, rect)
end

#to_world_space(rect) ⇒ Object



162
163
164
# File 'mygame/lib/sprite_kit/camera.rb', line 162

def to_world_space(rect)
  self.class.to_world_space(self, rect)
end

#to_world_space!(rect) ⇒ Object



166
167
168
# File 'mygame/lib/sprite_kit/camera.rb', line 166

def to_world_space!(rect)
  self.class.to_world_space!(self, rect)
end

#viewportObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'mygame/lib/sprite_kit/camera.rb', line 101

def viewport
  if @w && @h
    return {
      x: offset_x,
      y: offset_y,
      w: w,
      h: h,
      path: @path,
      primitive_marker: :sprite
    }
  end

  if Grid.origin_center?
    {
      x: offset_x,
      y: offset_y,
      w: w,
      h: h,
      anchor_x: 0.5,
      anchor_y: 0.5,
      path: @path,
      primitive_marker: :sprite
    }
  else
    {
      x: offset_x,
      y: offset_y,
      w: w,
      h: h,
      path: @path,
      primitive_marker: :sprite
    }
  end
end

#viewport_hObject



51
52
53
# File 'mygame/lib/sprite_kit/camera.rb', line 51

def viewport_h
  Grid.allscreen_h
end

#viewport_h_halfObject



67
68
69
70
71
72
73
# File 'mygame/lib/sprite_kit/camera.rb', line 67

def viewport_h_half
  if Grid.origin_center?
    0
  else
    viewport_h.fdiv(2).ceil
  end
end

#viewport_wObject



55
56
57
# File 'mygame/lib/sprite_kit/camera.rb', line 55

def viewport_w
  Grid.allscreen_w
end

#viewport_w_halfObject



59
60
61
62
63
64
65
# File 'mygame/lib/sprite_kit/camera.rb', line 59

def viewport_w_half
  if Grid.origin_center?
    0
  else
    viewport_w.fdiv(2).ceil
  end
end

#viewport_worldObject



206
207
208
# File 'mygame/lib/sprite_kit/camera.rb', line 206

def viewport_world
  to_world_space(viewport)
end