ft: prey collision

This commit is contained in:
Djairo Hougee 2025-12-20 00:03:13 +01:00
parent a5055a523f
commit 1a532a5afb
16 changed files with 66 additions and 135 deletions

View File

@ -1,14 +1,14 @@
extends CharacterBody2D extends CharacterBody2D
@export var speed = 400 @export var speed = 200
var screen_size var screen_size
var str = 1 var damage = 1
func _ready(): func _ready():
screen_size = get_viewport_rect().size screen_size = get_viewport_rect().size
func _process(delta): func _process(delta):
var velocity = Vector2.ZERO velocity = Vector2.ZERO
if Input.is_action_pressed("move_right"): if Input.is_action_pressed("move_right"):
velocity.x += 1 velocity.x += 1
if Input.is_action_pressed("move_left"): if Input.is_action_pressed("move_left"):
@ -18,5 +18,6 @@ func _process(delta):
if Input.is_action_pressed("move_up"): if Input.is_action_pressed("move_up"):
velocity.y -= 1 velocity.y -= 1
position += speed * velocity * delta move_and_collide(speed * velocity * delta)
#position += speed * velocity * delta
position = position.clamp(Vector2.ZERO, screen_size) position = position.clamp(Vector2.ZERO, screen_size)

View File

@ -6,7 +6,7 @@
[ext_resource type="Script" uid="uid://ceut2lrvkns75" path="res://debug_label.gd" id="4_mys4o"] [ext_resource type="Script" uid="uid://ceut2lrvkns75" path="res://debug_label.gd" id="4_mys4o"]
[ext_resource type="Script" uid="uid://2pmjtnrg5471" path="res://molecular/preyManager.gd" id="5_ojt85"] [ext_resource type="Script" uid="uid://2pmjtnrg5471" path="res://molecular/preyManager.gd" id="5_ojt85"]
[node name="MolecularStag" type="Node2D"] [node name="MolecularStage" type="Node2D"]
[node name="Background" type="Node2D" parent="."] [node name="Background" type="Node2D" parent="."]

View File

