diff --git a/Gameplay/Actor.cs b/Gameplay/Actor.cs new file mode 100644 index 0000000..7ebd4f6 --- /dev/null +++ b/Gameplay/Actor.cs @@ -0,0 +1,89 @@ +using Godot; +using System; +using System.Collections.Generic; +using System.Linq; + +public partial class Actor : Node +{ + public bool _available, _hovered, _selected; + public List _cues = new(); + public Cue _activeCue; + public List _balls = new(); + public Ball _activeBall; + + public static Actor Create(string SCENENAME) + { + PackedScene scene = ResourceLoader.Load("res://Gameplay/"+SCENENAME+".tscn"); + Actor newActor = scene.Instantiate(); + return newActor; + } + + public override void _Ready() + { + Ball newBall = Ball.Create("ball", 0); + AddChild(newBall); + _balls.Add(newBall); + if (_activeBall == null) + { + _activeBall = _balls[0]; + } + Cue newCue = Cue.Create("cue"); + AddChild(newCue); + _cues.Add(newCue); + if (_activeCue == null) + { + _activeCue = _cues[0]; + _activeCue.Shoot += OnCueShoot; + } + } + + public override void _Process(double DELTA_) + { + if (!_activeBall._placed) + { + Vector2 mousePosition = GetViewport().GetMousePosition(); + _activeBall.Position = mousePosition; + if (Input.IsActionJustReleased("left_click")) + { + _activeBall.Place(mousePosition); + } + } + else + { + _hovered = _activeBall._hovered; + if (_activeCue._shown) + { + if (!_activeBall._available) + { + _activeCue.HideCue(); + } + } + else // (!_activeCue._shown) + { + if (_activeBall._available) + { + _activeCue.ShowCue(_activeBall); + } + } + } + + + } + + + //public void ResetCueBall() + //{ + //_cueBall = Ball.Create("ball", 0, _startPosition); + //_cueBall.SetName("CueBall"); + //AddChild(_cueBall); + //Texture2D image = GD.Load("res://art/cue_ball.png"); + //_cueBall.GetNode("Image").Texture = image; + //_cueBall._placed = true; + //_balls = GetTree().GetNodesInGroup("balls").Select(b => (Ball)b).ToList(); + //} + + private void OnCueShoot(Vector2 IMPULSE) + { + _activeBall.ApplyCentralImpulse(IMPULSE); + } +} diff --git a/Gameplay/Actor.cs.uid b/Gameplay/Actor.cs.uid new file mode 100644 index 0000000..317323c --- /dev/null +++ b/Gameplay/Actor.cs.uid @@ -0,0 +1 @@ +uid://b4mr2vn8mw6r4 diff --git a/Gameplay/Ball.cs b/Gameplay/Ball.cs index 695e693..00aa1ac 100644 --- a/Gameplay/Ball.cs +++ b/Gameplay/Ball.cs @@ -3,57 +3,61 @@ using System; public partial class Ball : RigidBody2D { - - public bool _placed, _available, _hovered, _selected, _aimed, _moving; + public bool _placed, _potted, _available, _hovered, _selected, _aimed, _moving; public float _moveThreshold = 5.0f; - public static Ball Create(int number, Vector2 position) + public static Ball Create(string SCENENAME, int NUMBER) { - PackedScene scene = ResourceLoader.Load("res://Gameplay/ball.tscn"); + PackedScene scene = ResourceLoader.Load("res://Gameplay/"+SCENENAME+".tscn"); Ball newBall = scene.Instantiate(); string fileName; - if (number == 0) + if (NUMBER == 0) { fileName = "res://art/cue_ball.png"; } else { - fileName = "res://art/ball_"+number+".png"; + 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() { + SetProcess(false); _placed = false; + _available = false; _hovered = false; _selected = false; _aimed = false; _moving = false; - _available = false; } - public override void _Process(double delta_) + public override void _Process(double DELTA_) { - _moving = false; if (LinearVelocity.Length() > 0 && LinearVelocity.Length() < _moveThreshold) { Sleeping = true; + if (_moving) + { + _moving = false; + } } else if (LinearVelocity.Length() >= _moveThreshold) { - _moving = true; + if (!_moving) + { + _moving = true; + } } - if (!((Battle)GetParent()).CheckMovement()) + if (!Globals.Instance._anyMovement) { if (!_available) { _available = true; - GetParent().GetNode("Cue").ShowCue(); } } else @@ -61,9 +65,26 @@ public partial class Ball : RigidBody2D if (_available) { _available = false; - GetParent().GetNode("Cue").HideCue(); } } + + } + + public void Place(Vector2 POSITION) + { + _placed = true; + Position = POSITION; + SetProcess(true); + } + + private void OnMouseEntered() + { + _hovered = true; + } + + private void OnMouseExited() + { + _hovered = false; } } diff --git a/Gameplay/Battle.cs b/Gameplay/Battle.cs index 3957b3f..0d220b0 100644 --- a/Gameplay/Battle.cs +++ b/Gameplay/Battle.cs @@ -5,40 +5,37 @@ using System.Linq; public partial class Battle : Node { + [Signal] + public delegate void SetCurrentEventHandler(Battle BATTLE); + //[Signal] + //public delegate void DetectMovementEventHandler(bool BOOL); - public bool _anyMovement, _cueBallPotted; + public bool _current; public Vector2 _startPosition = new Vector2(890, 340); - public Ball _cueBall; + public Player _player; public List _potted = new(); public List _balls; + public override void _Ready() { + _player = GetNode("Player"); Start(); List pockets = GetNode("Table").GetChildren() .Where(n => n.GetName().ToString().ToLower().Contains("pocket")) - .Select(n => (Area2D)n).ToList(); + .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_) + 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; - } + CheckMovement(); } public void GenerateBalls() @@ -51,7 +48,8 @@ public partial class Battle : Node 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); + Ball ball = Ball.Create("ball", count); + ball.Place(position); AddChild(ball); count += 1; @@ -61,57 +59,59 @@ public partial class Battle : Node } } - public bool CheckMovement() + public void CheckMovement() { - return _balls.Any(b => b._moving); - } - - public void PottedBall(Node2D body) - { - if (body == _cueBall) + bool movementCheck = _balls.Any(b => b._moving && b._placed); + if (movementCheck) { - _cueBallPotted = true; - RemoveCueBall(); + if (!Globals.Instance._anyMovement) + { + Globals.Instance._anyMovement = true; + //EmitSignal(SignalName.DetectMovement, true); + + } } 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(); + if (Globals.Instance._anyMovement) + { + Globals.Instance._anyMovement = false; + //EmitSignal(SignalName.DetectMovement, false); + } } } - public void RemoveCueBall() + public void PottedBall(Node2D BODY) { - 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; + if (BODY.GetType() != typeof(Ball)){ + return; + } + if (BODY == _player._actor._activeBall) + { + GD.Print(1); + _player._actor._activeBall._potted = true; + _player._actor._activeBall._placed = false; + } + else + { + GD.Print(BODY); + Sprite2D ballSprite = new Sprite2D(); + AddChild(ballSprite); + ballSprite.Texture = BODY.GetNode("Image").Texture; + _potted.Add(ballSprite); + ballSprite.Position = new Vector2(50 * _potted.Count, 725); + ((Ball)BODY)._placed = false; + ((Ball)BODY)._potted = true; + BODY.QueueFree(); + } } public void Start() { - ResetCueBall(); + _current = true; GenerateBalls(); - GetNode("Cue").ShowCue(); - } - - private void OnCueShoot(Vector2 impulse) - { - _cueBall.ApplyCentralImpulse(impulse); + + EmitSignal(SignalName.SetCurrent, this); } + } diff --git a/Gameplay/Cue.cs b/Gameplay/Cue.cs index db90065..701643e 100644 --- a/Gameplay/Cue.cs +++ b/Gameplay/Cue.cs @@ -4,18 +4,28 @@ using System; public partial class Cue : Sprite2D { [Signal] - public delegate void ShootEventHandler(Vector2 impulse); + public delegate void ShootEventHandler(Vector2 IMPULSE); + public bool _shown; public int _powerDirection = 1; public float _power = 0.0f, _maxPower = 8.0f; public ProgressBar _progressBar; - public override void _Ready() + public static Cue Create(string SCENENAME) { - _progressBar = GetParent().GetNode("PowerBar"); + PackedScene scene = ResourceLoader.Load("res://Gameplay/"+SCENENAME+".tscn"); + Cue newCue = scene.Instantiate(); + Texture2D image = GD.Load("res://art/cue.png"); + newCue.Texture = image; + return newCue; } - public override void _Process(double delta_) + public override void _Ready() + { + //_progressBar = GetParent().GetNode("PowerBar"); + } + + public override void _Process(double DELTA_) { Vector2 mousePosition = GetViewport().GetMousePosition(); LookAt(mousePosition); @@ -46,19 +56,20 @@ public partial class Cue : Sprite2D public void HideCue() { + _shown = false; SetProcess(false); Hide(); - _progressBar.Hide(); + //_progressBar.Hide(); } - public void ShowCue() + public void ShowCue(Ball CUEBALL) { - Ball cueBall = GetParent().GetNode("CueBall"); + _shown = true; SetProcess(true); - Position = cueBall.Position; - _progressBar.Position = new Vector2(cueBall.Position.X - _progressBar.Size.X / 2, cueBall.Position.Y + _progressBar.Size.Y / 2); + Position = CUEBALL.Position; + //_progressBar.Position = new Vector2(CUEBALL.Position.X - _progressBar.Size.X / 2, CUEBALL.Position.Y + _progressBar.Size.Y / 2); Show(); - _progressBar.Show(); + //_progressBar.Show(); } } diff --git a/Gameplay/Globals.cs b/Gameplay/Globals.cs new file mode 100644 index 0000000..277afad --- /dev/null +++ b/Gameplay/Globals.cs @@ -0,0 +1,15 @@ +using Godot; +using System; + +public partial class Globals : Node +{ + public static Globals Instance; + + public bool _anyMovement; + public Battle _currentBattle; + + public override void _Ready() + { + Instance = this; + } +} diff --git a/Gameplay/Globals.cs.uid b/Gameplay/Globals.cs.uid new file mode 100644 index 0000000..41ae991 --- /dev/null +++ b/Gameplay/Globals.cs.uid @@ -0,0 +1 @@ +uid://brjvplo1avnoq diff --git a/Gameplay/Player.cs b/Gameplay/Player.cs new file mode 100644 index 0000000..686974e --- /dev/null +++ b/Gameplay/Player.cs @@ -0,0 +1,23 @@ +using Godot; +using System; + +public partial class Player : Node +{ + public bool _available, _selected; + public Actor _actor; + public Battle _currentBattle; + + public override void _Ready() + { + _actor = Actor.Create("actor"); + AddChild(_actor); + } + + public override void _Process(double DELTA_) + { + + } + + + +} diff --git a/Gameplay/Player.cs.uid b/Gameplay/Player.cs.uid new file mode 100644 index 0000000..afa3078 --- /dev/null +++ b/Gameplay/Player.cs.uid @@ -0,0 +1 @@ +uid://b66ysicyh0m1s diff --git a/Gameplay/PowerBar.cs b/Gameplay/PowerBar.cs index ce3a336..19b6e8f 100644 --- a/Gameplay/PowerBar.cs +++ b/Gameplay/PowerBar.cs @@ -8,8 +8,8 @@ public partial class PowerBar : ProgressBar } - public override void _Process(double delta) + public override void _Process(double DELTA_) { - Value = GetParent().GetNode("Cue")._power / GetParent().GetNode("Cue")._maxPower * 100; + Value = GetParent().GetNode("Actor").GetNode("Cue")._power / GetParent().GetNode("Actor").GetNode("Cue")._maxPower * 100; } } diff --git a/Gameplay/actor.tscn b/Gameplay/actor.tscn new file mode 100644 index 0000000..32dd72f --- /dev/null +++ b/Gameplay/actor.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://72tgm5p8d32r"] + +[ext_resource type="Script" uid="uid://b4mr2vn8mw6r4" path="res://Gameplay/Actor.cs" id="1_hr3hk"] + +[node name="Actor" type="Node"] +script = ExtResource("1_hr3hk") diff --git a/Gameplay/battle.tscn b/Gameplay/battle.tscn index 11696ad..2f8f5c0 100644 --- a/Gameplay/battle.tscn +++ b/Gameplay/battle.tscn @@ -1,9 +1,8 @@ -[gd_scene load_steps=8 format=3 uid="uid://5ymxo45j4ryt"] +[gd_scene load_steps=5 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"] +[ext_resource type="PackedScene" uid="uid://cdaxqopr35lll" path="res://Gameplay/player.tscn" id="2_ogveh"] [sub_resource type="StyleBoxFlat" id="StyleBoxFlat_tivnh"] bg_color = Color(0.111197, 0.111197, 0.111197, 1) @@ -12,32 +11,17 @@ 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"] +[node name="Battle" type="Node" groups=["battles"]] script = ExtResource("1_i431l") [node name="Table" parent="." instance=ExtResource("2_6t8i5")] +[node name="Player" parent="." instance=ExtResource("2_ogveh")] + [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"] +[connection signal="SetCurrent" from="." to="Player" method="BattleStart"] diff --git a/Gameplay/player.tscn b/Gameplay/player.tscn new file mode 100644 index 0000000..c17a902 --- /dev/null +++ b/Gameplay/player.tscn @@ -0,0 +1,23 @@ +[gd_scene load_steps=5 format=3 uid="uid://cdaxqopr35lll"] + +[ext_resource type="Script" uid="uid://b66ysicyh0m1s" path="res://Gameplay/Player.cs" id="1_4flbx"] +[ext_resource type="Script" uid="uid://dbfpn1p62siat" path="res://Gameplay/PowerBar.cs" id="2_onrkg"] + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_i3pqv"] +bg_color = Color(1, 1, 1, 0.458824) + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_hqtel"] +bg_color = Color(0.92549, 0.0901961, 0.0627451, 1) + +[node name="Player" type="Node"] +script = ExtResource("1_4flbx") + +[node name="PowerBar" type="ProgressBar" parent="."] +visible = false +z_index = 1 +offset_right = 100.0 +offset_bottom = 30.0 +theme_override_styles/background = SubResource("StyleBoxFlat_i3pqv") +theme_override_styles/fill = SubResource("StyleBoxFlat_hqtel") +show_percentage = false +script = ExtResource("2_onrkg") diff --git a/Gameplay/table.tscn b/Gameplay/table.tscn index 640feb1..fd4646f 100644 --- a/Gameplay/table.tscn +++ b/Gameplay/table.tscn @@ -32,7 +32,7 @@ radius = 28.75 height = 154.0 [sub_resource type="CircleShape2D" id="CircleShape2D_v5i0k"] -radius = 28.0713 +radius = 15.0333 [sub_resource type="RectangleShape2D" id="RectangleShape2D_s3o1g"] size = Vector2(192.5, 520) @@ -113,7 +113,7 @@ shape = SubResource("CapsuleShape2D_bhh2s") [node name="Pots" type="Area2D" parent="."] [node name="TL" type="CollisionShape2D" parent="Pots"] -position = Vector2(56, 616) +position = Vector2(55, 617) shape = SubResource("CircleShape2D_v5i0k") [node name="TR" type="CollisionShape2D" parent="Pots"] @@ -121,7 +121,7 @@ position = Vector2(56, 63) shape = SubResource("CircleShape2D_v5i0k") [node name="R" type="CollisionShape2D" parent="Pots"] -position = Vector2(593, 49) +position = Vector2(593, 43) shape = SubResource("CircleShape2D_v5i0k") [node name="BR" type="CollisionShape2D" parent="Pots"] @@ -133,7 +133,7 @@ position = Vector2(1135, 616) shape = SubResource("CircleShape2D_v5i0k") [node name="L" type="CollisionShape2D" parent="Pots"] -position = Vector2(593, 629) +position = Vector2(593, 634) shape = SubResource("CircleShape2D_v5i0k") [node name="PlayerStartArea" type="Area2D" parent="."] diff --git a/globals.tscn b/globals.tscn new file mode 100644 index 0000000..9949a71 --- /dev/null +++ b/globals.tscn @@ -0,0 +1,6 @@ +[gd_scene load_steps=2 format=3 uid="uid://dw3f81s6epejq"] + +[ext_resource type="Script" uid="uid://brjvplo1avnoq" path="res://Gameplay/Globals.cs" id="1_f6yec"] + +[node name="Globals" type="Node"] +script = ExtResource("1_f6yec") diff --git a/project.godot b/project.godot index 01bd0ec..3083532 100644 --- a/project.godot +++ b/project.godot @@ -15,6 +15,10 @@ run/main_scene="uid://yqtgkxjjexag" config/features=PackedStringArray("4.4", "C#", "Forward Plus") config/icon="res://icon.svg" +[autoload] + +Globals="*res://Gameplay/Globals.cs" + [display] window/size/viewport_width=1200