Class: OR2D::Composites::Projectile

Inherits:
OR2D::Composite show all
Defined in:
lib/or2d/composites/projectile.rb

Overview

The Composite Projectile class is used to create a composite projectile object.

Since:

  • 2023-04-26

Instance Attribute Summary

Attributes inherited from OR2D::Composite

#id, #layers

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OR2D::Composite

#add_layer, #each_layer, #hide, #remove_layer, #show, #toggle, #toggle_boundary

Constructor Details

#initialize(options) ⇒ Projectile

Construct a new Projectile object.

Parameters:

  • options (Hash)

    the options used to construct the Projectile object

Options Hash (options):

  • :asset (Hash)

    the options used to construct the Asset object

  • :angle (Float)

    the angle of the Projectile object

  • :direction (Hash)

    the direction of the Projectile object

  • :radius (Float)

    the radius of the Projectile object

  • :velocity (Float)

    the velocity of the Projectile object

  • :time_step (Float)

    the time step of the Projectile object

  • :stepping (Float)

    the time step of the Projectile object

  • :gravity_points (Array)

    the gravity points of the Projectile object

Since:

  • 2023-04-26



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/or2d/composites/projectile.rb', line 15

def initialize(options)
  super(options[:id] || "ProjectileComposite_#{SecureRandom.uuid}")
  @asset_id = add_layer(options[:asset][:type], options[:asset][:options].merge(show: true)) if options.key?(:asset)
  @angle = options[:angle] || 45
  @direction = { x: Math.cos(@angle * Math::PI / 180),
                 y: Math.sin(@angle * Math::PI / 180) }
  @radius = options[:radius] || 360
  @velocity = options[:velocity] || 15
  @velocity_y = @velocity * @direction[:y]
  @time_step = options[:time_step] || options[:stepping] || 0.1
  @elapsed = 0
  @points = { line: {}, gravity: options[: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.

Parameters:

  • stepping (Integer) (defaults to: 3)

    the number of gravity points to generate

  • gravity_minimum (Float) (defaults to: 5)

    the minimum gravity value

  • gravity_maximum (Float) (defaults to: 100)

    the maximum gravity value

  • interval (Float) (defaults to: 0.5)

    the interval between gravity points

Returns:

  • (Array)

    the generated gravity points

Since:

  • 2023-04-26



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.

Parameters:

  • radians (Float)

    the radians to convert

Returns:

  • (Float)

    the converted radians

Since:

  • 2023-04-26



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

#destroyObject

Destroy the Projectile object.

Since:

  • 2023-04-26



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?

Returns:

  • (Boolean)

    whether the Projectile object is out of bounds

Since:

  • 2023-04-26



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

#updateObject

Update the Projectile object.

Since:

  • 2023-04-26



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