8-4-25 @ 2:42 am

This commit is contained in:
2025-08-04 02:42:51 -04:00
parent 50e4f8fcb5
commit 7f65338679
15 changed files with 357 additions and 250 deletions

View File

@@ -1,11 +1,11 @@
using Godot;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
public partial class PlayerManager : Manager
{
public int _id = 0;
public List<Vector2I> _initialRackPositions =
[
new Vector2I(0, 1),
@@ -17,112 +17,129 @@ public partial class PlayerManager : Manager
new Vector2I(1, 3),
new Vector2I(2, 4)
];
public Ball _hoveredBall = null;
public BallSprite _hoveredBallSprite = null;
public BallSprite _heldBallSprite = null;
public Worker _hoveredWorker = null;
// denoted by c1r1 notation where (3,1) translates to row 1 column 3
// column 0 represents the ball return where the row represents its order in the return,
// (0,0) denotes a ball that is not on the screen
// numbers should increase from top to bottom and left to right from the player's perspective
public Node _hoveredNode = null;
public ManagerPanel _managerPanel = null;
public Panel _ballReturn = null;
public BallReturn _ballReturn = null;
public Rack _rack = null;
public override void _Ready()
{
base._Ready();
SetSprite("res://art/ness.png");
_managerPanel = GetNode<ManagerPanel>("Panel");
_managerPanel.SetPosition(new Vector2(100, 150));
_managerPanel.SetSprite(_imagePath);
_managerPanel.SetValue(_health);
_managerPanel.SetMax(_healthMax);
_ballReturn = _managerPanel.GetNode<Panel>("BallReturn");
GD.Print(_ballReturn.GlobalPosition);
_ballReturn = _managerPanel.GetNode<BallReturn>("BallReturn");
_rack = _managerPanel.GetNode<Rack>("Rack");
Ball newBall;
newBall = _ballScene.Instantiate<Ball>();
newBall.SetSprite("res://art/cue_ball.png");
newBall._rackPosition = _initialRackPositions[0];
newBall._ownerId = _id;
newBall._owner = this;
newBall.OnHover += SetHovered;
newBall.OnMovement += MovingChange;
_balls.Add(newBall);
BallSprite newBallSprite;
newBallSprite = _ballSpriteScene.Instantiate<BallSprite>();
newBallSprite.SetSprite("res://art/cue_ball.png");
newBallSprite.SetSprite(newBall._imagePath);
newBallSprite._rackPosition = _initialRackPositions[0];
newBallSprite._ownerId = _id;
newBallSprite._owner = this;
newBallSprite.OnHover += SetHovered;
_ballSprites.Add(newBallSprite);
for (int i = 1; i <= 7; i++)
{
newBall = _ballScene.Instantiate<Ball>();
newBall.SetSprite("res://art/ball_" + i + ".png");
newBall._rackPosition = _initialRackPositions[i];
newBall._ownerId = _id;
newBall._owner = this;
newBall.OnHover += SetHovered;
newBall.OnMovement += MovingChange;
_balls.Add(newBall);
newBallSprite = _ballSpriteScene.Instantiate<BallSprite>();
newBallSprite.SetSprite("res://art/ball_" + i + ".png");
newBallSprite.SetSprite(newBall._imagePath);
newBallSprite._rackPosition = _initialRackPositions[i];
newBallSprite._ownerId = _id;
_rack.SetSprite((int)newBallSprite._rackPosition.Y, (int)newBallSprite._rackPosition.X, newBallSprite._imagePath);
newBallSprite._owner = this;
newBallSprite.OnHover += SetHovered;
_ballSprites.Add(newBallSprite);
}
_managerPanel.SetManager(this);
}
public override void _Process(double DELTA_)
{
if (_ready)
if (_ballsMoving == 0 && _opponent._ballsMoving == 0)
{
if ((_selectedBall == null || _selectedBall != _hoveredBall) && (_hoveredBall?._available ?? false))
if (_ready)
{
if (Input.IsActionJustReleased("left_click"))
if (_hoveredNode is Ball || _selectedNode is Ball)
{
_selectedBall = _hoveredBall;
_selectedBall._selected = true;
_cue.Don(_selectedBall.Position);
}
}
else if (Input.IsActionJustReleased("right_click"))
{
_selectedBall._selected = false;
_selectedBall = null;
_cue.Doff();
}
else if (_hoveredBall == null)
{
if (Input.IsActionJustReleased("left_click") && _selectedBall != null && _cue._power == 0)
{
_selectedBall._selected = false;
_selectedBall = null;
_cue.Doff();
}
}
}
else
{
if (_heldBallSprite != null)
{
Vector2 mousePosition = GetViewport().GetMousePosition();
_heldBallSprite.Position = mousePosition;
if (Input.IsActionJustReleased("left_click"))
{
Ball ball = _balls.Single(b => b._rackPosition == _heldBallSprite._rackPosition);
PlaceBall(ball, mousePosition);
_heldBallSprite = null;
if (_balls.Where(b => b._placed).ToList().Count >= _placeLimit)
if ((Ball)_selectedNode == null || ((Ball)_selectedNode != _hoveredNode && _hoveredNode is Ball))
{
_ready = true;
if (Input.IsActionJustReleased("left_click"))
{
_selectedNode = (Ball)_hoveredNode;
((Ball)_selectedNode)._selected = true;
_cue.Don(((Ball)_selectedNode).Position);
}
}
else if (Input.IsActionJustReleased("right_click"))
{
((Ball)_selectedNode)._selected = false;
_selectedNode = null;
_cue.Doff();
}
else if (_hoveredNode == null)
{
if (Input.IsActionJustReleased("left_click") && (Ball)_selectedNode != null && _cue._power == 0)
{
((Ball)_selectedNode)._selected = false;
_selectedNode = null;
_cue.Doff();
}
}
}
}
else
{
if (Input.IsActionJustReleased("left_click"))
if (_hoveredNode is BallSprite || _selectedNode is BallSprite)
{
_heldBallSprite = _hoveredBallSprite;
if (_selectedNode != null)
{
Vector2 mousePosition = GetViewport().GetMousePosition();
((BallSprite)_selectedNode).Position = mousePosition;
if (Input.IsActionJustReleased("left_click"))
{
Ball ball = _balls.Single(b => b._rackPosition == ((BallSprite)_hoveredNode)._rackPosition);
PlaceBall(ball, mousePosition);
_hoveredNode = null;
_selectedNode = null;
if (_balls.Where(b => b._placed).ToList().Count >= _placeLimit)
{
_ready = true;
}
}
}
else
{
if (Input.IsActionJustReleased("left_click"))
{
_selectedNode = (BallSprite)_hoveredNode;
}
}
}
}
}
}
public override void ChangeHealth(int CHANGE)
@@ -131,6 +148,90 @@ public partial class PlayerManager : Manager
GetNode<ManagerPanel>("Panel").SetValue(_health);
}
public void MovingChange(Ball BALL, bool MOVING)
{
if (MOVING)
{
_ballsMoving++;
}
else
{
_ballsMoving--;
}
}
public override void PlaceBall(Ball BALL, Vector2 POSITION)
{
base.PlaceBall(BALL, POSITION);
BallSprite ballSprite = _ballSprites.Single(s => s._rackPosition == BALL._rackPosition);
ballSprite._active = false;
if (GetChildren().Contains(ballSprite))
{
RemoveChild(ballSprite);
}
_ballReturn._returnCount = _balls.Where(b => b._potted).ToList().Count;
}
public override void PotBall(Ball BALL)
{
base.PotBall(BALL);
BallSprite ballSprite = _ballSprites.Single(s => s._rackPosition == BALL._rackPosition);
int pottedCount = _ballReturn._returnCount;
ballSprite.Position = new Vector2(_ballReturn.GlobalPosition.X + _ballReturn.Size.X / 2, _ballReturn.GlobalPosition.Y + 50 * (pottedCount + 1));
ballSprite._active = true;
if (!GetChildren().Contains(ballSprite))
{
AddChild(ballSprite);
}
_ballReturn._returnCount = _balls.Where(b=>b._potted).ToList().Count;
}
public void SetHovered(Node NODE, bool HOVERED)
{
if (HOVERED)
{
if (_hoveredNode == null)
{
if (NODE is Ball)
{
_hoveredNode = (Ball)NODE;
}
else if (NODE is BallSprite)
{
_hoveredNode = (BallSprite)NODE;
}
}
}
else
{
_hoveredNode = null;
}
}
public void SetRack()
{
for (int i = 0; i < _ballSprites.Count; i++)
{
if ((int)_ballSprites[i]._rackPosition.Y > 0 && (int)_ballSprites[i]._rackPosition.X > 0)
{
_rack.SetSprite((int)_ballSprites[i]._rackPosition.Y, (int)_ballSprites[i]._rackPosition.X, _ballSprites[i]._imagePath);
}
}
for (int i = 0; i < _opponent._ballSprites.Count; i++)
{
if ((int)_opponent._ballSprites[i]._rackPosition.Y > 0 && (int)_opponent._ballSprites[i]._rackPosition.X > 0)
{
_rack.SetSprite((int)_opponent._ballSprites[i]._rackPosition.Y, (int)_opponent._ballSprites[i]._rackPosition.X, _opponent._ballSprites[i]._imagePath);
}
}
}
public override void Start()
{
@@ -142,56 +243,4 @@ public partial class PlayerManager : Manager
// // _ballSprites[i].AddChild(_ballSprites[i]);
// }
}
public void HoverBall(Ball BALL, bool HOVERED)
{
if (HOVERED)
{
if (_hoveredBall == null)
{
_hoveredBall = BALL;
}
}
else
{
if (_hoveredBall == BALL)
{
_hoveredBall = null;
}
}
}
public override void PlaceBall(Ball BALL, Vector2 POSITION)
{
base.PlaceBall(BALL, POSITION);
BallSprite ballSprite = _ballSprites.Single(s => s._rackPosition == BALL._rackPosition);
ballSprite._active = false;
if (GetChildren().Contains(ballSprite))
{
RemoveChild(ballSprite);
}
_managerPanel._returnCount = _balls.Where(b=>b._potted).ToList().Count;
}
public override void PotBall(Ball BALL)
{
base.PotBall(BALL);
BallSprite ballSprite = _ballSprites.Single(s => s._rackPosition == BALL._rackPosition);
int pottedCount = _managerPanel._returnCount;
GD.Print(_ballReturn.GlobalPosition);
ballSprite.Position = new Vector2(_ballReturn.GlobalPosition.X + _ballReturn.Size.X / 2, _ballReturn.GlobalPosition.Y + 50 * (pottedCount + 1));
ballSprite._active = true;
if (!GetChildren().Contains(ballSprite))
{
AddChild(ballSprite);
GD.Print(ballSprite.Position);
}
_managerPanel._returnCount = _balls.Where(b=>b._potted).ToList().Count;
}
}