import time
import math
import threading
from dataclasses import dataclass
from typing import Tuple

@dataclass
class Target:
    x: float
    y: float
    width: float
    height: float
    is_moving: bool = False

class AimAssist:
    def __init__(self):
        self.aim_strength = 0.3  
        self.snap_distance = 50.0  
        self.target_radius = 80.0  
        self.smooth_factor = 0.2  
        self.tracking_speed = 1.5  
        self.is_active = False
        
    def calculate_distance(self, pos1: Tuple[float, float], pos2: Tuple[float, float]) -> float:
        """计算两点之间的距离"""
        return math.sqrt((pos2[0] - pos1[0])**2 + (pos2[1] - pos1[1])**2)
    
    def find_nearest_target(self, player_pos: Tuple[float, float], targets: list) -> Target:
        """找到最近的目标"""
        nearest_target = None
        min_distance = float('inf')
        
        for target in targets:
            distance = self.calculate_distance(player_pos, (target.x, target.y))
            if distance < min_distance and distance < self.snap_distance:
                min_distance = distance
                nearest_target = target
                
        return nearest_target
    
    def smooth_aim(self, current_pos: Tuple[float, float], target_pos: Tuple[float, float]) -> Tuple[float, float]:
        """平滑移动到目标位置"""
        dx = target_pos[0] - current_pos[0]
        dy = target_pos[1] - current_pos[1]
        
        
        move_x = current_pos[0] + dx * self.smooth_factor
        move_y = current_pos[1] + dy * self.smooth_factor
        
        return (move_x, move_y)
    
    def enhanced_tracking(self, target: Target, player_sensitivity: float) -> float:
        """增强跟枪速度"""
        base_sensitivity = player_sensitivity
        if target.is_moving:
            return base_sensitivity * self.tracking_speed
        return base_sensitivity
    
    def aim_assist_loop(self):
        """主辅助瞄准循环"""
        print("Aim assist started...")
        
        while self.is_active:
           
            player_position = (0, 0)  
            mouse_sensitivity = 1.0   
            
           
            targets = [
                Target(100, 100, 30, 30, True),
                Target(200, 150, 30, 30, False),
            ]
            
            nearest_target = self.find_nearest_target(player_position, targets)
            
            if nearest_target:
                
                target_center = (
                    nearest_target.x + nearest_target.width / 2,
                    nearest_target.y + nearest_target.height / 2
                )
                
                
                if self.calculate_distance(player_position, target_center) < self.target_radius:
                    smoothed_pos = self.smooth_aim(player_position, target_center)
                    adjusted_sensitivity = self.enhanced_tracking(nearest_target, mouse_sensitivity)
                    
                    print(f"Aiming at target: {smoothed_pos}")
                    print(f"Adjusted sensitivity: {adjusted_sensitivity}")
            
            time.sleep(0.016)  
    
    def start(self):
        """启动辅助瞄准"""
        self.is_active = True
        thread = threading.Thread(target=self.aim_assist_loop)
        thread.daemon = True
        thread.start()
        print("Aim assist enabled")
    
    def stop(self):
        """停止辅助瞄准"""
        self.is_active = False
        print("Aim assist disabled")


if __name__ == "__main__":
    assist = AimAssist()
    
    
    assist.aim_strength = 0.4    
    assist.snap_distance = 70.0    
    assist.target_radius = 100.0  
    assist.tracking_speed = 2.0    
    
    
    assist.start()
    time.sleep(5)  
    assist.stop()