using Godot; using System; using System.Collections.Generic; using System.Linq; public partial class PlayerManager : Manager { public int _id = 0; public List _initialRackPositions = [ new Vector2I(0, 1), new Vector2I(1, 1), new Vector2I(3, 1), new Vector2I(4, 1), new Vector2I(2, 2), new Vector2I(4, 2), new Vector2I(1, 3), new Vector2I(2, 4) ]; public Ball _hoveredBall = null; public BallSprite _hoveredBallSprite = null; public BallSprite _heldBallSprite = null; public Worker _hoveredWorker = null; public ManagerPanel _managerPanel = null; public Panel _ballReturn = null; public override void _Ready() { SetSprite("res://art/ness.png"); _managerPanel = GetNode("Panel"); _managerPanel.SetPosition(new Vector2(100, 150)); _managerPanel.SetSprite(_imagePath); _managerPanel.SetValue(_health); _managerPanel.SetMax(_healthMax); _ballReturn = _managerPanel.GetNode("BallReturn"); GD.Print(_ballReturn.GlobalPosition); Ball newBall; newBall = _ballScene.Instantiate(); newBall.SetSprite("res://art/cue_ball.png"); newBall._rackPosition = _initialRackPositions[0]; newBall._ownerId = _id; _balls.Add(newBall); BallSprite newBallSprite; newBallSprite = _ballSpriteScene.Instantiate(); newBallSprite.SetSprite("res://art/cue_ball.png"); newBallSprite._rackPosition = _initialRackPositions[0]; newBallSprite._ownerId = _id; _ballSprites.Add(newBallSprite); for (int i = 1; i <= 7; i++) { newBall = _ballScene.Instantiate(); newBall.SetSprite("res://art/ball_" + i + ".png"); newBall._rackPosition = _initialRackPositions[i]; newBall._ownerId = _id; _balls.Add(newBall); newBallSprite = _ballSpriteScene.Instantiate(); newBallSprite.SetSprite("res://art/ball_" + i + ".png"); newBallSprite._rackPosition = _initialRackPositions[i]; newBallSprite._ownerId = _id; _ballSprites.Add(newBallSprite); } } public override void _Process(double DELTA_) { if (_ready) { if ((_selectedBall == null || _selectedBall != _hoveredBall) && (_hoveredBall?._available ?? false)) { if (Input.IsActionJustReleased("left_click")) { _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) { _ready = true; } } } else { if (Input.IsActionJustReleased("left_click")) { _heldBallSprite = _hoveredBallSprite; } } } } public override void ChangeHealth(int CHANGE) { base.ChangeHealth(CHANGE); GetNode("Panel").SetValue(_health); } public override void Start() { PotBall(_balls[0]); // for (int i = 0; i < _ballSprites.Count; i++) // { // GD.Print(_ballSprites[i]._rackPosition); // // _ballSprites[i].Position = new Vector2(_ballReturn.GlobalPosition.X + _ballReturn.Size.X / 2, _ballReturn.GlobalPosition.Y + 50 * (i + 1)); // // _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; } }