Files
hotdesking/Gameplay/Battle.cs
2025-07-13 03:38:14 -04:00

118 lines
2.5 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
public partial class Battle : Node
{
public bool _anyMovement, _cueBallPotted;
public Vector2 _startPosition = new Vector2(890, 340);
public Ball _cueBall;
public List<Sprite2D> _potted = new();
public List<Ball> _balls;
public override void _Ready()
{
Start();
List<Area2D> pockets = GetNode<Table>("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 += PottedBall;
}
_balls = GetTree().GetNodesInGroup("balls").Select(b => (Ball)b).ToList<Ball>();
}
public override void _Process(double delta_)
{
_anyMovement = CheckMovement();
//if (!_cueBall._available)
//{
//GD.Print(1);
//}
if (_cueBallPotted && !_anyMovement) // LOGIC WILL ONLY APPLY TO CUE BALLS
{
ResetCueBall();
_cueBallPotted = false;
}
}
public void GenerateBalls()
{
int count = 1;
int rows = 5;
int diameter = 36;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < rows; j++)
{
Vector2 position = new Vector2(250 + (i*(diameter)), 267 + (j*(diameter)) + (i*(diameter / 2)));
Ball ball = Ball.Create(count, position);
AddChild(ball);
count += 1;
}
rows -= 1;
}
}
public bool CheckMovement()
{
return _balls.Any(b => b._moving);
}
public void PottedBall(Node2D body)
{
if (body == _cueBall)
{
_cueBallPotted = true;
RemoveCueBall();
}
else
{
Sprite2D ballSprite = new Sprite2D();
AddChild(ballSprite);
ballSprite.Texture = body.GetNode<Sprite2D>("Image").Texture;
_potted.Add(ballSprite);
ballSprite.Position = new Vector2(50 * _potted.Count, 725);
body.QueueFree();
}
}
public void RemoveCueBall()
{
Ball oldCueBall = _cueBall;
_balls.Remove(oldCueBall);
RemoveChild(oldCueBall);
oldCueBall.QueueFree();
}
public void ResetCueBall()
{
_cueBall = Ball.Create(0, _startPosition);
_cueBall.SetName("CueBall");
AddChild(_cueBall);
Texture2D image = GD.Load<Texture2D>("res://art/cue_ball.png");
_cueBall.GetNode<Sprite2D>("Image").Texture = image;
_balls = GetTree().GetNodesInGroup("balls").Select(b => (Ball)b).ToList<Ball>();
//_takingShot = false;
}
public void Start()
{
ResetCueBall();
GenerateBalls();
GetNode<Cue>("Cue").ShowCue();
}
private void OnCueShoot(Vector2 impulse)
{
_cueBall.ApplyCentralImpulse(impulse);
}
}