Skip to content

Commit

Permalink
refactors block move in prep for ice sliding
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Swieboda committed Sep 17, 2019
1 parent 5a0269a commit 21115f3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 22 deletions.
54 changes: 44 additions & 10 deletions src/buzzle/block.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Buzzle
)

@moving_x = @moving_y = 0_f32
@moving_amount = 0

@trigger = Trigger.new(
x: x,
y: y,
Expand All @@ -27,8 +29,8 @@ module Buzzle
screen_x: screen_x,
screen_y: screen_y,
center_y: false,
x: x + @moving_x,
y: y + @moving_y
x: x,
y: y
)
end

Expand All @@ -40,22 +42,54 @@ module Buzzle
true
end

def move(dx, dy, direction : Direction, entities : Array(Entity))
return if lifting?
def update(frame_time, entities : Array(Entity))
super

moving_transition(frame_time, entities) if moving? && !lifting?
end

def move_now(dx, dy, entities : Array(Entity))
@x += dx
@y += dy

unless collision?(entities.select { |e| e.is_a?(Floor) && !e.is_a?(Floors::Pit) })
if !collision?(entities.select { |e| e.is_a?(Floor) && !e.is_a?(Floors::Pit) }) || directional_collision?(entities.select(&.collidable?), Direction.from_delta(dx: dx.sign, dy: dy.sign))
@x -= dx
@y -= dy

return
end
end

if directional_collision?(entities.select(&.collidable?), direction)
@x -= dx
@y -= dy
def move(dx = 0, dy = 0, amount = 2)
return if lifting?

@moving_amount = amount
@moving_x = dx.to_f32 * @moving_amount
@moving_y = dy.to_f32 * @moving_amount
end

def moving?
return false if lifting?

@moving_x.abs > 0 || @moving_y.abs > 0
end

def stop
@moving_x = 0_f32
@moving_y = 0_f32
end

def moving_transition(frame_time, entities)
dx = @moving_x.sign * @moving_amount
dy = @moving_y.sign * @moving_amount

@moving_x += dx
@x += dx

@moving_y += dy
@y += dy

# stop moving at next grid cell
if @moving_x.abs > Game::GRID_SIZE || @moving_y.abs > Game::GRID_SIZE
stop
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions src/buzzle/direction.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Buzzle
Left
Down
Right
None

def opposite?(other_direction)
self.opposite == other_direction
Expand All @@ -24,6 +25,22 @@ module Buzzle
end
end

def self.from_delta(dx, dy)
if dx == 0 && dy == -1
Up
elsif dx == -1 && dy == 0
Left
elsif dx == 0 && dy == 1
Down
elsif dx == 1 && dy == 0
Right
elsif dx == 0 && dy == 0
None
else
None
end
end

def to_delta
case self
when Up
Expand All @@ -34,6 +51,8 @@ module Buzzle
[0, 1]
when Right
[1, 0]
when None
[0, 0]
else
[0, 0]
end
Expand Down
2 changes: 1 addition & 1 deletion src/buzzle/entity.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Buzzle
class Entity < Obj
getter? lifting

def initialize(x : Int32 | Float32 = 0, y : Int32 | Float32 = 0, z = 0, width = 0, height = 0, direction = Direction::Down, hidden = false)
def initialize(x = 0, y = 0, z = 0, width = 0, height = 0, direction = Direction::Down, hidden = false)
super

@lifting = false
Expand Down
15 changes: 4 additions & 11 deletions src/buzzle/player.cr
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,20 @@ module Buzzle

if collision?(entities.select(&.traversable?))
if pushing_block?(dx, dy)
@held_block.try(&.move(dx * Game::GRID_SIZE, dy * Game::GRID_SIZE, direction, entities)) if !@move_block_timer.started?
@held_block.try(&.move_now(dx * Game::GRID_SIZE, dy * Game::GRID_SIZE, entities)) if !@move_block_timer.started?
end

unless directional_collision?(entities.select(&.collidable?), pulling_block?(dx, dy) ? direction.opposite : direction)
move(dx: dx, dy: dy)
@held_block.try(&.move(dx: dx, dy: dy, amount: MOVING_AMOUNT)) if !@move_block_timer.started?
end
end

@x -= dx * Game::GRID_SIZE
@y -= dy * Game::GRID_SIZE

if pushing_block?(dx, dy)
@held_block.try(&.move(-dx * Game::GRID_SIZE, -dy * Game::GRID_SIZE, direction, entities)) if !@move_block_timer.started?
@held_block.try(&.move_now(-dx * Game::GRID_SIZE, -dy * Game::GRID_SIZE, entities)) if !@move_block_timer.started?
end
end
end
Expand Down Expand Up @@ -149,10 +150,7 @@ module Buzzle
dx = @moving_x.sign * MOVING_AMOUNT
dy = @moving_y.sign * MOVING_AMOUNT

if pushing_block?(dx, dy)
# push block
@held_block.try(&.move(dx, dy, direction, entities)) if !@move_block_timer.started?
elsif pulling_block?(dx, dy) && @move_block_timer.started?
if pulling_block?(dx, dy) && @move_block_timer.started?
# stop, and don't pull while move block timer active
stop
return
Expand All @@ -173,11 +171,6 @@ module Buzzle
@moving_y += dy
@y += dy

if pulling_block?(dx, dy)
# pull block
@held_block.try(&.move(dx, dy, direction, entities)) if !@move_block_timer.started?
end

# stop moving at next grid cell
if @moving_x.abs > Game::GRID_SIZE || @moving_y.abs > Game::GRID_SIZE
stop
Expand Down
6 changes: 6 additions & 0 deletions src/buzzle/rooms/entrance.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ module Buzzle::Rooms
end

# block
# @entities << Block.new(5, 5)
@entities << Block.new(5, 7)

block = Block.new(3, 7)
@entities << block
puts block
block.move(dx: 0, dy: -1, amount: 2)

# river
(0..width - 1).each do |x|
@entities << Floors::River.new(x, 3)
Expand Down

0 comments on commit 21115f3

Please sign in to comment.