Class: Shape
- Inherits:
-
Object
- Object
- Shape
- Defined in:
- lib/shape.rb
Overview
General Shape class
Instance Attribute Summary collapse
-
#blocks ⇒ Object
readonly
Returns the value of attribute blocks.
-
#falling ⇒ Object
writeonly
Sets the attribute falling.
-
#rotation ⇒ Object
Returns the value of attribute rotation.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
-
#collides? ⇒ Boolean
checks whether the shape collides with walls or blocks.
-
#collides_with_blocks? ⇒ Boolean
checks whether the shape collides with any blocks.
-
#collides_with_walls? ⇒ Boolean
checks whether the shape collides with walls (and floor).
- #draw ⇒ Object
- #falling? ⇒ Boolean
-
#initialize(game) ⇒ Shape
constructor
A new instance of Shape.
-
#maximum_x_block ⇒ Object
gets the rightmost block of the shape.
-
#maximum_y_block ⇒ Object
gets the maximum y block of the shape note that maximum y means the lowest block!.
-
#minimum_x_block ⇒ Object
gets the lefmost block of the shape.
-
#move_x ⇒ Object
Move the shape on the x axis, if there is the need to.
-
#move_y ⇒ Object
Move the shape on the y axis – movement down.
-
#rotate ⇒ Object
rotates the shape.
-
#translate_by_y ⇒ Object
flips the shape by y axis.
- #update ⇒ Object
-
#update_move_x ⇒ Object
updates move_x counter returns nil if there is no need to update it.
-
#update_move_y ⇒ Object
updates move_y counter, checks for speed up.
Constructor Details
#initialize(game) ⇒ Shape
Returns a new instance of Shape.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/shape.rb', line 9 def initialize(game) @game = game @x = @y = 0 @blocks = [] # is the shape currently falling? @falling = true # by which block to rotate @rotation_block = @blocks[1] # how many rotation states are there @rotations = 1 # rotation state @rotation = 0 end |
Instance Attribute Details
#blocks ⇒ Object (readonly)
Returns the value of attribute blocks.
6 7 8 |
# File 'lib/shape.rb', line 6 def blocks @blocks end |
#falling=(value) ⇒ Object (writeonly)
Sets the attribute falling
7 8 9 |
# File 'lib/shape.rb', line 7 def falling=(value) @falling = value end |
#rotation ⇒ Object
Returns the value of attribute rotation.
5 6 7 |
# File 'lib/shape.rb', line 5 def rotation @rotation end |
#x ⇒ Object
Returns the value of attribute x.
5 6 7 |
# File 'lib/shape.rb', line 5 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
5 6 7 |
# File 'lib/shape.rb', line 5 def y @y end |
Instance Method Details
#collides? ⇒ Boolean
checks whether the shape collides with walls or blocks
101 102 103 104 |
# File 'lib/shape.rb', line 101 def collides? return true if collides_with_walls? || collides_with_blocks? false end |
#collides_with_blocks? ⇒ Boolean
checks whether the shape collides with any blocks
116 117 118 119 120 121 122 |
# File 'lib/shape.rb', line 116 def collides_with_blocks? get_blocks.each do |block| return true if block.collides_with_blocks? end false end |
#collides_with_walls? ⇒ Boolean
checks whether the shape collides with walls (and floor)
107 108 109 110 111 112 113 |
# File 'lib/shape.rb', line 107 def collides_with_walls? max_y = maximum_y_block min_x = minimum_x_block max_x = maximum_x_block min_x.x < 0 || max_x.x >= @game.screen_width || max_y.y + Block.height > @game.screen_height end |
#draw ⇒ Object
150 151 152 |
# File 'lib/shape.rb', line 150 def draw get_blocks.each(&:draw) end |
#falling? ⇒ Boolean
154 155 156 |
# File 'lib/shape.rb', line 154 def falling? @falling end |
#maximum_x_block ⇒ Object
gets the rightmost block of the shape
136 137 138 |
# File 'lib/shape.rb', line 136 def maximum_x_block get_blocks.max_by(&:x) end |
#maximum_y_block ⇒ Object
gets the maximum y block of the shape note that maximum y means the lowest block!
126 127 128 |
# File 'lib/shape.rb', line 126 def maximum_y_block get_blocks.max_by(&:y) end |
#minimum_x_block ⇒ Object
gets the lefmost block of the shape
131 132 133 |
# File 'lib/shape.rb', line 131 def minimum_x_block get_blocks.min_by(&:x) end |
#move_x ⇒ Object
Move the shape on the x axis, if there is the need to
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/shape.rb', line 38 def move_x return unless update_move_x old_x = @x if @game.(Gosu::KbLeft) @x -= 32 elsif @game.(Gosu::KbRight) @x += 32 end @x = old_x if collides? end |
#move_y ⇒ Object
Move the shape on the y axis – movement down
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/shape.rb', line 53 def move_y return unless update_move_y old_y = @y @y += 32 return unless collides? @y = old_y @falling = false end |
#rotate ⇒ Object
rotates the shape
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/shape.rb', line 66 def rotate return if @rotation_block.nil? (1..@rotation % @rotations).each do @blocks.each do |block| old_x = block.x old_y = block.y block.x = @rotation_block.x + (@rotation_block.y - old_y) block.y = @rotation_block.y - (@rotation_block.x - old_x) end end end |
#translate_by_y ⇒ Object
flips the shape by y axis
141 142 143 144 145 146 147 148 |
# File 'lib/shape.rb', line 141 def translate_by_y min = @blocks.min_by(&:x) max = @blocks.max_by(&:x) center = (min.x + max.x) / 2.0 @blocks.each do |block| block.x = 2 * center - block.x - Block.width end end |
#update ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/shape.rb', line 27 def update return unless @falling # first handle x change move_x # then handle y change move_y end |
#update_move_x ⇒ Object
updates move_x counter returns nil if there is no need to update it
81 82 83 84 85 |
# File 'lib/shape.rb', line 81 def update_move_x return unless @game.elapsed_seconds > @game.last_move_x + 0.05 @game.last_move_x = @game.elapsed_seconds end |
#update_move_y ⇒ Object
updates move_y counter, checks for speed up
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/shape.rb', line 88 def update_move_y if @game.(Gosu::KbDown) @game.game_speed *= 0.2 else @game.update_score end return unless @game.elapsed_seconds > @game.last_move_y + @game.game_speed @game.last_move_y = @game.elapsed_seconds end |