diff --git a/Gameplay/Ball.cs b/Gameplay/Ball.cs index 43d0796..695e693 100644 --- a/Gameplay/Ball.cs +++ b/Gameplay/Ball.cs @@ -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("res://Gameplay/ball.tscn"); + Ball newBall = scene.Instantiate(); + string fileName; + if (number == 0) + { + fileName = "res://art/cue_ball.png"; + } + else + { + fileName = "res://art/ball_"+number+".png"; + } + Texture2D image = GD.Load(fileName); + newBall.GetNode("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").ShowCue(); + } + } + else + { + if (_available) + { + _available = false; + GetParent().GetNode("Cue").HideCue(); + } + } + } + } diff --git a/Gameplay/Battle.cs b/Gameplay/Battle.cs new file mode 100644 index 0000000..3957b3f --- /dev/null +++ b/Gameplay/Battle.cs @@ -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 _potted = new(); + public List _balls; + + public override void _Ready() + { + Start(); + + List pockets = GetNode("Table").GetChildren() + .Where(n => n.GetName().ToString().ToLower().Contains("pocket")) + .Select(n => (Area2D)n).ToList(); + for (int i = 0; i < pockets.Count; i++) + { + pockets[i].BodyEntered += PottedBall; + } + _balls = GetTree().GetNodesInGroup("balls").Select(b => (Ball)b).ToList(); + + } + + 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("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("res://art/cue_ball.png"); + _cueBall.GetNode("Image").Texture = image; + _balls = GetTree().GetNodesInGroup("balls").Select(b => (Ball)b).ToList(); + //_takingShot = false; + } + + public void Start() + { + ResetCueBall(); + GenerateBalls(); + GetNode("Cue").ShowCue(); + } + + private void OnCueShoot(Vector2 impulse) + { + _cueBall.ApplyCentralImpulse(impulse); + } +} diff --git a/Gameplay/Battle.cs.uid b/Gameplay/Battle.cs.uid new file mode 100644 index 0000000..3b18a51 --- /dev/null +++ b/Gameplay/Battle.cs.uid @@ -0,0 +1 @@ +uid://0c1u4l8lu07h diff --git a/Gameplay/Cue.cs b/Gameplay/Cue.cs index 0d523bd..db90065 100644 --- a/Gameplay/Cue.cs +++ b/Gameplay/Cue.cs @@ -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("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("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(); + } + } diff --git a/Gameplay/Main.cs b/Gameplay/Main.cs index 7e2a06c..80eeb55 100644 --- a/Gameplay/Main.cs +++ b/Gameplay/Main.cs @@ -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 _ballImages = new(); - public List _potted = new(); - public override void _Ready() { - LoadBallImages(); - NewGame(); - List pockets = GetNode
("Table").GetChildren().Where(n => n.GetName().ToString().ToLower().Contains("pocket")).Select(n => (Area2D)n).ToList(); - - for (int i = 0; i < pockets.Count; i++) - { - pockets[i].BodyEntered += PottedBall; - } - //GetNode
("Table").GetNode("PocketTL").BodyEntered += PottedBall; - //GetNode
("Table").GetNode("PocketTR").BodyEntered += PottedBall; - //GetNode
("Table").GetNode("PocketR").BodyEntered += PottedBall; - //GetNode
("Table").GetNode("PocketBR").BodyEntered += PottedBall; - //GetNode
("Table").GetNode("PocketBL").BodyEntered += PottedBall; - //GetNode
("Table").GetNode("PocketL").BodyEntered += PottedBall; - } - - public override void _Process(double delta_) - { - bool moving = false; - List balls = GetTree().GetNodesInGroup("balls").Select(b => (Ball)b).ToList(); - 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(); - Vector2 position = new Vector2(250 + (i*(diameter)), 267 + (j*(diameter)) + (i*(diameter / 2))); - AddChild(ball); - - ball.Position = position; - ball.GetNode("Texture").Texture = _ballImages[count]; - count += 1; - - } - rows -= 1; - - } - } - - public void HideCue() - { - GetNode("Cue").SetProcess(false); - GetNode("Cue").Hide(); - GetNode("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(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("Texture").Texture; - _potted.Add(ballSprite); - ballSprite.Position = new Vector2(50 * _potted.Count, 725); - body.QueueFree(); - } - //GetNode
("Table").GetNode("Pockets").GetNode("Pockets") - } - - public void RemoveCueBall() - { - Ball oldCueBall = _cueBall; - RemoveChild(oldCueBall); - oldCueBall.QueueFree(); - } - - public void ResetCueBall() - { - _cueBall = BallScene.Instantiate(); - AddChild(_cueBall); - _cueBall.Position = _startPosition; - _cueBall.GetNode("Texture").Texture = _ballImages[^1]; - _takingShot = false; - - } - - public void ShowCue() - { - GetNode("Cue").SetProcess(true); - GetNode("Cue").Position = _cueBall.Position; - GetNode("PowerBar").Position = new Vector2(_cueBall.Position.X - GetNode("PowerBar").Size.X / 2, _cueBall.Position.Y + GetNode("PowerBar").Size.Y / 2); - GetNode("Cue").Show(); - GetNode("PowerBar").Show(); - } - - private void OnCueShoot(Vector2 impulse) - { - _cueBall.ApplyCentralImpulse(impulse); - } - } diff --git a/Gameplay/PowerBar.cs b/Gameplay/PowerBar.cs index 19b35e9..ce3a336 100644 --- a/Gameplay/PowerBar.cs +++ b/Gameplay/PowerBar.cs @@ -10,6 +10,6 @@ public partial class PowerBar : ProgressBar public override void _Process(double delta) { - Value = ((Main)GetParent()).GetNode("Cue")._power / ((Main)GetParent())._maxPower * 100; + Value = GetParent().GetNode("Cue")._power / GetParent().GetNode("Cue")._maxPower * 100; } } diff --git a/Table.cs b/Gameplay/Table.cs similarity index 100% rename from Table.cs rename to Gameplay/Table.cs diff --git a/Table.cs.uid b/Gameplay/Table.cs.uid similarity index 100% rename from Table.cs.uid rename to Gameplay/Table.cs.uid diff --git a/Gameplay/ball.tscn b/Gameplay/ball.tscn index 6637c07..ecc6dea 100644 --- a/Gameplay/ball.tscn +++ b/Gameplay/ball.tscn @@ -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"] diff --git a/Gameplay/battle.tscn b/Gameplay/battle.tscn new file mode 100644 index 0000000..11696ad --- /dev/null +++ b/Gameplay/battle.tscn @@ -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"] diff --git a/Gameplay/main.tscn b/Gameplay/main.tscn index f945db4..9b7bce3 100644 --- a/Gameplay/main.tscn +++ b/Gameplay/main.tscn @@ -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")] diff --git a/table.tscn b/Gameplay/table.tscn similarity index 87% rename from table.tscn rename to Gameplay/table.tscn index c96516f..640feb1 100644 --- a/table.tscn +++ b/Gameplay/table.tscn @@ -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") diff --git a/addons/godot-git-plugin/win64/~libgit_plugin.windows.editor.x86_64.dll b/addons/godot-git-plugin/win64/~libgit_plugin.windows.editor.x86_64.dll deleted file mode 100644 index 47bbb1d..0000000 Binary files a/addons/godot-git-plugin/win64/~libgit_plugin.windows.editor.x86_64.dll and /dev/null differ diff --git a/art/ball_16.png b/art/cue_ball.png similarity index 100% rename from art/ball_16.png rename to art/cue_ball.png diff --git a/art/ball_16.png.import b/art/cue_ball.png.import similarity index 73% rename from art/ball_16.png.import rename to art/cue_ball.png.import index 3d8ba60..9106230 100644 --- a/art/ball_16.png.import +++ b/art/cue_ball.png.import @@ -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]