You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			76 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			GDScript
		
	
			
		
		
	
	
			76 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			GDScript
		
	
| extends Node3D
 | |
| class_name AI
 | |
| 
 | |
| @onready var character: Character = (get_owner() as Character)
 | |
| @onready var status: Status = (%Status as Status)
 | |
| 
 | |
| var ai: BeehaveTree
 | |
| var is_init: bool = false
 | |
| var has_ai: bool  = false
 | |
| 
 | |
| 
 | |
| func _process(delta) -> void:
 | |
|     if not is_init:
 | |
|         init()
 | |
|     else:
 | |
|         process(delta)
 | |
| 
 | |
| 
 | |
| func init() -> void:
 | |
|     if not status.cfg:
 | |
|         return
 | |
|     is_init = true
 | |
|     var ai_scene: PackedScene = status.cfg.get_ai()
 | |
|     if not ai_scene:
 | |
|         return
 | |
|     ai = ai_scene.instantiate() as BeehaveTree
 | |
|     add_child(ai)
 | |
|     has_ai = true
 | |
|     on_init()
 | |
|     ai.init()
 | |
| 
 | |
| 
 | |
| func process(delta) -> void:
 | |
|     if has_ai and status.ai_is_alert:
 | |
|         on_process(delta)
 | |
|         ai.process(delta)
 | |
|     else:
 | |
|         if status.cfg.ai:
 | |
|             stroll()
 | |
|             var player: Character  = Global.character_mgr.get_player()
 | |
|             var dir: Vector2       = player.pos2D() - character.pos2D()
 | |
|             var dist: float        = dir.length()
 | |
|             var can_see: bool      = character.get_face_dir().angle_to(dir) < PI/2
 | |
|             var alert_range: float = status.cfg.ai.alert_range if can_see else status.cfg.ai.alert_range_back
 | |
|             var alert_speed: float = status.cfg.ai.alert_speed if can_see else status.cfg.ai.alert_speed_back
 | |
|             if dist < alert_range:
 | |
|                 status.ai_alert += alert_speed * delta
 | |
|                 status.emit_status("ai_alert")
 | |
|                 if status.ai_alert >= status.ai_alert_max:
 | |
|                     status.ai_is_alert = true
 | |
|                     status.ai_alert = 0
 | |
|                     status.emit_status("ai_is_alert")
 | |
|                     status.emit_status("ai_alert")
 | |
|             else:
 | |
|                 status.ai_alert -= status.cfg.ai.alert_recover * delta
 | |
|                 if status.ai_alert < 0:
 | |
|                     status.ai_alert = 0
 | |
|                 status.emit_status("ai_alert")
 | |
|         else:
 | |
|             status.ai_is_alert = true
 | |
|             status.emit_status("ai_is_alert")
 | |
| 
 | |
| 
 | |
| func stroll() -> void:
 | |
|     #todo 闲逛
 | |
|     character.move_stop()
 | |
|     pass
 | |
| 
 | |
| 
 | |
| func on_init() -> void:
 | |
|     pass
 | |
| 
 | |
| 
 | |
| func on_process(delta):
 | |
|     pass
 |