Godot version
Godot 4.7.2
Hello how to make the ennemies that still damage you when you still in his collsion i mean when you touch the ennemies one time it damage you okay , but when you are inside of his collision he cant damage you until you go away from his collisiion zone.
ennemies.gd
extends Node2D
const SPEED = 20
var direction = 1
var a_fait_demi_tour = false
@onready var ray_cast_right: RayCast2D = $RayCastRight
@onready var ray_cast_left: RayCast2D = $RayCastLeft
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var ray_cast_downright: RayCast2D = $RayCastDownright
@onready var ray_cast_downleft: RayCast2D = $RayCastDownleft
@onready var degat_timer: Timer = $DegatTimer
var prend_degats = false
var vie = 10;
var maxvie = 10;
var est_mort = false
@onready var bardevieennemie: ProgressBar = $Bardevieennemie
func _process(delta: float) -> void:
bardevieennemie.setvie(vie , maxvie)
if est_mort:
return
if not ray_cast_downright.is_colliding() and not a_fait_demi_tour:
direction *= -1
animated_sprite.flip_h = direction < 0
a_fait_demi_tour = true
if not ray_cast_downleft.is_colliding() and not a_fait_demi_tour:
direction *= 1
animated_sprite.flip_h = direction > 0
a_fait_demi_tour = true
if ray_cast_downright.is_colliding() or ray_cast_downleft.is_colliding():
a_fait_demi_tour = false
if direction == 1 and not ray_cast_downright.is_colliding():
direction = -1
animated_sprite.flip_h = true
elif direction == -1 and not ray_cast_downleft.is_colliding():
direction = 1
animated_sprite.flip_h = false
if ray_cast_right.is_colliding() :
direction = -1
animated_sprite.flip_h = true
if ray_cast_left.is_colliding():
direction = 1
animated_sprite.flip_h = false
position.x += direction * delta * SPEED
if not prend_degats:
animated_sprite.play("default")
func take_damage(damage: int):
if est_mort:
return
vie -= damage
if vie <= 0:
vie = 0
est_mort = true
direction = 0
animated_sprite.play("meurt")
return
prend_degats = true
animated_sprite.play("blesse")
degat_timer.start()
func _on_hurt_box_area_entered(area: Area2D) -> void:
if area.name == "Area2D" and not est_mort :
print("okokkok")
var player = get_tree().get_first_node_in_group("Player")
if player != null:
player.take_damage(degat())
if area.name == "Epee" and not est_mort:
print("aie")
take_damage(area.degat())
print(vie)
func degat():
return 3
func _on_degat_timer_timeout() -> void:
prend_degats = false
func _on_animated_sprite_2d_animation_finished() -> void:
if animated_sprite.animation == "meurt":
queue_free()
player.gd
extends CharacterBody2D
var transition_cartoonesque = preload("uid://rvjao2ktxeya")
const SPEED = 100.0
const JUMP_VELOCITY = -300.0
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
var epee: Area2D
@onready var degattimer: Timer = $Degattimer
var dash = 600
var dashing : bool = false
var acceleration = 0.2
@onready var dash_timer: Timer = $DashTimer
@onready var encore_dash_timer: Timer = $encoreDashTimer
var peut_dash = true
var vie = 5;
var maxvie = 10;
@onready var bardevieplayer: ProgressBar = $Bardevieplayer
var prend_degats = false
var attaque = false
var mort = false
@onready var actualiserbardevieplayer = get_tree().get_first_node_in_group("Interface")
func _ready() -> void:
pass
func take_damage(damage : int):
if mort:
return
vie -= damage
prend_degats = true
animated_sprite.play("aie")
degattimer.start()
if vie <= 0:
vie = 0
print("je suis 0 vie ")
mort = true
get_tree().current_scene.add_child(transition_cartoonesque)
return
func _physics_process(delta: float) -> void:
bardevieplayer.setvie(vie , maxvie)
actualiserbardevieplayer.barredevieenlive(vie, maxvie)
if mort:
print("lalalal")
respawn()
return
if epee != null and Input.is_action_just_pressed("attaque_epee") :
print("jai une epee")
epee.attaque()
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
# Handle jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("move_left", "move_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if direction > 0:
animated_sprite.flip_h = false
if epee != null:
epee.rotation_degrees = 0
elif direction < 0:
animated_sprite.flip_h = true
if epee != null:
epee.rotation_degrees = -100
if Input.is_action_just_pressed("dash") and peut_dash:
dashing = true
peut_dash = false
dash_timer.start()
encore_dash_timer.start()
if dashing:
velocity.x = lerp(velocity.x, direction * dash , acceleration)
print(velocity.x)
else:
velocity.x = lerp(velocity.x, direction * SPEED , acceleration)
if prend_degats:
animated_sprite.play("aie")
elif dashing:
animated_sprite.play("jedash")
elif is_on_floor():
if direction == 0:
animated_sprite.play("idle")
else:
animated_sprite.play("run")
else:
animated_sprite.play("jump")
move_and_slide()
func _on_area_2d_area_entered(area: Area2D) -> void:
if area.name == "Epee":
epee = area
area.reparent(self)
area.position = Vector2(0, 0)
area.hide()
func _on_dash_timer_timeout() -> void:
dashing = false
func _on_encore_dash_timer_timeout() -> void:
peut_dash = true # Replace with function body.
func _on_degattimer_timeout() -> void:
prend_degats = false
func meurto() -> bool:
if vie <= 0:
print("dead")
mort = true;
print(mort)
return true
else:
print("dead void")
#mort = true;
#print(mort)
return false
func respawn():
position = %Checkpoint.position
get_tree().reload_current_scene()
#Ajoute une autre barre de vie a l'interface du joueur (meme chose avec nombre de pieces)