optimize memory allocations
i made the allocations of objects go down a lot. which is cool :3!
This commit is contained in:
parent
425bf2ad27
commit
8ab2f09e62
6 changed files with 25 additions and 19 deletions
|
@ -1,4 +1,4 @@
|
||||||
[gd_scene load_steps=8 format=3]
|
[gd_scene load_steps=8 format=3 uid="uid://b1787fyse1y6m"]
|
||||||
|
|
||||||
[ext_resource type="Script" path="res://Scripts/Managers/Walls.cs" id="1_mxer2"]
|
[ext_resource type="Script" path="res://Scripts/Managers/Walls.cs" id="1_mxer2"]
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
[ext_resource type="Script" path="res://Scripts/Managers/PongSceneManager.cs" id="1_ee533"]
|
[ext_resource type="Script" path="res://Scripts/Managers/PongSceneManager.cs" id="1_ee533"]
|
||||||
[ext_resource type="PackedScene" uid="uid://clsmrwvyrt7av" path="res://Scenes/UI/Score.tscn" id="2_f3jwj"]
|
[ext_resource type="PackedScene" uid="uid://clsmrwvyrt7av" path="res://Scenes/UI/Score.tscn" id="2_f3jwj"]
|
||||||
[ext_resource type="PackedScene" path="res://Scenes/Objects/Ball.tscn" id="2_u2ksv"]
|
[ext_resource type="PackedScene" path="res://Scenes/Objects/Ball.tscn" id="2_u2ksv"]
|
||||||
[ext_resource type="PackedScene" path="res://Scenes/Objects/Walls.tscn" id="3_jfis7"]
|
[ext_resource type="PackedScene" uid="uid://b1787fyse1y6m" path="res://Scenes/Objects/Walls.tscn" id="3_jfis7"]
|
||||||
[ext_resource type="PackedScene" path="res://Scenes/Objects/Enemy.tscn" id="4_uwvof"]
|
[ext_resource type="PackedScene" path="res://Scenes/Objects/Enemy.tscn" id="4_uwvof"]
|
||||||
|
|
||||||
[node name="Pong" type="Node2D"]
|
[node name="Pong" type="Node2D"]
|
||||||
|
|
|
@ -4,9 +4,11 @@ namespace Pong.Scripts.Managers;
|
||||||
|
|
||||||
public partial class PongSceneManager : Node2D
|
public partial class PongSceneManager : Node2D
|
||||||
{
|
{
|
||||||
|
private readonly StringName _reloadAction = new("scene_reload");
|
||||||
|
|
||||||
public override void _Process(double delta)
|
public override void _Process(double delta)
|
||||||
{
|
{
|
||||||
if (Input.IsActionJustPressed("scene_reload"))
|
if (Input.IsActionJustPressed(_reloadAction))
|
||||||
{
|
{
|
||||||
GetTree().ReloadCurrentScene();
|
GetTree().ReloadCurrentScene();
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ public partial class Walls : Area2D
|
||||||
{
|
{
|
||||||
ball.Velocity = Vector2.Zero;
|
ball.Velocity = Vector2.Zero;
|
||||||
ball.Position = Vector2.Zero;
|
ball.Position = Vector2.Zero;
|
||||||
await ToSignal(GetTree().CreateTimer(0.25), "timeout");
|
await ToSignal(GetTree().CreateTimer(0.25), SceneTreeTimer.SignalName.Timeout);
|
||||||
ball.FlickBall();
|
ball.FlickBall();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,16 +11,26 @@ public partial class Enemy : RigidBody2D
|
||||||
/// draw shapes to the screen (like <see cref="_scanArea"/>).
|
/// draw shapes to the screen (like <see cref="_scanArea"/>).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[Export] private bool _drawDebugShapes;
|
[Export] private bool _drawDebugShapes;
|
||||||
|
|
||||||
[Export] private double _moveSpeed;
|
[Export] private double _moveSpeed;
|
||||||
|
|
||||||
private CollisionShape2D _collisionShape;
|
private CollisionShape2D _collisionShape;
|
||||||
private Rect2 _scanArea;
|
private Rect2 _scanArea;
|
||||||
|
|
||||||
|
private PhysicsDirectSpaceState2D _spaceState;
|
||||||
|
private readonly Variant _collider = "collider";
|
||||||
|
private PhysicsShapeQueryParameters2D _query;
|
||||||
|
|
||||||
public override void _Ready()
|
public override void _Ready()
|
||||||
{
|
{
|
||||||
_collisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
|
_collisionShape = GetNode<CollisionShape2D>("CollisionShape2D");
|
||||||
GenerateCastArea();
|
GenerateCastArea();
|
||||||
|
|
||||||
|
_spaceState = GetWorld2D().DirectSpaceState;
|
||||||
|
_query = new PhysicsShapeQueryParameters2D
|
||||||
|
{
|
||||||
|
Shape = new RectangleShape2D { Size = _scanArea.Size },
|
||||||
|
Exclude = new Array<Rid>(new[] { GetRid() })
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void _PhysicsProcess(double delta)
|
public override void _PhysicsProcess(double delta)
|
||||||
|
@ -36,15 +46,7 @@ public partial class Enemy : RigidBody2D
|
||||||
|
|
||||||
private void Scan(double delta)
|
private void Scan(double delta)
|
||||||
{
|
{
|
||||||
using var spaceState = GetWorld2D().DirectSpaceState;
|
var result = _spaceState.IntersectShape(_query);
|
||||||
|
|
||||||
using var query = new PhysicsShapeQueryParameters2D
|
|
||||||
{
|
|
||||||
Shape = new RectangleShape2D { Size = _scanArea.Size },
|
|
||||||
Exclude = new Array<Rid>(new[] { GetRid() })
|
|
||||||
};
|
|
||||||
|
|
||||||
var result = spaceState.IntersectShape(query);
|
|
||||||
|
|
||||||
if (result.Count <= 0)
|
if (result.Count <= 0)
|
||||||
{
|
{
|
||||||
|
@ -65,7 +67,7 @@ public partial class Enemy : RigidBody2D
|
||||||
private void TrackBall(double delta, IReadOnlyList<Dictionary> result)
|
private void TrackBall(double delta, IReadOnlyList<Dictionary> result)
|
||||||
{
|
{
|
||||||
// checks if the collider is a ball, if not, return.
|
// checks if the collider is a ball, if not, return.
|
||||||
if (result[0]["collider"].As<Objects.Ball>() is not { } ball) return;
|
if (result[0][_collider].As<Ball>() is not { } ball) return;
|
||||||
|
|
||||||
// gets the sign of the distance between the ball and the paddle on the y axis
|
// gets the sign of the distance between the ball and the paddle on the y axis
|
||||||
var normalisedDistance = new Vector2 { Y = Mathf.Sign(ball.Position.Y - Position.Y) };
|
var normalisedDistance = new Vector2 { Y = Mathf.Sign(ball.Position.Y - Position.Y) };
|
||||||
|
|
|
@ -7,12 +7,14 @@ public partial class Paddle : RigidBody2D
|
||||||
[Export] private double _moveSpeed;
|
[Export] private double _moveSpeed;
|
||||||
|
|
||||||
private double _verticalInput;
|
private double _verticalInput;
|
||||||
|
private readonly StringName _paddleUp = "paddle_up";
|
||||||
|
private readonly StringName _paddleDown = "paddle_down";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// property that multiples the moveSpeed by the <see cref="Constants.Meter">Meter</see> constant.
|
/// property that multiples the moveSpeed by the <see cref="Constants.Meter">Meter</see> constant.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private double MoveSpeed => _moveSpeed * Constants.Meter;
|
private double MoveSpeed => _moveSpeed * Constants.Meter;
|
||||||
|
|
||||||
public override void _Process(double delta)
|
public override void _Process(double delta)
|
||||||
{
|
{
|
||||||
_verticalInput = GetVerticalInput();
|
_verticalInput = GetVerticalInput();
|
||||||
|
@ -27,14 +29,14 @@ public partial class Paddle : RigidBody2D
|
||||||
/// a method that returns a number that represents input on the vertical axis.
|
/// a method that returns a number that represents input on the vertical axis.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>1 for up, -1 for down, 0 for nothing.</returns>
|
/// <returns>1 for up, -1 for down, 0 for nothing.</returns>
|
||||||
private static double GetVerticalInput()
|
private double GetVerticalInput()
|
||||||
{
|
{
|
||||||
if (Input.IsActionPressed("paddle_up"))
|
if (Input.IsActionPressed(_paddleUp))
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Input.IsActionPressed("paddle_down"))
|
if (Input.IsActionPressed(_paddleDown))
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue