7-13-25 3:38am

This commit is contained in:
2025-07-13 03:38:14 -04:00
parent 1d976cf322
commit 0a5f6be26d
15 changed files with 278 additions and 242 deletions

View File

@@ -3,4 +3,67 @@ using System;
public partial class Ball : RigidBody2D
{
public bool _placed, _available, _hovered, _selected, _aimed, _moving;
public float _moveThreshold = 5.0f;
public static Ball Create(int number, Vector2 position)
{
PackedScene scene = ResourceLoader.Load<PackedScene>("res://Gameplay/ball.tscn");
Ball newBall = scene.Instantiate<Ball>();
string fileName;
if (number == 0)
{
fileName = "res://art/cue_ball.png";
}
else
{
fileName = "res://art/ball_"+number+".png";
}
Texture2D image = GD.Load<Texture2D>(fileName);
newBall.GetNode<Sprite2D>("Image").Texture = image;
newBall.Position = position;
return newBall;
}
public override void _Ready()
{
_placed = false;
_hovered = false;
_selected = false;
_aimed = false;
_moving = false;
_available = false;
}
public override void _Process(double delta_)
{
_moving = false;
if (LinearVelocity.Length() > 0 && LinearVelocity.Length() < _moveThreshold)
{
Sleeping = true;
}
else if (LinearVelocity.Length() >= _moveThreshold)
{
_moving = true;
}
if (!((Battle)GetParent()).CheckMovement())
{
if (!_available)
{
_available = true;
GetParent().GetNode<Cue>("Cue").ShowCue();
}
}
else
{
if (_available)
{
_available = false;
GetParent().GetNode<Cue>("Cue").HideCue();
}
}
}
}

117
Gameplay/Battle.cs Normal file
View File

@@ -0,0 +1,117 @@
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);
}
}

1
Gameplay/Battle.cs.uid Normal file
View File

@@ -0,0 +1 @@
uid://0c1u4l8lu07h

View File

