2025-05-13 19:22:01 +08:00

319 lines
6.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using Godot;
using Godot.Bridge;
using Godot.NativeInterop;
[ScriptPath("res://Scripts/Main.cs")]
public partial class Main : Node2D
{
[Export(PropertyHint.None, "")]
public Color[] partyColors;
[Export(PropertyHint.None, "")]
public Label exitMes;
public static Main instance;
public static World2D worldSpace;
public static SceneTree tree;
public static RandomNumberGenerator random;
public static Coroutine inEvent;
private static Timer clock;
private static bool firstLoad;
private float exitTime;
private const float exitWait = 180f;
public static float deltaTime;
public static float physisDelta;
public static readonly Color colorWhite = new Color(1f, 1f, 1f);
public static readonly Color colorClear = new Color(1f, 1f, 1f, 0f);
public static readonly Color colorClearB = new Color(0f, 0f, 0f, 0f);
public static readonly Color colorDark = new Color(0.3f, 0.3f, 0.3f);
public static readonly Color colorTransparent = new Color(1f, 1f, 1f, 0.4f);
public static readonly Color colorGray = new Color(0.5f, 0.5f, 0.5f);
public static readonly Color colorYellow = new Color(1f, 1f, 0f);
public static readonly Color colorGreen = new Color(0f, 1f, 0f);
public static readonly Color colorBlack = new Color(0f, 0f, 0f);
public static readonly Color colorRed = new Color(1f, 0f, 0f);
public static readonly Color colorCyan = new Color(0f, 1f, 1f);
public static readonly Color colorBlue = new Color(0f, 0f, 1f);
public static readonly Color colorGlow = new Color(2f, 2f, 2f);
public static StringName[] keys = new StringName[12]
{
"Up", "Down", "Left", "Right", "Confirm", "Cancel", "Menu", "FastForward", "Escape", "Fullscreen",
"ResetBindings", "FPS"
};
public override void _EnterTree()
{
instance = this;
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
DisplayServer.WindowSetVsyncMode(DisplayServer.VSyncMode.Disabled);
worldSpace = GetWorld2D();
tree = GetTree();
random = new RandomNumberGenerator();
Room.current = null;
Timer node = new Timer
{
Name = "Clock",
WaitTime = 1.0,
Autostart = true,
OneShot = false
};
clock = node;
AddChild(node, forceReadableName: false, InternalMode.Disabled);
clock.Timeout += Clock;
SaveFile.current = new SaveFile();
SaveFile.current.lwItems.Add(Items.IDs.Hat);
SaveFile.current.activeParty.Add(0);
Party.SetUp();
Settings.LoadSettings();
Settings.file.lang = MainMenu.lastLang ?? "EnUS";
Texts.LoadText();
BattleDR.current = null;
BattleDR.reloadStats = null;
if (!firstLoad)
{
BattleDR.ShowFloatingText("999", Vector2.One * 999f);
}
if (CommonEvents.instance == null)
{
CommonEvents.instance = new CommonEvents();
}
AddChild(Audio.music = new AudioStreamPlayer
{
Name = "Music",
Bus = "Music"
}, forceReadableName: false, InternalMode.Disabled);
firstLoad = true;
UpdateFPS();
}
private void UpdateFPS()
{
switch (Settings.file.fpsMode)
{
case 0:
Engine.MaxFps = 60;
break;
case 1:
Engine.MaxFps = 30;
break;
case 2:
Engine.MaxFps = 0;
break;
}
}
private void Clock()
{
SaveFile.current.time[0]++;
if (SaveFile.current.time[0] >= 60)
{
SaveFile.current.time[1]++;
SaveFile.current.time[0] = 0;
if (SaveFile.current.time[1] >= 60 && SaveFile.current.time[2] < 999)
{
SaveFile.current.time[2]++;
SaveFile.current.time[1] = 0;
}
}
}
public override void _PhysicsProcess(double delta)
{
physisDelta = (float)delta * 60f;
}
public void GCCollect()
{
GC.Collect();
}
public override void _Process(double delta)
{
Coroutine coroutine = inEvent;
if (coroutine != null && coroutine.done)
{
inEvent = null;
if (Player.instance != null)
{
Player.instance.inputCD = 10f;
Player.instance.canInput = true;
Player.UpdateLastDir();
}
}
else if (inEvent != null && Player.instance != null)
{
Player.instance.waitForMove = true;
}
if (FightingGame.instance != null && !GodotObject.IsInstanceValid(FightingGame.instance))
{
FightingGame.instance = null;
}
deltaTime = (float)delta * 60f;
Coroutine.DoLoop();
if (Input.IsActionJustPressed(keys[10]))
{
Settings.file.keys = new Key[8]
{
Key.Up,
Key.Down,
Key.Left,
Key.Right,
Key.Z,
Key.X,
Key.C,
Key.Space
};
Settings.UpdateKeys();
}
if (Input.IsActionJustPressed(keys[11]))
{
Settings.file.fpsMode++;
if (Settings.file.fpsMode >= 3)
{
Settings.file.fpsMode = 0;
}
UpdateFPS();
}
if (Input.IsActionJustPressed(keys[9]))
{
Settings.file.fullScreen = !Settings.file.fullScreen;
Settings.UpdateWindow();
}
if (Input.IsActionPressed(keys[8]))
{
exitTime += deltaTime;
exitMes.Modulate = colorClear.Lerp(colorWhite, exitTime / 180f * 2f);
if (exitTime >= 180f)
{
tree.Quit();
}
}
else if (exitTime > 0f)
{
exitMes.Modulate = colorClear;
exitTime = 0f;
}
}
public static Node Particle(StringName name, Vector2 pos, int? sort = null, Node parent = null, bool localSpace = true)
{
Node node = GD.Load<PackedScene>(string.Concat("res://Objects/Particles/", name, ".tscn")).Instantiate(PackedScene.GenEditState.Disabled);
if (parent == null)
{
instance.AddChild(node, forceReadableName: false, InternalMode.Disabled);
}
else
{
parent.AddChild(node, forceReadableName: false, InternalMode.Disabled);
}
if (sort.HasValue && node is Node2D node2D)
{
node2D.ZIndex = sort.Value;
}
if (node is CpuParticles2D cpuParticles2D)
{
cpuParticles2D.Emitting = true;
if (localSpace)
{
cpuParticles2D.Position = pos;
}
else
{
cpuParticles2D.GlobalPosition = pos;
}
}
else if (node is Node2D node2D2)
{
if (localSpace)
{
node2D2.Position = pos;
}
else
{
node2D2.GlobalPosition = pos;
}
}
return node;
}
public static Vector2 GetDirection()
{
Vector2 zero = Vector2.Zero;
if (Input.IsActionPressed(keys[0]))
{
zero += Vector2.Up;
}
else if (Input.IsActionPressed(keys[1]))
{
zero += Vector2.Down;
}
if (Input.IsActionPressed(keys[2]))
{
zero += Vector2.Left;
}
else if (Input.IsActionPressed(keys[3]))
{
zero += Vector2.Right;
}
return zero;
}
public static float Repeat(float value, float max)
{
return Mathf.Clamp(value - Mathf.Floor(value / max) * max, 0f, max);
}
public static int RandomRange(int min, int max)
{
random.Randomize();
return random.RandiRange(min, max);
}
public static float RandomRange(float min, float max)
{
random.Randomize();
return random.RandfRange(min, max);
}
public static void SetActive(Node2D node, bool state)
{
node.ProcessMode = (ProcessModeEnum)(state ? 0 : 4);
node.Visible = state;
}
public static void SetActive(Control node, bool state)
{
node.ProcessMode = (ProcessModeEnum)(state ? 0 : 4);
node.Visible = state;
}
}