Skip to content

Commit

Permalink
perf: BreakShineController
Browse files Browse the repository at this point in the history
  • Loading branch information
LeZi9916 committed Oct 25, 2024
1 parent 64e57bd commit 47ee59c
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 88 deletions.
65 changes: 65 additions & 0 deletions Assets/Script/Game/Controllers/BreakSlideShineController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using MajdataPlay.Extensions;
using MajdataPlay.Interfaces;
using MajdataPlay.Types;
using MajdataPlay.Utils;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
#nullable enable
namespace MajdataPlay.Game.Controllers
{
public class BreakSlideShineController : MonoBehaviour
{
public IFlasher? Parent { get; set; } = null;
public SpriteRenderer[] Renderers { get; set; } = ArrayPool<SpriteRenderer>.Shared.Rent(0);

NoteStatus _state = NoteStatus.Start;
GamePlayManager _gpManager;

public void Initialize()
{
if (_state >= NoteStatus.Initialized)
return;
else if (Parent is null)
throw new NullReferenceException();
_gpManager = MajInstanceHelper<GamePlayManager>.Instance!;
if (Renderers.IsEmpty())
{
var parentObject = Parent.GameObject;
var barCount = parentObject.transform.childCount - 1;
Renderers = ArrayPool<SpriteRenderer>.Shared.Rent(barCount);
for (int i = 0; i < barCount; i++)
Renderers[i] = parentObject.transform.GetChild(i).GetComponent<SpriteRenderer>();
}
}
void Start()
{
Initialize();
}
void Update()
{
if (Renderers.IsEmpty())
return;
if (Parent is not null && Parent.CanShine)
{
var param = _gpManager.BreakParam;
foreach (var renderer in Renderers)
{
if (renderer is null)
continue;
renderer.material.SetFloat("_Brightness", param.Brightness);
renderer.material.SetFloat("_Contrast", param.Contrast);
}
}
}
void OnDestroy()
{
ArrayPool<SpriteRenderer>.Shared.Return(Renderers);
}
}

}
11 changes: 11 additions & 0 deletions Assets/Script/Game/Controllers/BreakSlideShineController.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Assets/Script/Game/Notes/SlideBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ public virtual void End(bool forceEnd = false)

foreach (GameObject obj in _slideBars)
obj.SetActive(false);

DestroyStars();
}
/// <summary>
Expand Down
7 changes: 5 additions & 2 deletions Assets/Script/Game/Notes/SlideDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,9 @@ protected override void LoadSkin()
barSprite = skin.Break;
starSprite = skin.Star.Break;
breakMaterial = skin.BreakMaterial;
var controller = gameObject.AddComponent<BreakSlideShineController>();
controller.Parent = this;
controller.Initialize();
}

foreach(var bar in bars)
Expand All @@ -474,8 +477,8 @@ protected override void LoadSkin()
if(breakMaterial != null)
{
barRenderer.material = breakMaterial;
var controller = bar.AddComponent<BreakShineController>();
controller.Parent = this;
//var controller = bar.AddComponent<BreakShineController>();
//controller.Parent = this;
}
}

