feat: make dragon exit after some time
This commit is contained in:
		
							parent
							
								
									4f5ad345ad
								
							
						
					
					
						commit
						1378252483
					
				
					 3 changed files with 30 additions and 1 deletions
				
			
		| 
						 | 
				
			
			@ -10,6 +10,8 @@ size = Vector2(128, 128)
 | 
			
		|||
script = ExtResource("1_jccds")
 | 
			
		||||
dragon = NodePath("CollisionShape2D/Dragon")
 | 
			
		||||
animation_duration = 10.0
 | 
			
		||||
min_exit_time = 60.0
 | 
			
		||||
max_exit_time = 600.0
 | 
			
		||||
 | 
			
		||||
[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
 | 
			
		||||
position = Vector2(0, 1)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,8 +5,13 @@ class_name DragonEntity
 | 
			
		|||
@export var dragon: DragonSprite
 | 
			
		||||
@export_range(3, 20) var animation_duration: float
 | 
			
		||||
 | 
			
		||||
signal on_pick(dragon_id: int, position: Vector2, hat: Texture2D, shirt: Texture2D, shoes: Texture2D, dragon_name: String)
 | 
			
		||||
@export var min_exit_time: float
 | 
			
		||||
@export var max_exit_time: float
 | 
			
		||||
 | 
			
		||||
signal on_pick(dragon_id: int, position: Vector2, hat: Texture2D, shirt: Texture2D, shoes: Texture2D, dragon_name: String)
 | 
			
		||||
signal on_quit(dragon_id: int)
 | 
			
		||||
 | 
			
		||||
var rng: RandomNumberGenerator = RandomNumberGenerator.new()
 | 
			
		||||
var id: int
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -15,10 +20,23 @@ func _ready() -> void:
 | 
			
		|||
	_play_initial_animation()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func _queue_exit():
 | 
			
		||||
	await get_tree().create_timer(rng.randf_range(min_exit_time, max_exit_time)).timeout
 | 
			
		||||
	var tween = get_tree().create_tween()
 | 
			
		||||
	tween.tween_property($CollisionShape2D, "position", Vector2(get_window().size.x + 200, 0), animation_duration)
 | 
			
		||||
	tween.tween_callback(_proceed_exit)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func _proceed_exit():
 | 
			
		||||
	on_quit.emit(id)
 | 
			
		||||
	queue_free()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func _play_initial_animation() -> void:
 | 
			
		||||
	$CollisionShape2D.position = Vector2(get_window().size.x + 200, 0)
 | 
			
		||||
	var tween = get_tree().create_tween()
 | 
			
		||||
	tween.tween_property($CollisionShape2D, "position", Vector2(0, 0), animation_duration)
 | 
			
		||||
	tween.tween_callback(_queue_exit)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func _input_event(viewport, event, shape_idx) -> void:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -113,6 +113,7 @@ func _instantiate_dragon_ingame(position: Vector2, hat: Texture2D, shirt: Textur
 | 
			
		|||
	dragon.id = id
 | 
			
		||||
	dragon.position = position
 | 
			
		||||
	dragon.on_pick.connect(_pick_dragon)
 | 
			
		||||
	dragon.on_quit.connect(_quit_dragon)
 | 
			
		||||
	_instantiated_dragons[id] = dragon
 | 
			
		||||
	return dragon
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -130,6 +131,14 @@ func _pick_dragon(id: int, position: Vector2, hat: Texture2D, shirt: Texture2D,
 | 
			
		|||
			_filled_spots.erase(spot)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func _quit_dragon(id: int):
 | 
			
		||||
	_instantiated_dragons.erase(id)
 | 
			
		||||
	
 | 
			
		||||
	for spot in _filled_spots:
 | 
			
		||||
		if _filled_spots[spot] == id:
 | 
			
		||||
			_filled_spots.erase(spot)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
func _dragon_place_back(dragon: Dragon):
 | 
			
		||||
	_dragon_entities.erase(dragon.id)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue