From 8ab2f09e624e94ef7dac4ee22b0052ae638e7fc3 Mon Sep 17 00:00:00 2001 From: Fries Date: Sun, 21 May 2023 18:19:05 -0700 Subject: [PATCH] optimize memory allocations i made the allocations of objects go down a lot. which is cool :3! --- Scenes/Objects/Walls.tscn | 2 +- Scenes/Pong.tscn | 2 +- Scripts/Managers/PongSceneManager.cs | 4 +++- Scripts/Managers/Walls.cs | 2 +- Scripts/Objects/Enemy.cs | 24 +++++++++++++----------- Scripts/Objects/Paddle.cs | 10 ++++++---- 6 files changed, 25 insertions(+), 19 deletions(-) diff --git a/Scenes/Objects/Walls.tscn b/Scenes/Objects/Walls.tscn index 1547fcd..0e2a530 100644 --- a/Scenes/Objects/Walls.tscn +++ b/Scenes/Objects/Walls.tscn @@ -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"] diff --git a/Scenes/Pong.tscn b/Scenes/Pong.tscn index 8141f75..c534e22 100644 --- a/Scenes/Pong.tscn +++ b/Scenes/Pong.tscn @@ -4,7 +4,7 @@ [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" 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"] [node name="Pong" type="Node2D"] diff --git a/Scripts/Managers/PongSceneManager.cs b/Scripts/Managers/PongSceneManager.cs index 8169bcb..da2a5a3 100644 --- a/Scripts/Managers/PongSceneManager.cs +++ b/Scripts/Managers/PongSceneManager.cs @@ -4,9 +4,11 @@ namespace Pong.Scripts.Managers; public partial class PongSceneManager : Node2D { + private readonly StringName _reloadAction = new("scene_reload"); + public override void _Process(double delta) { - if (Input.IsActionJustPressed("scene_reload")) + if (Input.IsActionJustPressed(_reloadAction)) { GetTree().ReloadCurrentScene(); } diff --git a/Scripts/Managers/Walls.cs b/Scripts/Managers/Walls.cs index c2d45da..be4179c 100644 --- a/Scripts/Managers/Walls.cs +++ b/Scripts/Managers/Walls.cs @@ -36,7 +36,7 @@ public partial class Walls : Area2D { ball.Velocity = Vector2.Zero; ball.Position = Vector2.Zero; - await ToSignal(GetTree().CreateTimer(0.25), "timeout"); + await ToSignal(GetTree().CreateTimer(0.25), SceneTreeTimer.SignalName.Timeout); ball.FlickBall(); } } diff --git a/Scripts/Objects/Enemy.cs b/Scripts/Objects/Enemy.cs index f4826f2..c0dc227 100644 --- a/Scripts/Objects/Enemy.cs +++ b/Scripts/Objects/Enemy.cs @@ -11,16 +11,26 @@ public partial class Enemy : RigidBody2D /// draw shapes to the screen (like ). /// [Export] private bool _drawDebugShapes; - [Export] private double _moveSpeed; private CollisionShape2D _collisionShape; private Rect2 _scanArea; + + private PhysicsDirectSpaceState2D _spaceState; + private readonly Variant _collider = "collider"; + private PhysicsShapeQueryParameters2D _query; public override void _Ready() { _collisionShape = GetNode("CollisionShape2D"); GenerateCastArea(); + + _spaceState = GetWorld2D().DirectSpaceState; + _query = new PhysicsShapeQueryParameters2D + { + Shape = new RectangleShape2D { Size = _scanArea.Size }, + Exclude = new Array(new[] { GetRid() }) + }; } public override void _PhysicsProcess(double delta) @@ -36,15 +46,7 @@ public partial class Enemy : RigidBody2D private void Scan(double delta) { - using var spaceState = GetWorld2D().DirectSpaceState; - - using var query = new PhysicsShapeQueryParameters2D - { - Shape = new RectangleShape2D { Size = _scanArea.Size }, - Exclude = new Array(new[] { GetRid() }) - }; - - var result = spaceState.IntersectShape(query); + var result = _spaceState.IntersectShape(_query); if (result.Count <= 0) { @@ -65,7 +67,7 @@ public partial class Enemy : RigidBody2D private void TrackBall(double delta, IReadOnlyList result) { // checks if the collider is a ball, if not, return. - if (result[0]["collider"].As() is not { } ball) return; + if (result[0][_collider].As() is not { } ball) return; // 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) }; diff --git a/Scripts/Objects/Paddle.cs b/Scripts/Objects/Paddle.cs index fc3d6d5..ba5bf16 100644 --- a/Scripts/Objects/Paddle.cs +++ b/Scripts/Objects/Paddle.cs @@ -7,12 +7,14 @@ public partial class Paddle : RigidBody2D [Export] private double _moveSpeed; private double _verticalInput; + private readonly StringName _paddleUp = "paddle_up"; + private readonly StringName _paddleDown = "paddle_down"; /// /// property that multiples the moveSpeed by the Meter constant. /// private double MoveSpeed => _moveSpeed * Constants.Meter; - + public override void _Process(double delta) { _verticalInput = GetVerticalInput(); @@ -27,14 +29,14 @@ public partial class Paddle : RigidBody2D /// a method that returns a number that represents input on the vertical axis. /// /// 1 for up, -1 for down, 0 for nothing. - private static double GetVerticalInput() + private double GetVerticalInput() { - if (Input.IsActionPressed("paddle_up")) + if (Input.IsActionPressed(_paddleUp)) { return 1; } - if (Input.IsActionPressed("paddle_down")) + if (Input.IsActionPressed(_paddleDown)) { return -1; }