@@ -237,7 +237,7 @@ const LEVELS = [
237237const XP_PER_LEVEL = 45 ;
238238
239239// Load saved level or start at 0
240- let currentLevel = parseInt ( localStorage . getItem ( 'userLevel' ) ) || 0 ;
240+ let currentLevel = Number ( localStorage . getItem ( 'userLevel' ) ) || 0 ;
241241
242242// Load saved XP or start at 0
243243let currentXP = parseInt ( localStorage . getItem ( 'userXP' ) ) || 0 ;
@@ -354,8 +354,17 @@ function getRank(lvl) {
354354 return rank ;
355355}
356356
357+ let isProcessingXP = false ;
358+
357359// Ensure this is in the GLOBAL scope (not hidden inside another function)
358360window . createFloatingXP = function ( e ) {
361+ // Prevent "spam" firing from high-speed mouse movement
362+ if ( isProcessingXP ) return ;
363+ isProcessingXP = true ;
364+
365+ // Release the lock after 50ms
366+ setTimeout ( ( ) => { isProcessingXP = false ; } , 50 ) ;
367+
359368 // 1. Create the XP element
360369 const popup = document . createElement ( 'div' ) ;
361370
@@ -875,48 +884,35 @@ function renderXP(value) {
875884 // console.log(`XP: ${currentXPNum}, Percent: ${percentage}%`);
876885}
877886
878-
879887async function addExperience ( amount ) {
880- if ( document . getElementById ( 'dev-tools' ) ?. getAttribute ( 'data-lock' ) === 'true' ) return ;
881-
882- // 1. Force everything to be a clean number to prevent negative/NaN math
888+ // 1. Force strict numeric types to prevent "1" + "1" = "11"
883889 let xpToAdd = Number ( amount ) || 0 ;
884890 currentXP = Number ( currentXP ) || 0 ;
885891 currentLevel = Number ( currentLevel ) || 0 ;
892+ const XP_THRESHOLD = 45 ;
886893
894+ // 2. Add the new XP
887895 currentXP += xpToAdd ;
888896
889- // 2. Level Up Loop
890- while ( currentXP >= XP_PER_LEVEL && currentLevel < 200 ) {
891- // Visual fill to end
892- renderXP ( XP_PER_LEVEL ) ;
893- await new Promise ( r => setTimeout ( r , 300 ) ) ;
894-
895- // The Math: Subtract 45 and increment level
896- currentXP -= XP_PER_LEVEL ;
897+ // 3. Process Level Ups one by one
898+ // Using a while loop ensures that if you gain 100 XP,
899+ // it processes Level 1, then Level 2, with the remainder left over.
900+ while ( currentXP >= XP_THRESHOLD && currentLevel < 200 ) {
901+ currentXP -= XP_THRESHOLD ; // Subtract exactly the cost of one level
897902 currentLevel ++ ;
898903
899- // Safety: Ensure XP never drops below 0
904+ // Safety: Ensure we don't end up with negative XP from rounding
900905 currentXP = Math . max ( 0 , currentXP ) ;
901906
902- // Reset bar visually for next level in the loop
903- const pb = document . getElementById ( 'level-progress' ) ;
904- if ( pb ) {
905- pb . style . transition = 'none' ;
906- renderXP ( 0 ) ;
907- void pb . offsetWidth ;
908- pb . style . transition = 'width 0.3s ease-in-out' ;
909- }
910-
911- const rank = getRank ( currentLevel ) ;
912- showLevelUpNotification ( rank ) ;
913- playSound ( 'levelUp' ) ;
907+ // Optional: Trigger level-up specific effects here
908+ console . log ( `Leveled Up! Now Level: ${ currentLevel } ` ) ;
914909 }
915910
916- // 3. Save clean numbers back to storage
911+ // 4. Persistence: Save clean numbers
917912 localStorage . setItem ( 'userLevel' , currentLevel . toString ( ) ) ;
918913 localStorage . setItem ( 'userXP' , currentXP . toString ( ) ) ;
919914
915+ // 5. Update UI
920916 updateGameUI ( ) ;
921917}
922918
0 commit comments