Skip to content

Instantly share code, notes, and snippets.

@mgrider
Last active February 25, 2025 14:54
Show Gist options
  • Save mgrider/e8191e4b74d130547dc830b67a937a37 to your computer and use it in GitHub Desktop.
Save mgrider/e8191e4b74d130547dc830b67a937a37 to your computer and use it in GitHub Desktop.
## returns an array of tetris column coordinates "just beneath" the current tetromino
func coordinates_below_current_mino() -> Array[Vector2i]:
# this is a dictionary, we'll use it to find just one coordinate in each column
var lowest_by_column :Dictionary = {}
# here we are looping through all 4 of the `Vector2i` values in the position of the currently falling block
# each one will be stored in the `block` variable every time through the loop - worth noting these are just
# offsets, so values like (0,1), etc.
for block: Vector2i in current_tetromino_array[rotation_index]:
# current_position is a Vector2i the player is manipulating (as well as "gravity") to move the block around the board
# Vector2i.DOWN is just (0,1)
# So this `coord` Vector2i holds a position on the board "below" the current block in our loop
var coord: Vector2i = current_position + block + Vector2i.DOWN
# if the coords x position exists in our dictionary, AND it's y is greater than the one we have stored
# overwrite it (so there is only one per column in the dictionary, and it's the lowest one)
if lowest_by_column.has(coord.x) and coord.y > lowest_by_column[coord.x].y:
lowest_by_column[coord.x] = coord
# otherwise, write it to the dictionary... but wait, this is a bug!
# we should only get here `if not lowest_by_column.has(coord.x)`
# so `if coord.y > lowest_by_column[coord.x].y` should be INSIDE the if above, and not
# part of the same condition! D'oh!
else:
lowest_by_column[coord.x] = coord
# the rest of this function is just turning our dictionary's values() array into an `Array[Vector2i]`
# this is only necessary because GodotScript's isn't very good at being strongly typed (And dictionaries don't have types yet)
var ret: Array[Vector2i] = []
for val: Vector2i in lowest_by_column.values():
ret.append(val)
# finally, return our value
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment