component campfire


version 3.5.2 topic campfire component

Ok, please sit down and worry not. This is not a full tutorial but just a scratch on Area2D body enter and exit principles that i used. I wanted a animated burning wood campfire component that will show a rest menu when you enter the campfire location. When resting it will provide healing.

How to set such component. I used:

Campfire:Node2D attach a script Campfire.gd here
__AnimatedSprite: used for burning wood animation
__Area2D: connect a signal to the script for body entered and body exited
___CollisionShape2D: use Rectangle2D
_SpriteRest: Sprite2D node that i use as the popup up texture for "here you can rest"
_AnimationPlayer: i use for the SpriteRest to show and hide after player enter or leaves the Area2D body

And the Campfire.gd script contains code for me to look at:

extends Node2D

# who entered the trigger
var player = null

# first time just make the menu sprite invisible
func _ready():
	$SpriteRest.visible = false

# wait for up key and start player resting phase when player entered trigger
func _process(delta):
	if player == null:
		return
	
	if player.getIsWalking() == false:
		return
		
	if Input.is_action_just_pressed("ui_up"):
		# toggle player state to resting
		player.setResting()

# show the menu when player enters area
func _on_Area2D_body_entered(body):
	if body.name == "PlayerCollider":
		player = body.get_parent()
		$AnimationPlayer.play("menu_show")

# hide the menu when player exits area
func _on_Area2D_body_exited(body):
	if body.name == "PlayerCollider":
		player = body.get_parent()
		$AnimationPlayer.play("menu_hide")

The Area2D works when player is KinematicBody2D. Then body.name tells you the collider inside player must be named “PlayerCollider” and when i need to know the node2D i do the body.get_parent() and this works only when the PlayerCollider is under the Player root node in the player scene component.

Ugh, slow to explain such things.