Skip to content

Commit fa055fb

Browse files
committed
dynamic music (for real)
1 parent 01e34da commit fa055fb

File tree

3 files changed

+140
-1
lines changed

3 files changed

+140
-1
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
--------------------------------------- Instructions ---------------------------------------
2+
3+
------- Require this in your script like so:
4+
5+
-- self.GameIntensityCalculator = require("Activities/Utility/GameIntensityCalculator");
6+
-- self.GameIntensityCalculator:Initialize(Activity, bool newGame, bool verboseLogging, number intensityValuePerHPBarLost, number intensityDegradationRatePerSecond);
7+
8+
-- Call self.GameIntensityCalculator:UpdateGameIntensityCalculator() every frame.
9+
10+
-- The main update function will return an intensity number between 0 and 1.
11+
-- This corresponds to how much damage has been dealt directly within all the player's views.
12+
-- Use the two number arguments to control how the intensity value rises and falls.
13+
-- One "HP Bar" is considered a standard 100 HP.
14+
15+
------- Saving/Loading
16+
17+
-- Saving and loading requires you to also have the SaveLoadHandler ready.
18+
-- Simply run OnSave(instancedSaveLoadHandler) and OnLoad(instancedSaveLoadHandler) when appropriate.
19+
20+
--------------------------------------- Misc. Information ---------------------------------------
21+
22+
--
23+
24+
25+
26+
27+
local GameIntensityCalculator = {};
28+
29+
function GameIntensityCalculator:Create()
30+
local Members = {};
31+
32+
setmetatable(Members, self);
33+
self.__index = self;
34+
35+
return Members;
36+
end
37+
38+
function GameIntensityCalculator:Initialize(activity, newGame, intensityValuePerHPBarLost, intensityDegradationRatePerSecond, verboseLogging)
39+
self.verboseLogging = verboseLogging
40+
41+
self.Activity = activity;
42+
43+
if not intensityValuePerHPBarLost then
44+
intensityValuePerHPBarLost = 0.2;
45+
end
46+
if not intensityDegradationRatePerSecond then
47+
intensityDegradationRatePerSecond = 0.01;
48+
end
49+
50+
-- One "HP Bar" is considered 100 HP
51+
self.intensityValuePerHPBarLost = intensityValuePerHPBarLost;
52+
53+
self.intensityDegradationRatePerSecond = intensityDegradationRatePerSecond;
54+
55+
if newGame then
56+
self.saveTable = {};
57+
self.saveTable.CurrentIntensity = -0.5;
58+
end
59+
60+
print("INFO: GameIntensityCalculator initialized!")
61+
end
62+
63+
function GameIntensityCalculator:OnLoad(saveLoadHandler)
64+
print("INFO: GameIntensityCalculator loading...");
65+
self.saveTable = saveLoadHandler:ReadSavedStringAsTable("GameIntensityCalculatorMainTable");
66+
print("INFO: GameIntensityCalculator loaded!");
67+
end
68+
69+
function GameIntensityCalculator:OnSave(saveLoadHandler)
70+
print("INFO: GameIntensityCalculator saving...");
71+
saveLoadHandler:SaveTableAsString("GameIntensityCalculatorMainTable", self.saveTable);
72+
print("INFO: GameIntensityCalculator saved!");
73+
end
74+
75+
function GameIntensityCalculator:GetCurrentIntensity()
76+
return math.min(1, self.saveTable.CurrentIntensity, math.max(0, self.saveTable.CurrentIntensity));
77+
end
78+
79+
function GameIntensityCalculator:UpdateGameIntensityCalculator()
80+
self.saveTable.CurrentIntensity = math.max(-0.5, self.saveTable.CurrentIntensity - (self.intensityDegradationRatePerSecond * TimerMan.DeltaTimeSecs));
81+
82+
local healthLostThisFrame = 0;
83+
84+
for player = Activity.PLAYER_1, Activity.MAXPLAYERCOUNT - 1 do
85+
if self.Activity:PlayerActive(player) and self.Activity:PlayerHuman(player) then
86+
local screenBox = Box(CameraMan:GetOffset(player), CameraMan:GetOffset(player) + Vector(FrameMan.PlayerScreenWidth, FrameMan.PlayerScreenHeight));
87+
for mo in MovableMan:GetMOsInBox(screenBox, -1, true) do
88+
if IsActor(mo) then
89+
mo = ToActor(mo);
90+
healthLostThisFrame = healthLostThisFrame + mo.PrevHealth - mo.Health;
91+
end
92+
end
93+
end
94+
end
95+
96+
if healthLostThisFrame > 0 then
97+
--print("health lost this frame: " .. healthLostThisFrame);
98+
end
99+
100+
self.saveTable.CurrentIntensity = math.min(1.25, self.saveTable.CurrentIntensity + self.intensityValuePerHPBarLost * (healthLostThisFrame/100))
101+
102+
--print("intensity: " .. self.saveTable.CurrentIntensity);
103+
104+
return math.min(1, self.saveTable.CurrentIntensity, math.max(0, self.saveTable.CurrentIntensity));
105+
end
106+
107+
108+
return GameIntensityCalculator:Create();

