Module: Smalruby3::SpriteMethod::Motion
- Included in:
- Smalruby3::Sprite
- Defined in:
- lib/smalruby3/sprite_method/motion.rb
Overview
Motion category methods
Instance Method Summary collapse
- #direction=(degrees) ⇒ Object
- #go_to(destination) ⇒ Object
- #move(steps) ⇒ Object
- #point_towards(towards) ⇒ Object
- #position=(val) ⇒ Object
- #turn_left(degrees) ⇒ Object
- #turn_right(degrees) ⇒ Object
- #x=(val) ⇒ Object
- #y=(val) ⇒ Object
Instance Method Details
#direction=(degrees) ⇒ Object
35 36 37 38 39 |
# File 'lib/smalruby3/sprite_method/motion.rb', line 35 def direction=(degrees) @direction = calc_direction(degrees) sync_direction end |
#go_to(destination) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/smalruby3/sprite_method/motion.rb', line 21 def go_to(destination) if destination == "_random_" new_x = rand(SmalrubyToDXRuby::SCREEN_LEFT..SmalrubyToDXRuby::SCREEN_RIGHT) new_y = rand(SmalrubyToDXRuby::SCREEN_BOTTOM..SmalrubyToDXRuby::SCREEN_TOP) new_pos = [new_x, new_y] elsif destination == "_mouse_" new_pos = calc_mouse_position elsif destination.is_a?(Array) new_pos = destination end self.position = new_pos direction end |
#move(steps) ⇒ Object
7 8 9 10 |
# File 'lib/smalruby3/sprite_method/motion.rb', line 7 def move(steps) self.position = [x + @vector[:x] * steps, y + @vector[:y] * steps] position end |
#point_towards(towards) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/smalruby3/sprite_method/motion.rb', line 41 def point_towards(towards) tx = sprite(towards).x ty = sprite(towards).y dx = tx - x dy = ty - y rad = Math.atan2(dx, dy) self.direction = rad * 180.0 / Math::PI end |
#position=(val) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/smalruby3/sprite_method/motion.rb', line 62 def position=(val) @x = calc_x(val[0]) @y = calc_y(val[1]) sync_position end |
#turn_left(degrees) ⇒ Object
17 18 19 |
# File 'lib/smalruby3/sprite_method/motion.rb', line 17 def turn_left(degrees) turn_right(-degrees) end |
#turn_right(degrees) ⇒ Object
12 13 14 15 |
# File 'lib/smalruby3/sprite_method/motion.rb', line 12 def turn_right(degrees) self.direction += degrees direction end |
#x=(val) ⇒ Object
50 51 52 53 54 |
# File 'lib/smalruby3/sprite_method/motion.rb', line 50 def x=(val) @x = calc_x(val) sync_position end |
#y=(val) ⇒ Object
56 57 58 59 60 |
# File 'lib/smalruby3/sprite_method/motion.rb', line 56 def y=(val) @y = calc_y(val) sync_position end |