From 6d78598711e0d0c4226a2056958330cd8cf0215f Mon Sep 17 00:00:00 2001 From: fram3d Date: Sun, 9 Jun 2024 19:22:49 +0200 Subject: [PATCH] add hitboxes --- src/NPCs/goblin.gd | 34 ++++++++++++++++++++++++++++++++++ src/NPCs/goblin.tscn | 33 ++++++++++++++++++++++++++++++--- src/NPCs/moss.gd | 11 ++++++++++- src/NPCs/moss.tscn | 31 ++++++++++++++++++++++++------- src/NPCs/ooze.gd | 9 +++++++++ src/NPCs/ooze.tscn | 31 +++++++++++++++++++++++-------- src/World.tscn | 10 +++++----- src/project.godot | 2 +- 8 files changed, 136 insertions(+), 25 deletions(-) create mode 100644 src/NPCs/goblin.gd diff --git a/src/NPCs/goblin.gd b/src/NPCs/goblin.gd new file mode 100644 index 0000000..9b25dac --- /dev/null +++ b/src/NPCs/goblin.gd @@ -0,0 +1,34 @@ +extends KinematicBody2D + + +# Declare member variables here. Examples: +# var a = 2 +# var b = "text" + +const MOVEMENT_VECTORS = [ + Vector2.UP, + Vector2.RIGHT, + Vector2.DOWN, + Vector2.LEFT +] + +# Called when the node enters the scene tree for the first time. +func _ready(): + pass # Replace with function body. + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _physics_process(delta): + var movement = MOVEMENT_VECTORS[randi() % 4] + move_and_collide(movement * 2) + pass + + +func _on_Hitbox_area_entered(area): + + pass # Replace with function body. + + +func _on_Hurtbox_area_entered(area): + queue_free() + pass # Replace with function body. diff --git a/src/NPCs/goblin.tscn b/src/NPCs/goblin.tscn index 222b772..758eb98 100644 --- a/src/NPCs/goblin.tscn +++ b/src/NPCs/goblin.tscn @@ -1,12 +1,39 @@ -[gd_scene load_steps=2 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://NPCs/goblin.png" type="Texture" id=1] +[ext_resource path="res://NPCs/goblin.gd" type="Script" id=2] + +[sub_resource type="RectangleShape2D" id=2] +extents = Vector2( 3.90875, 3.7602 ) + +[sub_resource type="RectangleShape2D" id=1] +extents = Vector2( 4.13172, 3.98318 ) + +[sub_resource type="RectangleShape2D" id=3] +extents = Vector2( 4.0574, 3.7602 ) [node name="goblin" type="KinematicBody2D"] +script = ExtResource( 2 ) [node name="goblin" type="Sprite" parent="."] position = Vector2( 0.380203, -0.127379 ) texture = ExtResource( 1 ) -[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."] -polygon = PoolVector2Array( -2.66919, -3.30936, -2.58081, 2.92202, 2.41314, 2.92202, 2.36894, -3.35355 ) +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 2 ) + +[node name="Hitbox" type="Area2D" parent="."] +collision_layer = 32 +collision_mask = 16 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Hitbox"] +shape = SubResource( 1 ) + +[node name="Hurtbox" type="Area2D" parent="."] +collision_layer = 64 +collision_mask = 2 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Hurtbox"] +shape = SubResource( 3 ) +[connection signal="area_entered" from="Hitbox" to="." method="_on_Hitbox_area_entered"] +[connection signal="area_entered" from="Hurtbox" to="." method="_on_Hurtbox_area_entered"] diff --git a/src/NPCs/moss.gd b/src/NPCs/moss.gd index b3fe967..3b1607a 100644 --- a/src/NPCs/moss.gd +++ b/src/NPCs/moss.gd @@ -13,5 +13,14 @@ func _ready(): # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): - pass + + + +func _on_Hitbox_area_entered(area): + pass # Replace with function body. + + +func _on_Hurtbox_area_entered(area): + queue_free() + pass # Replace with function body. diff --git a/src/NPCs/moss.tscn b/src/NPCs/moss.tscn index 60e89a5..b13c569 100644 --- a/src/NPCs/moss.tscn +++ b/src/NPCs/moss.tscn @@ -1,24 +1,41 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://NPCs/moss.png" type="Texture" id=1] [ext_resource path="res://NPCs/moss.gd" type="Script" id=2] +[sub_resource type="RectangleShape2D" id=2] +extents = Vector2( 3.73001, 3.41657 ) + [sub_resource type="RectangleShape2D" id=1] -extents = Vector2( 5.3049, 5.46393 ) +extents = Vector2( 3.96625, 3.8103 ) + +[sub_resource type="RectangleShape2D" id=3] +extents = Vector2( 3.96625, 3.96779 ) [node name="moss" type="Area2D"] script = ExtResource( 2 ) [node name="moss" type="Sprite" parent="."] -position = Vector2( 0.110608, -0.127491 ) +position = Vector2( 0.118118, -0.0393729 ) +scale = Vector2( 1.02953, 0.990157 ) texture = ExtResource( 1 ) -[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."] -polygon = PoolVector2Array( -3.90663, -4.01647, -3.77405, 3.8501, 4.04832, 3.8059, 4.09251, -4.01647 ) +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 2 ) [node name="Hitbox" type="Area2D" parent="."] -collision_layer = 2 -collision_mask = 4 +collision_layer = 2147483650 +collision_mask = 64 [node name="CollisionShape2D" type="CollisionShape2D" parent="Hitbox"] shape = SubResource( 1 ) + +[node name="Hurtbox" type="Area2D" parent="."] +collision_layer = 4 +collision_mask = 8 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Hurtbox"] +shape = SubResource( 3 ) +[connection signal="area_entered" from="Hitbox" to="." method="_on_Hitbox_area_entered"] +[connection signal="area_shape_entered" from="Hitbox" to="." method="_on_Hitbox_area_shape_entered"] +[connection signal="area_entered" from="Hurtbox" to="." method="_on_Hurtbox_area_entered"] diff --git a/src/NPCs/ooze.gd b/src/NPCs/ooze.gd index bcdbe26..a52b762 100644 --- a/src/NPCs/ooze.gd +++ b/src/NPCs/ooze.gd @@ -22,3 +22,12 @@ func _physics_process(delta): var movement = MOVEMENT_VECTORS[randi() % 4] move_and_collide(movement * 2) pass + + +func _on_Hitbox_area_entered(area): + pass # Replace with function body. + + +func _on_Hurtbox_area_entered(area): + queue_free() + pass # Replace with function body. diff --git a/src/NPCs/ooze.tscn b/src/NPCs/ooze.tscn index fa72ac2..24209ea 100644 --- a/src/NPCs/ooze.tscn +++ b/src/NPCs/ooze.tscn @@ -1,10 +1,16 @@ -[gd_scene load_steps=4 format=2] +[gd_scene load_steps=6 format=2] [ext_resource path="res://NPCs/ooze.png" type="Texture" id=1] [ext_resource path="res://NPCs/ooze.gd" type="Script" id=2] [sub_resource type="RectangleShape2D" id=1] -extents = Vector2( 4.91306, 4.99174 ) +extents = Vector2( 4.57893, 3.98952 ) + +[sub_resource type="RectangleShape2D" id=2] +extents = Vector2( 4.69029, 4.54633 ) + +[sub_resource type="RectangleShape2D" id=3] +extents = Vector2( 4.69029, 4.43497 ) [node name="ooze" type="KinematicBody2D"] script = ExtResource( 2 ) @@ -13,12 +19,21 @@ script = ExtResource( 2 ) position = Vector2( -0.061738, 0.18198 ) texture = ExtResource( 1 ) -[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."] -polygon = PoolVector2Array( 4.09251, -4.06066, 4.04832, 4.15946, -4.1276, 4.02687, -4.1718, -4.06066 ) +[node name="CollisionShape2D" type="CollisionShape2D" parent="."] +shape = SubResource( 1 ) [node name="Hitbox" type="Area2D" parent="."] -collision_layer = 4 -collision_mask = 2 +collision_layer = 8 +collision_mask = 4 -[node name="CollisionPolygon2D" type="CollisionShape2D" parent="Hitbox"] -shape = SubResource( 1 ) +[node name="CollisionShape2D" type="CollisionShape2D" parent="Hitbox"] +shape = SubResource( 2 ) + +[node name="Hurtbox" type="Area2D" parent="."] +collision_layer = 16 +collision_mask = 32 + +[node name="CollisionShape2D" type="CollisionShape2D" parent="Hurtbox"] +shape = SubResource( 3 ) +[connection signal="area_entered" from="Hitbox" to="." method="_on_Hitbox_area_entered"] +[connection signal="area_entered" from="Hurtbox" to="." method="_on_Hurtbox_area_entered"] diff --git a/src/World.tscn b/src/World.tscn index 9d9f136..831bb00 100644 --- a/src/World.tscn +++ b/src/World.tscn @@ -143,23 +143,23 @@ cell_custom_transform = Transform2D( 8, 0, 0, 8, 0, 0 ) format = 1 tile_data = PoolIntArray( 0, 1, 0, 1, 1, 0, 2, 1, 0, 3, 1, 0, 4, 1, 0, 5, 1, 0, 6, 1, 0, 7, 1, 0, 8, 1, 0, 9, 1, 0, 65536, 1, 0, 65537, 0, 0, 65538, 0, 0, 65539, 0, 0, 65540, 2, 65537, 65541, 2, 65537, 65542, 2, 65537, 65543, 1, 0, 65544, 1, 0, 65545, 1, 0, 131072, 1, 0, 131073, 0, 0, 131074, 1, 0, 131075, 0, 0, 131076, 0, 0, 131077, 1, 0, 131078, 2, 65537, 131079, 1, 0, 131080, 1, 0, 131081, 1, 0, 196608, 1, 0, 196609, 0, 0, 196610, 1, 0, 196611, 0, 0, 196612, 0, 0, 196613, 0, 0, 196614, 2, 65537, 196615, 2, 65537, 196616, 2, 2, 196617, 1, 0, 262144, 1, 0, 262145, 0, 0, 262146, 1, 0, 262147, 0, 0, 262148, 0, 0, 262149, 1, 0, 262150, 2, 65536, 262151, 2, 65537, 262152, 2, 65538, 262153, 1, 0, 327680, 1, 0, 327681, 0, 0, 327682, 1, 0, 327683, 0, 0, 327684, 0, 0, 327685, 0, 0, 327686, 2, 65536, 327687, 2, 65537, 327688, 2, 65538, 327689, 1, 0, 393216, 1, 0, 393217, 0, 0, 393218, 0, 0, 393219, 0, 0, 393220, 0, 0, 393221, 0, 0, 393222, 2, 131072, 393223, 2, 131073, 393224, 2, 131074, 393225, 1, 0, 458752, 1, 0, 458753, 1, 0, 458754, 1, 0, 458755, 1, 0, 458756, 1, 0, 458757, 1, 0, 458758, 1, 0, 458759, 1, 0, 458760, 1, 0, 458761, 1, 0 ) +[node name="moss" parent="." instance=ExtResource( 6 )] +position = Vector2( 36, 36 ) + [node name="Moss2" parent="." instance=ExtResource( 6 )] position = Vector2( 44, 44 ) [node name="Moss" parent="." instance=ExtResource( 6 )] -position = Vector2( 37.5938, 20.699 ) +position = Vector2( 36, 20 ) [node name="Moss3" parent="." instance=ExtResource( 6 )] position = Vector2( 44, 52 ) -[node name="Ooze" parent="." instance=ExtResource( 5 )] -position = Vector2( 28, 44 ) - [node name="Goblin" parent="." instance=ExtResource( 4 )] position = Vector2( 12, 20 ) [node name="Goblin2" parent="." instance=ExtResource( 4 )] -position = Vector2( 28, 36 ) +position = Vector2( 28, 28 ) [node name="Ooze2" parent="." instance=ExtResource( 5 )] position = Vector2( 36, 52 ) diff --git a/src/project.godot b/src/project.godot index 24dcfc1..eb96b34 100644 --- a/src/project.godot +++ b/src/project.godot @@ -35,7 +35,7 @@ window/stretch/mode="2d" [physics] -common/physics_fps=2 +common/physics_fps=5 [rendering]