Godot Version
4.2.1
Question
In my game, the enemies can be damaged by 2 different types of nodes.
- Player bullet
- Supermove projectile
Both types of nodes are Node2D and have a CollisionShape2D.
The “Enemy” node has an Area2D, which uses the “on_area_entered” signal to determine whether the enemy is damaged.
My requirement is to differentiate the damage values based on which player projectile type (Player bullet or Supermove projectile) enters the Area2D of the enemy node.
Ex: The Supermove projectile should do more damage to the enemy than a player bullet.
Below is the base code of the enemy.
extends CharacterBody2D
class_name EnemyBase
enum FACING {LEFT = -1, RIGHT = 1}
const OFF_SCREEN_KILL_ME = 1000
@onready var animation_player = $AnimationPlayer
@export var default_facing: FACING = FACING.LEFT
@export var points: int = 1
@export var speed: float = 30
@export var enemy_health: float = 2
var _gravity: float = ProjectSettings.get_setting("physics/2d/default_gravity")
var _facing: FACING = default_facing
var _player_ref: Player
var _dying: bool = false
# Called when the node enters the scene tree for the first time.
func _ready():
_player_ref = get_tree().get_nodes_in_group(Gamemanager.GROUP_PLAYER)[0]
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta):
fallen_off()
func fallen_off():
if global_position.y > OFF_SCREEN_KILL_ME:
queue_free()
func get_damaged():
enemy_health -= 1
if enemy_health <= 0:
die()
else:
return
func die():
if _dying == true:
return
_dying = true
Signalmanager.on_enemy_hit.emit(points, global_position)
ObjectMaker.create_explosion(global_position)
set_physics_process(false)
hide()
queue_free()
func _on_visible_on_screen_notifier_2d_screen_entered():
pass # Replace with function body.
func _on_visible_on_screen_notifier_2d_screen_exited():
pass # Replace with function body.
func _on_hitbox_area_entered(area):
get_damaged()
animation_player.play("damaged")
What would be the best solution for this query?
Thank you in advance.