Class: OR2D::Composites::Projectile
- Inherits:
-
OR2D::Composite
- Object
- OR2D::Composite
- OR2D::Composites::Projectile
- Defined in:
- lib/or2d/composites/projectile.rb
Overview
The Composite Projectile class is used to create a composite projectile object.
Instance Attribute Summary
Attributes inherited from OR2D::Composite
Class Method Summary collapse
-
.gravity_points(stepping: 3, gravity_minimum: 5, gravity_maximum: 100, interval: 0.5) ⇒ Array
Generate a random set of gravity points.
-
.rad_to_deg(radians) ⇒ Float
Convert radians to degrees.
Instance Method Summary collapse
-
#destroy ⇒ Object
Destroy the Projectile object.
-
#initialize(options) ⇒ Projectile
constructor
Construct a new Projectile object.
-
#out_of_bounds? ⇒ Boolean
Is the Projectile object out of bounds?.
-
#update ⇒ Object
Update the Projectile object.
Methods inherited from OR2D::Composite
#add_layer, #hide, #remove_layer, #show, #toggle
Constructor Details
#initialize(options) ⇒ Projectile
Construct a new Projectile object.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/or2d/composites/projectile.rb', line 15 def initialize() super([:id] || "ProjectileComposite_#{SecureRandom.uuid}") @asset_id = add_layer([:asset][:type], [:asset][:options].merge(show: true)) if .key?(:asset) @angle = [:angle] || 45 @direction = { x: Math.cos(@angle * Math::PI / 180), y: Math.sin(@angle * Math::PI / 180) } @radius = [:radius] || 360 @velocity = [:velocity] || 15 @velocity_y = @velocity * @direction[:y] @time_step = [:time_step] || [:stepping] || 0.1 @elapsed = 0 @points = { line: {}, gravity: [:gravity_points] || [] } @gravity = @points[:gravity].first[:value] || 0 end |
Class Method Details
.gravity_points(stepping: 3, gravity_minimum: 5, gravity_maximum: 100, interval: 0.5) ⇒ Array
Generate a random set of gravity points.
50 51 52 53 54 55 56 57 |
# File 'lib/or2d/composites/projectile.rb', line 50 def self.gravity_points(stepping: 3, gravity_minimum: 5, gravity_maximum: 100, interval: 0.5) gravity_points = [] stepping.times do |i| gravity_points << { time: i * interval, value: gravity_minimum + rand * (gravity_maximum - gravity_minimum) } end gravity_points end |
.rad_to_deg(radians) ⇒ Float
Convert radians to degrees.
62 63 64 |
# File 'lib/or2d/composites/projectile.rb', line 62 def self.rad_to_deg(radians) radians * 360.0 / Math::PI end |
Instance Method Details
#destroy ⇒ Object
Destroy the Projectile object.
67 68 69 70 71 72 |
# File 'lib/or2d/composites/projectile.rb', line 67 def destroy @layers.each_key do |id| OR2D.game.entities[id].destroy OR2D.game.remove_entity(id) end end |
#out_of_bounds? ⇒ Boolean
Is the Projectile object out of bounds?
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/or2d/composites/projectile.rb', line 76 def out_of_bounds? if @layers.any? do |id, _| OR2D.game.entities[id].x.negative? || OR2D.game.entities[id].x > OR2D.game.window.get(:width) || OR2D.game.entities[id].y.negative? || OR2D.game.entities[id].y > OR2D.game.window.get(:height) end true else false end end |
#update ⇒ Object
Update the Projectile object.
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/or2d/composites/projectile.rb', line 31 def update @elapsed += @time_step update_gravity @layers.each_key do |id| OR2D.game.entities[id].x += Math.cos(@angle * Math::PI / 180) * @velocity * @direction[:x] * @time_step OR2D.game.entities[id].y -= Math.sin(@angle * Math::PI / 180) * @velocity * @direction[:y] * @time_step - 0.5 * @gravity * @time_step**2 @velocity_y -= @gravity * @time_step @points[:line][id] ||= [] @points[:line][id] << [OR2D.game.entities[id].x, OR2D.game.entities[id].y] end end |