Class: SpriteKit::Camera

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

Constant Summary collapse

SCREEN_WIDTH =
1280
SCREEN_HEIGHT =
720

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: 1500, h: 1500) ⇒ Camera

Returns a new instance of Camera.



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

def initialize(
  x: 0, y: 0,
  target_x: 0, target_y: 0,
  target_scale: 2, scale: 2,
  w: 1500,
  h: 1500
)
  @x = x
  @y = y
  @target_x = target_x
  @target_y = target_y
  @target_scale = target_scale
  @scale = scale

  @w = w
  @h = h
end

Instance Attribute Details

#hObject

Returns the value of attribute h.



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

def h
  @h
end

#scaleObject

Returns the value of attribute scale.



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

def scale
  @scale
end

#target_scaleObject

Returns the value of attribute target_scale.



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

def target_scale
  @target_scale
end

#target_xObject

Returns the value of attribute target_x.



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

def target_x
  @target_x
end

#target_yObject

Returns the value of attribute target_y.



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

def target_y
  @target_y
end

#wObject

Returns the value of attribute w.



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

def w
  @w
end

#xObject

Returns the value of attribute x.



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

def x
  @x
end

#yObject

Returns the value of attribute y.



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:



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

def self.to_screen_space(camera, rect)
  x = rect.x * camera.scale - camera.x * camera.scale + (camera.w / 2)
  y = rect.y * camera.scale - camera.y * camera.scale + (camera.h / 2)
  w = rect.w * camera.scale
  h = rect.h * camera.scale
  rect.merge x: x, y: y, w: w, h: h
end

.to_world_space(camera, rect) ⇒ Object

Parameters:



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

def self.to_world_space(camera, rect)
  x = (rect.x - (camera.w / 2) + camera.x * camera.scale - camera.offset_x) / camera.scale
  y = (rect.y - (camera.h / 2) + camera.y * camera.scale - camera.offset_y) / camera.scale
  w = rect.w / camera.scale
  h = rect.h / camera.scale
  rect.merge(x: x, y: y, w: w, h: h)
end

Instance Method Details

#find_all_intersect_viewport(rects) ⇒ Object



75
76
77
# File 'mygame/lib/sprite_kit/camera.rb', line 75

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

#intersect_viewport?(rect) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'mygame/lib/sprite_kit/camera.rb', line 79

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

#offset_xObject



26
27
28
# File 'mygame/lib/sprite_kit/camera.rb', line 26

def offset_x
  (SCREEN_WIDTH - @w) / 2
end

#offset_yObject



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

def offset_y
  (SCREEN_HEIGHT - @h) / 2
end

#to_screen_space(rect) ⇒ Object



67
68
69
# File 'mygame/lib/sprite_kit/camera.rb', line 67

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

#to_world_space(rect) ⇒ Object



53
54
55
# File 'mygame/lib/sprite_kit/camera.rb', line 53

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

#viewportObject



34
35
36
37
38
39
40
41
# File 'mygame/lib/sprite_kit/camera.rb', line 34

def viewport
  {
    x: offset_x,
    y: offset_y,
    w: @w,
    h: @h
  }
end

#viewport_worldObject



71
72
73
# File 'mygame/lib/sprite_kit/camera.rb', line 71

def viewport_world
  to_world_space(viewport)
end