Files
hotdesking/Gameplay/Battle.cs
2025-07-19 01:16:38 -04:00

131 lines
2.9 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
public partial class Battle : Node
{
public bool _current;
public Vector2 _startPosition = new Vector2(890, 340);
public Player _player;
public Player _computer;
public Player _turn;
public List<Sprite2D> _potted = new();
public List<Ball> _balls = new();
public Table _table;
public static Battle _Create()
{
PackedScene scene = ResourceLoader.Load<PackedScene>("res://Gameplay/battle.tscn");
Battle newBattle = scene.Instantiate<Battle>();
Player newPlayer = Player._Create();
newBattle._player = newPlayer;
newBattle.AddChild(newPlayer);
Table newTable = Table._Create();
newTable.Position = Globals.Instance._screenCenter;
newBattle._table = newTable;
List<Area2D> pockets = newBattle._table.GetChildren()
.Where(n => n.GetName().ToString().ToLower().Contains("pocket"))
.Select(n => (Area2D)n)
.ToList<Area2D>();
for (int i = 0; i < pockets.Count; i++)
{
pockets[i].BodyEntered += newBattle.PottedBall;
}
newBattle.AddChild(newTable);
return newBattle;
}
public override void _Ready()
{
_balls = GetTree().GetNodesInGroup("balls").Select(b => (Ball)b).ToList<Ball>();
Start();
}
public override void _Process(double DELTA_)
{
CheckMovement();
}
public void GenerateBalls()
{
int count = 1;
int columns = 0;
int diameter = 36;
Table table = GetNode<Table>("Table");
for (int i = 0; i < 5; i++)
{
for (int j = 0; j <= columns; j++)
{
Vector2 position = new Vector2(table.GlobalPosition.X - (i * (diameter / 2)) + (j * (diameter)), table.GlobalPosition.Y - table.Texture.GetSize().Y / 4 - (i * diameter));
Ball ball = Ball._Create(count);
ball.Place(position);
AddChild(ball);
count += 1;
}
columns += 1;
}
}
public void CheckMovement()
{
bool movementCheck = _balls.Any(b => b._moving && b._placed);
if (movementCheck)
{
if (!Globals.Instance._anyMovement)
{
Globals.Instance._anyMovement = true;
//EmitSignal(SignalName.DetectMovement, true);
}
}
else
{
if (Globals.Instance._anyMovement)
{
Globals.Instance._anyMovement = false;
//EmitSignal(SignalName.DetectMovement, false);
}
}
}
public void PottedBall(Node2D BODY)
{
if (BODY.GetType() != typeof(Ball))
{
return;
}
if (((Ball)BODY)._isCue)
{
((Ball)BODY)._potted = true;
((Ball)BODY)._placed = false;
}
else
{
Panel pottedPanel = GetNode<Panel>("PottedPanel");
Sprite2D ballSprite = new Sprite2D();
AddChild(ballSprite);
ballSprite.Texture = BODY.GetNode<Sprite2D>("Image").Texture;
_potted.Add(ballSprite);
ballSprite.Position = new Vector2(pottedPanel.Position.X + pottedPanel.Size.X / 2, pottedPanel.Position.Y + (50 * _potted.Count));
((Ball)BODY)._placed = false;
((Ball)BODY)._potted = true;
BODY.QueueFree();
}
}
public void Start()
{
_current = true;
GenerateBalls();
Globals.Instance._currentBattle = this;
}
}