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: print("[Projectile] ERROR: No sprite node to apply fallback to!") return # Create a simple circular fallback sprite if SpriteGenerator: sprite.texture = SpriteGenerator.get_circle() sprite.modulate = Color(1.0, 0.8, 0.2) # Yellowish for projectiles sprite.scale = Vector2(0.5, 0.5) # Make it bigger so it's visible print("[Projectile] Using fallback sprite with SpriteGenerator") else: # If no SpriteGenerator, just leave it invisible for now print("[Projectile] Warning: No SpriteGenerator available for fallback") func _load_sprite(): if not sprite: print("[Projectile] ERROR: No sprite node to load texture into!") return print("[Projectile] Loading sprite with key: '%s'" % sprite_key) # Try to load projectile sprite from CDN if UpdateManager exists if sprite_key and not sprite_key.is_empty() and UpdateManager: var texture = UpdateManager.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): # Interpolate between server ticks using velocity 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("velocityX", data.get("vx", 0)) velocity.y = data.get("velocityY", data.get("vy", 0)) sprite_key = data.get("spriteKey", 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() func update_from_state(data: Dictionary): ## Snap to authoritative server position each tick, update velocity for interpolation. position.x = data.get("x", position.x) position.y = data.get("y", position.y) velocity.x = data.get("velocityX", data.get("vx", velocity.x)) velocity.y = data.get("velocityY", data.get("vy", velocity.y)) # Update linear metadata if present var new_linear = data.get("isLinear", null) if new_linear != null: is_linear = new_linear front_x = data.get("frontX", front_x) front_y = data.get("frontY", front_y) back_x = data.get("backX", back_x) back_y = data.get("backY", back_y) # Update sprite if it changed var new_sprite = data.get("spriteKey", data.get("sprite", "")) if new_sprite != "" and new_sprite != sprite_key: sprite_key = new_sprite _load_sprite() # Update rotation for linear projectiles if is_linear and velocity.length() > 0: rotation = velocity.angle()