Expand Down
7 changes: 5 additions & 2 deletions Assets/Script/Game/Notes/WifiDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,9 @@ protected override void LoadSkin()
barSprites = skin.Break;
starSprite = skin.Star.Break;
breakMaterial = skin.BreakMaterial;
var controller = gameObject.AddComponent<BreakSlideShineController>();
controller.Parent = this;
controller.Initialize();
}
foreach(var (i,bar) in bars.WithIndex())
{
Expand All @@ -341,8 +344,8 @@ protected override void LoadSkin()
if (breakMaterial != null)
{
barRenderer.material = breakMaterial;
var controller = bar.AddComponent<BreakShineController>();
controller.Parent = this;
//var controller = bar.AddComponent<BreakShineController>();
//controller.Parent = this;
}
}
foreach(var (i, star) in _stars.WithIndex())
Expand Down
2 changes: 1 addition & 1 deletion Assets/Script/Interfaces/IFlasher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace MajdataPlay.Interfaces
{
public interface IFlasher
public interface IFlasher : IGameObjectProvider
{
bool CanShine { get; }
}
Expand Down
7 changes: 4 additions & 3 deletions Assets/Script/Misc/Editor/LangVersionPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace MajdataPlay.Misc.Editor
public class LangVersionPatcher : AssetPostprocessor
{
const string OldVersion = "9.0";
const string NewVersion = "11.0";
const string NewVersion = "9.0";

[InitializeOnLoadMethod]
public static void Setup()
Expand All @@ -29,7 +29,8 @@ public static void Setup()
var arguments = PlayerSettings.GetAdditionalCompilerArguments(target);
if (arguments.Any(argument => argument.StartsWith(CscFlag)))
continue;
PlayerSettings.SetAdditionalCompilerArguments(target, arguments.Append(CscFlag + "preview").ToArray());
//PlayerSettings.SetAdditionalCompilerArguments(target, arguments.Append(CscFlag + "preview").ToArray());
PlayerSettings.SetAdditionalCompilerArguments(target, arguments.Append(CscFlag + "9.0").ToArray());
dirty = true;
}
catch
Expand All @@ -50,7 +51,7 @@ public static void Setup()
public static string OnGeneratedCSProject(string path, string content)
{
const string MsBuildFlag = "LangVersion";
return content.Replace($"<{MsBuildFlag}>{OldVersion}</{MsBuildFlag}>", $"<{MsBuildFlag}>11.0</{MsBuildFlag}>");
return content.Replace($"<{MsBuildFlag}>{OldVersion}</{MsBuildFlag}>", $"<{MsBuildFlag}>9.0</{MsBuildFlag}>");
}
}
}
161 changes: 82 additions & 79 deletions Assets/Script/Utils/Localization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,98 +7,101 @@
using System.Text.Json.Serialization;
using System.Text.Json;
#nullable enable
namespace MajdataPlay.Utils;
public static class Localization
namespace MajdataPlay.Utils
{
public static event EventHandler<Language>? OnLanguageChanged;
public static Language Current
public static class Localization
{
get => _current;
set
public static event EventHandler<Language>? OnLanguageChanged;
public static Language Current
{
_current = value;
if (OnLanguageChanged is not null)
OnLanguageChanged(null, value);
get => _current;
set
{
_current = value;
if (OnLanguageChanged is not null)
OnLanguageChanged(null, value);
}
}
}
readonly static JsonSerializerOptions jsonReaderOption = new()
{
readonly static JsonSerializerOptions jsonReaderOption = new()
{

Converters =
Converters =
{
new JsonStringEnumConverter()
},
ReadCommentHandling = JsonCommentHandling.Skip,
WriteIndented = true
};
public static Language[] Available { get; private set; } = Array.Empty<Language>();
public static void Initialize()
{
var path = GameManager.LangPath;
if (!Directory.Exists(path))
ReadCommentHandling = JsonCommentHandling.Skip,
WriteIndented = true
};
public static Language[] Available { get; private set; } = Array.Empty<Language>();
public static void Initialize()
{
Directory.CreateDirectory(path);
return;
var path = GameManager.LangPath;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
return;
}
var files = new DirectoryInfo(path).GetFiles()
.Where(x => x.Extension == ".json");
List<Language> loadedLangs = new();
foreach (var fileInfo in files)
{
var filePath = fileInfo.FullName;
var json = File.ReadAllText(filePath);
Language? lang = null;
if (Serializer.Json.TryDeserialize(json, out lang, jsonReaderOption) && lang is not null)
loadedLangs.Add(lang);
else
continue;
}
if (loadedLangs.IsEmpty())
return;
var grouped = loadedLangs.GroupBy(x => x.ToString());
Available = new Language[grouped.Count()];
foreach (var (i, grouping) in grouped.WithIndex())
Available[i] = grouping.First();
}
var files = new DirectoryInfo(path).GetFiles()
.Where(x => x.Extension == ".json");
List<Language> loadedLangs = new();
foreach (var fileInfo in files)
/// <summary>
/// Set language by code and author<para>such like: "zh-CN - Majdata"</para>
/// </summary>
/// <param name="langInfo"></param>
public static void SetLang(string langInfo)
{
var filePath = fileInfo.FullName;
var json = File.ReadAllText(filePath);
Language? lang = null;
if (Serializer.Json.TryDeserialize(json, out lang, jsonReaderOption) && lang is not null)
loadedLangs.Add(lang);
else
continue;
if (Available.IsEmpty())
return;
var result = Available.Find(x => x.ToString() == langInfo);
if (result is null)
return;
Current = result;
}
if (loadedLangs.IsEmpty())
return;
var grouped = loadedLangs.GroupBy(x => x.ToString());
Available = new Language[grouped.Count()];
foreach (var (i, grouping) in grouped.WithIndex())
Available[i] = grouping.First();
}
/// <summary>
/// Set language by code and author<para>such like: "zh-CN - Majdata"</para>
/// </summary>
/// <param name="langInfo"></param>
public static void SetLang(string langInfo)
{
if (Available.IsEmpty())
return;
var result = Available.Find(x => x.ToString() == langInfo);
if (result is null)
return;
Current = result;
}
public static void SetLangByCode(string code)
{
if (Available.IsEmpty())
return;
var result = Available.Find(x => x.Code == code);
if (result is null)
return;
Current = result;
}
public static string GetLocalizedText(MajText textType)
{
var table = Current.MappingTable;
var result = table.Find(x => x.Type == textType);
public static void SetLangByCode(string code)
{
if (Available.IsEmpty())
return;
var result = Available.Find(x => x.Code == code);
if (result is null)
return;
Current = result;
}
public static string GetLocalizedText(MajText textType)
{
var table = Current.MappingTable;
var result = table.Find(x => x.Type == textType);

return result?.Content ?? textType.ToString();
}
public static string GetLocalizedText(string origin)
{
var table = Current.MappingTable;
var result = table.Find(x => x.Origin == origin);
return result?.Content ?? textType.ToString();
}
public static string GetLocalizedText(string origin)
{
var table = Current.MappingTable;
var result = table.Find(x => x.Origin == origin);

return result?.Content ?? origin;
}
public static void GetLocalizedText(MajText textType, out string origin)
{
origin = GetLocalizedText(textType);
return result?.Content ?? origin;
}
public static void GetLocalizedText(MajText textType, out string origin)
{
origin = GetLocalizedText(textType);
}
static Language _current = Language.Default;
}
static Language _current = Language.Default;
}

0 comments on commit 47ee59c

Please sign in to comment.