Files
2025-08-31 21:05:06 -04:00

238 lines
7.2 KiB
Plaintext

// Globals
::joe_jukebox <- {}
// Config
::joe_jukebox.extra_vol_adjust <- 1;
::joe_jukebox.vol <- 0.4;
::joe_jukebox.dir <- "tf2jukebox";
::joe_jukebox.ext <- ".wav";
//How-To:
// 1. Create a new playlist, by "name" : [ {track_data_here} ] etc.
// 2. Add playlist to end of jukebox ents in hammer, i.e. jukebox_toggle_playlist
// 3. Create in sounds folder. File structure is as goes: "playlist_name/track_name.wav"
// 3. Done
::joe_jukebox.data <- {
"tf2": [
{
file = "it_hates_me_so_much_loop_adpcm"
name = "It Hates Me So Much"
artist = "Valve Studio Orchestra"
},
{
file = "Magnum_Force_loop_adpcm"
name = "Magnum Force"
artist = "Valve Studio Orchestra"
},
{
file = "mercenary_park_loop_adpcm"
name = "Mercenary Park"
artist = "Valve Studio Orchestra"
},
{
file = "More_Gun_loop_adpcm"
name = "More Gun"
artist = "Valve Studio Orchestra"
},
{
file = "Red_Bread_loop_adpcm"
name = "Red Bread"
artist = "Valve Studio Orchestra"
},
{
file = "Seduce_me_loop_adpcm"
name = "Seduce Me"
artist = "Valve Studio Orchestra"
},
{
file = "Sniper_Remix_loop_adpcm"
name = "Sniper Remix"
artist = "???"
},
],
"retro": [
],
}
// Cache
foreach(name, playlist in joe_jukebox.data)
foreach(idx, track in playlist) {
playlist[idx].file = joe_jukebox.dir + "/" + name + "/" + track.file + joe_jukebox.ext;
PrecacheScriptSound(playlist[idx].file);
}
// Methods
::joe_jukebox.GetPlaylist <- function(jukebox_ent) {
local targetname = GetPropString(jukebox_ent, "m_iName");
local targetname_parts = split(targetname, "_");
local playlist_key = targetname_parts[targetname_parts.len() - 1];
return playlist_key;
}
::joe_jukebox_play <- function(ply = null) {
local data = GetCustomKV(ply, "jukebox");
data.on <- true;
local track = data.playlist[data.track_idx];
local sound = {
sound_name = track.file,
channel = CHAN_STATIC,
volume = data.vol,
sound_level = 0,
entity = ply,
filter_type = RECIPIENT_FILTER_SINGLE_PLAYER,
flags = SND_SHOULDPAUSE & SND_DO_NOT_OVERWRITE_EXISTING_ON_CHANNEL & SND_CHANGE_VOL,
};
// Play sound
for(local i = 0; i < joe_jukebox.extra_vol_adjust; ++i)
EmitSoundEx(sound);
// Display text
DrawText(ply, DRAW_TEXT_CHANNEL.HUD, "Now Playing: \n" + track.artist + " - " + track.name, 5.0, null, 0.15, null, null, null, 1.0, 1.0, null, DRAW_TEXT_VISIBILITY.PLY);
}
::joe_jukebox_stop <- function(ply = null) {
local data = GetCustomKV(ply, "jukebox");
data.on <- false;
local track = data.playlist[data.track_idx];
EmitSoundEx({
sound_name = track.file,
channel = CHAN_STATIC,
volume = 0.0,
sound_level = 0,
entity = ply,
filter_type = RECIPIENT_FILTER_SINGLE_PLAYER,
flags = SND_STOP,
});
ClearText(ply, DRAW_TEXT_CHANNEL.HUD);
}
::joe_jukebox_toggle <- function(ply = null) {
if(ply == null) ply = activator;
local data = GetCustomKV(ply, "jukebox");
//Changing jukebox?
local playlist = joe_jukebox.GetPlaylist(self);
if(data["playlist_name"] != playlist) {
data["playlist_name"] <- playlist;
data.playlist <- joe_jukebox.data[playlist];
data.track_idx <- 0;
data.track <- joe_jukebox.data[playlist][data.track_idx];
}
//Toggling
if(data.on)
joe_jukebox_stop(ply);
else
joe_jukebox_play(ply);
}
::joe_jukebox_next <- function(ply = null) {
if(ply == null) ply = activator;
local data = GetCustomKV(ply, "jukebox");
if(!data.on) return;
joe_jukebox_stop(ply);
data["track_idx"] <- wrap(data["track_idx"] + 1, 0, data["playlist"].len() - 1);
joe_jukebox_play(ply);
}
::joe_jukebox_previous <- function(ply = null) {
if(ply == null) ply = activator;
local data = GetCustomKV(ply, "jukebox");
if(!data.on) return;
joe_jukebox_stop(ply);
data["track_idx"] <- wrap(data["track_idx"] - 1, 0, data["playlist"].len() - 1);
joe_jukebox_play(ply);
}
::joe_jukebox_vol_up <- function(ply = null) {
if(ply == null) ply = activator;
local data = GetCustomKV(ply, "jukebox");
if(!data.on) return;
data.vol <- max(data.vol + 0.1, 1.0);
EmitSoundEx({
sound_name = data.playlist[data.track_idx]["file"],
channel = CHAN_STATIC,
volume = data.vol,
sound_level = 0,
entity = ply,
filter_type = RECIPIENT_FILTER_SINGLE_PLAYER,
flags = SND_CHANGE_VOL,
});
}
::joe_jukebox_vol_down <- function(ply = null) {
if(ply == null) ply = activator;
local data = GetCustomKV(ply, "jukebox");
if(!data.on) return;
data.vol <- min(data.vol - 0.1, 0.0);
EmitSoundEx({
sound_name = data.playlist[data.track_idx]["file"],
channel = CHAN_STATIC,
volume = data.vol,
sound_level = 0,
entity = ply,
filter_type = RECIPIENT_FILTER_SINGLE_PLAYER,
flags = SND_CHANGE_VOL,
});
}
::joe_jukebox_list <- function(ply = null) {
if(ply == null) ply = activator;
ClientPrintSafe(ply, "This jukebox has the following tracks:\n");
foreach(idx, track in joe_jukebox.data[joe_jukebox.GetPlaylist(self)])
ClientPrintSafe(ply, "^#4CFFF" + (idx + 1) + ".) " + track.artist + " - " + track.name + "\n");
}
// Hooks
OnGameEvent("player_spawn", function(table) {
local ply = GetPlayerFromUserID(table.userid);
if(!GetCustomKV(ply, "jukebox")) {
AddCustomKV(ply, "jukebox", {
on = false,
playlist_name = "tf2",
playlist = joe_jukebox.data["tf2"],
track_idx = 0,
vol = joe_jukebox.vol,
});
}
})
OnGameEvent("teamplay_restart_round", function(table) {
foreach(_, ply in GetAllPlayers())
joe_jukebox_stop(ply);
});
// Initialize
for(local button; button = Entities.FindByClassname(button, "func_button");) {
local targetname = GetPropString(button, "m_iName");
if(startswith(targetname, "jukebox_toggle"))
{
button.ConnectOutput("OnPressed", "joe_jukebox_toggle");
button.ValidateScriptScope();
}
else if(startswith(targetname, "jukebox_prev"))
{
button.ConnectOutput("OnPressed", "joe_jukebox_previous");
button.ValidateScriptScope();
}
else if(startswith(targetname, "jukebox_next"))
{
button.ConnectOutput("OnPressed", "joe_jukebox_next");
button.ValidateScriptScope();
}
else if(startswith(targetname, "jukebox_vol_up"))
{
button.ConnectOutput("OnPressed", "joe_jukebox_vol_up");
button.ValidateScriptScope();
}
else if(startswith(targetname, "jukebox_vol_down"))
{
button.ConnectOutput("OnPressed", "joe_jukebox_vol_down");
button.ValidateScriptScope();
}
else if(startswith(targetname, "jukebox_list"))
{
button.ConnectOutput("OnPressed", "joe_jukebox_list");
button.ValidateScriptScope();
}
}