Files
fracturepoint/Assets/Scripts/CommonScripts/CommonData.cs
2026-01-29 06:36:26 -07:00

71 lines
2.3 KiB
C#

using Godot;
using System;
using System.Collections.Generic;
public partial class CommonData : Node
{
public bool Muted = true;
// Game Information
public const float FPS = 90.0f; // Max game fps
public float GlobalTimer = 0.0f; // Seconds elapsed since game start
int NumFramesElapsed = 0;
// Level information
public string CurrentLevel;
// Player stats
public float CurrentHealth;
public float MaxHealth = 100;
// Levels Completed
public HashSet<string> LevelsCompleted = new HashSet<string>();
public override void _Ready()
{
// Connect to the audio player
GetNode<AudioStreamPlayer>("AudioStreamPlayer").Finished += LoopAudio;
GetNode<AudioStreamPlayer>("AudioStreamPlayer").VolumeDb = -80; // temporary
// Start game by loading the camera
var scene = GD.Load<PackedScene>("res://Assets/Scenes/Persistant/Camera.tscn");
var inst = scene.Instantiate();
GetTree().Root.CallDeferred(Node.MethodName.AddChild, inst);
// Start game by loading the main menu
//scene = GD.Load<PackedScene>("res://Assets/Scenes/MenusAndLevels/MainMenu.tscn");
scene = GD.Load<PackedScene>("res://Assets/Scenes/DEVSCENES/EnemyTest.tscn");
inst = scene.Instantiate();
GetTree().Root.CallDeferred(Node.MethodName.AddChild, inst);
PlayMusic("MainMenu");
// Load in saved data
//....?
}
public override void _Process(double delta)
{
// Frame Counter
NumFramesElapsed += 1;
GlobalTimer = NumFramesElapsed / FPS;
GlobalTimer = (float)Convert.ToDouble(GlobalTimer.ToString("0.##")); // Truncates seconds elapsed to hundreds place (0.00)
//GD.Print(GlobalTimer);
if (Input.IsActionJustPressed("quit_game")) GetTree().Quit();
if (Input.IsActionJustPressed("mute")){
Muted = ! Muted;
GetNode<AudioStreamPlayer>("AudioStreamPlayer").VolumeDb = (Muted) ? -80 : 0;
}
}
// BG Audio Management
public void StopMusic(){GetNode<AudioStreamPlayer>("AudioStreamPlayer").Stop();}// Stop whatever audio is playing\
public void PlayMusic(string AudioName){
// Load target audio and play the audio
GetNode<AudioStreamPlayer>("AudioStreamPlayer").Stream = (Godot.AudioStream)GD.Load($"res://Assets/Audio/Music/{AudioName}.mp3");
GetNode<AudioStreamPlayer>("AudioStreamPlayer").Play();
}
void LoopAudio(){GetNode<AudioStreamPlayer>("AudioStreamPlayer").Play();}// Find when the stream finishes playing to play it again
}