using Godot; using System.Linq; public partial class Character : CharacterBody3D { [Export] protected PackedScene _fireProjectile; [Export] protected PackedScene _altFireProjectile; public Player OwnerPlayer; [Export] protected float _speed = 3.0f; public bool CanShoot { get; protected set; } public bool CanShootAlt { get; protected set; } protected GameManager _gameManager; private bool isPaused; public override void _EnterTree() { Position = OwnerPlayer.SpawnPoint.Position; CanShoot = true; CanShootAlt = true; _gameManager = GetTree().Root.GetNode("Main/GameManager"); } public void Initialize(Player ownerPlayer) { OwnerPlayer = ownerPlayer; } public override void _PhysicsProcess(double delta) { Velocity = CalculateCharacterMovement(delta); MoveAndSlide(); } public override void _Input(InputEvent @event) { if (Input.IsActionJustPressed("exit")) GetTree().Quit(); if (Input.IsActionJustPressed(OwnerPlayer.PlayerInput.Fire()) && CanShoot) Fire(); if (Input.IsActionJustPressed(OwnerPlayer.PlayerInput.AltFire()) && CanShootAlt) AltFire(); } private Vector3 CalculateCharacterMovement(double delta) { var velocity = Velocity; var inputDir = Input.GetVector(OwnerPlayer.PlayerInput.Left(), OwnerPlayer.PlayerInput.Right(), OwnerPlayer.PlayerInput.Up(), OwnerPlayer.PlayerInput.Down()); var direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized(); direction = new Vector3(direction.X, 0, direction.Z); if (direction != Vector3.Zero) { velocity.X = direction.X * _speed; velocity.Z = direction.Z * _speed; GetNode("Pivot").LookAt(Position + direction, Vector3.Up); } else { velocity.X = Mathf.MoveToward(Velocity.X, 0, _speed); velocity.Z = Mathf.MoveToward(Velocity.Z, 0, _speed); } return velocity; } private async void Fire() { var projectile = _fireProjectile.Instantiate(); projectile.Position = Position; GetParent().AddChild(projectile); var projectiles = projectile.GetChildren().OfType(); foreach (var proj in projectiles) { if (proj.HasRotation) proj.Rotation = GetNode("Pivot").Rotation; } CanShoot = false; await ToSignal(GetTree().CreateTimer(projectiles.First().Cooldown), "timeout"); CanShoot = true; } private async void AltFire() { var projectile = _altFireProjectile.Instantiate(); projectile.Position = Position; GetParent().AddChild(projectile); var projectiles = projectile.GetChildren().OfType(); foreach (var proj in projectiles) { if (proj.HasRotation) proj.Rotation = GetNode("Pivot").Rotation; } CanShootAlt = false; await ToSignal(GetTree().CreateTimer(projectiles.First().Cooldown), "timeout"); CanShootAlt = true; } public void OnHit(Node3D node) { OwnerPlayer.SelectedCharacter.Position = new Vector3(0, 0, -20); _gameManager.CallDeferred(GameManager.MethodName.RemoveCharacter, OwnerPlayer); } }