Module: AdventureRL::Modifiers::Velocity
- Defined in:
- lib/AdventureRL/Modifiers/Velocity.rb
Constant Summary collapse
- DEFAULT_VELOCITY_SETTINGS =
Settings.new( # Does NOT use Deltatime max_velocity: { x: 100, y: 100 }, # DOES use Deltatime velocity_decay: { x: 750, y: 750 }, # Does NOT use Deltatime base_velocity: { x: 0, y: 0 }, quick_turn_around: false )
Instance Method Summary collapse
-
#get_max_velocity(target = :all) ⇒ Object
Returns the max velocity.
-
#get_velocity(target = :all) ⇒ Object
Returns the velocity.
-
#increase_velocity_by(*args) ⇒ Object
(also: #add_velocity)
Increase the velocity.
- #initialize(settings = {}) ⇒ Object
-
#move ⇒ Object
Call this every frame to move with the stored velocity.
-
#reset_max_velocity ⇒ Object
Resets the max velocity to the original values.
- #set_max_velocity(*args) ⇒ Object
- #set_temporary_max_velocity(*args) ⇒ Object
- #set_velocity(*args) ⇒ Object
Instance Method Details
#get_max_velocity(target = :all) ⇒ Object
Returns the max velocity. Pass an optional target argument, which can be either an axis (:x or :y), or :all, which will return a Hash with both values.
57 58 59 60 61 62 |
# File 'lib/AdventureRL/Modifiers/Velocity.rb', line 57 def get_max_velocity target = :all target = target.to_sym return @max_velocity if (target == :all) return @max_velocity[target] if (@max_velocity.keys.include?(target)) return nil end |
#get_velocity(target = :all) ⇒ Object
Returns the velocity. Pass an optional target argument, which can be either an axis (:x or :y), or :all, which will return a Hash with both values.
46 47 48 49 50 51 |
# File 'lib/AdventureRL/Modifiers/Velocity.rb', line 46 def get_velocity target = :all target = target.to_sym return @velocity if (target == :all) return @velocity[target] if (@velocity.keys.include?(target)) return nil end |
#increase_velocity_by(*args) ⇒ Object Also known as: add_velocity
Increase the velocity. args may be:
Two integers, representing the <tt>x</tt> and <tt>y</tt> axes, respectively.
A hash containing one or both of the keys <tt>:x</tt> and <tt>:y</tt>.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/AdventureRL/Modifiers/Velocity.rb', line 74 def increase_velocity_by *args opts = {} opts = args.last if (args.last.is_a? Hash) quick_turn_around = @velocity_quick_turn_around quick_turn_around = opts[:quick_turn_around] unless (opts[:quick_turn_around].nil?) incremental_velocity = parse_position *args @velocity.keys.each do |axis| next unless (incremental_velocity.key? axis) velocity_sign = @velocity[axis].sign incremental_velocity_sign = incremental_velocity[axis].sign @velocity[axis] = 0 unless (velocity_sign == incremental_velocity_sign) if (quick_turn_around && !opts[:no_quick_turn_around]) @velocity[axis] = @base_velocity[axis] * incremental_velocity_sign if (@velocity[axis] == 0) @velocity[axis] += incremental_velocity[axis] case velocity_sign when 1 @velocity[axis] = get_max_velocity(axis) if (@velocity[axis] > get_max_velocity(axis)) when -1 @velocity[axis] = -get_max_velocity(axis) if (@velocity[axis] < -get_max_velocity(axis)) end @has_increased_velocity_for[axis] = true end end |
#initialize(settings = {}) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/AdventureRL/Modifiers/Velocity.rb', line 23 def initialize settings = {} @settings = DEFAULT_VELOCITY_SETTINGS.merge settings @velocity = { x: 0.0, y: 0.0 } @max_velocity_original = @settings.get :max_velocity @max_velocity = @max_velocity_original.dup @velocity_decay = @settings.get :velocity_decay @velocity_quick_turn_around = @settings.get :quick_turn_around @base_velocity = @settings.get :base_velocity @velocity_deltatime = Deltatime.new @has_increased_velocity_for = { x: false, y: false } super @settings end |
#move ⇒ Object
Call this every frame to move with the stored velocity.
119 120 121 122 123 124 125 126 127 |
# File 'lib/AdventureRL/Modifiers/Velocity.rb', line 119 def move move_by get_incremental_position_for_velocity if (any_velocity?) decrease_velocity @has_increased_velocity_for = { x: false, y: false } @velocity_deltatime.update end |
#reset_max_velocity ⇒ Object
Resets the max velocity to the original values.
114 115 116 |
# File 'lib/AdventureRL/Modifiers/Velocity.rb', line 114 def reset_max_velocity @max_velocity = @max_velocity_original.dup end |
#set_max_velocity(*args) ⇒ Object
98 99 100 101 102 103 104 |
# File 'lib/AdventureRL/Modifiers/Velocity.rb', line 98 def set_max_velocity *args new_max_velocity = parse_position *args @max_velocity_original.keys.each do |axis| @max_velocity_original[axis] = new_max_velocity[axis] if (new_max_velocity.key? axis) end @max_velocity = @max_velocity_original.dup end |
#set_temporary_max_velocity(*args) ⇒ Object
106 107 108 109 110 111 |
# File 'lib/AdventureRL/Modifiers/Velocity.rb', line 106 def set_temporary_max_velocity *args new_max_velocity = parse_position *args @max_velocity.keys.each do |axis| @max_velocity[axis] = new_max_velocity[axis] if (new_max_velocity.key? axis) end end |
#set_velocity(*args) ⇒ Object
64 65 66 67 68 |
# File 'lib/AdventureRL/Modifiers/Velocity.rb', line 64 def set_velocity *args new_velocity = parse_position *args @velocity[:x] = new_velocity[:x] if (new_velocity.key? :x) @velocity[:y] = new_velocity[:y] if (new_velocity.key? :y) end |