Class: Hook
- Inherits:
-
Object
- Object
- Hook
- Defined in:
- lib/lib/items/hook.rb
Instance Method Summary collapse
- #draw ⇒ Object
- #drop! ⇒ Object
-
#find_reachable_brick ⇒ Object
Find nearest brick under hook.
- #hold! ⇒ Object
- #holding? ⇒ Boolean
-
#initialize(window, brickyard, crane) ⇒ Hook
constructor
A new instance of Hook.
-
#reset! ⇒ Object
Load default setup.
- #update(button) ⇒ Object
Constructor Details
#initialize(window, brickyard, crane) ⇒ Hook
Returns a new instance of Hook.
14 15 16 17 18 19 20 21 22 |
# File 'lib/lib/items/hook.rb', line 14 def initialize(window, brickyard, crane) dir_path = File.dirname(__FILE__) @image = Gosu::Image.new(dir_path + '/../../media/square.png') @window = window @brickyard = brickyard @crane = crane reset! end |
Instance Method Details
#draw ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/lib/items/hook.rb', line 99 def draw @image.draw_rot(@x, CRANE_BOOM_Y, ZITEMS, 0, center_x = 0.5, center_y = 0, scale_x = 0.75, scale_y = TROLLEY_THICKNESS, color = Gosu::Color::BLACK) # trolley @image.draw_rot(@x, CRANE_BOOM_Y, ZITEMS, 0, center_x = 0.5, center_y = 0, # y: top scale_x = 0.08, scale_y = (@y - CRANE_BOOM_Y) / TILESIZE, color = Gosu::Color::BLACK) # cable @image.draw_rot(@x, @y, ZITEMS, 0, center_x = 0.5, center_y = 1, # y: bottom scale_x = 1, scale_y = HOOK_THICKNESS, color = Gosu::Color::BLACK) # hook itself if $debug and not holding? coords = Gosu::Image.from_text("[#{@x}, #{@y}, #{@angle}°]", LINE_HEIGHT) coords.draw_rot(@x, @y, ZTEXT, @angle) end end |
#drop! ⇒ Object
71 72 73 74 |
# File 'lib/lib/items/hook.rb', line 71 def drop! @held.start_falling! @held = nil end |
#find_reachable_brick ⇒ Object
Find nearest brick under hook
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/lib/items/hook.rb', line 88 def find_reachable_brick # TODO here reachables = @brickyard.bricks.select { |ii| ii.x.between?(@x - HOOK_MAGNET_X, @x + HOOK_MAGNET_X) } # brick is a bit to the left or right of hook reachables = reachables.select { |ii| ii.y.between?(@y, @y + HOOK_MAGNET_Y) } # brick is at the same height or bit lower than hook reachables[0] # if multiple in reach, pick the older end |
#hold! ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/lib/items/hook.rb', line 76 def hold! reachable_brick = find_reachable_brick if reachable_brick @held = reachable_brick @held.stop_falling! # to be sure @held.status = BRICK_STATUS_BEING_HELD else puts "no bricks nearby" end end |
#holding? ⇒ Boolean
67 68 69 |
# File 'lib/lib/items/hook.rb', line 67 def holding? !@held.nil? end |
#reset! ⇒ Object
Load default setup
25 26 27 28 29 30 |
# File 'lib/lib/items/hook.rb', line 25 def reset! @x = CENTRE @y = CENTRE @angle = 0 @held = nil end |
#update(button) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/lib/items/hook.rb', line 32 def update() case when Gosu::MS_LEFT then if holding? drop! else hold! end when Gosu::KB_R then if holding? @held.rotate! end end # TODO directional movement instead of teleporting @x = @window.mouse_x.to_int @y = @window.mouse_y.to_int # Block movement (by bricks) # TODO # Curtail movement (by borders of working area) @x = [@x, HOOK_MIN_X].max @x = [@x, HOOK_MAX_X].min @y = [@y, HOOK_MIN_Y].max @y = [@y, HOOK_MAX_Y].min # MAX_Y does not need to care about status of # holding? as when TRUE the held brick should have already blocked the move # Update held brick if @held @held.x = @x @held.y = @y end end |