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:
parent
9fdf16c557
commit
65ef70f583
5 changed files with 67 additions and 46 deletions
|
@ -4,15 +4,11 @@ public partial class BasePaddle: RigidBody2D
|
||||||
{
|
{
|
||||||
[Export] protected double MoveSpeed;
|
[Export] protected double MoveSpeed;
|
||||||
|
|
||||||
protected ScalingManager ScalingManager;
|
internal ScalingManager ScalingManager;
|
||||||
protected Vector2? NewPosition;
|
protected Vector2? NewPosition;
|
||||||
|
internal bool Running;
|
||||||
|
|
||||||
public override void _EnterTree()
|
internal void SetNewPosition(Vector2 newPosition)
|
||||||
{
|
|
||||||
ScalingManager = GetNode<ScalingManager>("../ScalingManager");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void SetNewPosition(Vector2 newPosition)
|
|
||||||
{
|
{
|
||||||
NewPosition = newPosition;
|
NewPosition = newPosition;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,23 @@
|
||||||
using System;
|
using System;
|
||||||
using Pong.Scripts.Data;
|
using Pong.Scripts.Data;
|
||||||
|
using Pong.Scripts.Objects;
|
||||||
|
|
||||||
namespace Pong.Scripts.Managers;
|
namespace Pong.Scripts.Managers;
|
||||||
|
|
||||||
public partial class ScalingManager : Node
|
public partial class ScalingManager : Node
|
||||||
{
|
{
|
||||||
private HFlowContainer _score;
|
private HFlowContainer _score;
|
||||||
private RigidBody2D _paddle;
|
private BasePaddle _paddle;
|
||||||
private CharacterBody2D _ball;
|
private Ball _ball;
|
||||||
|
private Enemy _enemy;
|
||||||
|
|
||||||
private Area2D _leftWall;
|
private Area2D _leftWall;
|
||||||
private Area2D _rightWall;
|
private Area2D _rightWall;
|
||||||
private StaticBody2D _topWall;
|
private StaticBody2D _topWall;
|
||||||
private StaticBody2D _bottomWall;
|
private StaticBody2D _bottomWall;
|
||||||
|
|
||||||
private RigidBody2D _enemy;
|
|
||||||
|
|
||||||
private Vector2I _gameResolution;
|
private Vector2I _gameResolution;
|
||||||
private GameArea _gameArea;
|
internal GameArea GameArea;
|
||||||
|
|
||||||
public event Action<Vector2> NewPaddlePosition;
|
public event Action<Vector2> NewPaddlePosition;
|
||||||
public event Action<Vector2> NewEnemyPosition;
|
public event Action<Vector2> NewEnemyPosition;
|
||||||
|
@ -41,15 +41,19 @@ public partial class ScalingManager : Node
|
||||||
private void GetNodes()
|
private void GetNodes()
|
||||||
{
|
{
|
||||||
_score = GetNode<HFlowContainer>("../Score");
|
_score = GetNode<HFlowContainer>("../Score");
|
||||||
_paddle = GetNode<RigidBody2D>("../Paddle");
|
_paddle = GetNode<BasePaddle>("../Paddle");
|
||||||
_ball = GetNode<CharacterBody2D>("../Ball");
|
_ball = GetNode<Ball>("../Ball");
|
||||||
|
_enemy = GetNode<Enemy>("../Enemy");
|
||||||
|
|
||||||
_leftWall = GetNode<Area2D>("../Walls/Left");
|
_leftWall = GetNode<Area2D>("../Walls/Left");
|
||||||
_rightWall = GetNode<Area2D>("../Walls/Right");
|
_rightWall = GetNode<Area2D>("../Walls/Right");
|
||||||
_topWall = GetNode<StaticBody2D>("../Walls/Top");
|
_topWall = GetNode<StaticBody2D>("../Walls/Top");
|
||||||
_bottomWall = GetNode<StaticBody2D>("../Walls/Bottom");
|
_bottomWall = GetNode<StaticBody2D>("../Walls/Bottom");
|
||||||
|
|
||||||
_enemy = GetNode<RigidBody2D>("../Enemy");
|
_paddle.ScalingManager = this;
|
||||||
|
_enemy.ScalingManager = this;
|
||||||
|
NewPaddlePosition += _paddle.SetNewPosition;
|
||||||
|
NewEnemyPosition += _enemy.SetNewPosition;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -57,18 +61,26 @@ public partial class ScalingManager : Node
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void AdaptToGameResolution()
|
private void AdaptToGameResolution()
|
||||||
{
|
{
|
||||||
|
_paddle.Running = false;
|
||||||
|
_enemy.Running = false;
|
||||||
|
|
||||||
var normalizedPaddlePosition =
|
var normalizedPaddlePosition =
|
||||||
_gameArea?.NormalizePosition(_paddle.Position) ?? new Vector2(0, 0.5);
|
GameArea?.NormalizePosition(_paddle.Position) ?? new Vector2(0, 0.5);
|
||||||
var normalizedEnemyPosition = _gameArea?.NormalizePosition(_enemy.Position) ?? new Vector2(1, 0.5);
|
var normalizedEnemyPosition = GameArea?.NormalizePosition(_enemy.Position) ?? new Vector2(1, 0.5);
|
||||||
var normalizedBallPosition = _gameArea?.NormalizePosition(_ball.Position) ?? new Vector2(0, 0);
|
var normalizedBallPosition = GameArea?.NormalizePosition(_ball.Position) ?? new Vector2(0, 0);
|
||||||
|
|
||||||
_gameResolution = DisplayServer.WindowGetSize();
|
_gameResolution = DisplayServer.WindowGetSize();
|
||||||
_gameArea = new GameArea(_gameResolution);
|
GameArea = new GameArea(_gameResolution);
|
||||||
|
|
||||||
SetWallPosition();
|
SetWallPosition();
|
||||||
SetScorePosition();
|
SetScorePosition();
|
||||||
SetPaddlePosition(normalizedPaddlePosition);
|
SetPaddlePosition(normalizedPaddlePosition);
|
||||||
SetEnemyPosition(normalizedEnemyPosition);
|
SetEnemyPosition(normalizedEnemyPosition);
|
||||||
|
|
||||||
|
_paddle.Running = true;
|
||||||
|
_enemy.GenerateCollisions();
|
||||||
|
_enemy.Running = true;
|
||||||
|
_ball.FlickBall();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -76,8 +88,8 @@ public partial class ScalingManager : Node
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void SetWallPosition()
|
private void SetWallPosition()
|
||||||
{
|
{
|
||||||
var xSize = _gameArea.X.ByWallSizeExtents;
|
var xSize = GameArea.X.ByWallSizeExtents;
|
||||||
var ySize = _gameArea.Y.ByWallSizeExtents;
|
var ySize = GameArea.Y.ByWallSizeExtents;
|
||||||
|
|
||||||
_leftWall.Position = new Vector2(-xSize, 0);
|
_leftWall.Position = new Vector2(-xSize, 0);
|
||||||
_rightWall.Position = new Vector2(xSize, 0);
|
_rightWall.Position = new Vector2(xSize, 0);
|
||||||
|
@ -96,7 +108,7 @@ public partial class ScalingManager : Node
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void SetScorePosition()
|
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);
|
_score.Size = new Vector2(_gameResolution.X - 60, _gameResolution.Y - 60);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -105,19 +117,19 @@ public partial class ScalingManager : Node
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void SetPaddlePosition(Vector2 normalizedPosition)
|
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);
|
NewPaddlePosition?.Invoke(newPaddlePos);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetEnemyPosition(Vector2 normalizedPosition)
|
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);
|
NewEnemyPosition?.Invoke(newEnemyPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SetBallPosition(Vector2 normalizedPosition)
|
private void SetBallPosition(Vector2 normalizedPosition)
|
||||||
{
|
{
|
||||||
var newBallPos = _gameArea.GlobalizePosition(normalizedPosition);
|
var newBallPos = GameArea.GlobalizePosition(normalizedPosition);
|
||||||
NewBallPosition?.Invoke(newBallPos);
|
NewBallPosition?.Invoke(newBallPos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,11 +10,6 @@ public partial class Ball : CharacterBody2D
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private double BallSpeed => _ballSpeed * Constants.Meter;
|
private double BallSpeed => _ballSpeed * Constants.Meter;
|
||||||
|
|
||||||
public override void _Ready()
|
|
||||||
{
|
|
||||||
FlickBall();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// flick the ball in a direction.
|
/// flick the ball in a direction.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -23,11 +23,24 @@ public partial class Enemy : BasePaddle
|
||||||
public override void _EnterTree()
|
public override void _EnterTree()
|
||||||
{
|
{
|
||||||
base._EnterTree();
|
base._EnterTree();
|
||||||
ScalingManager.NewEnemyPosition += SetNewPosition;
|
|
||||||
_collisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
|
_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
|
_query = new PhysicsShapeQueryParameters2D
|
||||||
{
|
{
|
||||||
Shape = new RectangleShape2D { Size = _scanArea.Size },
|
Shape = new RectangleShape2D { Size = _scanArea.Size },
|
||||||
|
@ -35,15 +48,20 @@ public partial class Enemy : BasePaddle
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _PhysicsProcess(double delta)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void _IntegrateForces(PhysicsDirectBodyState2D state)
|
public override void _IntegrateForces(PhysicsDirectBodyState2D state)
|
||||||
{
|
{
|
||||||
|
_spaceState ??= GetWorld2D().DirectSpaceState;
|
||||||
|
|
||||||
|
if (!Running) return;
|
||||||
|
|
||||||
if (NewPosition.HasValue)
|
if (NewPosition.HasValue)
|
||||||
{
|
{
|
||||||
state.Transform = new Transform2D(0, NewPosition.Value);
|
state.Transform = new Transform2D(0, NewPosition.Value);
|
||||||
|
|
||||||
|
_query.Dispose();
|
||||||
|
|
||||||
|
GenerateCastArea();
|
||||||
|
GenerateQuery();
|
||||||
NewPosition = null;
|
NewPosition = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +117,11 @@ public partial class Enemy : BasePaddle
|
||||||
if (_collisionShape.Shape is not RectangleShape2D shape)
|
if (_collisionShape.Shape is not RectangleShape2D shape)
|
||||||
throw new InvalidOperationException("the collision shape needs to be a rectangle 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.
|
// 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,12 +12,6 @@ public partial class Paddle : BasePaddle
|
||||||
private bool _canMove = true;
|
private bool _canMove = true;
|
||||||
private Vector2 _originalPosition;
|
private Vector2 _originalPosition;
|
||||||
|
|
||||||
public override void _EnterTree()
|
|
||||||
{
|
|
||||||
base._EnterTree();
|
|
||||||
ScalingManager.NewPaddlePosition += SetNewPosition;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void _Process(double delta)
|
public override void _Process(double delta)
|
||||||
{
|
{
|
||||||
_verticalInput = GetVerticalInput();
|
_verticalInput = GetVerticalInput();
|
||||||
|
@ -25,6 +19,8 @@ public partial class Paddle : BasePaddle
|
||||||
|
|
||||||
public override void _IntegrateForces(PhysicsDirectBodyState2D state)
|
public override void _IntegrateForces(PhysicsDirectBodyState2D state)
|
||||||
{
|
{
|
||||||
|
if (!Running) return;
|
||||||
|
|
||||||
if (NewPosition.HasValue)
|
if (NewPosition.HasValue)
|
||||||
{
|
{
|
||||||
state.Transform = new Transform2D(0, NewPosition.Value);
|
state.Transform = new Transform2D(0, NewPosition.Value);
|
||||||
|
|
Loading…
Reference in a new issue