Untitled
unknown
plain_text
a year ago
3.0 kB
1
Indexable
Never
extends CharacterBody2D var walk_speed = 70 var run_speed = 140 var current_speed = walk_speed var follow_distance = 300 var minimum_distance = 26 # Adjust this value as needed var player_follow = false var player = null var current_dir = "none" var last_dir = "down" var animated_sprite var linear_velocity = Vector2.ZERO var running = false var raycast var push_force = 300 # Attributes var maxHealth = 100 var currentHealth var minHealth = 0 var maxStamina = 100 var currentStamina var minStamina = 0 func _ready(): add_to_group("npc") animated_sprite = get_node("AnimatedSprite2D") $healthbar.max_value = maxHealth currentHealth = maxHealth $healthbar.value = currentHealth $staminabar.max_value = maxStamina currentStamina = maxStamina $staminabar.value = currentStamina raycast = get_node("RayCast2D") func _physics_process(delta): update_running_state() if player_follow: follow_player(delta) linear_velocity = velocity move_and_slide() check_for_head_collision(delta) else: velocity = Vector2.ZERO update_animation() func follow_player(_delta): var distance = global_position.distance_to(player.global_position) if distance <= follow_distance: velocity = (player.global_position - global_position).normalized() * current_speed if distance < minimum_distance: velocity = Vector2.ZERO else: velocity = Vector2.ZERO func update_running_state(): if currentStamina > 20: running = true current_speed = run_speed else: running = false current_speed = walk_speed func take_damage(damage, attacker_position): if currentStamina > 0: currentStamina -= damage update_stamina_bar() # Add this line else: currentHealth -= damage update_health_bar() # Add this line if currentHealth <= 0: print("dead") func update_health_bar(): $healthbar.value = currentHealth func update_stamina_bar(): $staminabar.value = currentStamina func update_animation(): var anim_name = "" var state = "walk" if running: state = "run" if velocity.is_equal_approx(Vector2.ZERO): anim_name = "idle_" + last_dir else: if abs(velocity.x) > 0: # Change this line if velocity.x > 0: last_dir = "right" else: last_dir = "left" else: if velocity.y > 0: last_dir = "down" else: last_dir = "up" anim_name = state + "_" + last_dir if animated_sprite.animation != anim_name: animated_sprite.play(anim_name) func check_for_head_collision(delta): if raycast.is_colliding(): if raycast.get_collider().name == "Player": print("se vio la cabeza") var direction = global_position - player.global_position velocity += direction.normalized() * delta * push_force func _on_detectionarea_body_entered(body): if body.is_in_group("player"): player = body player_follow = true set_physics_process(true) print("Following player") func _on_detectionarea_body_exited(body): if body.is_in_group("player"): player = null player_follow = false print("Not following player") func enemy(): pass