@@ -5,17 +5,24 @@ public partial class Cue : Sprite2D
{
[Signal]
public delegate void ShootEventHandler(Vector2 impulse);
public float _power = 0.0f;
public int _powerDirection = 1;
public override void _Process(double delta)
public int _powerDirection = 1;
public float _power = 0.0f, _maxPower = 8.0f;
public ProgressBar _progressBar;
public override void _Ready()
{
_progressBar = GetParent().GetNode<ProgressBar>("PowerBar");
}
public override void _Process(double delta_)
{
Vector2 mousePosition = GetViewport().GetMousePosition();
LookAt(mousePosition);
if (Input.IsActionPressed("left_click"))
{
_power += 0.1f * _powerDirection;
if (_power >= ((Main)GetParent())._maxPower)
if (_power >= _maxPower)
{
_powerDirection = -1;
}
@@ -37,4 +44,21 @@ public partial class Cue : Sprite2D
}
public void HideCue()
{
SetProcess(false);
Hide();
_progressBar.Hide();
}
public void ShowCue()
{
Ball cueBall = GetParent().GetNode<Ball>("CueBall");
SetProcess(true);
Position = cueBall.Position;
_progressBar.Position = new Vector2(cueBall.Position.X - _progressBar.Size.X / 2, cueBall.Position.Y + _progressBar.Size.Y / 2);
Show();
_progressBar.Show();
}
}

View File

@@ -1,173 +1,10 @@
using Godot;
using System;
using System.Collections.Generic;
using System.Linq;
public partial class Main : Node
{
[Export]
public PackedScene BallScene;
public bool _takingShot, _cueBallPotted;
public float _maxPower = 8.0f, _moveThreshold = 5.0f;
public Vector2 _startPosition = new Vector2(890, 340);
public Ball _cueBall;
public List<Texture2D> _ballImages = new();
public List<Sprite2D> _potted = new();
public override void _Ready()
{
LoadBallImages();
NewGame();
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;
}
//GetNode<Table>("Table").GetNode<Area2D>("PocketTL").BodyEntered += PottedBall;
//GetNode<Table>("Table").GetNode<Area2D>("PocketTR").BodyEntered += PottedBall;
//GetNode<Table>("Table").GetNode<Area2D>("PocketR").BodyEntered += PottedBall;
//GetNode<Table>("Table").GetNode<Area2D>("PocketBR").BodyEntered += PottedBall;
//GetNode<Table>("Table").GetNode<Area2D>("PocketBL").BodyEntered += PottedBall;
//GetNode<Table>("Table").GetNode<Area2D>("PocketL").BodyEntered += PottedBall;
}
public override void _Process(double delta_)
{
bool moving = false;
List<Ball> balls = GetTree().GetNodesInGroup("balls").Select(b => (Ball)b).ToList<Ball>();
for (int i = 0; i < balls.Count; i++)
{
if (balls[i].LinearVelocity.Length() > 0 && balls[i].LinearVelocity.Length() < _moveThreshold)
{
balls[i].Sleeping = true;
}
else if (balls[i].LinearVelocity.Length() >= _moveThreshold)
{
moving = true;
}
}
if (!moving)
{
if (_cueBallPotted)
{
ResetCueBall();
_cueBallPotted = false;
}
if (!_takingShot)
{
_takingShot = true;
ShowCue();
}
}
else
{
if (_takingShot)
{
_takingShot = false;
HideCue();
}
}
}
public void GenerateBalls()
{
int count = 0;
int rows = 5;
int diameter = 36;
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < rows; j++)
{
Ball ball = BallScene.Instantiate<Ball>();
Vector2 position = new Vector2(250 + (i*(diameter)), 267 + (j*(diameter)) + (i*(diameter / 2)));
AddChild(ball);
ball.Position = position;
ball.GetNode<Sprite2D>("Texture").Texture = _ballImages[count];
count += 1;
}
rows -= 1;
}
}
public void HideCue()
{
GetNode<Cue>("Cue").SetProcess(false);
GetNode<Cue>("Cue").Hide();
GetNode<ProgressBar>("PowerBar").Hide();
}
public void LoadBallImages()
{
_ballImages.Clear();
for (int i = 1; i < 17; i++)
{
string fileName = "res://art/ball_"+i+".png";
Texture2D image = GD.Load<Texture2D>(fileName);
_ballImages.Add(image);
}
}
public void NewGame()
{
ResetCueBall();
GenerateBalls();
ShowCue();
}
public void PottedBall(Node2D body)
{
if (body == _cueBall)
{
_cueBallPotted = true;
RemoveCueBall();
}
else
{
Sprite2D ballSprite = new Sprite2D();
AddChild(ballSprite);
ballSprite.Texture = body.GetNode<Sprite2D>("Texture").Texture;
_potted.Add(ballSprite);
ballSprite.Position = new Vector2(50 * _potted.Count, 725);
body.QueueFree();
}
//GetNode<Table>("Table").GetNode<Area2D>("Pockets").GetNode<Area2D>("Pockets")
}
public void RemoveCueBall()
{
Ball oldCueBall = _cueBall;
RemoveChild(oldCueBall);
oldCueBall.QueueFree();
}
public void ResetCueBall()
{
_cueBall = BallScene.Instantiate<Ball>();
AddChild(_cueBall);
_cueBall.Position = _startPosition;
_cueBall.GetNode<Sprite2D>("Texture").Texture = _ballImages[^1];
_takingShot = false;
}
public void ShowCue()
{
GetNode<Cue>("Cue").SetProcess(true);
GetNode<Cue>("Cue").Position = _cueBall.Position;
GetNode<ProgressBar>("PowerBar").Position = new Vector2(_cueBall.Position.X - GetNode<ProgressBar>("PowerBar").Size.X / 2, _cueBall.Position.Y + GetNode<ProgressBar>("PowerBar").Size.Y / 2);
GetNode<Cue>("Cue").Show();
GetNode<ProgressBar>("PowerBar").Show();
}
private void OnCueShoot(Vector2 impulse)
{
_cueBall.ApplyCentralImpulse(impulse);
}
}

View File

@@ -10,6 +10,6 @@ public partial class PowerBar : ProgressBar
public override void _Process(double delta)
{
Value = ((Main)GetParent()).GetNode<Cue>("Cue")._power / ((Main)GetParent())._maxPower * 100;
Value = GetParent().GetNode<Cue>("Cue")._power / GetParent().GetNode<Cue>("Cue")._maxPower * 100;
}
}

View File

