using System; using Godot; public partial class ArrowThrower : Node2D { [Export] public Node2D ArrowsNode; private bool isAiming = false; private Vector2 initialPosition; private PackedScene arrowRef = null; private RigidBody2D arrow = null; // Called when the node enters the scene tree for the first time. public override void _Ready() { arrowRef = GD.Load("res://Scenes/Entities/arrow.tscn"); MouseInput mouseInput = FindParent("Mouse Input") as MouseInput; mouseInput.OnMouseDown += OnMouseDown; mouseInput.OnMouseUp += OnMouseUp; } private void OnMouseUp() { isAiming = false; Vector2 direction = (initialPosition - GetGlobalMousePosition()).Normalized(); Vector2 vector2 = GetGlobalMousePosition() - initialPosition; arrow.Rotation = Mathf.Atan2(-vector2.X, vector2.Y); arrow.LinearVelocity = direction * 500f; } private void OnMouseDown() { isAiming = true; initialPosition = GetGlobalMousePosition(); arrow = arrowRef.Instantiate() as RigidBody2D; ArrowsNode.AddChild(arrow); arrow.GlobalPosition = initialPosition; } // Called every frame. 'delta' is the elapsed time since the previous frame. public override void _Process(double delta) { if (!isAiming) return; Vector2 vector2 = GetGlobalMousePosition() - initialPosition; arrow.Rotation = Mathf.Atan2(-vector2.X, vector2.Y); } }