Skip to content

Commit

Permalink
Click to grab. Click to release. So simple. It solves all of my probl…
Browse files Browse the repository at this point in the history
…ems, never mind the future.
  • Loading branch information
chrisl8 committed Mar 25, 2024
1 parent 289614b commit e5e6a48
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 30 deletions.
24 changes: 12 additions & 12 deletions map/map_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -703,34 +703,34 @@ func highlight_cell_at_global_position(
at_position: Vector2,
color: Color = Color.GREEN,
canvas_entry_name: String = "default",
offsetX: int = 8,
offsetY: int = 8,
sizeX: int = 16,
sizeY: int = 16
offset_x: int = 8,
offset_y: int = 8,
size_x: int = 16,
size_y: int = 16
) -> void:
var drawing_canvas: Node2D = _get_drawing_canvas(canvas_entry_name)
var at_cell_position: Vector2i = get_cell_position_at_global_position(at_position)
var cell_global_position: Vector2 = get_global_position_at_map_local_position(at_cell_position)
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - offsetX, cell_global_position.y - offsetY, sizeX, sizeY)] = {
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - offset_x, cell_global_position.y - offset_y, size_x, size_y)] = {
}
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - offsetX, cell_global_position.y - offsetY, sizeX, sizeY)].color = color
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - offset_x, cell_global_position.y - offset_y, size_x, size_y)].color = color
drawing_canvas.update_draw = true


func highlight_cell_at_map_position(
at_cell_position: Vector2i,
color: Color = Color.GREEN,
canvas_entry_name: String = "default",
offsetX: int = 8,
offsetY: int = 8,
sizeX: int = 16,
sizeY: int = 16
offset_x: int = 8,
offset_y: int = 8,
size_x: int = 16,
size_y: int = 16
) -> void:
var drawing_canvas: Node2D = _get_drawing_canvas(canvas_entry_name)
var cell_global_position: Vector2 = get_global_position_at_map_local_position(at_cell_position)
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - offsetX, cell_global_position.y - offsetY, sizeX, sizeY)] = {
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - offset_x, cell_global_position.y - offset_y, size_x, size_y)] = {
}
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - offsetX, cell_global_position.y - offsetY, sizeX, sizeY)].color = color
drawing_canvas.rectangles_to_draw[Rect2(cell_global_position.x - offset_x, cell_global_position.y - offset_y, size_x, size_y)].color = color
drawing_canvas.update_draw = true


Expand Down
2 changes: 1 addition & 1 deletion player/player_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func spawn_item() -> void:
var id: int = rng.randi()
var thing_name_to_spawn: String = str(player_spawnable_items[player_spawn_item_next], "-", id)
$"Interaction Controller".spawn_player_controlled_thing.rpc(
Vector2.ZERO, 0, thing_name_to_spawn, "Placing"
Vector2.ZERO, 0, thing_name_to_spawn, "Building"
)


Expand Down
29 changes: 12 additions & 17 deletions player/player_interaction_controller.gd
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var ball: Resource = preload("res://items/disc/disc.tscn")
var box: Resource = preload("res://items/square/square.tscn")
var soup_machine: Resource = preload("res://items/soup_machine/soup_machine.tscn")
var controlled_item: RigidBody2D
var controlled_item_type: String = "Held"
var controlled_item_type: String = "Holding"
var controlled_item_clear_of_collisions: bool = false


Expand Down Expand Up @@ -96,21 +96,14 @@ func _process(delta: float) -> void:

# 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!

if controlled_item_type == "Held":
if left_hand_tool_is_active:
controlled_item.set_position(to_local(mouse_position))
elif is_multiplayer_authority():
var held_item_name: String = controlled_item.name
var held_item_global_position: Vector2 = controlled_item.global_position
Spawner.place_thing.rpc_id(1, held_item_name, held_item_global_position)
_drop_held_thing.rpc()
elif controlled_item_type == "Placing":
if controlled_item_type == "Building" || controlled_item_type == "Holding":
if (
left_hand_tool_is_active
and is_multiplayer_authority()
and controlled_item_clear_of_collisions
):
Globals.player_has_done.built_an_item = true
if controlled_item_type == "Building":
Globals.player_has_done.built_an_item = true
var held_item_name: String = controlled_item.name
var held_item_global_position: Vector2 = controlled_item.global_position
Spawner.place_thing.rpc_id(1, held_item_name, held_item_global_position)
Expand Down Expand Up @@ -148,7 +141,7 @@ func _drop_held_thing() -> void:

@rpc("call_local")
func de_spawn_placing_item() -> void:
if controlled_item and controlled_item_type == "Placing":
if controlled_item and controlled_item_type == "Building":
# Don't allow us to de-spawn held items this way.
Globals.world_map.delete_drawing_canvas(controlled_item.name)
controlled_item.queue_free()
Expand Down Expand Up @@ -241,7 +234,7 @@ func tool_raycast() -> void:
# So just document it well and reorganize it later if it becomes more clear how to better organize it
# without also duplicating a lot of code.

# Note that the "placing" and/or "dropping" of build/held items is done in the _process() function AFTER this
# Note that the "Building" and/or "dropping" of build/held items is done in the _process() function AFTER this
# 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.
Expand All @@ -254,6 +247,7 @@ func tool_raycast() -> void:
var body: Node = result["collider"]
if body.has_method("grab"): # The RigidBody must have a "grab" method to be able to be picked up.
body.grab.rpc_id(1)
mouse_left_down = false # Reset now so it does not immediately drop.

# This is always set, even if we don't use it.
mining_particle_distance = (
Expand All @@ -273,6 +267,7 @@ func right_mouse_clicked() -> void:
# 1. The distance at which you can place is only restricted by the size and resolution of your screen.
# 2. You can place blocks in areas that you cannot access (no ray trace check)
# This will probably be addressed when we update the right mouse button to allow tool swapping and make it more like left click.

Globals.world_map.place_cell_at_position(get_global_mouse_position())


Expand All @@ -284,16 +279,16 @@ func spawn_player_controlled_thing(
thing_position: Vector2,
thing_rotation: float,
controlled_item_name: String,
spawned_item_type: String = "Held"
spawned_item_type: String = "Holding"
) -> void:
if controlled_item:
# We can never control a new thing if we are already controlling something
# Whoever called this should have called de_spawn_placing_item() first if they were serious
return
var parsed_thing_name: Dictionary = Helpers.parse_thing_name(controlled_item_name)
var action: String = "picked up"
if spawned_item_type == "Placing":
action = "being placed"
if spawned_item_type == "Building":
action = "being built"
Helpers.log_print(
str(parsed_thing_name.name, " ", parsed_thing_name.id, " ", action, " by ", name),
"Cornflowerblue"
Expand All @@ -315,7 +310,7 @@ func spawn_player_controlled_thing(
controlled_item_type = spawned_item_type
controlled_item.name = controlled_item_name

if spawned_item_type == "Held":
if spawned_item_type == "Holding":
controlled_item.set_spawn_location(thing_position, thing_rotation)

controlled_item.global_position = thing_position
Expand Down

0 comments on commit e5e6a48

Please sign in to comment.