@@ -1,10 +1,7 @@
[gd_scene load_steps=9 format=3 uid="uid://c8n4lue2bn25n"]
[gd_scene load_steps=6 format=3 uid="uid://c8n4lue2bn25n"]
[ext_resource type="Script" uid="uid://dgdxx8tceiljg" path="res://Gameplay/Ball.cs" id="1_7ritg"]
[ext_resource type="Shader" uid="uid://b6vvt5o0008ob" path="res://shaders/globe.gdshader" id="2_6v01e"]
[ext_resource type="Texture2D" uid="uid://duy62uv828xcg" path="res://art/shadow.png" id="4_803qd"]
[ext_resource type="Texture2D" uid="uid://c4jron4g4jalp" path="res://art/shine.png" id="5_fkve3"]
[ext_resource type="FontFile" uid="uid://dave7ql2luyk1" path="res://fonts/Xolonium-Regular.ttf" id="6_0l1om"]
[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_yj7wd"]
bounce = 5.0
@@ -27,7 +24,7 @@ contact_monitor = true
max_contacts_reported = 1
script = ExtResource("1_7ritg")
[node name="Texture" type="Sprite2D" parent="."]
[node name="Image" type="Sprite2D" parent="."]
texture_filter = 1
material = SubResource("ShaderMaterial_yj7wd")
@@ -35,31 +32,6 @@ material = SubResource("ShaderMaterial_yj7wd")
position = Vector2(0, -7.10543e-15)
shape = SubResource("CircleShape2D_803qd")
[node name="Shadow" type="Sprite2D" parent="."]
visible = false
position = Vector2(0, -9.36601e-08)
scale = Vector2(0.148438, 0.148438)
texture = ExtResource("4_803qd")
[node name="Shine" type="Sprite2D" parent="."]
visible = false
position = Vector2(0, -9.36601e-08)
scale = Vector2(0.046875, 0.046875)
texture = ExtResource("5_fkve3")
[node name="Number" type="Label" parent="."]
visible = false
offset_left = -9.0
offset_top = -10.0
offset_right = 9.0
offset_bottom = 10.0
theme_override_colors/font_color = Color(0, 0, 0, 1)
theme_override_fonts/font = ExtResource("6_0l1om")
theme_override_font_sizes/font_size = 10
text = "#"
horizontal_alignment = 1
vertical_alignment = 1
[connection signal="body_entered" from="." to="." method="OnBodyEntered"]
[connection signal="mouse_entered" from="." to="." method="OnMouseEntered"]
[connection signal="mouse_exited" from="." to="." method="OnMouseExited"]

43
Gameplay/battle.tscn Normal file
View File

@@ -0,0 +1,43 @@
[gd_scene load_steps=8 format=3 uid="uid://5ymxo45j4ryt"]
[ext_resource type="Script" uid="uid://0c1u4l8lu07h" path="res://Gameplay/Battle.cs" id="1_i431l"]
[ext_resource type="PackedScene" uid="uid://dsprg4uahkylm" path="res://Gameplay/table.tscn" id="2_6t8i5"]
[ext_resource type="PackedScene" uid="uid://dm4xk16ce0j" path="res://Gameplay/cue.tscn" id="3_dw4jg"]
[ext_resource type="Script" uid="uid://dbfpn1p62siat" path="res://Gameplay/PowerBar.cs" id="4_0npn6"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_tivnh"]
bg_color = Color(0.111197, 0.111197, 0.111197, 1)
border_width_left = 2
border_width_top = 2
border_width_right = 2
border_width_bottom = 2
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_ajequ"]
bg_color = Color(1, 1, 1, 0.458824)
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_aytjx"]
bg_color = Color(0.92549, 0.0901961, 0.0627451, 1)
[node name="Battle" type="Node"]
script = ExtResource("1_i431l")
[node name="Table" parent="." instance=ExtResource("2_6t8i5")]
[node name="PottedPanel" type="Panel" parent="."]
offset_top = 678.0
offset_right = 1200.0
offset_bottom = 778.0
theme_override_styles/panel = SubResource("StyleBoxFlat_tivnh")
[node name="Cue" parent="." instance=ExtResource("3_dw4jg")]
[node name="PowerBar" type="ProgressBar" parent="."]
z_index = 1
offset_right = 100.0
offset_bottom = 30.0
theme_override_styles/background = SubResource("StyleBoxFlat_ajequ")
theme_override_styles/fill = SubResource("StyleBoxFlat_aytjx")
show_percentage = false
script = ExtResource("4_0npn6")
[connection signal="Shoot" from="Cue" to="." method="OnCueShoot"]

View File

@@ -1,45 +1,9 @@
[gd_scene load_steps=9 format=3 uid="uid://yqtgkxjjexag"]
[gd_scene load_steps=3 format=3 uid="uid://yqtgkxjjexag"]
[ext_resource type="Script" uid="uid://v6ovq4snxruc" path="res://Gameplay/Main.cs" id="1_0xm2m"]
[ext_resource type="PackedScene" uid="uid://dsprg4uahkylm" path="res://table.tscn" id="2_vkc8e"]
[ext_resource type="PackedScene" uid="uid://c8n4lue2bn25n" path="res://Gameplay/ball.tscn" id="3_u78cq"]
[ext_resource type="PackedScene" uid="uid://dm4xk16ce0j" path="res://Gameplay/cue.tscn" id="4_u78cq"]
[ext_resource type="Script" uid="uid://dbfpn1p62siat" path="res://Gameplay/PowerBar.cs" id="5_tivnh"]
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_tivnh"]
bg_color = Color(0.111197, 0.111197, 0.111197, 1)
border_width_left = 2
border_width_top = 2
border_width_right = 2
border_width_bottom = 2
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_ajequ"]
bg_color = Color(1, 1, 1, 0.458824)
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_aytjx"]
bg_color = Color(0.92549, 0.0901961, 0.0627451, 1)
[ext_resource type="PackedScene" uid="uid://5ymxo45j4ryt" path="res://Gameplay/battle.tscn" id="3_vkc8e"]
[node name="Main" type="Node"]
script = ExtResource("1_0xm2m")
BallScene = ExtResource("3_u78cq")
[node name="Table" parent="." instance=ExtResource("2_vkc8e")]
[node name="PottedPanel" type="Panel" parent="."]
offset_top = 678.0
offset_right = 1200.0
offset_bottom = 778.0
theme_override_styles/panel = SubResource("StyleBoxFlat_tivnh")
[node name="Cue" parent="." instance=ExtResource("4_u78cq")]
[node name="PowerBar" type="ProgressBar" parent="."]
z_index = 1
offset_right = 100.0
offset_bottom = 30.0
theme_override_styles/background = SubResource("StyleBoxFlat_ajequ")
theme_override_styles/fill = SubResource("StyleBoxFlat_aytjx")
show_percentage = false
script = ExtResource("5_tivnh")
[connection signal="Shoot" from="Cue" to="." method="OnCueShoot"]
[node name="Battle" parent="." instance=ExtResource("3_vkc8e")]

View File

@@ -1,7 +1,7 @@
[gd_scene load_steps=11 format=3 uid="uid://dsprg4uahkylm"]
[gd_scene load_steps=12 format=3 uid="uid://dsprg4uahkylm"]
[ext_resource type="Texture2D" uid="uid://df2xulwx34fi" path="res://art/table.png" id="1_kl0hw"]
[ext_resource type="Script" uid="uid://82h0dmyohfj1" path="res://Table.cs" id="1_v5i0k"]
[ext_resource type="Script" uid="uid://82h0dmyohfj1" path="res://Gameplay/Table.cs" id="1_v5i0k"]
[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_v5i0k"]
bounce = 1.0
@@ -34,6 +34,9 @@ height = 154.0
[sub_resource type="CircleShape2D" id="CircleShape2D_v5i0k"]
radius = 28.0713
[sub_resource type="RectangleShape2D" id="RectangleShape2D_s3o1g"]
size = Vector2(192.5, 520)
[node name="Table" type="Sprite2D"]
texture_filter = 1
texture = ExtResource("1_kl0hw")
@@ -132,3 +135,15 @@ shape = SubResource("CircleShape2D_v5i0k")
[node name="L" type="CollisionShape2D" parent="Pots"]
position = Vector2(593, 629)
shape = SubResource("CircleShape2D_v5i0k")
[node name="PlayerStartArea" type="Area2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="PlayerStartArea"]
position = Vector2(983.75, 340)
shape = SubResource("RectangleShape2D_s3o1g")
[node name="EnemyStartArea" type="Area2D" parent="."]
[node name="CollisionShape2D" type="CollisionShape2D" parent="EnemyStartArea"]
position = Vector2(216, 340)
shape = SubResource("RectangleShape2D_s3o1g")

View File

Before

Width:  |  Height:  |  Size: 9.5 KiB

After

Width:  |  Height:  |  Size: 9.5 KiB

View File

@@ -3,15 +3,15 @@
importer="texture"
type="CompressedTexture2D"
uid="uid://ck4ta2f0ofjs0"
path="res://.godot/imported/ball_16.png-b2556cec5a1ae85449e8eb9a469008e8.ctex"
path="res://.godot/imported/cue_ball.png-82c061f2725a34218f16d54383e22ce9.ctex"
metadata={
"vram_texture": false
}
[deps]
source_file="res://art/ball_16.png"
dest_files=["res://.godot/imported/ball_16.png-b2556cec5a1ae85449e8eb9a469008e8.ctex"]
source_file="res://art/cue_ball.png"
dest_files=["res://.godot/imported/cue_ball.png-82c061f2725a34218f16d54383e22ce9.ctex"]
[params]