add an .editorconfig.

and i also added some events for when i want the ball and enemy paddle to move.
This commit is contained in:
Fries 2023-05-23 10:38:40 -07:00
parent 604d79e654
commit 3e61187065
6 changed files with 42 additions and 9 deletions

4
.editorconfig Normal file
View file

@ -0,0 +1,4 @@
[*]
end_of_line = lf
insert_final_newline = true
indent_style = tab

View file

@ -1,6 +1,6 @@
[gd_scene load_steps=8 format=3 uid="uid://bh3p1hnvsviu6"]
[ext_resource type="PackedScene" uid="uid://ijf4pw1wmb4t" path="res://Scenes/Objects/Paddle.tscn" id="1_5rs0o"]
[ext_resource type="PackedScene" path="res://Scenes/Objects/Paddle.tscn" id="1_5rs0o"]
[ext_resource type="Script" path="res://Scripts/Managers/PongSceneManager.cs" id="1_ee533"]
[ext_resource type="Script" path="res://Scripts/Managers/ScalingManager.cs" id="2_dc5sv"]
[ext_resource type="PackedScene" uid="uid://clsmrwvyrt7av" path="res://Scenes/UI/Score.tscn" id="2_f3jwj"]
@ -11,9 +11,6 @@
[node name="Pong" type="Node2D"]
script = ExtResource("1_ee533")
[node name="ScalingManager" type="Node" parent="."]
script = ExtResource("2_dc5sv")
[node name="Score" parent="." instance=ExtResource("2_f3jwj")]
offset_left = -370.0
offset_top = -270.0
@ -35,3 +32,6 @@ _maxRandomAngle = 0.42
[node name="Enemy" parent="." instance=ExtResource("4_uwvof")]
position = Vector2(350, 0)
_moveSpeed = 50.0
[node name="ScalingManager" type="Node" parent="."]
script = ExtResource("2_dc5sv")

View file

@ -1,6 +1,9 @@
namespace Pong.Scripts.Data;
public record GameArea
/// <summary>
/// a record class that represents the game area.
/// </summary>
public record class GameArea
{
/// <summary>
/// the X coordinate of the GameArea wrapped in a struct that makes operating on it easier. this is divided
@ -14,6 +17,10 @@ public record GameArea
/// </summary>
public GameCoordinate Y { get; }
/// <summary>
/// take a global position inside the game area and normalize it to a value between 0 and 1 which represents the game area.
/// </summary>
/// <param name="globalPosition">a Vector2 containing a global position inside the game area.</param>
public Vector2 NormalizePosition(Vector2 globalPosition)
{
var normalizedX = Mathf.InverseLerp(-X.ByWallSizeExtents, X.ByWallSizeExtents, globalPosition.X);
@ -22,6 +29,10 @@ public record GameArea
}
/// <summary>
/// take a normalized position and convert it back into a globalized position.
/// </summary>
/// <param name="normalizedPosition">a Vector2 containing a normalized position.</param>
public Vector2 GlobalizePosition(Vector2 normalizedPosition)
{
var globalizedX = Mathf.Lerp(-X.ByWallSizeExtents, X.ByWallSizeExtents, normalizedPosition.X);

View file

@ -20,6 +20,8 @@ public partial class ScalingManager : Node
private GameArea _gameArea;
public event EventHandler<Vector2> NewPaddlePosition;
public event EventHandler<Vector2> NewEnemyPosition;
public event EventHandler<Vector2> NewBallPosition;
public override void _EnterTree()
{
@ -57,6 +59,9 @@ public partial class ScalingManager : Node
{
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);
_gameResolution = DisplayServer.WindowGetSize();
_gameArea = new GameArea(_gameResolution);
@ -94,9 +99,24 @@ public partial class ScalingManager : Node
_score.Size = new Vector2(_gameResolution.X - 60, _gameResolution.Y - 60);
}
/// <summary>
/// set the paddle position to a normalized position that gets globalized. the paddle gets the new position with an event.
/// </summary>
private void SetPaddlePosition(Vector2 normalizedPosition)
{
var newPaddlePos = _gameArea.GlobalizePosition(normalizedPosition with { X = 0 }) + new Vector2(24, 0);
var newPaddlePos = _gameArea.GlobalizePosition(normalizedPosition with { X = 0 }) + new Vector2(48, 0);
NewPaddlePosition?.Invoke(this, newPaddlePos);
}
private void SetEnemyPosition(Vector2 normalizedPosition)
{
var newEnemyPos = _gameArea.GlobalizePosition(normalizedPosition with {X = 0}) + new Vector2(48,0);
NewEnemyPosition?.Invoke(this, newEnemyPos);
}
private void SetBallPosition(Vector2 normalizedPosition)
{
var newBallPos = _gameArea.GlobalizePosition(normalizedPosition);
NewBallPosition?.Invoke(this, newBallPos);
}
}

View file

@ -23,8 +23,6 @@ public partial class Paddle : RigidBody2D
public override void _EnterTree()
{
SetNotifyTransform(true);
SetNotifyLocalTransform(true);
_scalingManager = GetNode<ScalingManager>("../ScalingManager");
_scalingManager.NewPaddlePosition += (_, vector2) => _newPosition = vector2;
}