Class: PlayerInput

Inherits:
Component show all
Defined in:
lib/entities/components/player_input.rb

Constant Summary collapse

NAME_COLOR =

Dark green

Gosu::Color.argb(0xee084408)

Instance Attribute Summary collapse

Attributes inherited from Component

#object

Instance Method Summary collapse

Constructor Details

#initialize(name, camera, object_pool) ⇒ PlayerInput

Returns a new instance of PlayerInput.



6
7
8
9
10
11
12
# File 'lib/entities/components/player_input.rb', line 6

def initialize(name, camera, object_pool)
  super(nil)
  @name = name
  @stats = Stats.new(name)
  @camera = camera
  @object_pool = object_pool
end

Instance Attribute Details

#statsObject (readonly)

Returns the value of attribute stats.



4
5
6
# File 'lib/entities/components/player_input.rb', line 4

def stats
  @stats
end

Instance Method Details

#control(obj) ⇒ Object



14
15
16
17
# File 'lib/entities/components/player_input.rb', line 14

def control(obj)
  self.object = obj
  obj.components << self
end

#draw(viewport) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/entities/components/player_input.rb', line 47

def draw(viewport)
  @name_image ||= Gosu::Image.from_text(
    $window, @name, Gosu.default_font_name, 20)
  @name_image.draw(
    x - @name_image.width / 2 - 1,
    y + object.graphics.height / 2, 100,
    1, 1, Gosu::Color::WHITE)
  @name_image.draw(
    x - @name_image.width / 2,
    y + object.graphics.height / 2, 100,
    1, 1, NAME_COLOR)
end

#on_collision(with) ⇒ Object



19
20
# File 'lib/entities/components/player_input.rb', line 19

def on_collision(with)
end

#on_damage(amount) ⇒ Object



22
23
24
# File 'lib/entities/components/player_input.rb', line 22

def on_damage(amount)
  @stats.add_damage(amount)
end

#updateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/entities/components/player_input.rb', line 26

def update
  return respawn if object.health.dead?
  d_x, d_y = @camera.target_delta_on_screen
  atan = Math.atan2(($window.width / 2) - d_x - $window.mouse_x,
                    ($window.height / 2) - d_y - $window.mouse_y)
  object.gun_angle = -atan * 180 / Math::PI
  motion_buttons = [Gosu::KbW, Gosu::KbS, Gosu::KbA, Gosu::KbD]

  if any_button_down?(*motion_buttons)
    object.throttle_down = true
    object.physics.change_direction(
      change_angle(object.direction, *motion_buttons))
  else
    object.throttle_down = false
  end

  if Utils.button_down?(Gosu::MsLeft)
    object.shoot(*@camera.mouse_coords)
  end
end