Skip to content

Commit

Permalink
Constrain items with snap true to move within tile sized increments w…
Browse files Browse the repository at this point in the history
…hen placing or moving
  • Loading branch information
chrisl8 committed Mar 25, 2024
1 parent e5e6a48 commit ad3a94c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions global_variables.gd
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class MapTileSet:
var all_tiles_are_empty: bool = false
var tile_list: Array[Vector2i] = []
var tile_content: Dictionary = {}
var cell_aligned_center_position: Vector2


func get_resource_name(id: int) -> String:
Expand Down
18 changes: 17 additions & 1 deletion map/map_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,9 @@ func check_tile_location_and_surroundings(
width_in_tiles: int = 1,
highlight_name: String = ""
) -> Globals.MapTileSet:
var half_tile_offset_position: Vector2i = Vector2i(at_position.x + 8, at_position.y + 8)
var half_tile_offset_position: Vector2i = Vector2i(
at_position.x + (single_tile_width / 2), at_position.y + (single_tile_width / 2)
)
var cell_position_at_position: Vector2i = (
Globals.world_map.get_cell_position_at_global_position(half_tile_offset_position)
)
Expand All @@ -925,7 +927,20 @@ func check_tile_location_and_surroundings(

if highlight_name != "":
Globals.world_map.erase_drawing_canvas(highlight_name)
var cell_aligned_center_position: Vector2
for cell_position: Vector2i in return_data.tile_list:
if cell_aligned_center_position.is_zero_approx():
cell_aligned_center_position = get_global_position_at_map_local_position(cell_position)
cell_aligned_center_position = Vector2(
(
(cell_aligned_center_position.x - single_tile_width / 2)
+ width_in_tiles / 2 * single_tile_width
),
(
(cell_aligned_center_position.y - single_tile_width / 2)
+ height_in_tiles / 2 * single_tile_width
)
)
return_data.tile_content[cell_position] = (
Globals.world_map.get_cell_id_at_map_tile_position(cell_position)
)
Expand All @@ -938,4 +953,5 @@ func check_tile_location_and_surroundings(
cell_position, highlight_color, highlight_name
)

return_data.cell_aligned_center_position = cell_aligned_center_position
return return_data
14 changes: 9 additions & 5 deletions player/player_interaction_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ func _process(delta: float) -> void:
mining_particles.emitting = (left_hand_tool_is_active and left_hand_tool == Globals.Tools.MINE)

if controlled_item:
# Picked Up items drop when you release the mouse button because they "exist" already.
# Build items are placed when you click the left mouse button because they did not previously "exist".

# Note that mining and picking up things happens in the tool_raycast() function that was called earlier._add_constant_central_force
# Note that mining and picking up things happens in the tool_raycast() function that was called earlier.

# Note that by using the mouse position, with no restrictions, for placing items, your "place distance" is limited only by your screen size and resolution!

Expand All @@ -110,7 +107,6 @@ func _process(delta: float) -> void:
_drop_held_thing.rpc()
Globals.world_map.delete_drawing_canvas(held_item_name)
else:
controlled_item.set_position(to_local(mouse_position))
var intersecting_tiles: Globals.MapTileSet = (
Globals
. world_map
Expand All @@ -121,6 +117,12 @@ func _process(delta: float) -> void:
controlled_item.name
)
)
if controlled_item.snaps:
controlled_item.set_position(
to_local(intersecting_tiles.cell_aligned_center_position)
)
else:
controlled_item.set_position(to_local(mouse_position))
controlled_item_clear_of_collisions = (intersecting_tiles.all_tiles_are_empty)


Expand Down Expand Up @@ -238,6 +240,8 @@ func tool_raycast() -> void:
# function is called.
# This can be hard to keep in mind, but the reason is that mining and picking up depend on the raycast position,
# while dropping and placing only depend on the mouse position.
# Although, it could make sense to change that and raycast to the position where a thing could be placed, which would
# make it harder to place objects in terrain, and on the other side of walls.

if left_hand_tool == Globals.Tools.MINE:
if result["collider"] is TileMap and not controlled_item: # Do not mine while holding items, no matter what
Expand Down

0 comments on commit ad3a94c

Please sign in to comment.