196 lines
5.1 KiB
C#
196 lines
5.1 KiB
C#
using Godot;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
public partial class Actor : Node2D
|
|
{
|
|
public bool _available = false, _hovered = false, _selected = false;
|
|
public int _health, _healthMax;
|
|
public CollisionShape2D _startArea;
|
|
public List<Cue> _cues = new();
|
|
public Cue _selectedCue = null;
|
|
public List<Ball> _balls = new();
|
|
public List<Ball> _ballBag = new();
|
|
public Ball _hoveredBall = null;
|
|
public Ball _selectedBall = null;
|
|
public Ball _heldBall = null;
|
|
public ActorPanel _panel;
|
|
|
|
public Sprite2D _tempBallSprite = new();
|
|
|
|
public static Actor _Create()
|
|
{
|
|
PackedScene scene = ResourceLoader.Load<PackedScene>("res://Gameplay/actor.tscn");
|
|
Actor newActor = scene.Instantiate<Actor>();
|
|
|
|
newActor.GetNode<Sprite2D>("Sprite").Texture = GD.Load<Texture2D>("res://art/ness.png");
|
|
|
|
Ball newBall = Ball._Create(0);
|
|
newActor._balls.Add(newBall);
|
|
newBall = Ball._Create(0);
|
|
newActor._balls.Add(newBall);
|
|
|
|
Cue newCue = Cue._Create();
|
|
newActor.AddChild(newCue);
|
|
newActor._cues.Add(newCue);
|
|
for (int i = 0; i < newActor._cues.Count; i++)
|
|
{
|
|
newActor._cues[i].Shoot += newActor.OnCueShoot;
|
|
}
|
|
|
|
newActor._ballBag = new(newActor._balls);
|
|
|
|
newActor._health = 10;
|
|
newActor._healthMax = newActor._health;
|
|
|
|
ActorPanel newActorPanel = ActorPanel._Create(newActor);
|
|
newActor._panel = newActorPanel;
|
|
newActor.AddChild(newActorPanel);
|
|
|
|
return newActor;
|
|
}
|
|
|
|
public override void _Process(double DELTA_)
|
|
{
|
|
if (_panel._hovered)
|
|
{
|
|
if (Input.IsActionJustReleased("left_click"))
|
|
{
|
|
_selected = true;
|
|
}
|
|
}
|
|
if (_ballBag.Count > 0 && _selected)
|
|
{
|
|
if (_heldBall == null)
|
|
{
|
|
_heldBall = _ballBag[0];
|
|
}
|
|
else if (Input.IsActionJustPressed("scroll_up"))
|
|
{
|
|
_heldBall = _ballBag[(_ballBag.IndexOf(_heldBall) + 1) % _ballBag.Count];
|
|
}
|
|
else if (Input.IsActionJustPressed("scroll_down"))
|
|
{
|
|
_heldBall = _ballBag[(_ballBag.IndexOf(_heldBall) - 1) < 0 ? ^1 : (_ballBag.IndexOf(_heldBall) - 1)];
|
|
}
|
|
|
|
Vector2 mousePosition = GetViewport().GetMousePosition();
|
|
if (_tempBallSprite.Texture == null)
|
|
{
|
|
if (GetChildren().All(n => n != _tempBallSprite))
|
|
{
|
|
AddChild(_tempBallSprite);
|
|
}
|
|
_tempBallSprite.Texture = _heldBall.GetNode<Sprite2D>("Image").Texture;
|
|
_tempBallSprite.ZIndex = 1;
|
|
}
|
|
if (_startArea == null)
|
|
{
|
|
_startArea = Globals.Instance._currentBattle.GetNode<Table>("Table").GetNode<Area2D>("PlayerStartArea").GetNode<CollisionShape2D>("CollisionShape2D");
|
|
}
|
|
|
|
_tempBallSprite.Position = mousePosition;
|
|
if (_tempBallSprite.Position.X >= _startArea.GlobalPosition.X - ((RectangleShape2D)(_startArea.Shape)).Size.X / 2 && _tempBallSprite.Position.X <= _startArea.GlobalPosition.X + ((RectangleShape2D)(_startArea.Shape)).Size.X / 2 && _tempBallSprite.Position.Y >= _startArea.GlobalPosition.Y - ((RectangleShape2D)(_startArea.Shape)).Size.Y / 2 && _tempBallSprite.Position.Y <= _startArea.GlobalPosition.Y + ((RectangleShape2D)(_startArea.Shape)).Size.Y / 2)
|
|
{
|
|
if (Input.IsActionJustReleased("left_click"))
|
|
{
|
|
_heldBall.Place(_tempBallSprite.Position);
|
|
int ballIndex = _ballBag.IndexOf(_heldBall);
|
|
|
|
AddChild(_heldBall);
|
|
|
|
_ballBag.Remove(_heldBall);
|
|
_tempBallSprite.Texture = null;
|
|
if (_ballBag.Count > 0)
|
|
{
|
|
_heldBall = _ballBag[ballIndex - (ballIndex > _ballBag.Count ? 1 : 0)];
|
|
}
|
|
else
|
|
{
|
|
_heldBall = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
for (int i = 0; i < _balls.Count; i++)
|
|
{
|
|
if (_balls[i]._potted)
|
|
{
|
|
_balls[i]._potted = false;
|
|
_balls[i].Sleeping = true;
|
|
_ballBag.Add(_balls[i]);
|
|
RemoveChild(_balls[i]);
|
|
|
|
}
|
|
}
|
|
if (_balls.Any(b => b._hovered))
|
|
{
|
|
_hoveredBall = _balls.Single(b => b._hovered);
|
|
}
|
|
else
|
|
{
|
|
_hoveredBall = null;
|
|
}
|
|
|
|
if (_selectedCue == null && _cues.Count > 0)
|
|
{
|
|
_selectedCue = _cues[0];
|
|
}
|
|
|
|
if ((_selectedBall == null || _selectedBall != _hoveredBall) && (_hoveredBall?._available ?? false))
|
|
{
|
|
if (Input.IsActionJustReleased("left_click"))
|
|
{
|
|
_selectedBall = _hoveredBall;
|
|
_selectedBall._selected = true;
|
|
_selectedCue.Don(_selectedBall);
|
|
}
|
|
}
|
|
else if (Input.IsActionJustReleased("right_click"))
|
|
{
|
|
_selectedBall._selected = false;
|
|
_selectedBall = null;
|
|
_selectedCue.Doff();
|
|
}
|
|
else if (_hoveredBall == null)
|
|
{
|
|
if (Input.IsActionJustReleased("left_click") && _selectedBall != null && _selectedCue._power == 0)
|
|
{
|
|
_selectedBall._selected = false;
|
|
_selectedBall = null;
|
|
_selectedCue.Doff();
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
//public void ResetCueBall()
|
|
//{
|
|
//_cueBall = Ball._Create("ball", 0, _startPosition);
|
|
//_cueBall.SetName("CueBall");
|
|
//AddChild(_cueBall);
|
|
//Texture2D image = GD.Load<Texture2D>("res://art/cue_ball.png");
|
|
//_cueBall.GetNode<Sprite2D>("Image").Texture = image;
|
|
//_cueBall._placed = true;
|
|
//_balls = GetTree().GetNodesInGroup("balls").Select(b => (Ball)b).ToList<Ball>();
|
|
//}
|
|
|
|
|
|
|
|
private void OnCueShoot(Vector2 IMPULSE)
|
|
{
|
|
if (_selectedBall != null && _selectedBall._placed)
|
|
{
|
|
_selectedBall.ApplyCentralImpulse(IMPULSE);
|
|
_selectedBall._selected = false;
|
|
_selectedBall = null;
|
|
_selectedCue.Doff();
|
|
}
|
|
}
|
|
}
|