Class: Bestguigui::Camera
- Inherits:
-
Object
- Object
- Bestguigui::Camera
- Defined in:
- lib/bestguigui/camera.rb
Overview
Fait suivre une cible (typiquement le joueur) avec un lissage, et décale tout ce qui est dessiné dans #look en conséquence — pour un jeu qui scrolle plus large que la fenêtre.
@camera = Bestguigui::Camera.new(@player, viewport_width: 800, viewport_height: 600)
def update(dt)
@camera.update(dt)
end
def draw
@camera.look do
@player.draw
@coins.each(&:draw)
end
end
La cible doit juste répondre à #x et #y (toute Entity convient).
Instance Method Summary collapse
-
#initialize(target, viewport_width:, viewport_height:, smoothing: 0.1, offset_x: 0, offset_y: 0) ⇒ Camera
constructor
A new instance of Camera.
- #look ⇒ Object
- #update(dt) ⇒ Object
Constructor Details
#initialize(target, viewport_width:, viewport_height:, smoothing: 0.1, offset_x: 0, offset_y: 0) ⇒ Camera
Returns a new instance of Camera.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/bestguigui/camera.rb', line 21 def initialize(target, viewport_width:, viewport_height:, smoothing: 0.1, offset_x: 0, offset_y: 0) @target = target @viewport_width = @viewport_height = @smoothing = smoothing @offset_x = offset_x @offset_y = offset_y @x = target.x @y = target.y end |
Instance Method Details
#look ⇒ Object
44 45 46 |
# File 'lib/bestguigui/camera.rb', line 44 def look Gosu.translate(-@x, -@y) { yield } end |
#update(dt) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bestguigui/camera.rb', line 32 def update(dt) target_x = @target.x - @viewport_width / 2 + @offset_x target_y = @target.y - @viewport_height / 2 + @offset_y # smoothing pensé pour 60 fps, remis à l'échelle par dt pour rester # cohérent quel que soit le framerate — pas juste un lerp à facteur # fixe (qui varie avec le framerate). factor = (@smoothing * dt * 60).clamp(0.0, 1.0) @x += (target_x - @x) * factor @y += (target_y - @y) * factor end |