Class: BulletPhysics

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

Constant Summary collapse

START_DIST =
20
MAX_DIST =
500

Instance Attribute Summary

Attributes inherited from Component

#object

Instance Method Summary collapse

Methods inherited from Component

#draw

Constructor Details

#initialize(game_object, object_pool) ⇒ BulletPhysics

Returns a new instance of BulletPhysics.



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

def initialize(game_object, object_pool)
  super(game_object)
  x, y = point_at_distance(START_DIST)
  object.move(x, y)
  @object_pool = object_pool
  if trajectory_length > MAX_DIST
    object.target_x, object.target_y = point_at_distance(MAX_DIST)
  end
end

Instance Method Details

#point_at_distance(distance) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/entities/components/bullet_physics.rb', line 30

def point_at_distance(distance)
  if distance > trajectory_length
    return [object.target_x, object.target_y]
  end
  distance_factor = distance.to_f / trajectory_length
  p_x = x + (object.target_x - x) * distance_factor
  p_y = y + (object.target_y - y) * distance_factor
  [p_x, p_y]
end

#trajectory_lengthObject



26
27
28
# File 'lib/entities/components/bullet_physics.rb', line 26

def trajectory_length
  Utils.distance_between(object.target_x, object.target_y, x, y)
end

#updateObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/entities/components/bullet_physics.rb', line 15

def update
  fly_speed = Utils.adjust_speed(object.speed)
  now = Gosu.milliseconds
  @last_update ||= object.fired_at
  fly_distance = (now - @last_update) * 0.001 * fly_speed
  object.move(*point_at_distance(fly_distance))
  @last_update = now
  check_hit
  object.explode if arrived?
end