Data/Browncoats.rte/Activities/RefineryAssault.lua

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ function RefineryAssault:StartActivity(newGame)
144144

145145
self.particleUtility = require("Scripts/Utility/ParticleUtility");
146146

147-
self.MOUtility = require("Scripts/Utility/MOUtility");
147+
self.MOUtility = require("Scripts/Utility/MOUtility");
148+
149+
self.GameIntensityCalculator = require("Activities/Utility/GameIntensityCalculator");
150+
self.GameIntensityCalculator:Initialize(self, newGame, 0.15, 0.01, self.verboseLogging);
148151

149152
self.actorSpawnerReturnedActors = {};
150153

@@ -180,6 +183,8 @@ function RefineryAssault:StartActivity(newGame)
180183
SceneMan.Scene:AddNavigatableArea("Mission Stage Area 3");
181184
SceneMan.Scene:AddNavigatableArea("Mission Stage Area 4");
182185

186+
self.musicGraceTimer = Timer();
187+
183188
if newGame then
184189

185190
self.saveTable = {};
@@ -358,6 +363,7 @@ function RefineryAssault:ResumeLoadedGame()
358363
self.HUDHandler:OnLoad(self.saveLoadHandler);
359364

360365
self.MOUtility:OnLoad(self.saveLoadHandler);
366+
self.GameIntensityCalculator:OnLoad(self.saveLoadHandler);
361367

362368
self.buyDoorHandler:ReplaceBuyDoorTable(self.saveTable.buyDoorTables.All);
363369

@@ -379,6 +385,7 @@ function RefineryAssault:OnSave()
379385
self.HUDHandler:OnSave(self.saveLoadHandler);
380386

381387
self.MOUtility:OnSave(self.saveLoadHandler);
388+
self.GameIntensityCalculator:OnSave(self.saveLoadHandler);
382389
end
383390

384391
function RefineryAssault:PauseActivity(pause)
@@ -531,6 +538,30 @@ function RefineryAssault:UpdateActivity()
531538
end
532539
end
533540

541+
-- Music
542+
local gameIntensity = self.GameIntensityCalculator:UpdateGameIntensityCalculator();
543+
print(gameIntensity)
544+
print(self.saveTable.musicState)
545+
if self.saveTable.musicState ~= "Boss" and self.musicGraceTimer:IsPastRealMS(20000) then
546+
if gameIntensity > 0.67 then
547+
if self.saveTable.musicState ~= "Intense" then
548+
self.saveTable.musicState = "Intense";
549+
MusicMan:SetNextDynamicSongSection("Intense", true, true, true);
550+
end
551+
elseif gameIntensity > 0.2 then
552+
if self.saveTable.musicState ~= "Tense" then
553+
local playImmediately = self.saveTable.musicState == "Ambient" and true or false;
554+
self.saveTable.musicState = "Tense";
555+
MusicMan:SetNextDynamicSongSection("Tense", playImmediately, true, true);
556+
end
557+
else
558+
if self.saveTable.musicState ~= "Ambient" then
559+
self.saveTable.musicState = "Ambient";
560+
MusicMan:SetNextDynamicSongSection("Ambient", false, true, true);
561+
end
562+
end
563+
end
564+
534565
local debugDoorTrigger = UInputMan:KeyPressed(Key.J);
535566
if debugDoorTrigger then
536567
self:SendBuyDoorDelivery(self.humanTeam);
Binary file not shown.

0 commit comments

Comments
 (0)