@ -1,37 +1,41 @@
extends AbstractPrey extends AbstractPrey2D
@onready var sprite = get_node("AnimatedSprite2D") @onready var sprite = get_node("AnimatedSprite2D")
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready() -> void: func _ready() -> void:
$AnimatedSprite2D.animation = "Healthy"
$AnimatedSprite2D.play()
pass # Replace with function body. pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void: func _process(delta: float) -> void:
self.move(Vector3(randfn(0, 1), randfn(0, 1), 0))
return return
func _physics_process(delta: float) -> void:
self.move(Vector3(randfn(0, 1), randfn(0, 1), 0))
func move(destination: Vector3) -> void: func move(destination: Vector3) -> void:
self.position += Vector2(destination.x, destination.y) # stub move_and_collide(Vector2(destination.x, destination.y))
func die() -> void:
sprite.play("Dying")
super.die()
func _on_rigid_body_2d_body_entered(body: Node) -> void: func _on_area_2d_body_entered(body: Node2D) -> void:
# TODO: collision with other entities # TODO: collision with other entities
# check if colision with player # check if colision with player
print("we collidin")
if body.is_in_group("player"): if body.is_in_group("player"):
handle_collision_player(body) handle_collision_player(body)
else:
self.die()
# Function to handle collision logic # Function to handle collision logic
func handle_collision_player(player): func handle_collision_player(player):
print("Collision!") self.health -= player.damage
self.health -= player.str
if sprite: if sprite:
if self.health <= 0: if self.health <= 0:
self.die() self.die()
else: else:
sprite.play("injured") sprite.play("Injured")

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=10 format=3 uid="uid://c3iw2v3x6ngrb"] [gd_scene load_steps=10 format=3 uid="uid://c3iw2v3x6ngrb"]
[ext_resource type="PackedScene" uid="uid://bvsdg1v3ksixy" path="res://abstract_prey.tscn" id="1_qvulj"] [ext_resource type="PackedScene" uid="uid://bvsdg1v3ksixy" path="res://shared/npc/prey2D.tscn" id="1_qvulj"]
[ext_resource type="Script" uid="uid://bgossk6xo31gi" path="res://molecular/nucleotide_prey.gd" id="2_0227s"] [ext_resource type="Script" uid="uid://bgossk6xo31gi" path="res://molecular/nucleotide_prey.gd" id="2_0227s"]
[ext_resource type="Texture2D" uid="uid://bhcb5g7g7um8" path="res://molecular/assets/prey/prey-dying-frame0.png" id="2_lkj7f"] [ext_resource type="Texture2D" uid="uid://bhcb5g7g7um8" path="res://molecular/assets/prey/prey-dying-frame0.png" id="2_lkj7f"]
[ext_resource type="Texture2D" uid="uid://bxn11avw7dykl" path="res://molecular/assets/prey/prey-dying-frame1.png" id="3_svqyr"] [ext_resource type="Texture2D" uid="uid://bxn11avw7dykl" path="res://molecular/assets/prey/prey-dying-frame1.png" id="3_svqyr"]
@ -12,52 +12,54 @@
[sub_resource type="SpriteFrames" id="SpriteFrames_66x8p"] [sub_resource type="SpriteFrames" id="SpriteFrames_66x8p"]
animations = [{ animations = [{
"frames": [{ "frames": [{
"duration": 1.0, "duration": 20.0,
"texture": ExtResource("2_lkj7f") "texture": ExtResource("2_lkj7f")
}, { }, {
"duration": 1.0, "duration": 20.0,
"texture": ExtResource("3_svqyr") "texture": ExtResource("3_svqyr")
}], }],
"loop": true, "loop": true,
"name": &"Dying", "name": &"Dying",
"speed": 5.0 "speed": 1.0
}, { }, {
"frames": [{ "frames": [{
"duration": 1.0, "duration": 20.0,
"texture": ExtResource("4_ee1gb") "texture": ExtResource("4_ee1gb")
}, { }, {
"duration": 1.0, "duration": 20.0,
"texture": ExtResource("5_ae5nf") "texture": ExtResource("5_ae5nf")
}], }],
"loop": true, "loop": true,
"name": &"Healthy", "name": &"Healthy",
"speed": 5.0 "speed": 1.0
}, { }, {
"frames": [{ "frames": [{
"duration": 1.0, "duration": 20.0,
"texture": ExtResource("6_0f87h") "texture": ExtResource("6_0f87h")
}, { }, {
"duration": 1.0, "duration": 20.0,
"texture": ExtResource("7_w7inl") "texture": ExtResource("7_w7inl")
}], }],
"loop": true, "loop": true,
"name": &"Injured", "name": &"Injured",
"speed": 5.0 "speed": 1.0
}] }]
[node name="NucleotidePrey" instance=ExtResource("1_qvulj")] [node name="NucleotidePrey" instance=ExtResource("1_qvulj")]
collision_layer = 2
collision_mask = 3
motion_mode = 1
script = ExtResource("2_0227s") script = ExtResource("2_0227s")
maxHealth = 20
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="." index="0"] [node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="." index="0"]
scale = Vector2(0.1, 0.1) scale = Vector2(0.1, 0.1)
sprite_frames = SubResource("SpriteFrames_66x8p") sprite_frames = SubResource("SpriteFrames_66x8p")
animation = &"Healthy" animation = &"Dying"
frame_progress = 0.60472965
[node name="RigidBody2D" type="RigidBody2D" parent="." index="1"] [node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="Area2D" index="0"]
collision_layer = 2 position = Vector2(7.215866, -0.5034294)
collision_mask = 3 polygon = PackedVector2Array(-8.831272, -2.3390446, -5.7760534, -2.3993049, -4.0230684, 0.41277456, -5.8890305, 2.9948444, -8.862037, 2.9326901, -10.268076, 0.28495264)
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="RigidBody2D" index="0"] [connection signal="body_entered" from="Area2D" to="." method="_on_area_2d_body_entered"]
polygon = PackedVector2Array(-16.442108, -21.739502, 14.039459, -23.432922, 29.280273, 3.097351, 12.346039, 27.369736, -17.571075, 29.62761, -32.247375, 1.968399)
[connection signal="body_entered" from="RigidBody2D" to="." method="_on_rigid_body_2d_body_entered"]

View File

@ -1,82 +0,0 @@
[gd_scene load_steps=9 format=3 uid="uid://cum0iux1pv7bu"]
[ext_resource type="Texture2D" uid="uid://bhcb5g7g7um8" path="res://molecular/assets/prey/prey-dying-frame0.png" id="2_5mr0w"]
[ext_resource type="Texture2D" uid="uid://bxn11avw7dykl" path="res://molecular/assets/prey/prey-dying-frame1.png" id="3_eomdj"]
[ext_resource type="Texture2D" uid="uid://ctkehsavw6ghx" path="res://molecular/assets/prey/prey-healthy-frame0.png" id="4_t6vdg"]
[ext_resource type="Texture2D" uid="uid://uy28y3mkk6nt" path="res://molecular/assets/prey/prey-healthy-frame1.png" id="5_8jbq3"]
[ext_resource type="Texture2D" uid="uid://btnyajci8ptb2" path="res://molecular/assets/prey/prey-injured-frame0.png" id="6_o44iv"]
[ext_resource type="Texture2D" uid="uid://bqll8ge4cr2uf" path="res://molecular/assets/prey/prey-injured-frame1.png" id="7_wx0y7"]
[sub_resource type="GDScript" id="GDScript_56kmc"]
script/source = "extends RigidBody2D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
func _on_visible_on_screen_notifier_2d_screen_exited():
queue_free();
"
[sub_resource type="SpriteFrames" id="SpriteFrames_43b72"]
animations = [{
"frames": [{
"duration": 1.0,
"texture": ExtResource("2_5mr0w")
}, {
"duration": 1.0,
"texture": ExtResource("3_eomdj")
}],
"loop": true,
"name": &"Dying",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": ExtResource("4_t6vdg")
}, {
"duration": 1.0,
"texture": ExtResource("5_8jbq3")
}],
"loop": true,
"name": &"Healthy",
"speed": 5.0
}, {
"frames": [{
"duration": 1.0,
"texture": ExtResource("6_o44iv")
}, {
"duration": 1.0,
"texture": ExtResource("7_wx0y7")
}],
"loop": true,
"name": &"Injured",
"speed": 5.0
}]
[node name="Prey" type="RigidBody2D"]
gravity_scale = 0.379
script = SubResource("GDScript_56kmc")
metadata/_edit_group_ = true
[node name="VisibleOnScreenNotifier2D" type="VisibleOnScreenNotifier2D" parent="."]
[node name="AnimatedSprite2D" type="AnimatedSprite2D" parent="."]
scale = Vector2(0.1, 0.1)
sprite_frames = SubResource("SpriteFrames_43b72")
animation = &"Healthy"
frame_progress = 0.14205484
[node name="CollisionPolygon2D" type="CollisionPolygon2D" parent="."]
position = Vector2(0, 0.081250384)
scale = Vector2(0.1, 0.10208334)
polygon = PackedVector2Array(-15.967808, -28.469032, 14.526825, -28.826782, 30.048037, -3.7841516, 14.344225, 21.97399, -15.419998, 22.510618, -31.671623, -4.141903)
[connection signal="screen_exited" from="VisibleOnScreenNotifier2D" to="." method="_on_visible_on_screen_notifier_2d_screen_exited"]

View File

@ -1,6 +0,0 @@
[gd_scene load_steps=2 format=3 uid="uid://biup0eej85fq2"]
[ext_resource type="Script" uid="uid://biu3sctw15ga" path="res://npc.gd" id="1_ideak"]
[node name="NPC" type="Node2D"]
script = ExtResource("1_ideak")

View File

@ -1,5 +1,5 @@
extends Node2D extends CharacterBody2D
class_name NPC class_name NPC2D
@export var maxHealth: int = 0 @export var maxHealth: int = 0
var health: int = maxHealth var health: int = maxHealth
@ -15,6 +15,10 @@ func _process(delta: float) -> void:
func spawn() -> void: func spawn() -> void:
pass pass
func take_damage(dmg: int) -> void:
self.health -= dmg;
if self.health < 0:
self.die()
# I think the move per npc is to model concrete behaviours in functions. # I think the move per npc is to model concrete behaviours in functions.
# How the npc acts can be determined elsewhere, these functions just implement the behvaiour # How the npc acts can be determined elsewhere, these functions just implement the behvaiour

View File

@ -0,0 +1,8 @@
[gd_scene load_steps=2 format=3 uid="uid://biup0eej85fq2"]
[ext_resource type="Script" uid="uid://biu3sctw15ga" path="res://shared/npc/npc2D.gd" id="1_ucjfp"]
[node name="NPC" type="CharacterBody2D"]
script = ExtResource("1_ucjfp")
[node name="Area2D" type="Area2D" parent="."]

View File

@ -1,5 +1,5 @@
extends NPC extends NPC2D
class_name AbstractPredator class_name AbstractPredator2D
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready() -> void: func _ready() -> void:

View File

@ -1,7 +1,7 @@
[gd_scene load_steps=3 format=3 uid="uid://b7wqd5owafn6g"] [gd_scene load_steps=3 format=3 uid="uid://b7wqd5owafn6g"]
[ext_resource type="PackedScene" uid="uid://biup0eej85fq2" path="res://npc.tscn" id="1_eoj71"] [ext_resource type="PackedScene" uid="uid://biup0eej85fq2" path="res://shared/npc/npc2D.tscn" id="1_4llks"]
[ext_resource type="Script" uid="uid://dgfimmq53whll" path="res://abstract_predator.gd" id="2_hv0s6"] [ext_resource type="Script" uid="uid://dgfimmq53whll" path="res://shared/npc/predator2D.gd" id="2_rj1ok"]
[node name="AbstractPredator" instance=ExtResource("1_eoj71")] [node name="AbstractPredator" instance=ExtResource("1_4llks")]
script = ExtResource("2_hv0s6") script = ExtResource("2_rj1ok")

View File

@ -1,5 +1,5 @@
extends NPC extends NPC2D
class_name AbstractPrey class_name AbstractPrey2D
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready() -> void: func _ready() -> void:

View File

@ -1,7 +1,7 @@
[gd_scene load_steps=3 format=3 uid="uid://bvsdg1v3ksixy"] [gd_scene load_steps=3 format=3 uid="uid://bvsdg1v3ksixy"]
[ext_resource type="PackedScene" uid="uid://biup0eej85fq2" path="res://npc.tscn" id="1_2m1le"] [ext_resource type="PackedScene" uid="uid://biup0eej85fq2" path="res://shared/npc/npc2D.tscn" id="1_2m1le"]
[ext_resource type="Script" uid="uid://76jxpubyd8wp" path="res://abstract_prey.gd" id="2_dny00"] [ext_resource type="Script" uid="uid://76jxpubyd8wp" path="res://shared/npc/prey2D.gd" id="2_dny00"]
[node name="AbstractPrey" instance=ExtResource("1_2m1le")] [node name="AbstractPrey" instance=ExtResource("1_2m1le")]
script = ExtResource("2_dny00") script = ExtResource("2_dny00")

View File

@ -4,8 +4,8 @@
"version_minor": 5, "version_minor": 5,
"version_patch": 1, "version_patch": 1,
"version_status": "stable", "version_status": "stable",
"version_build": "official", "version_build": "arch_linux",
"version_full_name": "Godot Engine v4.5.1.stable.official", "version_full_name": "Godot Engine v4.5.1.stable.arch_linux",
"precision": "single" "precision": "single"
}, },
"builtin_class_sizes": [ "builtin_class_sizes": [