Class: Brick

Inherits:
Object
  • Object
show all
Defined in:
lib/lib/items/brick.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(brickyard, xx, yy, angle) ⇒ Brick

Returns a new instance of Brick.



14
15
16
17
18
19
20
21
22
23
# File 'lib/lib/items/brick.rb', line 14

def initialize(brickyard, xx, yy, angle)
  dir_path = File.dirname(__FILE__)
  @image = Gosu::Image.new(dir_path + '/../../media/square.png') # TODO
  @brickyard = brickyard

  @x = xx
  @y = yy
  @angle = angle
  start_falling!
end

Instance Attribute Details

#statusObject

Returns the value of attribute status.



12
13
14
# File 'lib/lib/items/brick.rb', line 12

def status
  @status
end

#xObject

Returns the value of attribute x.



12
13
14
# File 'lib/lib/items/brick.rb', line 12

def x
  @x
end

#yObject

Returns the value of attribute y.



12
13
14
# File 'lib/lib/items/brick.rb', line 12

def y
  @y
end

Instance Method Details

#drawObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/lib/items/brick.rb', line 66

def draw
  if horizontal?
    cx = 0.5
    cy = 0 # y: top
  else ## vertical
    cx = 0 # turned x: top
    cy = 0.5
  end

  @image.draw_rot(@x, @y, ZITEMS, @angle,
                  center_x = cx, center_y = cy,
                  scale_x = BRICK_LENGTH, scale_y = BRICK_THICKNESS,
                  color = Gosu::Color::GRAY)

  if $debug
    coords = Gosu::Image.from_text("[#{@x}, #{@y}, #{@angle}°, S#{@status}]", LINE_HEIGHT)
    coords.draw_rot(@x, @y, ZTEXT, @angle)
  end
end

#horizontal?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/lib/items/brick.rb', line 98

def horizontal?
  @angle == 0
end

#rotate!Object



86
87
88
# File 'lib/lib/items/brick.rb', line 86

def rotate!
  @angle = 90 - @angle
end

#start_falling!Object



25
26
27
28
29
# File 'lib/lib/items/brick.rb', line 25

def start_falling!
  @status = BRICK_STATUS_FALLING
  @falling_speed = 0
  @time_of_start_of_fall = Gosu.milliseconds
end

#stop_falling!Object



31
32
33
34
35
# File 'lib/lib/items/brick.rb', line 31

def stop_falling!
  @status = BRICK_STATUS_STANDING
  @falling_speed = nil
  @time_of_start_of_fall = nil
end

#topObject

TODO other sides



90
91
92
93
94
95
96
# File 'lib/lib/items/brick.rb', line 90

def top # TODO other sides
  if vertical?
    return 1 # @y - BRICK_LENGHT / 2
  else # vertical
    return 2 # @y - BRICK_THICKNESS / 2
  end
end

#update(button) ⇒ Object



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
# File 'lib/lib/items/brick.rb', line 37

def update(button)
  # Other states should not be needed here
  # - BRICK_STATUS_STANDING does not need any updates
  # - BRICK_STATUS_BEING_HELD is managed from Hook
  if status == BRICK_STATUS_FALLING
    # Apply falling speed to y-coords (if possible)
    seconds_falling = (Gosu.milliseconds - @time_of_start_of_fall) / 1000.0
    fall_accel = BRICK_FALL_ACCELERATION * seconds_falling * seconds_falling
    @falling_speed = (@falling_speed + fall_accel).round(BRICK_FALL_PRECISION)

    # Is there space to fall to? # TODO collide with other bricks too
    if horizontal?
      y_scaling = BRICK_THICKNESS
    else # vertical
      y_scaling = BRICK_LENGTH
    end

    fall_y = (@y + @falling_speed).round(1)
    on_grass_y = WINDOW_HEIGHT - (GRASS_THICKNESS + y_scaling) * TILESIZE

    if fall_y >= on_grass_y # stop falling
      @y = on_grass_y #
      stop_falling!
    else # continue falling
      @y = fall_y
    end
  end
end