From b84c1781c11a48f38de73367d2c23f27b09cb6e0 Mon Sep 17 00:00:00 2001 From: Matteo Balugani Date: Sun, 1 Oct 2023 14:43:52 +0200 Subject: [PATCH] valuable and evaluator works (enough) --- collector/collector.tscn | 9 +-------- entity/coin_copper.tscn | 6 +++++- root.tscn | 2 +- value/evaluator.gd | 33 ++++++++++++++++++++++----------- value/evaluator.tscn | 7 +++---- value/valuable.gd | 13 ++++++++----- 6 files changed, 40 insertions(+), 30 deletions(-) diff --git a/collector/collector.tscn b/collector/collector.tscn index 85a12d3..0273735 100644 --- a/collector/collector.tscn +++ b/collector/collector.tscn @@ -1,15 +1,8 @@ -[gd_scene load_steps=3 format=3 uid="uid://c5w3b55aiui6c"] +[gd_scene load_steps=2 format=3 uid="uid://c5w3b55aiui6c"] [ext_resource type="Script" path="res://collector/collector.gd" id="1_1xtt5"] -[sub_resource type="RectangleShape2D" id="RectangleShape2D_vcox2"] -size = Vector2(113, 65) - [node name="Collector" type="Area2D"] script = ExtResource("1_1xtt5") -[node name="CollisionShape2D" type="CollisionShape2D" parent="."] -position = Vector2(87.5, 48.5) -shape = SubResource("RectangleShape2D_vcox2") - [connection signal="body_entered" from="." to="." method="_on_body_entered"] diff --git a/entity/coin_copper.tscn b/entity/coin_copper.tscn index c3538a8..20fb5ea 100644 --- a/entity/coin_copper.tscn +++ b/entity/coin_copper.tscn @@ -1,9 +1,10 @@ -[gd_scene load_steps=6 format=3 uid="uid://c3kitncwpi42j"] +[gd_scene load_steps=7 format=3 uid="uid://c3kitncwpi42j"] [ext_resource type="PhysicsMaterial" uid="uid://c6kn1an85lccr" path="res://entity/coin_physics_material.tres" id="1_8k46m"] [ext_resource type="Texture2D" uid="uid://dbdkb4vt7dh85" path="res://entity/coin_copper_4.png" id="1_wedw1"] [ext_resource type="Texture2D" uid="uid://2vtvoj6ua3cb" path="res://entity/coin_copper_outline_2.png" id="2_2ifq3"] [ext_resource type="PackedScene" uid="uid://bk1vvq5rug01m" path="res://collector/collectible.tscn" id="4_yefrx"] +[ext_resource type="PackedScene" uid="uid://ujpra0s1kpqi" path="res://value/valuable.tscn" id="5_jdvvd"] [sub_resource type="RectangleShape2D" id="RectangleShape2D_c6byl"] size = Vector2(16, 5) @@ -32,3 +33,6 @@ texture = ExtResource("2_2ifq3") [node name="Collectible" parent="." instance=ExtResource("4_yefrx")] type = &"Copper" + +[node name="Valuable" parent="." instance=ExtResource("5_jdvvd")] +value = 1 diff --git a/root.tscn b/root.tscn index 139e6aa..4f716d2 100644 --- a/root.tscn +++ b/root.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=6 format=3 uid="uid://cbccs6kwwf265"] +[gd_scene load_steps=9 format=3 uid="uid://cbccs6kwwf265"] [ext_resource type="Script" path="res://root.gd" id="1_8jrhk"] [ext_resource type="Theme" uid="uid://ba5utvfhnxa5i" path="res://interface/interface_theme.tres" id="1_h26ax"] diff --git a/value/evaluator.gd b/value/evaluator.gd index a16124f..3a2cd07 100644 --- a/value/evaluator.gd +++ b/value/evaluator.gd @@ -9,22 +9,33 @@ var total_value: int = 0 ## The types of [Collectible]s to value. ## ## The strings will match only if exactly the same. -@export var collecting_types: Array[StringName] +#@export var collecting_types: Array[StringName] ## The collision mask to check colliding body against. @export_flags_2d_physics var collecting_collision_mask: int - -## The evaluator has added the value of an object. +## The evaluator has added the value of an object to the total. signal added(what: PhysicsBody2D) - -## The evaluator has removed the value of an object. +## The evaluator has removed the value of an object to the total. signal removed(what: PhysicsBody2D) - -func _on_body_entered(body: Node2D): - pass - - - +func _on_body_entered(body): + if body is PhysicsBody2D: + if body.collision_layer & collecting_collision_mask: + var evaluable: Valuable = body.get_node("Valuable") + print("sommato") + total_value += evaluable.value + evaluable.evaluate() + added.emit(body) + print("totale= "+str(total_value)) + +func _on_body_exited(body): + if body is PhysicsBody2D: + if body.collision_layer & collecting_collision_mask: + var evaluable: Valuable = body.get_node("Valuable") + print("sottratto") + total_value -= evaluable.value + evaluable.evaluate() + added.emit(body) + print("totale= "+str(total_value)) diff --git a/value/evaluator.tscn b/value/evaluator.tscn index 5016a5e..e797c93 100644 --- a/value/evaluator.tscn +++ b/value/evaluator.tscn @@ -2,9 +2,8 @@ [ext_resource type="Script" path="res://value/evaluator.gd" id="1_sjx78"] -[node name="Evaluator" type="Node2D"] +[node name="Evaluator" type="Area2D"] script = ExtResource("1_sjx78") -[node name="Area2D" type="Area2D" parent="."] - -[connection signal="body_entered" from="Area2D" to="." method="_on_area_2d_body_entered"] +[connection signal="body_entered" from="." to="." method="_on_body_entered"] +[connection signal="body_exited" from="." to="." method="_on_body_exited"] diff --git a/value/valuable.gd b/value/valuable.gd index a5e8162..ef50710 100644 --- a/value/valuable.gd +++ b/value/valuable.gd @@ -3,8 +3,11 @@ class_name Valuable @export var value: int -#signal object_value -# -# -#func collect(): -# emit_signal("collected") +## Emitted when this entity has been counted. +signal evaluated + +## Mark this entity as evaluated. +## +func evaluate(): + emit_signal("evaluated") +