From 8b1e7008f00b588d8e4cb3b67fa88e234d9aa52c Mon Sep 17 00:00:00 2001 From: wolfofRome Date: Sat, 8 Apr 2023 06:52:56 -0700 Subject: [PATCH] Code refactoring --- .../Pal3/Actor/ActorMovementController.cs | 6 +-- .../Pal3/Actor/Mv3ActorActionController.cs | 2 +- Assets/Scripts/Pal3/Actor/Path.cs | 13 +++-- .../Scripts/Pal3/Renderer/Mv3ModelRenderer.cs | 47 +++++++------------ 4 files changed, 26 insertions(+), 42 deletions(-) diff --git a/Assets/Scripts/Pal3/Actor/ActorMovementController.cs b/Assets/Scripts/Pal3/Actor/ActorMovementController.cs index 6016e991..9c174bdb 100644 --- a/Assets/Scripts/Pal3/Actor/ActorMovementController.cs +++ b/Assets/Scripts/Pal3/Actor/ActorMovementController.cs @@ -676,9 +676,9 @@ private void ReachingToEndOfPath() case EndOfPathActionType.WaitAndReverse: { _actionController.PerformAction(_actor.GetIdleAction()); - var waypoints = _currentPath.GetAllWayPoints(); - waypoints.Reverse(); - StartCoroutine(WaitForSomeTimeAndFollowPathAsync(waypoints.ToArray(), + Vector3[] waypoints = _currentPath.GetAllWayPoints(); + Array.Reverse(waypoints); + StartCoroutine(WaitForSomeTimeAndFollowPathAsync(waypoints, _currentPath.MovementMode, _movementCts.Token)); break; diff --git a/Assets/Scripts/Pal3/Actor/Mv3ActorActionController.cs b/Assets/Scripts/Pal3/Actor/Mv3ActorActionController.cs index 8480459f..5ede6daf 100644 --- a/Assets/Scripts/Pal3/Actor/Mv3ActorActionController.cs +++ b/Assets/Scripts/Pal3/Actor/Mv3ActorActionController.cs @@ -220,7 +220,7 @@ internal override void DisposeCurrentAction() if (_mv3AnimationRenderer != null) { _mv3AnimationRenderer.AnimationLoopPointReached -= AnimationLoopPointReached; - _mv3AnimationRenderer.DisposeAnimation(); + _mv3AnimationRenderer.Dispose(); } base.DisposeCurrentAction(); diff --git a/Assets/Scripts/Pal3/Actor/Path.cs b/Assets/Scripts/Pal3/Actor/Path.cs index 2c706468..9d55ded1 100644 --- a/Assets/Scripts/Pal3/Actor/Path.cs +++ b/Assets/Scripts/Pal3/Actor/Path.cs @@ -5,7 +5,6 @@ namespace Pal3.Actor { - using System.Collections.Generic; using UnityEngine; public enum EndOfPathActionType @@ -23,7 +22,7 @@ public class Path public bool IgnoreObstacle { get; private set; } - private readonly List _wayPoints = new (); + private Vector3[] _wayPoints = {}; private int _currentWayPointIndex; @@ -34,7 +33,7 @@ public class Path { Clear(); - _wayPoints.AddRange(wayPoints); + _wayPoints = wayPoints; _currentWayPointIndex = 0; MovementMode = movementMode; @@ -46,21 +45,21 @@ public void Clear() { MovementMode = 0; EndOfPathAction = EndOfPathActionType.Idle; - _wayPoints.Clear(); + _wayPoints = new Vector3[] {}; _currentWayPointIndex = 0; } public bool MoveToNextWayPoint() { - return ++_currentWayPointIndex < _wayPoints.Count; + return ++_currentWayPointIndex < _wayPoints.Length; } public bool IsEndOfPath() { - return !(_wayPoints.Count > 0 && _currentWayPointIndex < _wayPoints.Count); + return !(_wayPoints.Length > 0 && _currentWayPointIndex < _wayPoints.Length); } - public List GetAllWayPoints() + public Vector3[] GetAllWayPoints() { return _wayPoints; } diff --git a/Assets/Scripts/Pal3/Renderer/Mv3ModelRenderer.cs b/Assets/Scripts/Pal3/Renderer/Mv3ModelRenderer.cs index e98a5ca7..9c7c1ef0 100644 --- a/Assets/Scripts/Pal3/Renderer/Mv3ModelRenderer.cs +++ b/Assets/Scripts/Pal3/Renderer/Mv3ModelRenderer.cs @@ -64,7 +64,7 @@ public class Mv3ModelRenderer : MonoBehaviour, IDisposable ITextureResourceProvider tagNodeTextureProvider = default, Color? tagNodeTintColor = default) { - DisposeAnimation(); + Dispose(); _materialFactory = materialFactory; _textureProvider = textureProvider; @@ -106,7 +106,7 @@ public class Mv3ModelRenderer : MonoBehaviour, IDisposable var materialId = mesh.Attributes[0].MaterialId; Mv3Material material = mv3File.Materials[materialId]; - InitSubMeshes(i, mesh, material); + InitSubMeshes(i, ref mesh, ref material); } if (tagNodePolFile != null && _tagNodesInfo is {Length: > 0}) @@ -136,8 +136,8 @@ public class Mv3ModelRenderer : MonoBehaviour, IDisposable } private void InitSubMeshes(int index, - Mv3Mesh mv3Mesh, - Mv3Material material) + ref Mv3Mesh mv3Mesh, + ref Mv3Material material) { var textureName = material.TextureNames[0]; @@ -254,10 +254,21 @@ public void PauseAnimation() } } - public void DisposeAnimation() + public void Dispose() { PauseAnimation(); + if (_renderMeshComponents != null) + { + foreach (RenderMeshComponent renderMeshComponent in _renderMeshComponents) + { + Destroy(renderMeshComponent.Mesh); + Destroy(renderMeshComponent.MeshRenderer); + } + + _renderMeshComponents = null; + } + if (_meshObjects != null) { foreach (GameObject meshObject in _meshObjects) @@ -461,31 +472,5 @@ private void OnDisable() { Dispose(); } - - public void Dispose() - { - DisposeAnimation(); - - if (_renderMeshComponents != null) - { - foreach (RenderMeshComponent renderMeshComponent in _renderMeshComponents) - { - Destroy(renderMeshComponent.Mesh); - Destroy(renderMeshComponent.MeshRenderer); - } - - _renderMeshComponents = null; - } - - if (_meshObjects != null) - { - foreach (GameObject meshObject in _meshObjects) - { - Destroy(meshObject); - } - - _meshObjects = null; - } - } } } \ No newline at end of file