extends Node2D ## Projectile instance - represents a flying arrow, bolt, or thrown weapon ## Handles visual rendering and smooth movement interpolation var sprite: Sprite2D = null var projectile_id: int = -1 var source_type: String = "" # "player" or "mob" var source_id: int = 0 var velocity: Vector2 = Vector2.ZERO var sprite_key: String = "" var is_linear: bool = false var front_x: int = 0 # Arrow tip X coordinate in sprite var front_y: int = 0 # Arrow tip Y coordinate in sprite var back_x: int = 0 # Arrow tail X coordinate in sprite var back_y: int = 0 # Arrow tail Y coordinate in sprite var spin_speed: float = 10.0 # Radians per second for spinning projectiles func _ready(): # Create sprite node sprite = Sprite2D.new() sprite.name = "Sprite" add_child(sprite) # Set default fallback _use_fallback_sprite() func _use_fallback_sprite(): if not sprite: return # Create a simple circular fallback sprite if has_node("/root/SpriteGenerator"): var sprite_gen = get_node("/root/SpriteGenerator") sprite.texture = sprite_gen.get_circle() sprite.modulate = Color(1.0, 0.8, 0.2) # Yellowish for projectiles sprite.scale = Vector2(0.3, 0.3) else: # If no SpriteGenerator, just leave it invisible for now print("[Projectile] Warning: No SpriteGenerator available for fallback") func _load_sprite(): if not sprite: return # Try to load projectile sprite from CDN if UpdateManager exists if sprite_key and not sprite_key.is_empty() and has_node("/root/UpdateManager"): var update_manager = get_node("/root/UpdateManager") var texture = update_manager.load_texture(sprite_key) if texture: sprite.texture = texture sprite.modulate = Color.WHITE # Reset modulate sprite.scale = Vector2(0.5, 0.5) # Scale down projectiles # For linear projectiles, offset sprite so it rotates around the midpoint # between front (tip) and back (tail) coordinates if is_linear and front_x > 0 and back_x > 0: # Calculate center point between arrow tip and tail var center_x = (front_x + back_x) / 2.0 var center_y = (front_y + back_y) / 2.0 # Offset the sprite so the center point is at (0,0) in local space sprite.offset = Vector2(-center_x, -center_y) # Calculate the arrow's natural angle in the sprite (from back to front) var arrow_angle = Vector2(front_x - back_x, front_y - back_y).angle() # Store the arrow's natural angle so we can rotate it correctly # The sprite is oriented at arrow_angle, but we want it to face the velocity direction sprite.rotation = -arrow_angle else: print("[Projectile] Failed to load sprite: ", sprite_key) _use_fallback_sprite() else: _use_fallback_sprite() func _process(delta): # Move projectile based on velocity (client-side prediction) if velocity.length() > 0: position += velocity * delta # Rotation behavior depends on projectile type if is_linear: # Linear projectiles (arrows) point in direction of travel rotation = velocity.angle() else: # Spinning projectiles rotate continuously rotation += spin_speed * delta func initialize(data: Dictionary): projectile_id = data.get("id", -1) source_type = data.get("sourceType", "") source_id = data.get("sourceId", 0) position.x = data.get("x", 0) position.y = data.get("y", 0) velocity.x = data.get("vx", 0) velocity.y = data.get("vy", 0) sprite_key = data.get("sprite", "") is_linear = data.get("isLinear", false) front_x = data.get("frontX", 0) front_y = data.get("frontY", 0) back_x = data.get("backX", 0) back_y = data.get("backY", 0) # Set rotation based on initial velocity (for linear projectiles) if velocity.length() > 0 and is_linear: rotation = velocity.angle() # Load sprite after initialization _load_sprite() print("[Projectile] Initialized projectile ", projectile_id, " at (", position.x, ", ", position.y, ") with velocity (", velocity.x, ", ", velocity.y, "), linear=", is_linear)