update
This commit is contained in:
parent
2ab20b0e8c
commit
016c834f21
13 changed files with 1270 additions and 0 deletions
235
Assets/_/Features/Cheat/Runtime/Cheat.cs
Normal file
235
Assets/_/Features/Cheat/Runtime/Cheat.cs
Normal file
|
|
@ -0,0 +1,235 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Core.Runtime;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Cheat.Runtime
|
||||
{
|
||||
public class Cheat : BaseMonobehaviour
|
||||
{
|
||||
|
||||
#region Publics
|
||||
|
||||
//
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Unity API
|
||||
|
||||
void OnValidate()
|
||||
{
|
||||
# if UNITY_EDITOR
|
||||
|
||||
if (!Application.isPlaying)
|
||||
{
|
||||
GetComponentInChildren<Canvas>().enabled = false;
|
||||
}
|
||||
|
||||
# endif
|
||||
}
|
||||
|
||||
void Awake()
|
||||
{
|
||||
// Liste des actions possibles
|
||||
_commands.Add("quit", args => Application.Quit());
|
||||
_commands.Add("add money", args =>
|
||||
{
|
||||
if (args.Length == 0)
|
||||
{
|
||||
Warning("Usage: add money <amount>");
|
||||
return;
|
||||
}
|
||||
if (!int.TryParse(args[0], out int amount))
|
||||
{
|
||||
Warning($"Invalid amount: {args[0]}");
|
||||
return;
|
||||
}
|
||||
AddMoney(amount);
|
||||
});
|
||||
|
||||
_commands.Add("help", args => CommandsList());
|
||||
|
||||
_canvas = GetComponentInChildren<Canvas>();
|
||||
_canvas.enabled = false;
|
||||
_historyZone = GetComponentInChildren<TMP_Text>();
|
||||
}
|
||||
|
||||
void Start()
|
||||
{
|
||||
Warning("‼️☣️ CHEATCODE ENABLED");
|
||||
Info($"<color=green>Liste des commandes disponibles</color>");
|
||||
foreach (var c in _commands)
|
||||
{
|
||||
Info($"<color=yellow>{c}</color>");
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.Tab))
|
||||
{
|
||||
ToggleConsole();
|
||||
}
|
||||
|
||||
if (_consoleOpen)
|
||||
{
|
||||
foreach (char c in Input.inputString)
|
||||
{
|
||||
if (c == '\b') // Backspace
|
||||
{
|
||||
if (_input.Length > 0)
|
||||
_input = _input.Substring(0, _input.Length - 1);
|
||||
}
|
||||
else if (c == '\n' || c == '\r') // Entrée
|
||||
{
|
||||
ProcessCommand(_input);
|
||||
_input = string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
_input += c;
|
||||
}
|
||||
RefreshUI();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Main Methods
|
||||
|
||||
//
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
#region Utils
|
||||
|
||||
/* Fonctions privées utiles */
|
||||
|
||||
void RefreshUI()
|
||||
{
|
||||
if (_historyZone == null) return;
|
||||
_historyZone.text = string.Join("\n", _history.ToArray());
|
||||
}
|
||||
|
||||
void CommandsList()
|
||||
{
|
||||
AppendHistory("Rappel des commandes ");
|
||||
foreach (var cmd in _commands)
|
||||
{
|
||||
AppendHistory(cmd.Key);
|
||||
}
|
||||
}
|
||||
|
||||
// Ajoute une ligne d'historique et limite à _historyMaxLines
|
||||
void AppendHistory(string line)
|
||||
{
|
||||
if (string.IsNullOrEmpty(line)) return;
|
||||
|
||||
_history.Enqueue(line);
|
||||
while (_history.Count > _historyMaxLines)
|
||||
_history.Dequeue();
|
||||
}
|
||||
|
||||
void ProcessCommand(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input)) return;
|
||||
string raw = input.Trim();
|
||||
string lower = raw.ToLower();
|
||||
|
||||
// Find the longest matching command key at the start of the input
|
||||
string matchedKey = _commands.Keys
|
||||
.OrderByDescending(k => k.Length)
|
||||
.FirstOrDefault(k => lower.StartsWith(k));
|
||||
|
||||
if (!string.IsNullOrEmpty(matchedKey))
|
||||
{
|
||||
string argString = lower.Substring(matchedKey.Length).Trim();
|
||||
string[] args = string.IsNullOrEmpty(argString)
|
||||
? Array.Empty<string>()
|
||||
: argString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
AppendHistory(raw);
|
||||
try
|
||||
{
|
||||
_commands[matchedKey].Invoke(args);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Warning($"Error executing '{matchedKey}': {e.Message}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Warning($"🚨‼️ Command '{lower}' not found");
|
||||
}
|
||||
|
||||
_input = string.Empty;
|
||||
}
|
||||
|
||||
void ToggleConsole()
|
||||
{
|
||||
_consoleOpen = !_consoleOpen;
|
||||
_canvas.enabled = _consoleOpen;
|
||||
GameManager.Instance.IsOnPause = _consoleOpen;
|
||||
|
||||
if (_consoleOpen)
|
||||
{
|
||||
TextCaptured();
|
||||
}
|
||||
}
|
||||
|
||||
void TextCaptured()
|
||||
{
|
||||
foreach (char c in Input.inputString)
|
||||
{
|
||||
_input += c;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Commands Console
|
||||
|
||||
void AddMoney(int amount)
|
||||
{
|
||||
// Update or create a Money fact
|
||||
int current;
|
||||
bool hasMoney = true;
|
||||
try
|
||||
{
|
||||
current = GetFact<int>("Money");
|
||||
}
|
||||
catch
|
||||
{
|
||||
current = 0;
|
||||
hasMoney = false;
|
||||
}
|
||||
|
||||
int newValue = current + amount;
|
||||
SetFact("Money", newValue, FactPersistence.Persistent);
|
||||
AppendHistory($"Money set to {newValue}");
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Privates and Protected
|
||||
|
||||
// Variables privées
|
||||
bool _consoleOpen = false;
|
||||
Canvas _canvas;
|
||||
string _input = string.Empty;
|
||||
Dictionary<string, Action<string[]>> _commands = new Dictionary<string, Action<string[]>>();
|
||||
Queue<string> _history = new Queue<string>();
|
||||
const int _historyMaxLines = 5;
|
||||
TMP_Text _historyZone;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue