From 0e1dd1b2f20fcada2f1c29c1cac6d2d14b312eaf Mon Sep 17 00:00:00 2001 From: teatov Date: Sun, 6 Oct 2024 20:00:14 +1000 Subject: [PATCH] create `Gathering` component for picking up honeydew --- scenes/units/ant_nitwit.tscn | 5 ++- scenes/units/components/gathering.tscn | 13 ++++++++ scripts/globals/cursor_manager.gd | 5 ++- scripts/globals/hovering_manager.gd | 3 ++ scripts/units/components/gathering.gd | 42 ++++++++++++++++++++++++++ 5 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 scenes/units/components/gathering.tscn create mode 100644 scripts/units/components/gathering.gd diff --git a/scenes/units/ant_nitwit.tscn b/scenes/units/ant_nitwit.tscn index 73a6aca..c0fb8fa 100644 --- a/scenes/units/ant_nitwit.tscn +++ b/scenes/units/ant_nitwit.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=12 format=3 uid="uid://k11usj2shbtr"] +[gd_scene load_steps=13 format=3 uid="uid://k11usj2shbtr"] [ext_resource type="PackedScene" uid="uid://bi231xk2sp410" path="res://assets/models/ant.glb" id="1_1u65s"] [ext_resource type="Script" path="res://scripts/units/ant_nitwit.gd" id="2_f1bdv"] [ext_resource type="Texture2D" uid="uid://dehqm00kiljut" path="res://assets/textures/selection_unit_decal.png" id="3_hxm2m"] [ext_resource type="Texture2D" uid="uid://duf132faskeid" path="res://assets/textures/selection_unit_hover_decal.png" id="4_vrkmw"] +[ext_resource type="PackedScene" uid="uid://fal1ond30jey" path="res://scenes/units/components/gathering.tscn" id="5_j8w6w"] [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_0iwx2"] albedo_color = Color(0.344076, 0.344076, 0.344076, 1) @@ -74,3 +75,5 @@ neighbor_distance = 10.0 [node name="VisibleOnScreenNotifier3D" type="VisibleOnScreenNotifier3D" parent="." index="7"] aabb = AABB(-0.5, 0, -0.5, 1, 0.5, 1) + +[node name="Gathering" parent="." index="8" instance=ExtResource("5_j8w6w")] diff --git a/scenes/units/components/gathering.tscn b/scenes/units/components/gathering.tscn new file mode 100644 index 0000000..a0df213 --- /dev/null +++ b/scenes/units/components/gathering.tscn @@ -0,0 +1,13 @@ +[gd_scene load_steps=3 format=3 uid="uid://fal1ond30jey"] + +[ext_resource type="Script" path="res://scripts/units/components/gathering.gd" id="1_ciwtf"] + +[sub_resource type="SphereShape3D" id="SphereShape3D_eqrj3"] +radius = 15.0 + +[node name="Gathering" type="Area3D"] +input_ray_pickable = false +script = ExtResource("1_ciwtf") + +[node name="NearbyItemsSearch" type="CollisionShape3D" parent="."] +shape = SubResource("SphereShape3D_eqrj3") diff --git a/scripts/globals/cursor_manager.gd b/scripts/globals/cursor_manager.gd index ff7873b..b830238 100644 --- a/scripts/globals/cursor_manager.gd +++ b/scripts/globals/cursor_manager.gd @@ -14,6 +14,8 @@ func _ready() -> void: func _input(event: InputEvent) -> void: if event is InputEventMouseButton: + if disable_confinement: + return var button_event := event as InputEventMouseButton if ( button_event.button_index == MOUSE_BUTTON_LEFT @@ -28,9 +30,10 @@ func _input(event: InputEvent) -> void: disable_confinement = not disable_confinement if disable_confinement: Input.mouse_mode = Input.MOUSE_MODE_VISIBLE + Input.set_custom_mouse_cursor(null) else: Input.mouse_mode = Input.MOUSE_MODE_CONFINED - + _set_cursor(cursor_normal) func _set_cursor(image: Resource) -> void: diff --git a/scripts/globals/hovering_manager.gd b/scripts/globals/hovering_manager.gd index a53526b..2851358 100644 --- a/scripts/globals/hovering_manager.gd +++ b/scripts/globals/hovering_manager.gd @@ -7,6 +7,9 @@ var mouse_pos: Vector2 = Vector2.ZERO func _physics_process(_delta: float) -> void: + if SelectionManager.selecting: + return + var space_state := get_world_3d().direct_space_state var from := camera.project_ray_origin(mouse_pos) var to := from + camera.project_ray_normal(mouse_pos) * (camera.far - 1) diff --git a/scripts/units/components/gathering.gd b/scripts/units/components/gathering.gd new file mode 100644 index 0000000..72b7e10 --- /dev/null +++ b/scripts/units/components/gathering.gd @@ -0,0 +1,42 @@ +extends Area3D +class_name Gathering + +enum GatherState { + PICKING_UP, + DEPOSITING, +} + +var items: Dictionary = {} +var max_carrying: int = 3 +var carrying: int = 0 +var state: GatherState = GatherState.PICKING_UP +var target: Honeydew + + +func _ready() -> void: + body_entered.connect(_on_body_entered) + body_exited.connect(_on_body_exited) + + +func gather(item: Honeydew)->void: + target = item + state = GatherState.PICKING_UP + + +func _on_body_entered(item: Node3D) -> void: + if item is not Honeydew: + return + + var item_id := item.get_instance_id() + if items.keys().has(item_id): + return + + items[item_id] = item + + +func _on_body_exited(item: Node3D) -> void: + var item_id := item.get_instance_id() + if not items.keys().has(item_id): + return + + items.erase(item_id)