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.
This commit is contained in:
Fries 2023-06-02 17:15:44 -07:00
parent 9fdf16c557
commit 65ef70f583
5 changed files with 67 additions and 46 deletions

View file

@ -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>("../ScalingManager");
}
protected void SetNewPosition(Vector2 newPosition)
internal void SetNewPosition(Vector2 newPosition)
{
NewPosition = newPosition;
}

View file

@ -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<Vector2> NewPaddlePosition;
public event Action<Vector2> NewEnemyPosition;
@ -41,15 +41,19 @@ public partial class ScalingManager : Node
private void GetNodes()
{
_score = GetNode<HFlowContainer>("../Score");
_paddle = GetNode<RigidBody2D>("../Paddle");
_ball = GetNode<CharacterBody2D>("../Ball");
_paddle = GetNode<BasePaddle>("../Paddle");
_ball = GetNode<Ball>("../Ball");
_enemy = GetNode<Enemy>("../Enemy");
_leftWall = GetNode<Area2D>("../Walls/Left");
_rightWall = GetNode<Area2D>("../Walls/Right");
_topWall = GetNode<StaticBody2D>("../Walls/Top");
_bottomWall = GetNode<StaticBody2D>("../Walls/Bottom");
_enemy = GetNode<RigidBody2D>("../Enemy");
_paddle.ScalingManager = this;
_enemy.ScalingManager = this;
NewPaddlePosition += _paddle.SetNewPosition;
NewEnemyPosition += _enemy.SetNewPosition;
}
/// <summary>
@ -57,18 +61,26 @@ public partial class ScalingManager : Node
/// </summary>
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();
}
/// <summary>
@ -76,8 +88,8 @@ public partial class ScalingManager : Node
/// </summary>
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
/// </summary>
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
/// </summary>
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);
}

View file

@ -10,11 +10,6 @@ public partial class Ball : CharacterBody2D
/// </summary>
private double BallSpeed => _ballSpeed * Constants.Meter;
public override void _Ready()
{
FlickBall();
}
/// <summary>
/// flick the ball in a direction.
/// </summary>

View file

@ -23,11 +23,24 @@ public partial class Enemy : BasePaddle
public override void _EnterTree()
{
base._EnterTree();
ScalingManager.NewEnemyPosition += SetNewPosition;
_collisionShape = GetNode<CollisionShape2D>("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);
}
}

View file

@ -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);