Sha256: f2fc71d6c7cedee77f825beda6b251c4f6147af22dd2c99731d5471e693d710f

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

module AdventureRL
  module Modifiers
    module Gravity
      #include AdventureRL::Modifiers::Velocity  # NOTE: This modifier relies on Modifiers::Velocity

      DEFAULT_GRAVITY_SETTINGS = Settings.new(
        gravity_force:     1000.0,
        gravity_direction: {
          x: 0.0,
          y: 1.0
        }
      )

      def initialize settings = {}
        @settings          = DEFAULT_GRAVITY_SETTINGS.merge settings
        @gravity           = 0.0
        @gravity_force     = @settings.get :gravity_force
        @gravity_direction = @settings.get :gravity_direction
        #@max_velocity = @max_velocity_original.dup
        super @settings
        @max_velocity_original[:y] = Float::INFINITY
        @max_velocity[:y]          = @max_velocity_original[:y].dup
        @velocity_decay[:y]        = 0
      end

      # Apply gravitational force.
      def gravitize
        previous_position = get_position.dup
        get_gravity_directions.each do |axis, multiplier|
          next  if (@has_increased_velocity_for[axis])
          set_position axis => (get_position(axis) + multiplier)  unless (multiplier == 0)
          if (in_collision?)
            @velocity[axis] = 0.0
          else  #if ([0, @gravity_direction[axis].sign].include? get_velocity(axis).sign)
            increase_velocity_by(
              axis => ((@gravity_force * @gravity_direction[axis]) * @velocity_deltatime.dt),
              no_quick_turn_around: true
            )
          end
          set_position previous_position
        end
      end

      # Overwrite Modifiers::Velocity#move,
      # so we can update the gravity.
      def move
        super
        gravitize
      end

      private

        def get_gravity_directions
          return @gravity_direction.select do |axis, value|
            next value != 0
          end
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
adventure_rl-0.0.2 lib/AdventureRL/Modifiers/Gravity.rb
adventure_rl-0.0.1.pre.ld42 lib/AdventureRL/Modifiers/Gravity.rb