samples/04_physics_and_collisions/04_box_collision/app/main.rb
def calc_world_lookup

    # If the tile size isn't equal to the previous tile size,
    # the previous tile size is set to the tile size,
    # and world_lookup hash is set to empty.
    if state.tile_size != state.previous_tile_size
      state.previous_tile_size = state.tile_size
      state.world_lookup = {} # empty hash
    end

    # return if the world_lookup hash has keys (or, in other words, is not empty)
    # return unless the world collection has values inside of it (or is not empty)
    return if state.world_lookup.keys.length > 0
    return unless state.world.length > 0

    # Starts with an empty hash for world_lookup.
    # Searches through the world and finds the coordinates that exist.
    state.world_lookup = {}
    state.world.each { |x, y| state.world_lookup[[x, y]] = true }

    # Assigns world_collision_rects for every sprite drawn.
    state.world_collision_rects =
      state.world_lookup
          .keys
          .map do |coord_x, coord_y|
            s = state.tile_size
            # multiply by tile size so the grid coordinates; sets pixel value
            # don't forget that position is denoted by bottom left corner
            # set x = coord_x or y = coord_y and see what happens!
            x = s * coord_x
            y = s * coord_y
            {
              # The values added to x, y, and s position the world_collision_rects so they all appear
              # stacked (on top of world rects) but don't directly overlap.
              # Remove these added values and mess around with the rect placement!
              args:       [coord_x, coord_y],
              left_right: [x,     y + 4, s,     s - 6], # hash keys and values
              top:        [x + 4, y + 6, s - 8, s - 6],
              bottom:     [x + 1, y - 1, s - 2, s - 8],
            }
          end
  end
# Performs calculations on world_lookup and sets values.
Performs calculations on world_lookup and sets values.
Performs calculations on world_lookup and sets values .
def calc_world_lookup
    if state.tile_size != state.previous_tile_size
      state.previous_tile_size = state.tile_size
      state.world_lookup = {} 
    end
    return if state.world_lookup.keys.length > 0
    return unless state.world.length > 0
    state.world_lookup = {}
    state.world.each { |x, y| state.world_lookup[[x, y]] = true }
    state.world_collision_rects =
      state.world_lookup
          .keys
          .map do |coord_x, coord_y|
            s = state.tile_size
            x = s * coord_x
            y = s * coord_y
            {
              args:       [coord_x, coord_y],
              left_right: [x,     y + 4, s,     s - 6], 
              top:        [x + 4, y + 6, s - 8, s - 6],
              bottom:     [x + 1, y - 1, s - 2, s - 8],
            }
          end
  end
def calc_world_lookup if state . tile_size != state . previous_tile_size state . previous_tile_size = state . tile_size state . world_lookup = { } end return if state . world_lookup . keys . length > 0 return unless state . world . length > 0 state . world_lookup = { } state . world . each { | x , y | state . world_lookup [ [ x , y ] ] = true } state . world_collision_rects = state . world_lookup . keys . map do | coord_x , coord_y | s = state . tile_size x = s * coord_x y = s * coord_y { args : [ coord_x , coord_y ] , left_right : [ x , y + 4 , s , s - 6 ] , top : [ x + 4 , y + 6 , s - 8 , s - 6 ] , bottom : [ x + 1 , y - 1 , s - 2 , s - 8 ] , } end end
Performs calculations on world_lookup and sets values.
Performs calculations on world_lookup and sets values .
# If the tile size isn't equal to the previous tile size, # the previous tile size is set to the tile size, # and world_lookup hash is set to empty. # empty hash # return if the world_lookup hash has keys (or, in other words, is not empty) # return unless the world collection has values inside of it (or is not empty) # Starts with an empty hash for world_lookup. # Searches through the world and finds the coordinates that exist. # Assigns world_collision_rects for every sprite drawn. # multiply by tile size so the grid coordinates; sets pixel value # don't forget that position is denoted by bottom left corner # set x = coord_x or y = coord_y and see what happens! # The values added to x, y, and s position the world_collision_rects so they all appear # stacked (on top of world rects) but don't directly overlap. # Remove these added values and mess around with the rect placement! # hash keys and values