Class: Camera

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#targetObject

Returns the value of attribute target.



3
4
5
# File 'lib/entities/camera.rb', line 3

def target
  @target
end

#xObject

Returns the value of attribute x.



2
3
4
# File 'lib/entities/camera.rb', line 2

def x
  @x
end

#yObject

Returns the value of attribute y.



2
3
4
# File 'lib/entities/camera.rb', line 2

def y
  @y
end

#zoomObject

Returns the value of attribute zoom.



2
3
4
# File 'lib/entities/camera.rb', line 2

def zoom
  @zoom
end

Instance Method Details

#desired_spotObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/entities/camera.rb', line 11

def desired_spot
  if @target.physics.moving?
    Utils.point_at_distance(
      @target.x, @target.y,
      @target.direction,
      @target.physics.speed.ceil * 25)
  else
    [@target.x, @target.y]
  end
end

#draw_crosshairObject



89
90
91
92
93
94
95
96
97
# File 'lib/entities/camera.rb', line 89

def draw_crosshair
  factor = 0.5
  x = $window.mouse_x
  y = $window.mouse_y
  c = crosshair
  c.draw(x - c.width * factor / 2,
         y - c.height * factor / 2,
         1000, factor, factor)
end

#mouse_coordsObject



22
23
24
25
26
27
28
29
# File 'lib/entities/camera.rb', line 22

def mouse_coords
  x, y = target_delta_on_screen
  mouse_x_on_map = @target.x +
    (x + $window.mouse_x - ($window.width / 2)) / @zoom
  mouse_y_on_map = @target.y +
    (y + $window.mouse_y - ($window.height / 2)) / @zoom
  [mouse_x_on_map, mouse_y_on_map].map(&:round)
end

#target_delta_on_screenObject



85
86
87
# File 'lib/entities/camera.rb', line 85

def target_delta_on_screen
  [(@x - @target.x) * @zoom, (@y - @target.y) * @zoom]
end

#to_sObject



79
80
81
82
83
# File 'lib/entities/camera.rb', line 79

def to_s
  "FPS: #{Gosu.fps}. " <<
    "#{@x}:#{@y} @ #{'%.2f' % @zoom}. " <<
    'WASD to move, arrows to zoom.'
end

#updateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/entities/camera.rb', line 31

def update
  des_x, des_y = desired_spot
  shift = Utils.adjust_speed(
    @target.physics.speed).floor *
    @target.speed_modifier + 1
  if @x < des_x
    if des_x - @x < shift
      @x = des_x
    else
      @x += shift
    end
  elsif @x > des_x
    if @x - des_x < shift
      @x = des_x
    else
      @x -= shift
    end
  end
  if @y < des_y
    if des_y - @y < shift
      @y = des_y
    else
      @y += shift
    end
  elsif @y > des_y
    if @y - des_y < shift
      @y = des_y
    else
      @y -= shift
    end
  end

  zoom_delta = @zoom > 0 ? 0.01 : 1.0
  zoom_delta = Utils.adjust_speed(zoom_delta)
  if $window.button_down?(Gosu::KbUp)
    @zoom -= zoom_delta unless @zoom < 0.7
  elsif $window.button_down?(Gosu::KbDown)
    @zoom += zoom_delta unless @zoom > 10
  else
    target_zoom = @target.physics.speed > 1.1 ? 0.75 : 1.0
    if @zoom <= (target_zoom - 0.01)
      @zoom += zoom_delta / 3
    elsif @zoom > (target_zoom + 0.01)
      @zoom -= zoom_delta / 3
    end
  end
end

#viewportObject



99
100
101
102
103
104
105
# File 'lib/entities/camera.rb', line 99

def viewport
  x0 = @x - ($window.width / 2)  / @zoom
  x1 = @x + ($window.width / 2)  / @zoom
  y0 = @y - ($window.height / 2) / @zoom
  y1 = @y + ($window.height / 2) / @zoom
  [x0, x1, y0, y1]
end