33 lines
		
	
	
		
			817 B
		
	
	
	
		
			GDScript3
		
	
	
	
	
	
		
		
			
		
	
	
			33 lines
		
	
	
		
			817 B
		
	
	
	
		
			GDScript3
		
	
	
	
	
	
| 
								 | 
							
								class_name BadGuy extends Area2D
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
							 | 
						||
| 
								 | 
							
								@onready var right_ray: RayCast2D = $RightRay
							 | 
						||
| 
								 | 
							
								@onready var left_ray: RayCast2D = $LeftRay
							 | 
						||
| 
								 | 
							
								@onready var right_floor_ray: RayCast2D = $RightFloorRay
							 | 
						||
| 
								 | 
							
								@onready var left_floor_ray: RayCast2D = $LeftFloorRay
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const speed := 60
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								var direction = 1
							 | 
						||
| 
								 | 
							
								 
							 | 
						||
| 
								 | 
							
								signal playerHurt
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								func _process(delta: float) -> void:
							 | 
						||
| 
								 | 
							
									if not right_floor_ray.is_colliding():
							 | 
						||
| 
								 | 
							
										#about to fall off the right
							 | 
						||
| 
								 | 
							
										direction = -1
							 | 
						||
| 
								 | 
							
										sprite.flip_h = not sprite.flip_h
							 | 
						||
| 
								 | 
							
									if not left_floor_ray.is_colliding():
							 | 
						||
| 
								 | 
							
										#about to fall off the left
							 | 
						||
| 
								 | 
							
										sprite.flip_h = not sprite.flip_h
							 | 
						||
| 
								 | 
							
										direction = 1
							 | 
						||
| 
								 | 
							
										
							 | 
						||
| 
								 | 
							
									position.x += direction*speed*delta
							 | 
						||
| 
								 | 
							
									
							 | 
						||
| 
								 | 
							
								func _on_body_entered(body: Node2D) -> void:
							 | 
						||
| 
								 | 
							
									if body.is_in_group("player"):
							 | 
						||
| 
								 | 
							
										print("player hurt")
							 | 
						||
| 
								 | 
							
										playerHurt.emit()
							 | 
						||
| 
								 | 
							
										# queue_free()
							 | 
						||
| 
								 | 
							
										
							 |