65 lines
1.3 KiB
C#
65 lines
1.3 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class Cue : Sprite2D
|
|
{
|
|
[Signal]
|
|
public delegate void ShootEventHandler(Vector2 impulse);
|
|
|
|
public int _powerDirection = 1;
|
|
public float _power = 0.0f, _maxPower = 8.0f;
|
|
public ProgressBar _progressBar;
|
|
|
|
public override void _Ready()
|
|
{
|
|
_progressBar = GetParent().GetNode<ProgressBar>("PowerBar");
|
|
}
|
|
|
|
public override void _Process(double delta_)
|
|
{
|
|
Vector2 mousePosition = GetViewport().GetMousePosition();
|
|
LookAt(mousePosition);
|
|
if (Input.IsActionPressed("left_click"))
|
|
{
|
|
_power += 0.1f * _powerDirection;
|
|
if (_power >= _maxPower)
|
|
{
|
|
_powerDirection = -1;
|
|
}
|
|
else if (_power <= 0)
|
|
{
|
|
_powerDirection = 1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (_power > 0f)
|
|
{
|
|
_powerDirection = 1;
|
|
Vector2 direction = mousePosition - Position;
|
|
EmitSignal(SignalName.Shoot, _power * direction);
|
|
_power = 0;
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public void HideCue()
|
|
{
|
|
SetProcess(false);
|
|
Hide();
|
|
_progressBar.Hide();
|
|
}
|
|
|
|
public void ShowCue()
|
|
{
|
|
Ball cueBall = GetParent().GetNode<Ball>("CueBall");
|
|
SetProcess(true);
|
|
Position = cueBall.Position;
|
|
_progressBar.Position = new Vector2(cueBall.Position.X - _progressBar.Size.X / 2, cueBall.Position.Y + _progressBar.Size.Y / 2);
|
|
Show();
|
|
_progressBar.Show();
|
|
}
|
|
|
|
}
|