From 65ef70f583bdd134eea2dfdf15c3d033c5f67e5d Mon Sep 17 00:00:00 2001 From: Fries Date: Fri, 2 Jun 2023 17:15:44 -0700 Subject: [PATCH] the enemy can now see when the screen is adjusted. the scaling manager now controls when the various objects are active so stuff won't be null because of stuff running too fast or slow. --- Scripts/Managers/BasePaddle.cs | 10 ++---- Scripts/Managers/ScalingManager.cs | 50 ++++++++++++++++++------------ Scripts/Objects/Ball.cs | 5 --- Scripts/Objects/Enemy.cs | 40 ++++++++++++++++++------ Scripts/Objects/Paddle.cs | 8 ++--- 5 files changed, 67 insertions(+), 46 deletions(-) diff --git a/Scripts/Managers/BasePaddle.cs b/Scripts/Managers/BasePaddle.cs index f1ce280..1dbf827 100644 --- a/Scripts/Managers/BasePaddle.cs +++ b/Scripts/Managers/BasePaddle.cs @@ -4,15 +4,11 @@ public partial class BasePaddle: RigidBody2D { [Export] protected double MoveSpeed; - protected ScalingManager ScalingManager; + internal ScalingManager ScalingManager; protected Vector2? NewPosition; + internal bool Running; - public override void _EnterTree() - { - ScalingManager = GetNode("../ScalingManager"); - } - - protected void SetNewPosition(Vector2 newPosition) + internal void SetNewPosition(Vector2 newPosition) { NewPosition = newPosition; } diff --git a/Scripts/Managers/ScalingManager.cs b/Scripts/Managers/ScalingManager.cs index a9c497d..78c64f9 100644 --- a/Scripts/Managers/ScalingManager.cs +++ b/Scripts/Managers/ScalingManager.cs @@ -1,23 +1,23 @@ using System; using Pong.Scripts.Data; +using Pong.Scripts.Objects; namespace Pong.Scripts.Managers; public partial class ScalingManager : Node { private HFlowContainer _score; - private RigidBody2D _paddle; - private CharacterBody2D _ball; + private BasePaddle _paddle; + private Ball _ball; + private Enemy _enemy; private Area2D _leftWall; private Area2D _rightWall; private StaticBody2D _topWall; private StaticBody2D _bottomWall; - private RigidBody2D _enemy; - private Vector2I _gameResolution; - private GameArea _gameArea; + internal GameArea GameArea; public event Action NewPaddlePosition; public event Action NewEnemyPosition; @@ -41,15 +41,19 @@ public partial class ScalingManager : Node private void GetNodes() { _score = GetNode("../Score"); - _paddle = GetNode("../Paddle"); - _ball = GetNode("../Ball"); + _paddle = GetNode("../Paddle"); + _ball = GetNode("../Ball"); + _enemy = GetNode("../Enemy"); _leftWall = GetNode("../Walls/Left"); _rightWall = GetNode("../Walls/Right"); _topWall = GetNode("../Walls/Top"); _bottomWall = GetNode("../Walls/Bottom"); - - _enemy = GetNode("../Enemy"); + + _paddle.ScalingManager = this; + _enemy.ScalingManager = this; + NewPaddlePosition += _paddle.SetNewPosition; + NewEnemyPosition += _enemy.SetNewPosition; } /// @@ -57,18 +61,26 @@ public partial class ScalingManager : Node /// private void AdaptToGameResolution() { + _paddle.Running = false; + _enemy.Running = false; + var normalizedPaddlePosition = - _gameArea?.NormalizePosition(_paddle.Position) ?? new Vector2(0, 0.5); - var normalizedEnemyPosition = _gameArea?.NormalizePosition(_enemy.Position) ?? new Vector2(1, 0.5); - var normalizedBallPosition = _gameArea?.NormalizePosition(_ball.Position) ?? new Vector2(0, 0); + GameArea?.NormalizePosition(_paddle.Position) ?? new Vector2(0, 0.5); + var normalizedEnemyPosition = GameArea?.NormalizePosition(_enemy.Position) ?? new Vector2(1, 0.5); + var normalizedBallPosition = GameArea?.NormalizePosition(_ball.Position) ?? new Vector2(0, 0); _gameResolution = DisplayServer.WindowGetSize(); - _gameArea = new GameArea(_gameResolution); + GameArea = new GameArea(_gameResolution); SetWallPosition(); SetScorePosition(); SetPaddlePosition(normalizedPaddlePosition); SetEnemyPosition(normalizedEnemyPosition); + + _paddle.Running = true; + _enemy.GenerateCollisions(); + _enemy.Running = true; + _ball.FlickBall(); } /// @@ -76,8 +88,8 @@ public partial class ScalingManager : Node /// private void SetWallPosition() { - var xSize = _gameArea.X.ByWallSizeExtents; - var ySize = _gameArea.Y.ByWallSizeExtents; + var xSize = GameArea.X.ByWallSizeExtents; + var ySize = GameArea.Y.ByWallSizeExtents; _leftWall.Position = new Vector2(-xSize, 0); _rightWall.Position = new Vector2(xSize, 0); @@ -96,7 +108,7 @@ public partial class ScalingManager : Node /// private void SetScorePosition() { - _score.Position = new Vector2(-_gameArea.X.ByWallSize, -_gameArea.Y.ByWallSize); + _score.Position = new Vector2(-GameArea.X.ByWallSize, -GameArea.Y.ByWallSize); _score.Size = new Vector2(_gameResolution.X - 60, _gameResolution.Y - 60); } @@ -105,19 +117,19 @@ public partial class ScalingManager : Node /// private void SetPaddlePosition(Vector2 normalizedPosition) { - var newPaddlePos = _gameArea.GlobalizePosition(normalizedPosition with { X = 0 }) + new Vector2(48, 0); + var newPaddlePos = GameArea.GlobalizePosition(normalizedPosition with { X = 0 }) + new Vector2(48, 0); NewPaddlePosition?.Invoke(newPaddlePos); } private void SetEnemyPosition(Vector2 normalizedPosition) { - var newEnemyPos = _gameArea.GlobalizePosition(normalizedPosition with { X = 1 }) + new Vector2(-48, 0); + var newEnemyPos = GameArea.GlobalizePosition(normalizedPosition with { X = 1 }) + new Vector2(-48, 0); NewEnemyPosition?.Invoke(newEnemyPos); } private void SetBallPosition(Vector2 normalizedPosition) { - var newBallPos = _gameArea.GlobalizePosition(normalizedPosition); + var newBallPos = GameArea.GlobalizePosition(normalizedPosition); NewBallPosition?.Invoke(newBallPos); } diff --git a/Scripts/Objects/Ball.cs b/Scripts/Objects/Ball.cs index 177c56c..5e3e4fa 100644 --- a/Scripts/Objects/Ball.cs +++ b/Scripts/Objects/Ball.cs @@ -10,11 +10,6 @@ public partial class Ball : CharacterBody2D /// private double BallSpeed => _ballSpeed * Constants.Meter; - public override void _Ready() - { - FlickBall(); - } - /// /// flick the ball in a direction. /// diff --git a/Scripts/Objects/Enemy.cs b/Scripts/Objects/Enemy.cs index 4f063e7..d6ba637 100644 --- a/Scripts/Objects/Enemy.cs +++ b/Scripts/Objects/Enemy.cs @@ -23,11 +23,24 @@ public partial class Enemy : BasePaddle public override void _EnterTree() { base._EnterTree(); - ScalingManager.NewEnemyPosition += SetNewPosition; _collisionShape = GetNode("CollisionShape2D"); - GenerateCastArea(); + } - _spaceState = GetWorld2D().DirectSpaceState; + public override void _ExitTree() + { + _spaceState.Dispose(); + _query.Dispose(); + _collisionShape.Dispose(); + } + + internal void GenerateCollisions() + { + GenerateCastArea(); + GenerateQuery(); + } + + private void GenerateQuery() + { _query = new PhysicsShapeQueryParameters2D { Shape = new RectangleShape2D { Size = _scanArea.Size }, @@ -35,18 +48,23 @@ public partial class Enemy : BasePaddle }; } - public override void _PhysicsProcess(double delta) - { - } - public override void _IntegrateForces(PhysicsDirectBodyState2D state) { + _spaceState ??= GetWorld2D().DirectSpaceState; + + if (!Running) return; + if (NewPosition.HasValue) { state.Transform = new Transform2D(0, NewPosition.Value); + + _query.Dispose(); + + GenerateCastArea(); + GenerateQuery(); NewPosition = null; } - + Scan(GetPhysicsProcessDeltaTime(), state); } @@ -99,7 +117,11 @@ public partial class Enemy : BasePaddle if (_collisionShape.Shape is not RectangleShape2D shape) throw new InvalidOperationException("the collision shape needs to be a rectangle shape"); + var gameArea = ScalingManager.GameArea; + var verticalGrowSize = gameArea.Y.ByWallSizeExtents / 2; + // grow the area the enemy can see by around half of the screen area. - _scanArea = shape.GetRect().GrowSide(Side.Left, 400).GrowIndividual(0, 190, 0, 190); + _scanArea = shape.GetRect().GrowSide(Side.Left, gameArea.X.ByWallSizeExtents) + .GrowIndividual(0, verticalGrowSize, 0, verticalGrowSize); } } diff --git a/Scripts/Objects/Paddle.cs b/Scripts/Objects/Paddle.cs index 7182aaf..4f3b0cd 100644 --- a/Scripts/Objects/Paddle.cs +++ b/Scripts/Objects/Paddle.cs @@ -12,12 +12,6 @@ public partial class Paddle : BasePaddle private bool _canMove = true; private Vector2 _originalPosition; - public override void _EnterTree() - { - base._EnterTree(); - ScalingManager.NewPaddlePosition += SetNewPosition; - } - public override void _Process(double delta) { _verticalInput = GetVerticalInput(); @@ -25,6 +19,8 @@ public partial class Paddle : BasePaddle public override void _IntegrateForces(PhysicsDirectBodyState2D state) { + if (!Running) return; + if (NewPosition.HasValue) { state.Transform = new Transform2D(0, NewPosition.Value);