Skip to content

Instantly share code, notes, and snippets.

@colinbellino
Last active September 16, 2022 10:31
Show Gist options
  • Save colinbellino/710be695f010c18bc9c70324335a2805 to your computer and use it in GitHub Desktop.
Save colinbellino/710be695f010c18bc9c70324335a2805 to your computer and use it in GitHub Desktop.
func move_cursor(direction: Vector3):
var gridmap := Globals.gridmap_node # Specific to my codebase, ignore
var cursor_position := Globals.state.cursor_position # Specific to my codebase, ignore
var next_position := cursor_position
var bound_max_x := 10
var bound_min_x := 0
var bound_max_z := 10
var bound_min_z := 0
while true:
next_position += direction
print("next_position: ", [next_position])
# Handle looping on the other side
# I'm pretty sure there is a way to do this without having to branch here, but i wrote this at 2am so... :D
# Edit: i made a version without branching that should be way faster, but might be harder to read maybe? (see other move_cursor_no_branching.gd)
if next_position.x >= bound_max_x:
next_position.x = fmod(cursor_position.x + 1, bound_max_x)
if next_position.x < bound_min_x:
next_position.x = fmod(cursor_position.x + bound_max_x - 1, bound_max_x)
if next_position.z >= bound_max_z:
next_position.z = fmod(cursor_position.z + 1, bound_max_z)
if next_position.z < bound_min_z:
next_position.z = fmod(cursor_position.z + bound_max_z - 1, bound_max_z)
var cell := gridmap.get_cell_item(next_position.x, next_position.y, next_position.z)
if cell > -1:
# we found it
break
# TODO: We could maybe break after gridmap.get_used_cells().size() tries if we wanted to, just to avoid infinite loops, but i don't think this can happen
# Uncomment this to debug, just to avoid infinite loops
# yield(get_tree(), "idle_frame")
Globals.state.cursor_position = next_position # Specific to my codebase, ignore
get_node("%Cursor").transform.origin = next_position # Specific to my codebase, ignore
func move_cursor(direction: Vector3):
var gridmap := Globals.gridmap_node # Specific to my codebase, ignore
var cursor_position := Globals.state.cursor_position # Specific to my codebase, ignore
var next_position := cursor_position
var bound_max_x := 10
var bound_max_z := 10
while true:
# This code is assuming our map doesn't have negative coordinates (starts at 0,0,0)
# Basically we add direction.x and if we go out of bounds_max, we loop around using modulo (fmod here)
next_position = Vector3(
fmod(bound_max_x + fmod(next_position.x + direction.x, bound_max_x), bound_max_x),
next_position.y,
fmod(bound_max_z + fmod(next_position.z + direction.z, bound_max_z), bound_max_z)
)
print("next_position: ", [next_position])
var cell := gridmap.get_cell_item(int(next_position.x), int(next_position.y), int(next_position.z))
if cell > -1:
# we found it
break
# Uncomment this to debug, in case we are never breaking out of the loop
# yield(get_tree(), "idle_frame")
Globals.state.cursor_position = next_position # Specific to my codebase, ignore
get_node("%Cursor").transform.origin = next_position # Specific to my codebase, ignore
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment