-
Notifications
You must be signed in to change notification settings - Fork 0
/
slidingblock.rb
47 lines (44 loc) · 1.51 KB
/
slidingblock.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
require_relative 'block'
class SlidingBlock < Block
def initialize(relative_position,
direction = :horizontal,
length = 4, speed = 0.05,
movement_direction = :forward)
super relative_position
@direction = direction
@length = length.u
@current_step = @length / 2
@speed = speed.u
@start_step = @current_step
@movement_direction = movement_direction
@start_position = @relative_position
@moving = true
@color = Gosu::Color::RED
end
# Public: make new object that is equal to self.
#
# Returns this new object.
def clone
SlidingBlock.new(@relative_position, @direction, @length, @speed, @movement_direction)
end
def update(blocks_array = [], old_blocks_array = [])
@current_step += @speed if @movement_direction == :forward
@current_step -= @speed if @movement_direction == :backward
if @current_step > @length
@current_step -= (@current_step % @length)
@movement_direction = :backward
elsif @current_step < 0.0
@current_step = -@current_step
@movement_direction = :forward
end
if @direction == :horizontal
@relative_position = [@start_position.x + \
@current_step - @start_step,
@start_position.y].to_vec2d
elsif @direction == :vertical
@relative_position = [@start_position.x,
@start_position.y + \
@current_step - @start_step].to_vec2d
end
end
end