0,iUHòy ‘ê}�‘umÞ8e@m}L²…DÊY8æÚ�ÿÿÿ$0D,mPîΕ ÐÿÿÿÿÿÿÿÿÿÿÿÿÿC‡ÊÝÝݬG¦ÌÝÝݼHª¥ª¥™”™I™”™””DD”!”ËÝÝÌÌ«x$‡ËÌÝÝÍ–Z»»»»»š!Cu¹ì1T†Ëÿ2t¨Ëþ wgfîwwæîwgwæwffffwffîfffîîffînfnnîîînffîîîîæîîînfîîîfffnfffwfvwwîfîîîîæwîînfæînfæîffffffffffgffænffwnffvîfvˆîff‡înfvîf‡ˆîfw‡înfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwfîîgfîîˆwîîˆxæîxgæîgfæîffæîˆfæîîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîæçnfffîîçnfvçfænwgfîfffîfæfîffnçfæîînfîîîfnîæîfænænfînæîæîfîîîfævgvnfæî~æînçæîîîîîîîînîæîînfîîw戈ˆˆˆˆ»»ˆ¸»»ˆ»»»¸»»»¸»»»¸»»»¸»»»ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆw‡ˆxD„ˆDDtˆˆˆˆˆîîîînœiæž™ÉæÎÆžîfflîfflfgflfgvvgfffffffffœig–™œvÆÆ–vfÆfwfÇgffvgfnæîîîîîîîîîîîîîfîîgfî~gfnwwgwwwçfvwffffgfffwffvfffgffwæfwnnfîæffwffæfgfgænfwwææ~fgnîffffænffffnfnffwnffvîfvˆîff‡înfvîf‡ˆîfw‡înfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwfîîgfîîˆwîîˆxæîxgæîgfæîffæîˆfæîîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîînfˆîfw‡îf‡ˆînfvîff‡îfvˆnffvnffwwwwwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwwwww‡ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ¸»»»¸»»»¸»»»¸»»»ˆ»»»ˆ¸»»ˆˆ»»ˆˆˆˆˆˆˆˆˆˆˆˆˆœ‰ˆ˜™œˆÈȘˆˆÈˆˆˆÇ‡ˆhhvˆîîîîn–Éænœ™éîélìîÆfffÆfffÆfvvggvfæˆhw‡èfDwGˆDDD„ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwwænfffænæîîîîîîîîîîîîwwwwwfwgfffîffînffîfff~fffwffnffæîfænnæînffffgfîfwfçîffvçîffçnffvîffvgîffvnffwnffvîfvˆîff‡înfvîf‡ˆîfw‡înfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆw‡xwˆwwwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwwwxwwwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ‡wwwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆw‡xwwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîînfˆîfw‡îf‡ˆînfvîff‡îfvˆnffvnffwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆfæîffæîgfæîxgæîˆxæîˆwîîgfîîwfîî»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»ˆˆˆˆ»»ˆˆ»»‹ˆ»»»ˆ»»»‹»»»‹»»»‹»»»‹»»»‹»»»‹»»»‹»»»‹»»»ˆ»»‹ˆ»»ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆœ‰ˆ˜™œˆÈȘˆˆÈˆˆˆÈˆˆˆˆˆˆwffænîîîîîîîîîînîîîwî~wgvwffffînîîffîfffnfffffænwæîîæîîîîînfffæîænfffînffæîæffîæffæævfffîwgfîîwwîîwæîînfînîæîîîîæîîîæînçfæî~vgvnîî~îîîçæîfîîî~~îîîîîîînfînfffffîççîgînfgîfvgffvæfgvînvîævçffçînfvî~~vfæîvgfîngffîgvfnîgæff~gfæî~îçîîn~îîîîfîîççîîîîîfæîîffæîîfffnwîîfæîînîæîîîîîîîîn~æînçînfægvgînfˆîfw‡îf‡ˆînfvîff‡îfvˆnffvnffwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆfæîffæîgfæîxgæîˆxæîˆwîîgfîîwfîîÑÝшˆˆˆˆˆˆˆˆˆˆˆff†hf�aˆa��ˆ�ˆ�ˆˆ�f��ffffffffffffgfffwgffˆxwgˆˆˆhˆˆˆˆæîîîfæîîffæîfîîîfæfîffffffffxfffîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæînævvnfvwffwgffwffffæffffæffænffwwæwwwfæ~æîfæîfffîfff~çffwîçggæwwççîîwvîî~æfîægçîf~vîfænîffææffffîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæînffwnffvîfvˆîff‡înfvîf‡ˆîfw‡înfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîvˆˆˆf†ˆˆf†ˆˆf†ˆˆf†ˆˆf†ˆˆfvˆˆfffˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆfffˆˆgfˆˆhfˆˆhfˆˆhfˆˆhfˆˆhfˆˆˆgîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîffvgnfgff~fæfgfîvfænfænfvînææîffîf~wîffwfnffffffnfffnffnææfænînfgfff~fffwgffffffffffffæffîffffnfîîfwîîfvîîwˆîn‡ˆænv‡ænfvænffænfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwxˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ‡xwxˆˆwxˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ‡xwxˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæînff‡înffnæffîfffîîînîîînîîîîîîîˆˆ†ˆˆˆvw‡ˆffvwfffvffffînffîîffˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆgˆˆhfˆˆhfˆˆhfˆˆhfˆˆhfˆˆgfˆfffîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîînæææfnnnîfîîæ�înæ�~æçîægfwnfæîfáî�áî�án��án��ˆfgæffîfæîîffæînnæîîfîn~æffçînfægvgîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæînffwnffvîfvˆîff‡înfvîf‡ˆîfw‡înfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîæîîîæîîîîîænîînfîfffîfffæfffæff‡îfffîîîîfffffffvffvwvw‡ˆ†ˆˆˆˆˆˆˆˆˆˆˆˆˆˆhˆxwgwgffgfffffîîffîîffæîxffnfffnfffîffîîfæîîîîîîîîîîîîîîîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîw‡ˆwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ‡ˆxwqwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆw‡ˆxw‡xwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwwîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîfffˆfvˆˆf†ˆˆf†ˆˆf†ˆˆf†ˆˆf†ˆˆvˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwˆˆˆgwggfffffææfæîîîîîîîîîîfæîîffæægfffˆgwgˆwˆˆˆˆˆˆˆˆˆˆîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîægvgçînf~æînîîînîîîînîæîfæîînwîîîînfîîfæîæfînfînfænæînæîîfînnfîîfæ~nîîffgfæ~ænf~îfvwîfffîfnf~æffˆˆ‰ˆˆˆ‰ˆˆˆ‰™ˆˆ™ˆˆˆ™ˆˆ˜˜‰˜™™™™™ˆ™ˆˆˆˆ™™‰ˆ‰ˆ‰ˆˆ˜ˆˆˆ˜ˆˆ™™™™™™™ˆˆ‰ˆˆîîîîîîîîîîîîîîînîîîîîînîîînfnînfîîîîîîîæîîîfîîffffffnfgfînffînînîîîîîîîîffæîffffæfffffffffffffffîîîîîîîîîæîîffîæînfffffffffffnfîîîîîîîîîîîîîfînffffffffffffnîîffîîîîîîîîîîîffffæîfnffffffîînffffîîîîîîîîîîîîffffænîîîfîîfîîfffffîîîæîîîîîîîîæîîîîîînnæînæfænfîîîîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîww‡wˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆw‡ˆˆwwx‡ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwwwwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆxwww˜‰ˆ‰˜ˆˆ™˜ˆˆ™˜˜™˜˜‰ˆˆˆˆˆˆˆˆˆˆˆˆˆˆ˜ˆˆˆ‰ˆˆˆˆˆˆˆ‰ˆˆˆ‰ˆˆˆ™ˆˆˆ˜ˆˆˆˆˆˆˆnîffîîfvnînnnîfnînnæîîffîînfîînfnfæîffffçfff~îæwægvwfîæ~nænææf~fnfgffîîîfwî~çffw~wwwg»»»·ëîî{îîfæ~wnvgÝwgf}wwwmwªªª»ªªªîª­ªîªÝÚggffgffv}wwwvwwwªªªªªªªªÝÚÚÝ­­­ªffffw×}ww××vw×wfªªªªªªªªÝÝÝ­­­ÚÚffæîfwffwfffffffªªjfªªjfÚÚzgª­zfîînîæfæîfvîîffæînfvîfvwæffvæfffvw‡ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆww‡wˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwwx‡xwwwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwwwwînfˆînffînfvînv‡în‡ˆîîwˆîîfvîîfwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆfæîxwfîˆxfîgfæîxffîˆgfîgffæwff戈ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ¨ˆˆˆ¨ˆ¨ˆŠˆxˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ¨ˆˆˆ¨ˆˆˆ§‡x‡¨»»îînfîîîîîænîîgnæî~~wîînvîîgvîîfvwî~wfçw·nîw·îî~·îî~èî~è~wèw·{çægfgfwvq§±q‹ˆ¸­nª­­¦ªªªêªªªêwçw¡ç~¡wwªª­­Úª­­Ý­ªªªªªªªªªªJ¤©š¤ª©š­ÝÝÝ­Ýݪªªªªªªªª¡ªªªªª©ª™šššªª­zw­­zwªªjwªªzfªªzªªªzššªzš©ªzgfgvwwgvwgffffgfgf†aww�awwhwwfnwîîfæîînîæîîîîîîîîn~æînçînfægvgnfîîîfînînæîfænænfînîæfîîîfæîînf~æffîfnfîfffîfvwænf~gfæ~îîfffæ~nînfˆînffînfvînv‡în‡ˆîîwˆîîfvîîfwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆfæîxwfîˆxfîgfæîxffîˆgfîgffæwffæ¨x‡ª¨ˆ»»§»»»»»»ˆ«x‡ˆ«Šˆˆˆˆˆˆˆˆˆˆ¸»»‹»»ˆˆwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆæîgfæîwfæîvæînfgîng‡înw†înfînfv·��w·��q��q��qñ¸ˆ¸��¸�ˆøˆß�ß�¸ßÿ¸ˆˆˆˆˆˆq§zªzwªwªwªwªwwqwDª™™ª¤©šDª©šªªªªªªªªffgšššªš™šªššª™ªªªªªªªªš©ªÚššªÚšª©ÚªªªÚªªªÚÛÛÛ}gffwwfwwhfwgffw݈fw×hfw fffÖfîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîægvgçînf~æînîîînîîîînîæîfæîînwîîîînfîîfæîæfînfînfænæînæîîfînnfîîfæ~nîîffgfæ~ænf~îfvwîfffîfnf~æffˆˆˆˆ§Šˆˆ«ˆˆ¨»w‡§»»»§·»»»ˆˆw»ˆˆˆ‡ˆˆˆˆˆŠ¨ˆˆŠ¨ˆ¨ˆ¨ˆ¨ˆ¨ˆxx§Š»»xª¸»»wnfffffvˆnfvnff‡n~f‡nvw‡æfwffv†ˆˆ�ˆ��ˆ±�ˆ±�±��ˆ�ˆˆˆˆˆˆˆˆˆˆˆ¸ˆˆˆ¸ˆˆˆ¸ˆˆˆ¸ˆˆˆ¸™‘‘™»»±‘±fff·»»»ñÿÿÿÿ�ûÿÿˆ»ÿÿˆffvw{wff»»»»ADAÔgfhafff»ff†±»ëfën±»î±·k‹x±wwwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwww‡ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆxww‡ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ‡wwwwwwwˆˆxwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwwwwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆfæîxwfîˆxfîgfæîxffîˆgfîgffæwff戈ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆ‡»»ˆx‡ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆfxv‡faw·fxw·gqw·fvwvñw‹ªw‡†ªww��ˆ�ˆ�؈�݈ˆ��ˆˆ�Øxw�ˆww�¸ˆˆˆˆˆˆˆˆØØÝØ�Ý������ÝÝ݈�ÝØˆ»»»‹¸¸»»»ˆˆˆ�ˆˆˆ�ˆˆˆ»»»»ˆˆˆˆˆˆˆˆˆˆ�ˆ»±±»»»ˆˆ»ˆˆˆ�ˆˆˆˆˆˆ»ˆ»»»ˆˆ�ˆˆˆ�ˆˆˆˆˆøˆˆˆ±a±¸a»‹ˆfˆhfˆh†hDMDmDMDm�ˆˆhîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîînfˆînffînfvînv‡în‡ˆîîwˆîîfvîîfwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆfæîxwfîˆxfîgfæîxffîˆgfîgffæwff戈ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwwwwfvˆˆfˆˆˆf‡ˆˆvˆˆˆfˆˆˆf‡ˆˆfˆˆˆf‡ˆˆføwwañwwgñwfÑw§aÑw—a†ˆˆˆhfvˆˆhfww�ˆww�ˆww�ˆyw�ˆywˆˆˆˆˆ�ˆh›™‹ˆ›�‹ˆ›™‹ˆ›™‹ˆˆˆˆˆfafffavvˆˆˆ�ˆ�ˆˆˆ�ˆˆˆ�ˆˆˆˆafffqfffˆøÿˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwaffw‘™™ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆvafwi‘™fÿ�ˆˆˆˆˆˆˆˆˆˆˆˆˆDDæîîfff�foffaOˆaŸ�aŸˆfDdfaîFÝdfvDdîîfwîîfvîîwˆîn‡ˆînv‡înfvînffînfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆwffægffæˆgfîxffîgfæîˆxfîxwfîˆfæîînfˆînffînfvînv‡în‡ˆîîwˆîîfvîîfwˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆfæîxwfîˆxfîgfæîxffîˆgfîgffæwff戈gfˆˆˆfˆˆxfˆˆˆgˆˆˆfˆˆxfˆˆˆfˆˆxfˆˆˆˆˆˆˆˆˆwˆx‡g‡wgfwggfwçfæîîîîîîhfffˆˆhfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆffff††ˆfˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆffff†ˆˆfˆˆˆhˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆffff†ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆfflƈˆlƈˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆfflfˆ†Œˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆffffˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆffffˆ†ˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆˆú0Ì0Ì0Ì03ÿñÿÿÝ�ÌÃÌ 33ÿÿÙÝ0Ì0Ì33ÿñÿÿÝ�ÃÌ33ÿÿÿÙÝ0Ì0Ì0Ì03ÿñÿÿÐÝÌÃÌ 33ÿÿÙÀÌÌÀÌÌÀÌÌÌÌÌÌÌÌÀÌ ÌÌÌ Ì Ìñÿñÿñÿ"!"ÿÿ"ññÿÿÿñÿ"!"ÿÿÿ"!!!ñ!ÿÿÿ""Ý�ß�ß�OD@ðÀ °»ÙÝÙýÙýDô@ðÀ °»Ý�ß�Ð�@D@ðÀ ° ÙÝÙýÙýDôðÀ ° ÐÝÐßðÝDDÿÌ»ÙÙÙðDDðÀ »»ÌÌÌÌÌÌÌÌÌÌÌÌÀ ÀÀ ÀÀ ÀÀÌÀÌÌÌÌ Ì"¢""""_UPUðÀ °»"""UUÿÌ» "¢""/"PUPUðÀ ° """UÿÌ»!""ñUUÿÌ»/"""UUðÀ ° "!"_Uð_À ° "UUÿÌ»0Ì0Ì0Ì03ÿñÿÿÌÃ Ì 33ÿÿ0Ì0Ì33ÿñÿÿÝ�ÃÌ33ÿÿÿÙÝ0Ì0Ì0Ì03ÿñÿÿÐÝÌÃÌ 33ÿÿÙÌÌÀÌÌÀÌÌÀÌÌ ñÿñÿñÿ"ÿÿññÿÿÿñÿ"!"ÿÿÿ"!!!ñ!ÿÿÿ""Ý�Ý�ß�ß�ODðÀ °»ÙÝÙÝÙýÙýDôðÀ °»Ý�ß�ß�ODðÀ ° ÙÝÙýÙ D@ðÀ ° ÐÝÐßÐßODðÀ°ÙÙÙDD »ÀÌÌÀÌÌÌÌ ÀÌÀ """ò""""_UðÀ °»""""UÿÌ» "¢""""_Uð_À ° """UUÿÌ»!""_UðÀ°/""UU "!"PUPUðÀ ° "UÿÌ»Ì0333ÝÝÌ333ÝÝÌ0333ÝÝÌ333ÝÝ0Ì0Ì0Ì03ÿñÿÿÐÝÌÃÌ 33ÿÿÙ ÀÌñÿñÿñÿ"!"ÿÿ"ññÿÿÿñÿ"!"ÿÿÿ"ññÿÿÿñÿ"!"ÿÿÿ"ñð¿ðÿðÿ`f¿ÿ»fÝÝßÝßÝODðÀ ° ÝÝÝýÝ D@ðÀ ° ÝÝßÝÐÝ@D@ðÀ ° ÝÝÝýÝýDôðÀ ° ÐÝÐÿÐÝDDÿÌ»ÙÿÿùDDðÀ »» "¢""""RUPUðÀ °»"òÿ"ÿUUÿÌ» "¢""ÿÿPUPUðÀ ° ""ÿUÿÌ»"¢""ÿÿPUð_À ° ""ÿUUÿÌ»fff¶ffÿoÿoÿf€ˆk¶»fffkfffˆÿðÿðûÿûÿÿ¿¿ÿðÿðÿÿÿÿÿÿÿðÿðÿÿÿÿÿÿÿÿÿÿÿ0Ì0Ì33ÿñÿÿÝ�ÃÌ33ÿÿÿÙÝ0Ì0Ì33ÿñÿÿÝ�ÃÌ33ÿÿÿÙÝñð¿ðÿðÿ`fff¿ÿ»fk»»»ÿÿÿÿûðÿÿÿÿÿðÿÿÿðÿÿÿð»»»ÿÿÿðÿÿ»ÿÿû¿ÿÿÿÿÿÿÿÿ»»»ÿÿÿÿÿÿÿðÿÿÿÿÿðÿÿÿðÿÿÿðÿÿÿÿÿÿðÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿðÿÿÿÿÿðÿÿÿðÿÿÿðÿÿÿðÿÿÿÿÿÿðÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÝ�ÿÿÐ�@DðÀ ° ÙÝÿÿÙ D@ðÀ ° Ý�ÿÿÐ�@DðÀ ° ÙÝÿÿÙ D@ðÀ ° f¶ffÿoÿoÿf€ˆðÿ¶»fffkfffˆÿÀÀÀÀÌÀœÀË Ë@ADD@ÿðÿðûÿûÿûÿÿ¿¿ÿ»ÿÿðÿðûÿûÿûÿÿ¿¿ÿ»ÿðÿðÿÿÿÿÿÿ¿¿ÿ¿ÿÿðÿðÿÿÿÿÿÿ¿¿¿ÿÿ™ˆ°™ˆÿÿðÿÿDD°KD»Ḛ̈»»Ì™™ÿÿD´°¼ ´»»@™™™ˆ°™ˆÿÿðÿÿDD°KD»ËÌ™™™ÿÿD´ °¼»´Àœ»À™™œ™™ÀÌÌÌÀÀÀÀÀ ›Ì™É™™ ÌÌÌÀÀÀÀÀ ™™™�™™™™™™™™™™™�™™™™™™@»DD»»@»»»ðÿÿÿÿÿðÿÿÿðÿÿÿð»»»ðÿÿÿÿÿÿðÿÿÿû¿ÿÿÿÿÿÿÿÿ»»»ÿÿÿÿÿÿÿÿðÿÿÿÿÿðÿÿÿðÿ»»ð»»»ð¿»»ÿ»»ðÿÿÿû¿ÿÿÿÿÿ»»ÿ»»»»»û»»ÿÿÿðÿÿÿÿÿðÿÿÿðÿÿÿðÿÿðÿÿÿÿÿðÿÿÿÿÿÿÿÿÿÿÿðÿÿÿÿÿðÿÿÿðÿÿÿðÿÿÿðÿÿ¿ÿÿÿðÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ»»»»ÿÿÿÿÿ° »»° »»ð»»»»»»»K»°û»»»»»°»»Ì° »»° »»ð»»»»»»»@»»K»°û»»»»™ ™›»»¹™»»™›»»  ¡9ªÉLŠIŠ ¨» » @»KD@@@� Éœ™™Ä’™™Ä“Éœ� ""bbrvvGwwÄt~Fvྻî»vwfwwGwÄtFgkæëÀÀÀÀÌÀœÀË ËÀÀÀÀÌÀœÀ¼ ËÀÀÀÀÌÀœÀ» ˰°°°» ª Ë ü°°°°» ª ¼ ü°°°°» ª ¼ ü°°°°» ª Ì üfvçwpfvà`w`p`îæççnvgàgàvgîÀœËÀ™™œ™™ÀÌÌÌÀÀÀÀÀ ›Ì™É™™ ÌÌÌÀÀÀÀÀ Àœ¼À™™œ™™ÀÌÌÌÀÀÀÀÀ ›Ì™É™É ÌÌÌÀÀÀÀÀ ÀœËÀ™™œ™™ÀÌÌÌÀÀÀÀÀ ›Ì™É™™ ÌÌÌÀÀÀÀÀ ªË ªªªªª ªªú°°°°° üª¿ª¿ÿ¿ÿ°°°°° °ª¼ ªªªªª ªªú°°°°° ü¿ª¯ªÿÿÿÿ°°°°° °ªÌ ªªªªª ªªú°°°°° ü¿ª¯ªÿÿÿÿ°°°°° °ª¼ ªªªªª ªªú°°°°° ü¿ª¯ªÿÿÿÿ°°°°° » » @»KD@ðOÿÿ@»»»DDÿÿ@ÿ """bbrvvGwwÄt~Fvྻî»wfwwGwÄtFgkæë�™™™™™™™™™™™™™™ »» »» »› ™™™™™™™@D™€ˆ€Ì€Ì€Ì�¨ŠÀÌÌÀÌÌÌÌÌÌÌÌÌÌÌÀÌÌÌÌÌÀ """"" """"""""""" """""" ""pà€™™™™™™™™™™™™™™™™™™™™™™™™™™ �™™ ™™™™›¹¹»»»»@@»DD»@»»»» »»» ÿÿ ù ™›»»¹™»»™›»»ð¹™››¹™»»»»»»»»vfvçwpfvà`w`pîæççnvgàvàgî�™�™�™�™™™�™™™™™™™™™™™™™™ ‘‘ ™™ DÌL™0D™™�ªª¨Š�€� ÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ À ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ ÀÀ» » @»KD@@@¹¹™¹™›¹¹™™™¹»»»»»»»»»»»»@°° °° » ° °  ¡9ªÉLÿIÿ ÿ �™™™ ÿÿ0üü<0ÃÌ3ð3<ó°»°» š™ ™©�™ª ™ª�©º�©«�©ª™© ª™ ª™ ª™ «© «© «ª  š™ ™©�™ª ™ª�©º�©«�©ª™© ª™ ª™ ª™ «© «© «ª  š™ ™©�™ª ™ª�©º�©ª�©ª™© ª™ ª™ «™ «© «© «ª �™™™™™™™�™™™�™ÉÌ Ä $à Äà ÝÝÝÝ”)%”WU”)R”™™ ÝÝ %’U•Rr9™DÀÌÌÀ ÀÌÌÌÌ ÌÀ ÌÌÌ 0ÌÃ0ÌÌ 0333ñÿÿÿÿÿñÿÿÿÿÿ ™™ �©š �ªš �ªª �©š  ™™ �ÌÌ �™™ ð�™™ �ÃL �Ä< �ÄL @ ©º�©«�©º ™ª š™�ÉÌ�™™ªª «ª «š ª™ ™© ÌÌ ™™  ©º�©»�©ª ™ª š™�ÉÌ�™™ªª »ª ªš ª™ ™© ÌÌ ™™  ©ª�©º�©ª ™ª š™�ÉÌ�™™«ª »ª ªš ª™ ™© ÌÌ ™™ ÉÌ �™@Ì™@Ì™@™™@9‰”™”™@DÝ™I@ÉL@ÉL@˜IDˆD™D vfvçwpfvà`w`pîæççnvgàvàgî� � ™™™™™™™œ™™É™™™™™�™™ ™™� Ì ÀÌÀ ÀÀª~45454545454555454545EDCDEDEDEDEECDECDECCDCDCDEDECDECDCDCDCCDE !" !" ! !"E !" !" !" !" !" !" !" " !" !" !" !" !" !"DEDEDEDEDEDEEEDEDEDEUTSTUTUTUTUUSTUSTUSSTSTSTUTUSTUSTSTSTSSTU01201201012U0120120012012 !@ÓÔsÖÖÔÕTUUcdUTcdU…cd…cdecdcd…cdecdcdcdecd…cdecdecdecd…ccdecd…cd…cdecdecdecd…cdcdÄÅ" " !"cde cdecdecdecdecde!"!"01PABãäåCäåDE“”•“”•“”•stutu“”•stustusstu“”•stustustu“”•sstu“”•“”•stustustu“”•stuÓÔÕlm>?01 !" !" !" !" !" !" !" !" !" !"01`ABóôõPÖôõTU)*+£¤¥£¤¥£¤¥CDCDE£¤¥CDEECDECDE£¤¥CDECDECDE£¤¥C !"£¤¥£¤¥ !"" ! !"£¤¥CDEãäå!!"|}AAAA AAAAAAAAAAAAAA/ O012012012012012012012012012012pQR€�‚£A�‚TU³´µ@AB³´µSTSTU³´µSTUUSTUSTU³´µSTUSTUSTU³´µS012³´µ³´µ012 !"012�‘’STU@AB1!"012çAAAAAAAAAAAAAAAAAAAAAAö !"€ab�‘’C~‘’cdijkPQRPQRCDEEEijk" ÃÄÅlm§>? ÃÄÅfghƒCDE@AB`abCDE0122lmA>?CPQR12 !"ç/AAAAAAAAAAAAAAAAAAAAAö012 !@WX ¡¢s ¡¢tu&'(yz{`ab`abSTUUUyz{2ÓÔÕ@ A¶·AÆÇhÓÔÕvwxUSTUPQRpqrSTU !"U|DS£¤¥£Q¥DEEDECDEDEàáâàáâCDEÈÉÊËÌÍÎÏCDEàáâàáâCDCDEC !"£¤¥789:;VWXstuYZ[AAAAAA/AAAAAAAAAA/ØÙÚÛÜÝÞß !"9:;|}AAA?001ijkfghFGHcdeÃÄÅ ! l= MAP_TILES_W or ty < 0 or ty >= MAP_TILES_H then return nil, nil end return tx, ty end local function rects_overlap(ax, ay, aw, ah, bx, by, bw, bh) return ax < bx + bw and bx < ax + aw and ay < by + bh and by < ay + ah end -- Convert a pixel position into the tile index currently under that pixel. local function tile_at_pixel(px, py) local tx, ty = pixel_to_tile(px, py) if tx == nil or ty == nil then return nil end return mget(tx, ty) end local function is_path_tile(tx, ty) if tx == nil or ty == nil then return false end if tx < 0 or tx >= MAP_TILES_W or ty < 0 or ty >= MAP_TILES_H then return false end local tile_id = mget(tx, ty) return tile_id and fget(tile_id, WALKABLE_FLAG_INDEX) or false end local function tile_center_px(tx, ty) local cx = tx * TILE_SIZE + math.floor(TILE_SIZE / 2) local cy = ty * TILE_SIZE + math.floor(TILE_SIZE / 2) return cx, cy end -- Treat sprites with flag 0 set as “walkable pathâ€� tiles. local function is_path_pixel(px, py) local tile = tile_at_pixel(px, py) return tile and fget(tile, WALKABLE_FLAG_INDEX) or false end local function is_hole_tile(tx, ty) if tx == nil or ty == nil then return false end if tx < 0 or tx >= MAP_TILES_W or ty < 0 or ty >= MAP_TILES_H then return false end local tile_id = mget(tx, ty) return tile_id and fget(tile_id, BOTTOMLESS_PIT.flag) or false end local function is_player_walk_pixel(px, py) local tx, ty = pixel_to_tile(px, py) if tx == nil or ty == nil then return false end local arena_rect = BILL_BOSS.arena_rect if BILL_BOSS.arena_active and arena_rect and tx >= arena_rect.x and tx < (arena_rect.x + arena_rect.w) and ty >= arena_rect.y and ty < (arena_rect.y + arena_rect.h) then return true end local tile = mget(tx, ty) if not tile then return false end if fget(tile, WALKABLE_FLAG_INDEX) then return true end if not fget(tile, BOTTOMLESS_PIT.flag) then return false end local lock_rect = BOTTOMLESS_PIT.lock_rect if lock_rect and tx >= lock_rect.x and tx < (lock_rect.x + lock_rect.w) and ty >= lock_rect.y and ty < (lock_rect.y + lock_rect.h) then return false end return true end local function tile_in_rect(tx, ty, rect) if not rect then return false end return tx >= rect.x and tx < (rect.x + rect.w) and ty >= rect.y and ty < (rect.y + rect.h) end local function tile_in_rect_margin(tx, ty, rect, margin) if not rect then return false end margin = margin or 0 return tx >= (rect.x - margin) and tx < (rect.x + rect.w + margin) and ty >= (rect.y - margin) and ty < (rect.y + rect.h + margin) end local MOVEMENT_DIRS, MOVEMENT_DIR_LOOKUP MOVEMENT_DIRS = { {name = "left", dx = -1, dy = 0}, {name = "right", dx = 1, dy = 0}, {name = "up", dx = 0, dy = -1}, {name = "down", dx = 0, dy = 1} } MOVEMENT_DIR_LOOKUP = {} for _, dir in ipairs(MOVEMENT_DIRS) do MOVEMENT_DIR_LOOKUP[dir.name] = dir end local WALKABLE_TILE_CACHE = nil local function collect_walkable_tiles() if WALKABLE_TILE_CACHE then return WALKABLE_TILE_CACHE end local tiles = {} for ty = 0, MAP_TILES_H - 1 do for tx = 0, MAP_TILES_W - 1 do if is_path_tile(tx, ty) then tiles[#tiles + 1] = {tx = tx, ty = ty} end end end WALKABLE_TILE_CACHE = tiles return tiles end local function pick_random_powerup_tile(tiles, occupied_lookup, excluded_tile) if not tiles or #tiles == 0 then return nil end local attempts = POWERUP_RESPAWN_ATTEMPTS or (#tiles * 2) local excluded_key = nil if excluded_tile and excluded_tile.tx and excluded_tile.ty then excluded_key = marker_tile_key(excluded_tile.tx, excluded_tile.ty) end for _ = 1, attempts do local candidate = tiles[math.random(#tiles)] local key = marker_tile_key(candidate.tx, candidate.ty) local occupied = occupied_lookup and occupied_lookup[key] if not occupied and key ~= excluded_key then return {tx = candidate.tx, ty = candidate.ty} end end return nil end local function pick_distant_tile(tiles, player_cx, player_cy, min_distance) if not tiles or #tiles == 0 then return {tx = PLAYER_SPAWN_TILE_X, ty = PLAYER_SPAWN_TILE_Y} end local min_sq = (min_distance or 0) * (min_distance or 0) local best = nil for attempt = 1, #tiles do local candidate = tiles[math.random(#tiles)] local cx, cy = tile_center_px(candidate.tx, candidate.ty) local dx = cx - player_cx local dy = cy - player_cy local dist_sq = dx * dx + dy * dy if dist_sq >= min_sq then return candidate end if not best or dist_sq > best.dist_sq then best = {tile = candidate, dist_sq = dist_sq} end end return best and best.tile or tiles[1] end local get_float_bob_offset local wrap_intro_dialogue_line local get_cardinal_dir_from_vector local build_sprite_chain_positions local function collect_tiles_in_chunk(tiles, chunk_x, chunk_y) if not tiles or #tiles == 0 then return nil end local filtered = {} local min_tx = chunk_x * SCREEN_TILES_W local max_tx = min_tx + SCREEN_TILES_W local min_ty = chunk_y * SCREEN_TILES_H local max_ty = min_ty + SCREEN_TILES_H for _, tile in ipairs(tiles) do if tile.tx >= min_tx and tile.tx < max_tx and tile.ty >= min_ty and tile.ty < max_ty then filtered[#filtered + 1] = tile end end return filtered end -- Produce a subtle bob effect so pellets feel animated. local function get_path_marker_bob(tile_x, tile_y) if PATH_MARKER_BOB_AMPLITUDE <= 0 or type(time) ~= "function" then return 0 end local seconds = time() / 1000 local base_angle = seconds * PATH_MARKER_BOB_FREQUENCY_HZ * math.pi * 2 local phase = (tile_x + tile_y) * PATH_MARKER_BOB_PHASE_STRIDE return math.floor(math.sin(base_angle + phase) * PATH_MARKER_BOB_AMPLITUDE + 0.5) end local function spawn_random_journals(walkable_tiles, occupied_lookup) local journals = {} local occupied = occupied_lookup or {} for idx, sprite in ipairs(JOURNAL_SPRITE_IDS) do local tile = pick_random_powerup_tile(walkable_tiles, occupied, nil) if tile then local key = marker_tile_key(tile.tx, tile.ty) occupied[key] = true journals[#journals + 1] = { id = idx, tile_x = tile.tx, tile_y = tile.ty, sprite = sprite, collected = false } end end return journals end -- Draw pellets (sprite 304) on any tile with the marker flag enabled. local function draw_path_markers(cam_tile_x, cam_tile_y, cam_px, cam_py, collected_lookup) local sprite_px_w = PATH_MARKER_SPRITE_W * TILE_SIZE local sprite_px_h = PATH_MARKER_SPRITE_H * TILE_SIZE local center_offset_x = math.floor((TILE_SIZE - sprite_px_w) / 2) local center_offset_y = math.floor((TILE_SIZE - sprite_px_h) / 2) for dy = 0, SCREEN_TILES_H - 1 do local tile_y = cam_tile_y + dy if tile_y >= 0 and tile_y < MAP_TILES_H then for dx = 0, SCREEN_TILES_W - 1 do local tile_x = cam_tile_x + dx if tile_x >= 0 and tile_x < MAP_TILES_W then local tile_id = mget(tile_x, tile_y) if tile_id and fget(tile_id, PATH_MARKER_FLAG_INDEX) then local key = marker_tile_key(tile_x, tile_y) local already_collected = collected_lookup and collected_lookup[key] if not already_collected then local px = tile_x * TILE_SIZE - cam_px + center_offset_x local py = tile_y * TILE_SIZE - cam_py + center_offset_y local delta = PATH_MARKER_OFFSETS[tile_id] if delta then px = px + delta.dx py = py + delta.dy end py = py + get_path_marker_bob(tile_x, tile_y) spr( PATH_MARKER_SPRITE_ID, px, py, 0, 1, 0, 0, PATH_MARKER_SPRITE_W, PATH_MARKER_SPRITE_H ) end end end end end end end -- Shadow helpers ----------------------------------------------------- -- Blend the color under the shadow toward the configured tint. local function tint_shadow_color(color_index) if color_index == nil then return nil end local cached = SHADOW_BLEND_CACHE[color_index] if cached ~= nil then return cached end local source = PALETTE_RGB[color_index + 1] local target = PALETTE_RGB[SHADOW_TINT_TARGET_INDEX + 1] if not source or not target then SHADOW_BLEND_CACHE[color_index] = SHADOW_TINT_TARGET_INDEX return SHADOW_TINT_TARGET_INDEX end local blend_r = source.r + (target.r - source.r) * SHADOW_TINT_BLEND local blend_g = source.g + (target.g - source.g) * SHADOW_TINT_BLEND local blend_b = source.b + (target.b - source.b) * SHADOW_TINT_BLEND local best_index = SHADOW_TINT_TARGET_INDEX + 1 local best_dist = math.huge for idx, rgb in ipairs(PALETTE_RGB) do local dr = rgb.r - blend_r local dg = rgb.g - blend_g local db = rgb.b - blend_b local dist = dr * dr + dg * dg + db * db if dist < best_dist then best_dist = dist best_index = idx end end local final_index = best_index - 1 SHADOW_BLEND_CACHE[color_index] = final_index return final_index end -- Simple filled ellipse renderer used to fake a soft blob shadow. local function draw_shadow_ellipse(cx, cy, rx, ry) for dx = -rx, rx do local nx = dx / rx local ny_span_sq = 1 - nx * nx if ny_span_sq >= 0 then local span = math.floor(math.sqrt(ny_span_sq) * ry) local px = cx + dx if px >= 0 and px < SCREEN_W then for dy = -span, span do local py = cy + dy if py >= 0 and py < SCREEN_H then local existing = pix(px, py) if existing ~= nil then local tinted = tint_shadow_color(existing) if tinted ~= nil then pix(px, py, tinted) end end end end end end end end local function draw_sprite_silhouette(frame, px, py, flip, color) if type(pal) ~= "function" then spr(frame, px, py, 0, 1, flip or 0, 0, 2, 2) return end color = color or PLAYER_TRAIL_COLORS[1] for i = 0, 15 do pal(i, color, 0) end spr(frame, px, py, 0, 1, flip or 0, 0, 2, 2) pal() end --[[-------------------------------------------------------------------- PLAYER ENTITY ------------- Handles input, collision checks, walk animation, shadow drawing, and the temporary speed boost visual effects triggered by power-ups. --------------------------------------------------------------------]]-- local Player = {} Player.__index = Player -- Each entry stores one or more top-left sprite IDs of contiguous 2x2 blocks. -- Add or reorder IDs per direction to configure the walking cycle. local PLAYER_ANIMATION_FRAMES = { right = {260, 292}, left = {260, 292}, up = {320, 322}, down = {258, 290}, neutral = {256, 288} } -- Set flip=1 on any direction that should mirror horizontally instead of -- providing dedicated sprite art (0 keeps sprites as-is). local PLAYER_DIRECTION_FLIPS = { right = 0, left = 1, up = 0, down = 0, neutral = 0 } -- Alternate art bank used after the player loses a life. These default to the -- base sprites so the new art can be dropped in without touching logic. local PLAYER_ALTERNATE_ANIMATION_FRAMES = { right = {268, 300}, left = {268, 300}, up = {302, 270}, down = {266, 298}, neutral = {264, 296} } local PLAYER_ALTERNATE_DIRECTION_FLIPS = { right = 0, left = 1, up = 0, down = 0, neutral = 0 } local PLAYER_RESPAWN_APPEARANCES = { { id = "default", frames = PLAYER_ANIMATION_FRAMES, flips = PLAYER_DIRECTION_FLIPS }, { id = "alternate", frames = PLAYER_ALTERNATE_ANIMATION_FRAMES, flips = PLAYER_ALTERNATE_DIRECTION_FLIPS } } local function get_player_appearance_by_index(index) if not PLAYER_RESPAWN_APPEARANCES or #PLAYER_RESPAWN_APPEARANCES == 0 then return nil, nil end index = math.floor(index or 1) if index < 1 then index = 1 elseif index > #PLAYER_RESPAWN_APPEARANCES then index = #PLAYER_RESPAWN_APPEARANCES end return PLAYER_RESPAWN_APPEARANCES[index], index end local PLAYER_ANIM_FRAME_DELAY = 8 -- frames to wait before advancing walk animation local PLAYER_NEUTRAL_ANIM_FRAME_DELAY = 12 -- slower idle loop; tweak as needed local PLAYER_CORNER_WIGGLE_PX = 4 -- tolerance for snapping toward tile centers while turning function Player.new(opts) local self = setmetatable({}, Player) self.x = (opts and opts.x) or PLAYER_SPAWN_X self.y = (opts and opts.y) or PLAYER_SPAWN_Y self.speed = (opts and opts.speed) or 1 self.base_speed = self.speed self.dir = "down" self.last_dir = self.dir self.bob_t = 0 self.bob_offset = 0 self.anim_frame_index = 1 self.anim_timer = 0 self.anim_dir = self.dir self.special_anim_frames = nil self.special_anim_active = false self.special_anim_frame_index = 1 self.special_anim_timer = 0 self.special_anim_delay = PLAYER_ANIM_FRAME_DELAY self.hidden = false self.special_anim_mode = nil self.special_anim_on_finish = nil self.pending_respawn = false self.movement_locked = false self.footsteps_enabled = true self.beam_visual_active = nil self.appearance = nil self.appearance_index = 1 self.animation_frames = PLAYER_ANIMATION_FRAMES self.direction_flips = PLAYER_DIRECTION_FLIPS local appearance_index = (opts and opts.appearance_index) or 1 self:setAppearanceByIndex(appearance_index) return self end function Player:setAppearance(appearance, index) self.appearance = appearance if index then self.appearance_index = index elseif not self.appearance_index then self.appearance_index = 1 end if appearance and appearance.frames then self.animation_frames = appearance.frames else self.animation_frames = PLAYER_ANIMATION_FRAMES end if appearance and appearance.flips then self.direction_flips = appearance.flips else self.direction_flips = PLAYER_DIRECTION_FLIPS end self.anim_frame_index = 1 self.anim_timer = 0 end function Player:setAppearanceByIndex(index) local appearance, normalized_index = get_player_appearance_by_index(index) if not normalized_index then normalized_index = 1 end self:setAppearance(appearance, normalized_index) end function Player:getAppearanceIndex() return self.appearance_index or 1 end function Player:getAppearanceId() if self.appearance and self.appearance.id then return self.appearance.id end return "default" end function Player:setMovementLocked(locked) self.movement_locked = locked and true or false end function Player:isMovementLocked() return self.movement_locked or false end function Player:setBeamVisual(active, item_id) if active then self.beam_visual_active = item_id or ITEMS.ray_gun else self.beam_visual_active = nil end end function Player:isBeamVisualActive() return self.beam_visual_active == ITEMS.ray_gun end function Player:isHeldItemVisualActive(item_id) return self.beam_visual_active == item_id end function Player:getFacingDir() if self.dir and self.dir ~= "neutral" then return self.dir end return self.last_dir or "down" end function Player:applySpeedBoost(multiplier) multiplier = multiplier or 1 self.speed = self.base_speed * multiplier end function Player:clearSpeedBoost() self.speed = self.base_speed end function Player:update() -- Step animation helpers separately so input handling stays clean. self:updateBob() if self.special_anim_active then self:updateSpecialAnimation() return end if self.hidden then return end local moved = self:handleInput() self:updateWalkAnimation(moved) end function Player:updateBob() -- Disable vertical bobbing so the player remains grounded. self.bob_t = 0 self.bob_offset = 0 end function Player:handleInput() if self:isMovementLocked() then return false end -- Movement attempts run in priority order and stop after the first success -- so the player never travels diagonally within a single frame. if btn(2) then if self:tryMove(-self.speed, 0, "left") then return true end end if btn(3) then if self:tryMove(self.speed, 0, "right") then return true end end if btn(0) then if self:tryMove(0, -self.speed, "up") then return true end end if btn(1) then if self:tryMove(0, self.speed, "down") then return true end end -- When no direction is pressed (or movement rejected) fall back to idle art. self.dir = "neutral" return false end function Player:updateWalkAnimation(is_moving) if self.anim_dir ~= self.dir then self.anim_dir = self.dir self.anim_frame_index = 1 self.anim_timer = 0 end local anim_frames = self.animation_frames or PLAYER_ANIMATION_FRAMES local frames = anim_frames[self.dir] or anim_frames.neutral or PLAYER_ANIMATION_FRAMES.neutral local should_animate = (#frames > 1) and (is_moving or self.dir == "neutral") if not should_animate then self.anim_frame_index = 1 self.anim_timer = 0 return end self.anim_timer = self.anim_timer + 1 local frame_delay = PLAYER_ANIM_FRAME_DELAY if self.dir == "neutral" and PLAYER_NEUTRAL_ANIM_FRAME_DELAY then frame_delay = PLAYER_NEUTRAL_ANIM_FRAME_DELAY end if self.anim_timer >= frame_delay then self.anim_timer = 0 self.anim_frame_index = self.anim_frame_index + 1 if self.anim_frame_index > #frames then self.anim_frame_index = 1 end if is_moving and self.footsteps_enabled ~= false then play_sfx_on_channel(AUDIO.footstep.id, AUDIO.footstep.channel) end end end function Player:updateSpecialAnimation() if not self.special_anim_active or not self.special_anim_frames then return end self.special_anim_timer = self.special_anim_timer + 1 local delay = self.special_anim_delay or PLAYER_ANIM_FRAME_DELAY if self.special_anim_timer >= delay then self.special_anim_timer = 0 self.special_anim_frame_index = self.special_anim_frame_index + 1 if self.special_anim_frame_index > #self.special_anim_frames then self.special_anim_active = false if self.special_anim_mode == "disappear" then self.hidden = true self.pending_respawn = true elseif self.special_anim_mode == "reappear" then self.hidden = false end if self.special_anim_on_finish then self.special_anim_on_finish(self) end self.special_anim_mode = nil self.special_anim_on_finish = nil self.special_anim_frames = nil end end end function Player:startSpecialAnimation(frames, frame_delay, opts) if not frames or #frames == 0 then return end opts = opts or {} self.special_anim_frames = frames self.special_anim_delay = frame_delay or PLAYER_ANIM_FRAME_DELAY self.special_anim_active = true self.special_anim_frame_index = 1 self.special_anim_timer = 0 self.special_anim_mode = opts.mode self.special_anim_on_finish = opts.on_finish if opts.start_hidden ~= nil then self.hidden = opts.start_hidden and true or false else self.hidden = false end end function Player:isHidden() return self.hidden or false end function Player:isPlayingSpecialAnimation() return self.special_anim_active end function Player:getBounds() return self.x, self.y, TILE_SIZE * 2, TILE_SIZE * 2 end function Player:getMazeEnemyCollisionBounds() return self.x, self.y, TILE_SIZE * 2, TILE_SIZE end function Player:needsRespawn() return self.pending_respawn end function Player:clearRespawnFlag() self.pending_respawn = false end function Player:respawnAtSpawn() self.x = PLAYER_SPAWN_X self.y = PLAYER_SPAWN_Y self.dir = "down" self.last_dir = self.dir self.bob_t = 0 self.bob_offset = 0 self.anim_frame_index = 1 self.anim_timer = 0 end function Player:tryMove(dx, dy, facing) local new_x = self.x + dx local new_y = self.y + dy local foot_x = new_x + PLAYER_FOOT_OFFSET_X local foot_y = new_y + PLAYER_FOOT_OFFSET_Y local tile_x, tile_y = pixel_to_tile(foot_x, foot_y) if tile_x and tile_y and PLAYER_CORNER_WIGGLE_PX and PLAYER_CORNER_WIGGLE_PX > 0 then local center_x, center_y = tile_center_px(tile_x, tile_y) if dx ~= 0 and center_y then if math.abs(foot_y - center_y) <= PLAYER_CORNER_WIGGLE_PX then foot_y = center_y new_y = foot_y - PLAYER_FOOT_OFFSET_Y end elseif dy ~= 0 and center_x then if math.abs(foot_x - center_x) <= PLAYER_CORNER_WIGGLE_PX then foot_x = center_x new_x = foot_x - PLAYER_FOOT_OFFSET_X end end end if is_player_walk_pixel(foot_x, foot_y) then local aligned_x = new_x local aligned_y = new_y local tile_x, tile_y = pixel_to_tile(foot_x, foot_y) if tile_x and tile_y then local center_x, center_y = tile_center_px(tile_x, tile_y) if dx ~= 0 then aligned_y = center_y - PLAYER_FOOT_OFFSET_Y foot_y = center_y end if dy ~= 0 then aligned_x = center_x - PLAYER_FOOT_OFFSET_X foot_x = center_x end end self.x = aligned_x self.y = aligned_y self.dir = facing self.last_dir = facing return true end return false end function Player:drawShadow(cam_px, cam_py) local cx = self.x + PLAYER_FOOT_OFFSET_X - cam_px local cy = self.y + PLAYER_FOOT_OFFSET_Y - cam_py -- Pulse shadow size opposite to bobbing height for a subtle breathing effect. local pulse = 1 + (self.bob_offset or 0) * SHADOW_PULSE_SCALE if pulse < 0.4 then pulse = 0.4 end local rx = math.max(1, math.floor(SHADOW_RADIUS_X * pulse)) local ry = math.max(1, math.floor(SHADOW_RADIUS_Y * pulse)) draw_shadow_ellipse(cx, cy, rx, ry) end function Player:getCurrentSprite() local base_frame local flip = 0 if self.special_anim_active and self.special_anim_frames then local special_frames = self.special_anim_frames base_frame = special_frames[self.special_anim_frame_index] or special_frames[#special_frames] else local anim_frames = self.animation_frames or PLAYER_ANIMATION_FRAMES local frames = anim_frames[self.dir] if not frames or #frames == 0 then frames = anim_frames.neutral or PLAYER_ANIMATION_FRAMES.neutral end base_frame = frames[self.anim_frame_index] or frames[1] or PLAYER_ANIMATION_FRAMES.neutral[1] local flips = self.direction_flips or PLAYER_DIRECTION_FLIPS local flip_dir = self.dir if flip_dir == "neutral" and self.last_dir and self.last_dir ~= "neutral" then flip_dir = self.last_dir end flip = flips[flip_dir] or flips.neutral or 0 end return base_frame, flip end function Player:drawHeldItem(cam_px, cam_py, held_item, flip) if not held_item then return end local px = self.x - cam_px local py = self.y - cam_py local facing = self:getFacingDir() local sprite_id = nil local item_x = nil local item_y = nil local item_flip = flip or 0 if held_item == ITEMS.ray_gun then if not self:isHeldItemVisualActive(ITEMS.ray_gun) or facing == "up" then return end sprite_id = HELD_RAY_GUN.side_sprite item_x = px + HELD_RAY_GUN.side_x item_y = py + HELD_RAY_GUN.side_y if facing == "down" then sprite_id = HELD_RAY_GUN.down_sprite item_x = px + HELD_RAY_GUN.down_x item_y = py + HELD_RAY_GUN.down_y item_flip = 0 elseif flip == 1 then item_x = px + HELD_RAY_GUN.flip_x end elseif held_item == ITEMS.leaf_blower then if not self:isHeldItemVisualActive(ITEMS.leaf_blower) or facing == "up" then return end sprite_id = HELD_LEAF_BLOWER.sprite item_x = px + HELD_LEAF_BLOWER.side_x item_y = py + HELD_LEAF_BLOWER.y if facing == "down" then sprite_id = HELD_LEAF_BLOWER.down_sprite or HELD_LEAF_BLOWER.sprite item_x = px + (HELD_LEAF_BLOWER.down_x or HELD_LEAF_BLOWER.side_x) item_y = py + (HELD_LEAF_BLOWER.down_y or HELD_LEAF_BLOWER.y) item_flip = 0 elseif flip == 1 then item_x = px + HELD_LEAF_BLOWER.flip_x end elseif held_item == ITEMS.grappling_hook then if not self:isHeldItemVisualActive(ITEMS.grappling_hook) or facing == "up" then return end sprite_id = HELD_GRAPPLING_HOOK.sprite item_x = px + HELD_GRAPPLING_HOOK.side_x item_y = py + HELD_GRAPPLING_HOOK.y if facing == "down" then item_x = px + HELD_GRAPPLING_HOOK.down_x item_y = py + HELD_GRAPPLING_HOOK.down_y item_flip = 0 elseif flip == 1 then item_x = px + HELD_GRAPPLING_HOOK.flip_x end else return end spr(sprite_id, item_x, item_y, 0, 1, item_flip, 0, 1, 1) end function Player:draw(cam_px, cam_py, held_item) if self.hidden and not self.special_anim_active then return end -- Draw shadow first so the player sprite sits on top of the darkened pixels. self:drawShadow(cam_px, cam_py) local base_frame, flip = self:getCurrentSprite() local px = self.x - cam_px local py = self.y - cam_py if self.beam_visual_active then local facing = self:getFacingDir() local beam_frame = PLAYER_BEAM_POSES.default_side local beam_flip = 0 if self:getAppearanceId() == "alternate" then beam_frame = PLAYER_BEAM_POSES.alternate_side or beam_frame end if facing == "left" then beam_flip = 1 elseif facing == "up" then beam_frame = base_frame beam_flip = flip elseif facing == "down" then local down_frames = PLAYER_BEAM_POSES.default_down if self:getAppearanceId() == "alternate" then down_frames = PLAYER_BEAM_POSES.alternate_down end beam_frame = down_frames[1] or base_frame beam_flip = 0 end spr(beam_frame, px, py, 0, 1, beam_flip, 0, 2, 2) self:drawHeldItem(cam_px, cam_py, held_item, flip) return end spr(base_frame, px, py, 0, 1, flip, 0, 2, 2) self:drawHeldItem(cam_px, cam_py, held_item, flip) end --[[-------------------------------------------------------------------- POWER-UP ENTITY (MABEL JUICE) ----------------------------- Represents a single pickup on the map, handles respawn timing, and draws the 2x2 sprite whenever it’s active. --------------------------------------------------------------------]]-- local Powerup = {} Powerup.__index = Powerup function Powerup.new(tile) local self = setmetatable({}, Powerup) self.active = false self.respawn_timer = 0 self:setTile(tile) if tile then self.active = true else self.respawn_timer = 1 end return self end function Powerup:setTile(tile) if not tile then return end self.tile_x = tile.tx self.tile_y = tile.ty self.x = self.tile_x * TILE_SIZE self.y = self.tile_y * TILE_SIZE end function Powerup:deactivate() self.active = false self.respawn_timer = POWERUP_RESPAWN_FRAMES end function Powerup:update(walkable_tiles, occupied_lookup, player_tile) if self.active then return end if self.respawn_timer and self.respawn_timer > 0 then self.respawn_timer = self.respawn_timer - 1 if self.respawn_timer <= 0 then local tile = pick_random_powerup_tile(walkable_tiles, occupied_lookup, player_tile) if tile then self:setTile(tile) self.active = true self.respawn_timer = 0 else self.respawn_timer = math.ceil(POWERUP_RESPAWN_FRAMES / 4) end end end end function Powerup:getBounds() return self.x, self.y, POWERUP_SPRITE_W * TILE_SIZE, POWERUP_SPRITE_H * TILE_SIZE end function Powerup:isActive() return self.active end function Powerup:draw(cam_px, cam_py) if not self.active then return end spr( POWERUP_SPRITE_ID, self.x - cam_px, self.y - cam_py, 0, 1, 0, 0, POWERUP_SPRITE_W, POWERUP_SPRITE_H ) end local LeafBlower = {} LeafBlower.__index = LeafBlower function LeafBlower.new(opts) local self = setmetatable({}, LeafBlower) self.tile_x = (opts and opts.tile_x) or LEAF_BLOWER_ITEM.tile_x self.tile_y = (opts and opts.tile_y) or LEAF_BLOWER_ITEM.tile_y self.x = self.tile_x * TILE_SIZE self.y = self.tile_y * TILE_SIZE self.collected = false return self end function LeafBlower:getBounds() return self.x, self.y, LEAF_BLOWER_ITEM.w * TILE_SIZE, LEAF_BLOWER_ITEM.h * TILE_SIZE end function LeafBlower:collect() self.collected = true end function LeafBlower:isCollected() return self.collected end function LeafBlower:draw(cam_px, cam_py) if self.collected then return end spr( LEAF_BLOWER_ITEM.sprite, self.x - cam_px, self.y - cam_py, 0, 1, 0, 0, LEAF_BLOWER_ITEM.w, LEAF_BLOWER_ITEM.h ) end local RayGun = {} RayGun.__index = RayGun function RayGun.new(opts) local self = setmetatable({}, RayGun) self.tile_x = (opts and opts.tile_x) or RAY_GUN_PICKUP.tile_x self.tile_y = (opts and opts.tile_y) or RAY_GUN_PICKUP.tile_y self.x = self.tile_x * TILE_SIZE self.y = self.tile_y * TILE_SIZE self.collected = false return self end function RayGun:getBounds() return self.x, self.y, RAY_GUN_PICKUP.w * TILE_SIZE, RAY_GUN_PICKUP.h * TILE_SIZE end function RayGun:collect() self.collected = true end function RayGun:isCollected() return self.collected end function RayGun:draw(cam_px, cam_py) if self.collected then return end spr( RAY_GUN_PICKUP.sprite, self.x - cam_px, self.y - cam_py, 0, 1, 0, 0, RAY_GUN_PICKUP.w, RAY_GUN_PICKUP.h ) end local Microphone = {} Microphone.__index = Microphone function Microphone.new(opts) local self = setmetatable({}, Microphone) self.tile_x = (opts and opts.tile_x) or STORY_EVENT.microphone.tile_x self.tile_y = (opts and opts.tile_y) or STORY_EVENT.microphone.tile_y self.x = self.tile_x * TILE_SIZE self.y = self.tile_y * TILE_SIZE self.collected = false return self end function Microphone:getBounds() return self.x, self.y, STORY_EVENT.microphone.w * TILE_SIZE, STORY_EVENT.microphone.h * TILE_SIZE end function Microphone:collect() self.collected = true end function Microphone:isCollected() return self.collected end function Microphone:draw(cam_px, cam_py) if self.collected then return end spr( STORY_EVENT.microphone.sprite, self.x - cam_px, self.y - cam_py, 0, 1, 0, 0, STORY_EVENT.microphone.w, STORY_EVENT.microphone.h ) end --[[-------------------------------------------------------------------- ENEMY (PORTAL SPRITE) --------------------- Simple AI that patrols/chases the player using pathfinding on walkable tiles. Mode switches (chase/vulnerable) tweak speed and visuals. --------------------------------------------------------------------]]-- local Enemy = {} Enemy.__index = Enemy function Enemy.new(opts) local self = setmetatable({}, Enemy) self.spawn_tile_x = (opts and opts.tile_x) or PORTAL_TILE_X self.spawn_tile_y = (opts and opts.tile_y) or PORTAL_TILE_Y self.tile_x = self.spawn_tile_x self.tile_y = self.spawn_tile_y self.target_tile_x = self.tile_x self.target_tile_y = self.tile_y self.prev_tile_x = self.tile_x self.prev_tile_y = self.tile_y self.mode = "chase" self.mode_timer = 0 self.vulnerable_timer = 0 self.dir = "down" self.anim_index = 1 self.anim_timer = 0 self.anim_dir = self.dir self.x, self.y = Enemy.tile_to_position(self.tile_x, self.tile_y) self.speed_multiplier = 1 self.sfx_timer = 0 self.moved_this_frame = false self:setMode("chase") return self end function Enemy.tile_to_position(tile_x, tile_y) local cx, cy = tile_center_px(tile_x, tile_y) return cx - ENEMY_HALF_W, cy - ENEMY_HALF_H end function Enemy:setMode(mode) if self.mode == mode then return end self.mode = mode self.mode_timer = 0 if mode == "vulnerable" then self.vulnerable_timer = 0 end end function Enemy:getSpeed() if self.mode == "chase" then return ENEMY_CHASE_SPEED elseif self.mode == "vulnerable" then return ENEMY_VULNERABLE_SPEED end return ENEMY_PATROL_SPEED end function Enemy:updateMode(player) self.mode_timer = self.mode_timer + 1 self.vulnerable_timer = self.vulnerable_timer + 1 if self.mode ~= "vulnerable" then self:setMode("chase") end if self.mode ~= "vulnerable" and self.vulnerable_timer >= ENEMY_VULNERABLE_INTERVAL then self:setMode("vulnerable") elseif self.mode == "vulnerable" and self.mode_timer >= ENEMY_VULNERABLE_DURATION then self:setMode("chase") end end function Enemy:getNeighborTiles() local neighbors = {} for _, dir in ipairs(MOVEMENT_DIRS) do local next_tx = self.tile_x + dir.dx local next_ty = self.tile_y + dir.dy if is_path_tile(next_tx, next_ty) then neighbors[#neighbors + 1] = {tx = next_tx, ty = next_ty, dir = dir.name} end end return neighbors end local function distance_squared_tile(tx, ty, target_px, target_py) local cx, cy = tile_center_px(tx, ty) local dx = target_px - cx local dy = target_py - cy return dx * dx + dy * dy end function Enemy:chooseNextTarget(player) local neighbors = self:getNeighborTiles() if #neighbors == 0 then self.target_tile_x = self.tile_x self.target_tile_y = self.tile_y return end local best = nil local player_cx = player.x + PLAYER_FOOT_OFFSET_X local player_cy = player.y + PLAYER_FOOT_OFFSET_Y local function not_reverse(candidate) return not (candidate.tx == self.prev_tile_x and candidate.ty == self.prev_tile_y) end if self.mode == "chase" then local min_dist = nil for _, candidate in ipairs(neighbors) do local dist = distance_squared_tile(candidate.tx, candidate.ty, player_cx, player_cy) if not_reverse(candidate) then if not min_dist or dist < min_dist then min_dist = dist best = candidate end end end elseif self.mode == "vulnerable" then local max_dist = nil for _, candidate in ipairs(neighbors) do local dist = distance_squared_tile(candidate.tx, candidate.ty, player_cx, player_cy) if not_reverse(candidate) then if not max_dist or dist > max_dist then max_dist = dist best = candidate end end end else -- patrol for _, candidate in ipairs(neighbors) do if candidate.dir == self.dir and not_reverse(candidate) then best = candidate break end end end if not best then local pool = {} for _, candidate in ipairs(neighbors) do if not_reverse(candidate) then pool[#pool + 1] = candidate end end if #pool == 0 then pool = neighbors end best = pool[math.random(#pool)] end self.prev_tile_x = self.tile_x self.prev_tile_y = self.tile_y self.target_tile_x = best.tx self.target_tile_y = best.ty self.dir = best.dir or self.dir end function Enemy:updateAnimation() if self.anim_dir ~= self.dir then self.anim_dir = self.dir self.anim_index = 1 self.anim_timer = 0 end local frames = ENEMY_ANIMATION_FRAMES[self.dir] or ENEMY_ANIMATION_FRAMES.neutral self.anim_timer = self.anim_timer + 1 if self.anim_timer >= ENEMY_ANIM_FRAME_DELAY then self.anim_timer = 0 self.anim_index = self.anim_index + 1 if self.anim_index > #frames then self.anim_index = 1 end end end function Enemy:advancePosition(player) local speed = self:getSpeed() local target_x, target_y = Enemy.tile_to_position(self.target_tile_x, self.target_tile_y) local dx = target_x - self.x local dy = target_y - self.y local dist = math.sqrt(dx * dx + dy * dy) if dist < 0.0001 then self.x = target_x self.y = target_y self.tile_x = self.target_tile_x self.tile_y = self.target_tile_y self:chooseNextTarget(player) return end if dist <= speed then self.x = target_x self.y = target_y self.tile_x = self.target_tile_x self.tile_y = self.target_tile_y self:chooseNextTarget(player) else self.x = self.x + dx / dist * speed self.y = self.y + dy / dist * speed end end function Enemy:update(player) if not player then return end local old_x = self.x local old_y = self.y self:updateMode(player) self:updateAnimation() self:advancePosition(player) self.moved_this_frame = (math.abs(self.x - old_x) > 0.01 or math.abs(self.y - old_y) > 0.01) end function Enemy:draw(cam_px, cam_py) local dir = self.dir or "down" local frames = ENEMY_ANIMATION_FRAMES[dir] or ENEMY_ANIMATION_FRAMES.neutral local base_frame = frames[self.anim_index] or frames[1] local flip = ENEMY_DIRECTION_FLIPS[dir] or 0 if self.mode == "vulnerable" then -- simple blink by toggling visibility if (self.mode_timer % 20) < 10 then flip = 1 - flip end end spr( base_frame, self.x - cam_px, self.y - cam_py, 0, 1, flip, 0, ENEMY_SPRITE_W, ENEMY_SPRITE_H ) end function Enemy:getBounds() return self.x, self.y, ENEMY_PX_W, ENEMY_PX_H end function Enemy:getMazeCollisionBounds() return self.x, self.y, ENEMY_PX_W, TILE_SIZE end function Enemy:resetToSpawn() self.tile_x = self.spawn_tile_x self.tile_y = self.spawn_tile_y self.target_tile_x = self.tile_x self.target_tile_y = self.tile_y self.prev_tile_x = self.tile_x self.prev_tile_y = self.tile_y self.mode_timer = 0 self.vulnerable_timer = 0 self.dir = "down" self.anim_index = 1 self.anim_timer = 0 self.anim_dir = self.dir self.sfx_timer = 0 self.moved_this_frame = false self.x, self.y = Enemy.tile_to_position(self.tile_x, self.tile_y) self:setMode("chase") end function Enemy:onPlayerCollision() self:setMode("vulnerable") end local Gnome = {} Gnome.__index = Gnome function Gnome.new(opts) local self = setmetatable({}, Gnome) self.x = (opts and opts.x) or 0 self.y = (opts and opts.y) or 0 self.alerted = false self.home_x = self.x self.home_y = self.y self.idle_speed = (opts and opts.idle_speed) or GNOME_IDLE_SPEED self.chase_speed = (opts and opts.chase_speed) or GNOME_CHASE_SPEED self.idle_timer = math.random(GNOME_IDLE_HOP_INTERVAL) self.target_x = self.x self.target_y = self.y self.blown = false self.removed = false self.blow_vx = 0 self.blow_vy = 0 self.spin = 0 return self end function Gnome:calm() if self.blown or self.removed then return end self.alerted = false self:setIdleTarget(true) end function Gnome:getCenter() return self.x + GNOME_HALF_W, self.y + GNOME_HALF_H end function Gnome:setIdleTarget(to_home) if to_home then self.target_x = self.home_x self.target_y = self.home_y else local angle = math.random() * math.pi * 2 local radius = math.random() * GNOME_IDLE_WANDER_RADIUS_PX self.target_x = self.home_x + math.cos(angle) * radius self.target_y = self.home_y + math.sin(angle) * radius end self.idle_timer = GNOME_IDLE_HOP_INTERVAL end function Gnome:moveTowards(tx, ty, speed) local dx = tx - self.x local dy = ty - self.y local dist = math.sqrt(dx * dx + dy * dy) if dist < 0.0001 then self.x = tx self.y = ty return true end if dist <= speed then self.x = tx self.y = ty return true end self.x = self.x + dx / dist * speed self.y = self.y + dy / dist * speed return false end function Gnome:updateIdle() self.idle_timer = (self.idle_timer or 0) - 1 if not self.target_x or self.idle_timer <= 0 then self:setIdleTarget(false) end local arrived = self:moveTowards(self.target_x, self.target_y, self.idle_speed) if arrived then self:setIdleTarget(false) end end function Gnome:update(player) if self.removed then return end if self.blown then self.x = self.x + self.blow_vx self.y = self.y + self.blow_vy self.spin = (self.spin + 1) % 4 local margin = LEAF_BLOWER_ITEM.gust_offscreen_margin_px or 0 if self.x < -margin or self.y < -margin or self.x > (MAP_TILES_W * TILE_SIZE) + margin or self.y > (MAP_TILES_H * TILE_SIZE) + margin then self.removed = true end return end local player_visible = player and not (player.isHidden and player:isHidden()) and not (player.isPlayingSpecialAnimation and player:isPlayingSpecialAnimation()) if not player_visible then if self.alerted then self.alerted = false self:setIdleTarget(true) end self:updateIdle() return end local player_cx = player.x + PLAYER_FOOT_OFFSET_X local player_cy = player.y + PLAYER_FOOT_OFFSET_Y local gx, gy = self:getCenter() local dx = player_cx - gx local dy = player_cy - gy local dist_sq = dx * dx + dy * dy if dist_sq <= GNOME_ALERT_RADIUS_SQ then self.alerted = true elseif self.alerted then self.alerted = false self:setIdleTarget(true) self:updateIdle() return else self:updateIdle() return end local target_x = player_cx - GNOME_HALF_W local target_y = player_cy - GNOME_HALF_H self:moveTowards(target_x, target_y, self.chase_speed) end function Gnome:getBounds() return self.x, self.y, GNOME_PX_W, GNOME_PX_H end function Gnome:blowAway(dir) if self.blown or self.removed then return end local dir_info = MOVEMENT_DIR_LOOKUP[dir] or MOVEMENT_DIR_LOOKUP.right local speed = LEAF_BLOWER_ITEM.gust_push_speed or 3 self.blown = true self.alerted = false self.blow_vx = (dir_info and dir_info.dx or 1) * speed self.blow_vy = (dir_info and dir_info.dy or 0) * speed end function Gnome:draw(cam_px, cam_py) if self.removed then return end spr( GNOME_SPRITE_ID, self.x - cam_px, self.y - cam_py, 0, 1, 0, self.blown and self.spin or 0, GNOME_SPRITE_W, GNOME_SPRITE_H ) end local HoodedFigure = {} HoodedFigure.__index = HoodedFigure function HoodedFigure.new(opts) local self = setmetatable({}, HoodedFigure) self.spawn_tile_x = (opts and opts.tile_x) or HOODED_FIGURE.tile_x self.spawn_tile_y = (opts and opts.tile_y) or HOODED_FIGURE.tile_y self.tile_x = self.spawn_tile_x self.tile_y = self.spawn_tile_y self.target_tile_x = self.tile_x self.target_tile_y = self.tile_y self.prev_tile_x = self.tile_x self.prev_tile_y = self.tile_y self.dir = "right" self.speed = HOODED_FIGURE.speed self.state = "idle" self.state_timer = 0 self.escape_dir = "right" self.journal_dropped = false self.anim_timer = 0 self.flash_visible = false self.x, self.y = Enemy.tile_to_position(self.tile_x, self.tile_y) return self end function HoodedFigure:getCenter() return self.x + TILE_SIZE, self.y + TILE_SIZE end function HoodedFigure:getBounds() return self.x, self.y, HOODED_FIGURE.w * TILE_SIZE, HOODED_FIGURE.h * TILE_SIZE end function HoodedFigure:isDefeated() return self.journal_dropped end function HoodedFigure:getNeighborTiles() local neighbors = {} for _, dir in ipairs(MOVEMENT_DIRS) do local next_tx = self.tile_x + dir.dx local next_ty = self.tile_y + dir.dy if is_path_tile(next_tx, next_ty) then neighbors[#neighbors + 1] = {tx = next_tx, ty = next_ty, dir = dir.name} end end return neighbors end function HoodedFigure:chooseNextTarget(player) local neighbors = self:getNeighborTiles() if #neighbors == 0 then self.target_tile_x = self.tile_x self.target_tile_y = self.tile_y return end local player_cx = player.x + PLAYER_FOOT_OFFSET_X local player_cy = player.y + PLAYER_FOOT_OFFSET_Y local best = nil local max_dist = nil for _, candidate in ipairs(neighbors) do local reversing = candidate.tx == self.prev_tile_x and candidate.ty == self.prev_tile_y if not reversing then local dist = distance_squared_tile(candidate.tx, candidate.ty, player_cx, player_cy) if not max_dist or dist > max_dist then max_dist = dist best = candidate end end end if not best then best = neighbors[math.random(#neighbors)] end self.prev_tile_x = self.tile_x self.prev_tile_y = self.tile_y self.target_tile_x = best.tx self.target_tile_y = best.ty self.dir = best.dir or self.dir end function HoodedFigure:advancePosition(player) local target_x, target_y = Enemy.tile_to_position(self.target_tile_x, self.target_tile_y) local dx = target_x - self.x local dy = target_y - self.y local dist = math.sqrt(dx * dx + dy * dy) if dist < 0.0001 then self.x = target_x self.y = target_y self.tile_x = self.target_tile_x self.tile_y = self.target_tile_y self:chooseNextTarget(player) return end if dist <= self.speed then self.x = target_x self.y = target_y self.tile_x = self.target_tile_x self.tile_y = self.target_tile_y self:chooseNextTarget(player) else self.x = self.x + dx / dist * self.speed self.y = self.y + dy / dist * self.speed end end function HoodedFigure:runOffscreen(cEamera_px, camera_py) local speed = HOODED_FIGURE.escape_speed or self.speed if self.escape_dir == "left" then self.x = self.x - speed self.dir = "left" else self.x = self.x + speed self.dir = "right" end local margin = HOODED_FIGURE.offscreen_margin_px or 0 local screen_left = (camera_px or 0) - margin local screen_right = (camera_px or 0) + SCREEN_W + margin if (self.x + HOODED_FIGURE.w * TILE_SIZE) < screen_left or self.x > screen_right then self.state = "gone" end end function HoodedFigure:update(player, camera_px, camera_py) if self.state == "gone" or not player then return end self.anim_timer = (self.anim_timer + 1) % math.max(1, HOODED_FIGURE.move_anim_period or 1) self.flash_visible = false if self.state == "flash" then self.flash_visible = (self.state_timer % 2) == 0 self.state_timer = self.state_timer - 1 if self.state_timer <= 0 then self.state = "look_left" self.state_timer = HOODED_FIGURE.look_frames self.flash_visible = false end return end if self.state == "look_left" then self.dir = "left" self.state_timer = self.state_timer - 1 if self.state_timer <= 0 then self.state = "look_right" self.state_timer = HOODED_FIGURE.look_frames end return end if self.state == "look_right" then self.dir = "right" self.state_timer = self.state_timer - 1 if self.state_timer <= 0 then self.state = "escape" end return end if self.state == "escape" then self:runOffscreen(camera_px, camera_py) return end if player.isHidden and player:isHidden() then return end if player.isPlayingSpecialAnimation and player:isPlayingSpecialAnimation() then return end local player_cx = player.x + PLAYER_FOOT_OFFSET_X local player_cy = player.y + PLAYER_FOOT_OFFSET_Y local hood_cx, hood_cy = self:getCenter() local dx = player_cx - hood_cx local dy = player_cy - hood_cy local radius = HOODED_FIGURE.activate_radius_px if self.state == "idle" and (dx * dx + dy * dy) <= (radius * radius) then self.state = "flee" self:chooseNextTarget(player) end if self.state ~= "flee" then return end self:advancePosition(player) end function HoodedFigure:dropJournal() if self.journal_dropped then return nil end self.journal_dropped = true local drop_tx = self.tile_x local drop_ty = self.tile_y return { id = 2, tile_x = drop_tx, tile_y = drop_ty, sprite = JOURNAL_SPRITE_IDS[2] or JOURNAL_SPRITE_IDS[1], collected = false, dropped = true } end function HoodedFigure:onShot(player) if self.journal_dropped then return nil end local hood_cx = self.x + TILE_SIZE local player_cx = hood_cx if player then player_cx = player.x + PLAYER_FOOT_OFFSET_X end self.escape_dir = hood_cx < player_cx and "left" or "right" self.state = "flash" self.state_timer = HOODED_FIGURE.flash_frames self.target_tile_x = self.tile_x self.target_tile_y = self.tile_y return self:dropJournal() end function HoodedFigure:draw(cam_px, cam_py) if self.state == "gone" then return end if self.flash_visible then return end local flip = 0 if self.dir == "left" then flip = 1 end local bob = 0 if self.state == "flee" or self.state == "escape" then if self.anim_timer >= math.floor((HOODED_FIGURE.move_anim_period or 2) / 2) then bob = -(HOODED_FIGURE.move_bob_px or 0) end end spr( HOODED_FIGURE.sprite, self.x - cam_px, self.y - cam_py + bob, 0, 1, flip, 0, HOODED_FIGURE.w, HOODED_FIGURE.h ) end --[[-------------------------------------------------------------------- GAME CONTROLLER --------------- Owns every runtime system: splash/start screens, player/enemy updates, power-up management, HUD rendering, and the master tick order. --------------------------------------------------------------------]]-- local Game = {} Game.__index = Game function Game:resetWorld() self.player_appearance_index = 1 self.player = Player.new({appearance_index = self.player_appearance_index}) self.camera_chunk_x = 0 self.camera_chunk_y = 0 self.camera_px = 0 self.camera_py = 0 self.camera_tile_x = 0 self.camera_tile_y = 0 self.collected_markers = {} self.score = 0 self.run_frames = 0 self.lives = HUD.player_start_lives self.walkable_tiles = self.walkable_tiles or collect_walkable_tiles() self.powerups = {} local occupied = {} local player_tile = self:getPlayerTile() if self.walkable_tiles then for i = 1, MAX_ACTIVE_POWERUPS do local tile = pick_random_powerup_tile(self.walkable_tiles, occupied, player_tile) if tile then occupied[marker_tile_key(tile.tx, tile.ty)] = true self.powerups[#self.powerups + 1] = Powerup.new(tile) end end end local placement_occupied = self:getPowerupOccupancy() placement_occupied[marker_tile_key(GNOME_JOURNAL_TILE_X, GNOME_JOURNAL_TILE_Y)] = true placement_occupied[marker_tile_key(RAY_GUN_PICKUP.tile_x, RAY_GUN_PICKUP.tile_y)] = true placement_occupied[marker_tile_key(HOODED_FIGURE.tile_x, HOODED_FIGURE.tile_y)] = true placement_occupied[marker_tile_key(LEAF_BLOWER_ITEM.tile_x, LEAF_BLOWER_ITEM.tile_y)] = true placement_occupied[marker_tile_key(STORY_EVENT.microphone.tile_x, STORY_EVENT.microphone.tile_y)] = true self.journals = {} self:addPermanentJournal() self:addStoryTriggerJournal() self.collected_journals = 0 self.enemy = nil self:respawnEnemyAwayFromPlayer() self:spawnGnomes() self:updateCamera(true) self.active_powerup_timer = 0 self.powerup_overlay_timer = 0 self.player_trail = {} self.player_trail_timer = 0 self.show_splash = true self.splash_timer = FRONTEND.splash.duration_frames self.show_start = false self.show_intro_cutscene = false self.show_victory = false self.show_results = false self.show_high_scores = false self.frontend_start_transition = nil self.waiting_for_input_release = false self.start_menu_index = 1 self.frontend_timer = 0 self.objective_prompt_timer = 0 self.post_intro_page = nil self.post_intro_lock_timer = 0 self.game_over_source = nil self.game_over_timer = 0 self.soos_death_message_index = 0 self.soos_death_message_line = nil self.soos_death_message_timer = 0 self.frontend_music_mode = nil if type(music) == "function" then music(-1) end self.pickup_audio_timer = 0 self.light_beam = nil self.leaf_blower_blast = nil self.hole_teleport_cooldown = 0 self.hole_exit_lock_index = nil BOTTOMLESS_PIT.lock_rect = nil self.ray_gun = RayGun.new({tile_x = RAY_GUN_PICKUP.tile_x, tile_y = RAY_GUN_PICKUP.tile_y}) self.has_ray_gun = false self.hooded_figure = HoodedFigure.new({tile_x = HOODED_FIGURE.tile_x, tile_y = HOODED_FIGURE.tile_y}) self.leaf_blower = LeafBlower.new({tile_x = LEAF_BLOWER_ITEM.tile_x, tile_y = LEAF_BLOWER_ITEM.tile_y}) self.has_leaf_blower = false self.microphone = Microphone.new({tile_x = STORY_EVENT.microphone.tile_x, tile_y = STORY_EVENT.microphone.tile_y}) self.has_microphone = false self.show_game_over = false self.equipped_item = nil self.grapple_shot = nil self.has_grappling_hook = false self.grappling_hook = nil self.results_data = nil BILL_BOSS.arena_active = false self:resetBillBoss() self:resetStoryEvent() self:resetIntroCutscene() end function Game:respawnEnemyAwayFromPlayer() if DEV.mode and DEV.disable_enemy then self.enemy = nil return end self.walkable_tiles = self.walkable_tiles or collect_walkable_tiles() local player_cx = self.player.x + PLAYER_FOOT_OFFSET_X local player_cy = self.player.y + PLAYER_FOOT_OFFSET_Y local chunk_x = math.floor(player_cx / SCREEN_W) local chunk_y = math.floor(player_cy / SCREEN_H) local chunk_tiles = collect_tiles_in_chunk(self.walkable_tiles, chunk_x, chunk_y) local spawn_tile = pick_distant_tile(chunk_tiles, player_cx, player_cy, ENEMY_MIN_SPAWN_DISTANCE) if not spawn_tile then spawn_tile = pick_distant_tile(self.walkable_tiles, player_cx, player_cy, ENEMY_MIN_SPAWN_DISTANCE) end if not spawn_tile then spawn_tile = {tx = PORTAL_TILE_X, ty = PORTAL_TILE_Y} end if self.enemy then self.enemy.spawn_tile_x = spawn_tile.tx self.enemy.spawn_tile_y = spawn_tile.ty self.enemy:resetToSpawn() else self.enemy = Enemy.new({tile_x = spawn_tile.tx, tile_y = spawn_tile.ty}) end end function Game:addPermanentJournal() if not self.journals then self.journals = {} end local sprite = GNOME_JOURNAL_SPRITE_ID or JOURNAL_SPRITE_IDS[1] self.journals[#self.journals + 1] = { id = 1, tile_x = GNOME_JOURNAL_TILE_X, tile_y = GNOME_JOURNAL_TILE_Y, sprite = sprite, collected = false, permanent = true } end function Game:addStoryTriggerJournal() if not self.journals then self.journals = {} end self.journals[#self.journals + 1] = { id = "story_trigger", tile_x = STORY_EVENT.trigger_journal.tile_x, tile_y = STORY_EVENT.trigger_journal.tile_y, sprite = STORY_EVENT.trigger_journal.sprite, collected = false, story_trigger = true } end function Game:storyActorFootTileToPx(tx, ty, h_tiles) h_tiles = h_tiles or 2 return tx * TILE_SIZE, (ty - h_tiles + 1) * TILE_SIZE end function Game:setPlayerFootTile(tx, ty) local foot_px, foot_py = tile_center_px(tx, ty) self.player.x = foot_px - PLAYER_FOOT_OFFSET_X self.player.y = foot_py - PLAYER_FOOT_OFFSET_Y end function Game:resetBillBoss() local bill_x, bill_y = self:storyActorFootTileToPx( BILL_BOSS.bill_spawn_foot_tile_x, BILL_BOSS.bill_spawn_foot_tile_y, 2 ) local soos_x, soos_y = self:storyActorFootTileToPx( BILL_BOSS.soos.foot_tile_x, BILL_BOSS.soos.foot_tile_y, BILL_BOSS.soos.h ) local mabel_x, mabel_y = self:storyActorFootTileToPx( BILL_BOSS.ending.mabel_foot_tile_x, BILL_BOSS.ending.mabel_foot_tile_y, 2 ) self.bill_boss = { phase = "inactive", timer = 0, flash_timer = 0, dialogue_index = 1, ending_form = "bill", hook_spawn_index = 1, arena_powerup = nil, player_hit_timer = 0, player_invulnerable_timer = 0, player_hit_dx = 0, player_hit_dy = 0, player_hit_speed = BILL_BOSS.player_hit_push_speed or 1.4, hit_streak = 0, shockwave_timer = 0, bill = { x = bill_x, y = bill_y, target_x = bill_x, target_y = bill_y, dir = "neutral", health = BILL_BOSS.health, invulnerable_timer = 0, attack_state = "move", attack_timer = BILL_BOSS.move_phase_frames or 75, zap = nil, visible = false }, soos_x = soos_x, soos_y = soos_y, mabel_x = mabel_x, mabel_y = mabel_y } end function Game:isBillBossActive() local boss = self.bill_boss return boss and boss.phase and boss.phase ~= "inactive" end function Game:isBillBossArenaPhase() local boss = self.bill_boss if not boss then return false end return boss.phase == "arena_spawn" or boss.phase == "fight" or boss.phase == "death" or boss.phase == "soos" or boss.phase == "outro" end function Game:spawnGrapplingHook(index) local spawn_tiles = BILL_BOSS.hook.spawn_tiles or {} if #spawn_tiles == 0 then self.grappling_hook = nil return end index = ((index or 1) - 1) % #spawn_tiles + 1 local tile = spawn_tiles[index] self.bill_boss.hook_spawn_index = index self.grappling_hook = { tile_x = tile.tx, tile_y = tile.ty, x = tile.tx * TILE_SIZE, y = tile.ty * TILE_SIZE, active = true } self.has_grappling_hook = false if self.equipped_item == ITEMS.grappling_hook then self.equipped_item = nil end end function Game:spawnBillArenaPowerup() local boss = self.bill_boss if not boss then return end boss.arena_powerup = Powerup.new({ tx = BILL_BOSS.powerup_tile_x, ty = BILL_BOSS.powerup_tile_y }) end function Game:getBillBounds() local boss = self.bill_boss if not boss then return nil end local bill = boss.bill return bill.x, bill.y, TILE_SIZE * 2, TILE_SIZE * 2 end function Game:chooseBillTarget() local boss = self.bill_boss if not boss then return end local rect = BILL_BOSS.arena_rect local margin = BILL_BOSS.arena_margin_tiles or 2 local min_tx = rect.x + margin local max_tx = rect.x + rect.w - margin - 2 local min_ty = rect.y + margin local max_ty = rect.y + rect.h - margin - 2 if max_tx < min_tx then max_tx = min_tx end if max_ty < min_ty then max_ty = min_ty end boss.bill.target_x = math.random(min_tx, max_tx) * TILE_SIZE boss.bill.target_y = math.random(min_ty, max_ty) * TILE_SIZE end function Game:teleportBill() local boss = self.bill_boss if not boss or not boss.bill then return end local bill = boss.bill local min_dist_px = (BILL_BOSS.teleport_min_distance_tiles or 6) * TILE_SIZE local min_dist_sq = min_dist_px * min_dist_px for _ = 1, 8 do self:chooseBillTarget() local target_x = bill.target_x or bill.x local target_y = bill.target_y or bill.y if not self.player then bill.x = target_x bill.y = target_y return end local player_cx = self.player.x + PLAYER_FOOT_OFFSET_X local player_cy = self.player.y + PLAYER_FOOT_OFFSET_Y local bill_cx = target_x + TILE_SIZE local bill_cy = target_y + TILE_SIZE local dx = player_cx - bill_cx local dy = player_cy - bill_cy if (dx * dx + dy * dy) >= min_dist_sq then bill.x = target_x bill.y = target_y return end end bill.x = bill.target_x or bill.x bill.y = bill.target_y or bill.y end function Game:setBillIntroDirection() local boss = self.bill_boss if not boss or not boss.bill then return end local look_interval = math.max(1, BILL_BOSS.intro_look_interval or 14) local look_step = math.floor(math.max(0, (boss.timer or 0) - 1) / look_interval) if (look_step % 2) == 0 then boss.bill.dir = "left" else boss.bill.dir = "right" end end function Game:faceBillTowardPlayer() local boss = self.bill_boss if not boss or not boss.bill or not self.player then return end local bill_cx = boss.bill.x + TILE_SIZE local bill_cy = boss.bill.y + TILE_SIZE local player_cx = self.player.x + PLAYER_FOOT_OFFSET_X local player_cy = self.player.y + PLAYER_FOOT_OFFSET_Y local dx = player_cx - bill_cx local dy = player_cy - bill_cy if math.abs(dx) > math.abs(dy) then boss.bill.dir = dx < 0 and "left" or "right" else boss.bill.dir = dy < 0 and "up" or "down" end end function Game:applyBillPlayerHit() local boss = self.bill_boss if not boss or boss.phase ~= "fight" or not self.player then return end if (boss.player_invulnerable_timer or 0) > 0 then return end self:applyBillPlayerPush( BILL_BOSS.player_hit_push_frames or 18, BILL_BOSS.player_hit_push_speed or 1.4, BILL_BOSS.player_hit_invulnerable_frames or 90 ) self:loseLife() self:clearItemAction() end function Game:applyBillPlayerPush(push_frames, push_speed, invulnerable_frames) local boss = self.bill_boss if not boss or boss.phase ~= "fight" or not self.player then return end local bill = boss.bill local bill_cx = bill.x + TILE_SIZE local bill_cy = bill.y + TILE_SIZE local player_cx = self.player.x + PLAYER_FOOT_OFFSET_X local player_cy = self.player.y + PLAYER_FOOT_OFFSET_Y local dx = player_cx - bill_cx local dy = player_cy - bill_cy local dist = math.sqrt(dx * dx + dy * dy) if dist < 0.0001 then dx = -1 dy = 0 dist = 1 end boss.player_hit_dx = dx / dist boss.player_hit_dy = dy / dist boss.player_hit_timer = push_frames or (BILL_BOSS.player_hit_push_frames or 18) boss.player_hit_speed = push_speed or (BILL_BOSS.player_hit_push_speed or 1.4) boss.player_invulnerable_timer = math.max(boss.player_invulnerable_timer or 0, invulnerable_frames or 0) if self.player.setMovementLocked then self.player:setMovementLocked(true) end end function Game:updateBillPlayerRecovery() local boss = self.bill_boss if not boss or not self.player then return end if (boss.player_invulnerable_timer or 0) > 0 then boss.player_invulnerable_timer = boss.player_invulnerable_timer - 1 end if (boss.player_hit_timer or 0) <= 0 then return end boss.player_hit_timer = boss.player_hit_timer - 1 local rect = BILL_BOSS.arena_rect local speed = boss.player_hit_speed or BILL_BOSS.player_hit_push_speed or 1.4 local min_x = rect.x * TILE_SIZE local max_x = (rect.x + rect.w) * TILE_SIZE - TILE_SIZE * 2 local min_y = rect.y * TILE_SIZE local max_y = (rect.y + rect.h) * TILE_SIZE - TILE_SIZE * 2 local new_x = self.player.x + (boss.player_hit_dx or 0) * speed local new_y = self.player.y + (boss.player_hit_dy or 0) * speed if new_x < min_x then new_x = min_x end if new_x > max_x then new_x = max_x end if new_y < min_y then new_y = min_y end if new_y > max_y then new_y = max_y end self.player.x = new_x self.player.y = new_y if boss.player_hit_timer <= 0 and self.player.setMovementLocked then self.player:setMovementLocked(false) end end function Game:startBillIntro() local boss = self.bill_boss if not boss or boss.phase ~= "inactive" then return end self:clearItemAction() self.grapple_shot = nil self.grappling_hook = nil self.has_grappling_hook = false self.equipped_item = nil BILL_BOSS.arena_active = false boss.phase = "pre_intro" boss.timer = 0 boss.flash_timer = 0 boss.dialogue_index = 1 boss.player_hit_timer = 0 boss.player_invulnerable_timer = 0 boss.player_hit_speed = BILL_BOSS.player_hit_push_speed or 1.4 boss.hit_streak = 0 boss.shockwave_timer = 0 boss.arena_powerup = nil boss.bill.visible = true boss.bill.health = BILL_BOSS.health boss.bill.invulnerable_timer = 0 boss.bill.attack_state = "move" boss.bill.attack_timer = BILL_BOSS.move_phase_frames or 75 boss.bill.zap = nil local facing = (self.player.getFacingDir and self.player:getFacingDir()) or "right" local dir_info = MOVEMENT_DIR_LOOKUP[facing] or MOVEMENT_DIR_LOOKUP.right boss.bill.x = self.player.x + (dir_info.dx or 1) * ((BILL_BOSS.intro_distance_tiles or 5) * TILE_SIZE) boss.bill.y = self.player.y + (dir_info.dy or 0) * ((BILL_BOSS.intro_distance_tiles or 5) * TILE_SIZE) boss.bill.dir = facing if self.player and self.player.setMovementLocked then self.player:setMovementLocked(true) end end function Game:beginBillArena() local boss = self.bill_boss if not boss then return end BILL_BOSS.arena_active = true boss.phase = "arena_spawn" boss.timer = 0 boss.flash_timer = BILL_BOSS.arena_spawn_flash_frames or 20 boss.player_hit_timer = 0 boss.player_invulnerable_timer = 0 boss.player_hit_speed = BILL_BOSS.player_hit_push_speed or 1.4 boss.hit_streak = 0 boss.shockwave_timer = 0 boss.bill.health = BILL_BOSS.health boss.bill.invulnerable_timer = 0 boss.bill.attack_state = "move" boss.bill.attack_timer = BILL_BOSS.move_phase_frames or 75 boss.bill.zap = nil boss.bill.visible = true boss.bill.x, boss.bill.y = self:storyActorFootTileToPx( BILL_BOSS.bill_spawn_foot_tile_x, BILL_BOSS.bill_spawn_foot_tile_y, 2 ) boss.bill.dir = "left" self:chooseBillTarget() self:setPlayerFootTile(BILL_BOSS.player_spawn_foot_tile_x, BILL_BOSS.player_spawn_foot_tile_y) if self.player and self.player.setMovementLocked then self.player:setMovementLocked(true) end self:spawnGrapplingHook(1) self:spawnBillArenaPowerup() self.player.hidden = true self.player:startSpecialAnimation( PORTAL_PLAYER_ANIM_FRAMES_REVERSED, PORTAL_PLAYER_ANIM_DELAY, { mode = "reappear", start_hidden = true, on_finish = function(player) if self.bill_boss and self.bill_boss.phase == "arena_spawn" then self.bill_boss.phase = "fight" self.bill_boss.timer = 0 if player and player.setMovementLocked then player:setMovementLocked(false) end end end } ) self:updateCamera(true) end function Game:launchBillZap() local boss = self.bill_boss if not boss or not self.player then return end local bill = boss.bill local bob_period = math.max(1, BILL_BOSS.bob_period or 96) local bob_angle = (((self.run_frames or 0) + (boss.timer or 0)) % bob_period) / bob_period * math.pi * 2 local bob_y = math.floor(math.sin(bob_angle) * 2 + 0.5) local start_x = bill.x + (BILL_ZAP_VISUAL.eye_offset_x or 8) local start_y = bill.y + bob_y + (BILL_ZAP_VISUAL.eye_offset_y or 7) local target_x = self.player.x + PLAYER_FOOT_OFFSET_X local target_y = self.player.y + PLAYER_FOOT_OFFSET_Y local dx = target_x - start_x local dy = target_y - start_y local dist = math.sqrt(dx * dx + dy * dy) if dist < 0.0001 then dist = 1 end local speed = BILL_BOSS.zap_speed or 2 bill.zap = { x = start_x, y = start_y, vx = dx / dist * speed, vy = dy / dist * speed, life = BILL_BOSS.zap_life_frames or 30 } end function Game:startBillOutro() local boss = self.bill_boss if not boss then return end self:clearItemAction() boss.phase = "outro" boss.timer = 0 boss.flash_timer = BILL_BOSS.outro_flash_frames or 12 boss.bill.zap = nil boss.arena_powerup = nil if self.player and self.player.setMovementLocked then self.player:setMovementLocked(true) end if self.player and not self.player:isHidden() and not self.player:isPlayingSpecialAnimation() then self.player:startSpecialAnimation( PORTAL_PLAYER_ANIM_FRAMES, PORTAL_PLAYER_ANIM_DELAY, {mode = "disappear", start_hidden = false} ) end end function Game:damageBill() local boss = self.bill_boss if not boss or boss.phase ~= "fight" then return end local bill = boss.bill if bill.invulnerable_timer and bill.invulnerable_timer > 0 then return end bill.health = math.max(0, (bill.health or BILL_BOSS.health) - (BILL_BOSS.damage_per_hit or 1)) bill.invulnerable_timer = BILL_BOSS.invulnerable_frames or 18 bill.attack_state = "recover" bill.attack_timer = BILL_BOSS.recover_frames or 45 bill.zap = nil boss.hit_streak = (boss.hit_streak or 0) + 1 if bill.health > 0 and (boss.hit_streak or 0) >= (BILL_BOSS.hits_per_shockwave or 3) then boss.hit_streak = 0 boss.shockwave_timer = (BILL_BOSS.shockwave and BILL_BOSS.shockwave.frames) or 18 self:applyBillPlayerPush( (BILL_BOSS.shockwave and BILL_BOSS.shockwave.push_frames) or 14, (BILL_BOSS.shockwave and BILL_BOSS.shockwave.push_speed) or 1.8, (BILL_BOSS.shockwave and BILL_BOSS.shockwave.invulnerable_frames) or 20 ) self:teleportBill() self:faceBillTowardPlayer() end if bill.health <= 0 then boss.phase = "death" boss.timer = 0 boss.bill.zap = nil boss.bill.attack_state = "recover" boss.bill.attack_timer = 0 self.grapple_shot = nil self.grappling_hook = nil boss.arena_powerup = nil boss.hit_streak = 0 boss.shockwave_timer = 0 if self.player and self.player.setMovementLocked then self.player:setMovementLocked(true) end end end function Game:updateBillBoss() local boss = self.bill_boss if not boss or boss.phase == "inactive" then return end if boss.flash_timer and boss.flash_timer > 0 then boss.flash_timer = boss.flash_timer - 1 end if boss.phase == "pre_intro" then if self.player and self.player.setMovementLocked then self.player:setMovementLocked(true) end if self:isConfirmPressed() then boss.dialogue_index = (boss.dialogue_index or 1) + 1 if boss.dialogue_index > #(BILL_BOSS.pre_intro_dialogue or {}) then boss.phase = "intro" boss.timer = 0 boss.flash_timer = 0 end end return end if boss.phase == "intro" then boss.timer = boss.timer + 1 local look_frames = (BILL_BOSS.intro_look_interval or 14) * (BILL_BOSS.intro_look_count or 4) local charge_end = look_frames + (BILL_BOSS.intro_charge_frames or 18) local flash_end = charge_end + (BILL_BOSS.intro_flash_frames or 18) if boss.timer <= look_frames then self:setBillIntroDirection() elseif boss.timer <= charge_end then self:faceBillTowardPlayer() elseif boss.timer <= flash_end then self:faceBillTowardPlayer() boss.flash_timer = 2 else self:beginBillArena() end return end self:updateBillPlayerRecovery() if boss.phase == "arena_spawn" then boss.timer = boss.timer + 1 self:faceBillTowardPlayer() return end if boss.phase == "fight" then boss.timer = (boss.timer or 0) + 1 if (boss.shockwave_timer or 0) > 0 then boss.shockwave_timer = boss.shockwave_timer - 1 end local bill = boss.bill if bill.invulnerable_timer and bill.invulnerable_timer > 0 then bill.invulnerable_timer = bill.invulnerable_timer - 1 end if bill.attack_state == "move" then bill.attack_timer = (bill.attack_timer or (BILL_BOSS.move_phase_frames or 75)) - 1 local dx = bill.target_x - bill.x local dy = bill.target_y - bill.y local dist = math.sqrt(dx * dx + dy * dy) if dist < 2 or (boss.timer % math.max(1, BILL_BOSS.move_target_interval or 40)) == 0 then self:chooseBillTarget() dx = bill.target_x - bill.x dy = bill.target_y - bill.y dist = math.sqrt(dx * dx + dy * dy) end if dist > 0.0001 then bill.x = bill.x + dx / dist * (BILL_BOSS.move_speed or 0.7) bill.y = bill.y + dy / dist * (BILL_BOSS.move_speed or 0.7) if math.abs(dx) > math.abs(dy) then bill.dir = dx < 0 and "left" or "right" else bill.dir = dy < 0 and "up" or "down" end else bill.dir = "neutral" end if bill.attack_timer <= 0 and not bill.zap then bill.attack_state = "aim" bill.attack_timer = BILL_BOSS.aim_frames or 20 end elseif bill.attack_state == "aim" then bill.attack_timer = (bill.attack_timer or (BILL_BOSS.aim_frames or 20)) - 1 self:faceBillTowardPlayer() if bill.attack_timer <= 0 then self:launchBillZap() bill.attack_state = "recover" bill.attack_timer = BILL_BOSS.recover_frames or 45 end elseif bill.attack_state == "recover" then bill.attack_timer = (bill.attack_timer or (BILL_BOSS.recover_frames or 45)) - 1 if bill.attack_timer <= 0 and not bill.zap then bill.attack_state = "move" bill.attack_timer = BILL_BOSS.move_phase_frames or 75 self:chooseBillTarget() end end if bill.zap then bill.zap.x = bill.zap.x + bill.zap.vx bill.zap.y = bill.zap.y + bill.zap.vy bill.zap.life = bill.zap.life - 1 local px, py, pw, ph = self.player:getBounds() if bill.zap.x >= px and bill.zap.x <= (px + pw) and bill.zap.y >= py and bill.zap.y <= (py + ph) then bill.zap = nil self:applyBillPlayerHit() elseif bill.zap.life <= 0 then bill.zap = nil end end self:updateGrappleShot() elseif boss.phase == "death" then boss.timer = boss.timer + 1 if boss.timer >= (BILL_BOSS.death_frames or 60) then boss.phase = "ending" boss.timer = 0 boss.dialogue_index = 1 boss.ending_form = "bill" boss.bill.visible = true if self.player and self.player.setMovementLocked then self.player:setMovementLocked(true) end end elseif boss.phase == "ending" then boss.timer = boss.timer + 1 if self.player and self.player.setMovementLocked then self.player:setMovementLocked(true) end if self:isConfirmPressed() then boss.dialogue_index = (boss.dialogue_index or 1) + 1 if boss.dialogue_index == 2 then boss.bill.visible = false boss.ending_form = "pac" boss.flash_timer = 6 elseif boss.dialogue_index == 3 then boss.ending_form = "normal" boss.flash_timer = 6 elseif boss.dialogue_index > #(BILL_BOSS.ending.dialogue or {}) then self:beginResults("win") end end elseif boss.phase == "soos" then boss.timer = boss.timer + 1 if boss.timer >= 10 and self:isConfirmPressed() then self:startBillOutro() end elseif boss.phase == "outro" then boss.timer = boss.timer + 1 if boss.timer >= (BILL_BOSS.outro_frames or 26) then self:beginResults("win") end end end function Game:resetStoryEvent() local zombie_x = STORY_EVENT.zombie.spawn_tile_x * TILE_SIZE local zombie_y = STORY_EVENT.zombie.spawn_tile_y * TILE_SIZE local stan_x, stan_y = self:storyActorFootTileToPx( STORY_EVENT.stan.outside_foot_tile_x, STORY_EVENT.stan.outside_foot_tile_y, STORY_EVENT.stan.h ) local mabel_x, mabel_y = self:storyActorFootTileToPx( STORY_EVENT.mabel.foot_tile_x, STORY_EVENT.mabel.foot_tile_y, STORY_EVENT.mabel.h ) self.story_event = { state = "idle", path = nil, path_index = 1, rise_timer = 0, surround_timer = 0, siege_timer = 0, shack_damage_timer = 0, shack_health = STORY_EVENT.shack.health or 4, song_timer = 0, explode_timer = 0, dialogue_page = 1, dialogue_timer = 0, zombie_x = zombie_x, zombie_y = zombie_y + (STORY_EVENT.zombie.rise_px or 0), zombie_base_y = zombie_y, zombie_visible = false, stan_x = stan_x, stan_y = stan_y, stan_visible = false, mabel_x = mabel_x, mabel_y = mabel_y, mabel_visible = false, journal_dropped = false } end function Game:isPlayerNearStoryShack() if not self.player then return false end local shack_cx, shack_cy = tile_center_px(STORY_EVENT.shack.tile_x, STORY_EVENT.shack.tile_y) local player_cx = self.player.x + PLAYER_FOOT_OFFSET_X local player_cy = self.player.y + PLAYER_FOOT_OFFSET_Y local dx = player_cx - shack_cx local dy = player_cy - shack_cy local radius = STORY_EVENT.shack.interaction_radius_px or TILE_SIZE * 2 return (dx * dx + dy * dy) <= (radius * radius) end function Game:dropStoryJournal() local story = self.story_event if not story or story.journal_dropped then return end story.journal_dropped = true self.journals[#self.journals + 1] = { id = 3, tile_x = STORY_EVENT.journal.tile_x, tile_y = STORY_EVENT.journal.tile_y, sprite = STORY_EVENT.journal.sprite, collected = false, dropped = true } end function Game:findNearestWalkableTile(tx, ty, max_radius) if is_path_tile(tx, ty) then return {tx = tx, ty = ty} end max_radius = max_radius or 4 for radius = 1, max_radius do for dy = -radius, radius do for dx = -radius, radius do if math.abs(dx) == radius or math.abs(dy) == radius then local next_tx = tx + dx local next_ty = ty + dy if is_path_tile(next_tx, next_ty) then return {tx = next_tx, ty = next_ty} end end end end end return nil end function Game:buildTilePath(start_tx, start_ty, goal_tx, goal_ty) local start_tile = self:findNearestWalkableTile(start_tx, start_ty, 4) local goal_tile = self:findNearestWalkableTile(goal_tx, goal_ty, 4) if not start_tile or not goal_tile then return nil end local start_key = marker_tile_key(start_tile.tx, start_tile.ty) local goal_key = marker_tile_key(goal_tile.tx, goal_tile.ty) local queue = {{tx = start_tile.tx, ty = start_tile.ty}} local parents = {} local visited = {[start_key] = true} local head = 1 while head <= #queue do local node = queue[head] head = head + 1 local node_key = marker_tile_key(node.tx, node.ty) if node_key == goal_key then break end for _, dir in ipairs(MOVEMENT_DIRS) do local next_tx = node.tx + dir.dx local next_ty = node.ty + dir.dy local next_key = marker_tile_key(next_tx, next_ty) if is_path_tile(next_tx, next_ty) and not visited[next_key] then visited[next_key] = true parents[next_key] = node_key queue[#queue + 1] = {tx = next_tx, ty = next_ty} end end end if not visited[goal_key] then return { {tx = goal_tile.tx, ty = goal_tile.ty} } end local reverse_path = {} local current_key = goal_key while current_key and current_key ~= start_key do local sep = string.find(current_key, ":") local tx = tonumber(string.sub(current_key, 1, sep - 1)) local ty = tonumber(string.sub(current_key, sep + 1)) reverse_path[#reverse_path + 1] = {tx = tx, ty = ty} current_key = parents[current_key] end local path = {} for i = #reverse_path, 1, -1 do path[#path + 1] = reverse_path[i] end return path end function Game:startStoryEvent() local story = self.story_event if not story or story.state ~= "idle" then return end local zombie = STORY_EVENT.zombie story.state = "intro" story.path = nil story.path_index = 1 story.rise_timer = 0 story.surround_timer = 0 story.siege_timer = 0 story.shack_damage_timer = 0 story.shack_health = STORY_EVENT.shack.health or 4 story.song_timer = 0 story.explode_timer = 0 story.dialogue_page = 1 story.dialogue_timer = 0 story.zombie_x = (zombie.approach_tile_x or zombie.spawn_tile_x) * TILE_SIZE story.zombie_base_y = zombie.spawn_tile_y * TILE_SIZE story.zombie_y = story.zombie_base_y + (zombie.rise_px or 0) story.zombie_visible = true story.stan_x, story.stan_y = self:storyActorFootTileToPx( STORY_EVENT.stan.outside_foot_tile_x, STORY_EVENT.stan.outside_foot_tile_y, STORY_EVENT.stan.h ) story.stan_visible = true story.mabel_x, story.mabel_y = self:storyActorFootTileToPx( STORY_EVENT.mabel.foot_tile_x, STORY_EVENT.mabel.foot_tile_y, STORY_EVENT.mabel.h ) story.mabel_visible = false if self.player and self.player.setMovementLocked then self.player:setMovementLocked(true) end end function Game:moveStoryZombieAlongPath() local story = self.story_event if not story then return end local target_x = (STORY_EVENT.zombie.approach_tile_x or STORY_EVENT.zombie.spawn_tile_x) * TILE_SIZE local target_y = (STORY_EVENT.zombie.approach_tile_y or STORY_EVENT.shack.tile_y) * TILE_SIZE local dx = target_x - story.zombie_x local dy = target_y - story.zombie_y local dist = math.sqrt(dx * dx + dy * dy) local speed = STORY_EVENT.zombie.speed or 0.5 if dist <= speed or dist < 0.0001 then story.zombie_x = target_x story.zombie_y = target_y story.state = "surround" story.surround_timer = 0 return end story.zombie_x = story.zombie_x + dx / dist * speed story.zombie_y = story.zombie_y + dy / dist * speed end function Game:getStoryZombieFormationOffsets() local story = self.story_event local zombie = STORY_EVENT.zombie local approach = zombie.approach_formation_offsets or {{x = 0, y = 0}} local siege = zombie.siege_formation_offsets or approach if not story then return approach end if story.state ~= "surround" then if story.state == "siege" or story.state == "sing" or story.state == "explode" or story.state == "retreat" or story.state == "done" then return siege end return approach end local blend = (story.surround_timer or 0) / math.max(1, zombie.surround_frames or 1) if blend < 0 then blend = 0 end if blend > 1 then blend = 1 end local offsets = {} local count = math.max(#approach, #siege) for i = 1, count do local a = approach[i] or approach[#approach] or {x = 0, y = 0} local b = siege[i] or siege[#siege] or a offsets[#offsets + 1] = { x = a.x + (b.x - a.x) * blend, y = a.y + (b.y - a.y) * blend } end return offsets end function Game:moveStanInside() local story = self.story_event if not story or not story.stan_visible then return true end local target_x, target_y = self:storyActorFootTileToPx( STORY_EVENT.stan.inside_foot_tile_x, STORY_EVENT.stan.inside_foot_tile_y, STORY_EVENT.stan.h ) local dx = target_x - story.stan_x local dy = target_y - story.stan_y local dist = math.sqrt(dx * dx + dy * dy) local speed = STORY_EVENT.stan.retreat_speed or 1 if dist <= speed or dist < 0.0001 then story.stan_x = target_x story.stan_y = target_y story.stan_visible = false return true end story.stan_x = story.stan_x + dx / dist * speed story.stan_y = story.stan_y + dy / dist * speed return false end function Game:triggerStoryGameOver() self:clearItemAction() if self.player and self.player.setMovementLocked then self.player:setMovementLocked(false) end local death_audio = AUDIO and AUDIO.player_death if death_audio then play_sfx_on_channel(death_audio.id, death_audio.channel) end self.lives = 0 self.game_over_source = "story" self.game_over_timer = 0 self:beginResults("game_over") end function Game:tryStartZombieSong() local story = self.story_event if not story then return false end if story.state == "sing" then return true end if story.state ~= "siege" then return false end if self.equipped_item ~= ITEMS.microphone or not self.has_microphone or not self:isItemActionHeld() or not self:isPlayerNearStoryShack() then return false end story.state = "sing" story.song_timer = 0 story.mabel_visible = true if self.player and self.player.setMovementLocked then self.player:setMovementLocked(true) end return true end function Game:updateStoryEvent() local story = self.story_event if not story or self.show_game_over then return end if story.state == "idle" then if self.player:isHidden() or self.player:isPlayingSpecialAnimation() then return end return end if story.state == "intro" then story.rise_timer = story.rise_timer + 1 local progress = story.rise_timer / math.max(1, STORY_EVENT.zombie.rise_frames or 1) if progress >= 1 then story.zombie_y = story.zombie_base_y else story.zombie_y = story.zombie_base_y + math.floor((1 - progress) * (STORY_EVENT.zombie.rise_px or 0)) end if progress >= 1 and self:isConfirmPressed() then local page_count = #(STORY_EVENT.dialogue_pages or {}) if (story.dialogue_page or 1) < page_count then story.dialogue_page = (story.dialogue_page or 1) + 1 else story.state = "march" if self.player and self.player.setMovementLocked then self.player:setMovementLocked(false) end end end elseif story.state == "march" then self:moveStoryZombieAlongPath() elseif story.state == "surround" then story.surround_timer = story.surround_timer + 1 if story.surround_timer >= (STORY_EVENT.zombie.surround_frames or 1) then story.state = "siege" story.siege_timer = 0 story.shack_damage_timer = 0 end elseif story.state == "siege" then story.siege_timer = story.siege_timer + 1 story.shack_damage_timer = (story.shack_damage_timer or 0) + 1 local damage_interval = math.max(1, STORY_EVENT.shack.damage_interval_frames or 60) if story.shack_damage_timer >= damage_interval then story.shack_damage_timer = 0 story.shack_health = math.max(0, (story.shack_health or 0) - 1) end if (story.shack_health or 0) <= 0 or story.siege_timer >= (STORY_EVENT.zombie.siege_frames or 240) then self:triggerStoryGameOver() end elseif story.state == "sing" then story.song_timer = story.song_timer + 1 if story.song_timer >= (STORY_EVENT.song_frames or 90) then story.state = "explode" story.explode_timer = 0 story.zombie_visible = true self:dropStoryJournal() if self.player and self.player.setMovementLocked then self.player:setMovementLocked(false) end end elseif story.state == "explode" then story.explode_timer = story.explode_timer + 1 if story.explode_timer >= (STORY_EVENT.zombie.explode_frames or 20) then story.zombie_visible = false story.mabel_visible = false story.state = "retreat" end elseif story.state == "retreat" then if self:moveStanInside() then story.state = "done" end end end function Game:drawStoryEvent() local story = self.story_event if not story then return end if story.zombie_visible then local formation = self:getStoryZombieFormationOffsets() local zombie_sprite = STORY_EVENT.zombie.sprite local zombie_melt_px = 0 if story.state == "intro" then local rise_sprites = STORY_EVENT.zombie.rise_sprites if rise_sprites and #rise_sprites > 0 then local progress = (story.rise_timer or 0) / math.max(1, STORY_EVENT.zombie.rise_frames or 1) local frame_index = math.floor(progress * #rise_sprites) + 1 if frame_index < 1 then frame_index = 1 end if frame_index > #rise_sprites then frame_index = #rise_sprites end zombie_sprite = rise_sprites[frame_index] or zombie_sprite end elseif story.state == "explode" then local total_height = STORY_EVENT.zombie.h * TILE_SIZE local melt_progress = (story.explode_timer or 0) / math.max(1, STORY_EVENT.zombie.explode_frames or 1) zombie_melt_px = math.floor(total_height * melt_progress) if zombie_melt_px < 0 then zombie_melt_px = 0 end if zombie_melt_px > total_height then zombie_melt_px = total_height end end for index, offset in ipairs(formation) do local zombie_draw_x = math.floor(story.zombie_x + (offset.x or 0) + 0.5) local zombie_draw_y = math.floor(story.zombie_y + (offset.y or 0) + 0.5) if story.state == "intro" or story.state == "siege" or story.state == "sing" then local period = math.max(1, STORY_EVENT.zombie.siege_bob_period or 18) local bob_timer = story.siege_timer or 0 if story.state == "intro" then bob_timer = story.rise_timer or 0 elseif story.state == "sing" then bob_timer = story.song_timer or 0 end local bob_phase = math.floor((bob_timer + index * math.floor(period / 2)) / period) % 2 if bob_phase == 0 then zombie_draw_y = zombie_draw_y - (STORY_EVENT.zombie.siege_bob_px or 1) end end local draw_x = zombie_draw_x - self.camera_px local draw_y = zombie_draw_y - self.camera_py if story.state == "explode" and type(clip) == "function" then local visible_height = STORY_EVENT.zombie.h * TILE_SIZE - zombie_melt_px if visible_height > 0 then clip(draw_x, draw_y + zombie_melt_px, STORY_EVENT.zombie.w * TILE_SIZE, visible_height) spr( zombie_sprite, draw_x, draw_y, 0, 1, 0, 0, STORY_EVENT.zombie.w, STORY_EVENT.zombie.h ) clip() end else spr( zombie_sprite, draw_x, draw_y, 0, 1, 0, 0, STORY_EVENT.zombie.w, STORY_EVENT.zombie.h ) end end end if story.stan_visible then local stan_y = story.stan_y if story.state == "sing" then local bob = ((story.song_timer or 0) % (STORY_EVENT.sing_anim_period or 10)) < math.floor((STORY_EVENT.sing_anim_period or 10) / 2) if bob then stan_y = stan_y - (STORY_EVENT.sing_bob_px or 0) end end spr( STORY_EVENT.stan.sprite, math.floor(story.stan_x + 0.5) - self.camera_px, math.floor(stan_y + 0.5) - self.camera_py, 0, 1, 0, 0, STORY_EVENT.stan.w, STORY_EVENT.stan.h ) end if story.mabel_visible then local frame = STORY_EVENT.mabel.frames[1] local bob = 0 if story.state == "sing" then local anim_period = STORY_EVENT.sing_anim_period or 10 local anim_index = math.floor((story.song_timer or 0) / math.max(1, math.floor(anim_period / 2))) % #STORY_EVENT.mabel.frames frame = STORY_EVENT.mabel.frames[anim_index + 1] or frame if ((story.song_timer or 0) % anim_period) < math.floor(anim_period / 2) then bob = STORY_EVENT.sing_bob_px or 0 end end spr( frame, math.floor(story.mabel_x + 0.5) - self.camera_px, math.floor(story.mabel_y + 0.5) - self.camera_py - bob, 0, 1, 0, 0, STORY_EVENT.mabel.w, STORY_EVENT.mabel.h ) end end function Game:drawStoryDialogue() local story = self.story_event if not story or story.state ~= "intro" then return end local pages = STORY_EVENT.dialogue_pages or {} local page = pages[story.dialogue_page or 1] if not page then return end local box_w = SCREEN_W - 20 local box_h = 16 local box_x = 10 local box_y = SCREEN_H - 24 rect(box_x, box_y, box_w, box_h, 0) rectb(box_x, box_y, box_w, box_h, 15) for i, line in ipairs(page) do print(line, box_x + 5, box_y + 4 + (i - 1) * 7, 15) end print("A/SPACE", box_x + box_w - 46, box_y + box_h - 7, 12) end function Game:spawnGnomes() self.gnomes = {} local center_x, center_y = tile_center_px(GNOME_HOME_TILE_X, GNOME_HOME_TILE_Y) local min_radius = TILE_SIZE local max_radius = math.max(min_radius, GNOME_SCATTER_RADIUS_TILES * TILE_SIZE) for _ = 1, GNOME_GROUP_COUNT do local angle = math.random() * math.pi * 2 local radius = min_radius + math.random() * (max_radius - min_radius) local gx = center_x + math.cos(angle) * radius - GNOME_HALF_W local gy = center_y + math.sin(angle) * radius - GNOME_HALF_H self.gnomes[#self.gnomes + 1] = Gnome.new({ x = gx, y = gy, idle_speed = GNOME_IDLE_SPEED, chase_speed = GNOME_CHASE_SPEED }) end end function Game:calmGnomes() if not self.gnomes then return end for _, gnome in ipairs(self.gnomes) do if gnome.calm then gnome:calm() else gnome.alerted = false end end end function Game:advancePlayerAppearance() local total = (PLAYER_RESPAWN_APPEARANCES and #PLAYER_RESPAWN_APPEARANCES) or 0 if total <= 0 then return end local current = self.player_appearance_index or 1 if current < 1 then current = 1 end current = ((current - 1 + 1) % total) + 1 self.player_appearance_index = current end function Game.new() local self = setmetatable({}, Game) self:loadHighScores() self:resetWorld() return self end function Game:cloneInitials(chars) local copy = {} for i = 1, 3 do copy[i] = (chars and chars[i]) or HIGH_SCORE_SYSTEM.initials[i] end return copy end function Game:encodeInitials(chars) local code = 0 for i = 1, 3 do local ch = (chars and chars[i]) or "A" local byte = string.byte(ch) or string.byte("A") local value = byte - string.byte("A") if value < 0 then value = 0 end if value > 25 then value = 25 end code = code * 26 + value end return code end function Game:decodeInitials(code) local chars = {"A", "A", "A"} code = math.max(0, math.floor(code or 0)) for i = 3, 1, -1 do local value = code % 26 chars[i] = string.char(string.byte("A") + value) code = math.floor(code / 26) end return chars end function Game:initialsToString(chars) return table.concat(self:cloneInitials(chars), "") end function Game:loadHighScores() self.high_scores = {} for i = 1, HIGH_SCORE_SYSTEM.limit do local score_slot = HIGH_SCORE_SYSTEM.pmem_base + (i - 1) * 2 local initials_slot = score_slot + 1 local score = 0 local initials = self:cloneInitials(HIGH_SCORE_SYSTEM.initials) if type(pmem) == "function" then score = pmem(score_slot) or 0 initials = self:decodeInitials(pmem(initials_slot) or 0) end self.high_scores[i] = { score = math.max(0, math.floor(score or 0)), initials = initials } end end function Game:saveHighScores() if type(pmem) ~= "function" then return end for i = 1, HIGH_SCORE_SYSTEM.limit do local entry = self.high_scores[i] or {score = 0, initials = HIGH_SCORE_SYSTEM.initials} local score_slot = HIGH_SCORE_SYSTEM.pmem_base + (i - 1) * 2 local initials_slot = score_slot + 1 pmem(score_slot, math.max(0, math.floor(entry.score or 0))) pmem(initials_slot, self:encodeInitials(entry.initials)) end end function Game:getElapsedSeconds() return math.floor((self.run_frames or 0) / 60) end function Game:buildResultsData(result_kind) local seconds = self:getElapsedSeconds() local journal_bonus = (self.collected_journals or 0) * (SCORE_SYSTEM.journal_points or 0) local life_bonus = math.max(0, self.lives or 0) * (SCORE_SYSTEM.life_bonus or 0) if result_kind ~= "win" then life_bonus = 0 end local time_bonus = 0 if result_kind == "win" then time_bonus = math.max(0, (SCORE_SYSTEM.time_bonus_max or 0) - seconds * (SCORE_SYSTEM.time_bonus_per_second or 0)) end local final_score = math.max(0, (self.score or 0) + journal_bonus + life_bonus + time_bonus) return { result = result_kind, base_score = self.score or 0, journal_bonus = journal_bonus, life_bonus = life_bonus, time_bonus = time_bonus, time_seconds = seconds, final_score = final_score, initials = self:cloneInitials(HIGH_SCORE_SYSTEM.initials), initials_index = 1, qualifies = false, saved = false } end function Game:getHighScoreInsertIndex(score) local scores = self.high_scores or {} for i = 1, HIGH_SCORE_SYSTEM.limit do local entry = scores[i] if not entry or score > (entry.score or 0) then return i end end if #scores < HIGH_SCORE_SYSTEM.limit then return #scores + 1 end return nil end function Game:getDisplayedHighScores() local scores = {} for i = 1, HIGH_SCORE_SYSTEM.limit do local entry = self.high_scores and self.high_scores[i] if entry then scores[#scores + 1] = { score = entry.score or 0, initials = self:cloneInitials(entry.initials) } end end local results = self.results_data if results and results.qualifies and not results.saved then local insert_at = self:getHighScoreInsertIndex(results.final_score) if insert_at then table.insert(scores, insert_at, { score = results.final_score, initials = self:cloneInitials(results.initials), pending = true }) while #scores > HIGH_SCORE_SYSTEM.limit do table.remove(scores) end end end return scores end function Game:commitResultHighScore() local results = self.results_data if not results or results.saved then return end if results.qualifies then local insert_at = self:getHighScoreInsertIndex(results.final_score) if insert_at then table.insert(self.high_scores, insert_at, { score = results.final_score, initials = self:cloneInitials(results.initials) }) while #self.high_scores > HIGH_SCORE_SYSTEM.limit do table.remove(self.high_scores) end self:saveHighScores() end end results.saved = true end function Game:beginResults(result_kind) self.results_data = self:buildResultsData(result_kind) self.results_data.qualifies = self:getHighScoreInsertIndex(self.results_data.final_score) ~= nil self.show_results = true self.show_game_over = false self.show_victory = false self.show_high_scores = false self.waiting_for_input_release = true end function Game:isMazeEnemyPaused() local story = self.story_event if story and story.state and story.state ~= "idle" and story.state ~= "done" then return true end return self:isBillBossActive() end -- Keep the camera locked to 30x17 “chunksâ€�: whenever the player crosses a -- screen boundary we snap to the next chunk, clamped to the map bounds. function Game:updateCamera(force) local chunk_x = math.floor(self.player.x / SCREEN_W) local chunk_y = math.floor(self.player.y / SCREEN_H) local max_chunk_x = math.max(0, MAP_CHUNKS_W - 1) local max_chunk_y = math.max(0, MAP_CHUNKS_H - 1) if chunk_x < 0 then chunk_x = 0 elseif chunk_x > max_chunk_x then chunk_x = max_chunk_x end if chunk_y < 0 then chunk_y = 0 elseif chunk_y > max_chunk_y then chunk_y = max_chunk_y end if force or chunk_x ~= self.camera_chunk_x or chunk_y ~= self.camera_chunk_y then self.camera_chunk_x = chunk_x self.camera_chunk_y = chunk_y self.camera_px = self.camera_chunk_x * SCREEN_W self.camera_py = self.camera_chunk_y * SCREEN_H self.camera_tile_x = self.camera_chunk_x * SCREEN_TILES_W self.camera_tile_y = self.camera_chunk_y * SCREEN_TILES_H end end function Game:getPlayerTile() if not self.player then return nil end local foot_px = self.player.x + PLAYER_FOOT_OFFSET_X local foot_py = self.player.y + PLAYER_FOOT_OFFSET_Y local tile_x, tile_y = pixel_to_tile(foot_px, foot_py) if tile_x == nil or tile_y == nil then return nil end return {tx = tile_x, ty = tile_y} end function Game:checkBottomlessPit() if not self.player then return end if BILL_BOSS.arena_active then return end self.hole_teleport_cooldown = math.max(0, (self.hole_teleport_cooldown or 0) - 1) if self.player:isHidden() or self.player:isPlayingSpecialAnimation() then return end local player_tile = self:getPlayerTile() if not player_tile then return end local lock_index = self.hole_exit_lock_index if lock_index then local lock_portal = PIT_PORTALS[lock_index] local margin = BOTTOMLESS_PIT.release_margin_tiles or 0 if lock_portal and tile_in_rect_margin(player_tile.tx, player_tile.ty, lock_portal.entry, margin) then return end self.hole_exit_lock_index = nil BOTTOMLESS_PIT.lock_rect = nil end if self.hole_teleport_cooldown > 0 then return end if not is_hole_tile(player_tile.tx, player_tile.ty) then return end for idx, portal in ipairs(PIT_PORTALS) do if tile_in_rect(player_tile.tx, player_tile.ty, portal.entry) then local foot_px, foot_py = tile_center_px(portal.exit.x, portal.exit.y) self.player.x = foot_px - PLAYER_FOOT_OFFSET_X self.player.y = foot_py - PLAYER_FOOT_OFFSET_Y self.hole_teleport_cooldown = BOTTOMLESS_PIT.cooldown_frames self.hole_exit_lock_index = portal.lock_index or (((idx) % #PIT_PORTALS) + 1) local lock_portal = PIT_PORTALS[self.hole_exit_lock_index] BOTTOMLESS_PIT.lock_rect = lock_portal and lock_portal.entry or nil self:updateCamera(true) return end end end function Game:collectMarkerUnderPlayer() local foot_px = self.player.x + PLAYER_FOOT_OFFSET_X local foot_py = self.player.y + PLAYER_FOOT_OFFSET_Y local tile_x, tile_y = pixel_to_tile(foot_px, foot_py) if tile_x == nil or tile_y == nil then return end local tile_id = mget(tile_x, tile_y) if tile_id and fget(tile_id, PATH_MARKER_FLAG_INDEX) then local key = marker_tile_key(tile_x, tile_y) if not self.collected_markers[key] then self.collected_markers[key] = true self.score = self.score + PATH_MARKER_POINTS end end end function Game:drawHUD() local life_x = 2 local life_y = 2 for i = 1, math.max(0, self.lives) do local icon_x = life_x + (i - 1) * HUD.life_icon_spacing spr(HUD.life_sprite_id, icon_x, life_y, 0, 1, 0, 0, 1, 1) end local journal_base_x = life_x + math.max(0, self.lives) * HUD.life_icon_spacing + 6 for i = 1, (self.collected_journals or 0) do local icon_x = journal_base_x + (i - 1) * JOURNAL_UI_SPACING spr(JOURNAL_UI_SPRITE_ID, icon_x, life_y, 0, 1, 0, 0, 1, 1) end local score_padding = 4 local score_text = string.format("SCORE %04d", self.score) local text_width = #score_text * 6 -- TIC-80 default font width local score_x = SCREEN_W - text_width - score_padding if score_x < 0 then score_x = 0 end if self.bill_boss and self.bill_boss.phase and (self.bill_boss.phase == "arena_spawn" or self.bill_boss.phase == "fight" or self.bill_boss.phase == "death") then local bar_x = journal_base_x + math.max(0, self.collected_journals or 0) * JOURNAL_UI_SPACING + 8 local bar_y = 3 local bar_w = math.max(24, score_x - bar_x - 8) local bar_h = 5 rect(bar_x, bar_y, bar_w, bar_h, 1) rectb(bar_x, bar_y, bar_w, bar_h, 15) local health_ratio = 0 if self.bill_boss.bill and BILL_BOSS.health and BILL_BOSS.health > 0 then health_ratio = math.max(0, self.bill_boss.bill.health or 0) / BILL_BOSS.health end local fill_w = math.max(0, math.floor((bar_w - 2) * health_ratio)) if fill_w > 0 then rect(bar_x + 1, bar_y + 1, fill_w, bar_h - 2, 2) end elseif self.story_event and self.story_event.state and self.story_event.state ~= "idle" and self.story_event.state ~= "done" then local bar_x = journal_base_x + math.max(0, self.collected_journals or 0) * JOURNAL_UI_SPACING + 8 local bar_y = 3 local segments = math.max(1, STORY_EVENT.shack.health or 1) local gap = 1 local total_w = math.max(24, score_x - bar_x - 8) local segment_w = math.max(4, math.floor((total_w - (segments - 1) * gap) / segments)) local bar_w = segments * segment_w + (segments - 1) * gap local bar_h = 5 rect(bar_x, bar_y, bar_w + 2, bar_h + 2, 1) rectb(bar_x, bar_y, bar_w + 2, bar_h + 2, 15) for i = 1, segments do local seg_x = bar_x + 1 + (i - 1) * (segment_w + gap) local color = 1 if i <= math.max(0, self.story_event.shack_health or 0) then color = 8 end rect(seg_x, bar_y + 1, segment_w, bar_h, color) end end print(score_text, score_x, 2, HUD.score_text_color) self:drawInventoryUI() end function Game:drawInventoryUI() if not INVENTORY_UI or not INVENTORY_UI.slots then return end for index, slot in ipairs(INVENTORY_UI.slots) do local x = INVENTORY_UI.x + (index - 1) * (INVENTORY_UI.slot_size + INVENTORY_UI.slot_gap) local y = INVENTORY_UI.y rect(x - 1, y - 1, INVENTORY_UI.slot_size + 2, INVENTORY_UI.slot_size + 2, 0) if self.equipped_item == slot.item_id and self:hasInventoryItem(slot.item_id) then rectb(x - 1, y - 1, INVENTORY_UI.slot_size + 2, INVENTORY_UI.slot_size + 2, 15) else rectb(x - 1, y - 1, INVENTORY_UI.slot_size + 2, INVENTORY_UI.slot_size + 2, 5) end if self:hasInventoryItem(slot.item_id) then spr(slot.sprite, x, y, 0, 1, 0, 0, 1, 1) end end end function Game:checkEnemyCollision() if DEV.mode and DEV.disable_hostile_collisions then return end if not self.enemy then return end if self:isMazeEnemyPaused() then return end if self.player:isHidden() or self.player:isPlayingSpecialAnimation() then return end local px, py, pw, ph = self.player:getMazeEnemyCollisionBounds() local ex, ey, ew, eh = self.enemy:getMazeCollisionBounds() if rects_overlap(px, py, pw, ph, ex, ey, ew, eh) then self.enemy:onPlayerCollision() play_sfx_on_channel(AUDIO.player_death.id, AUDIO.player_death.channel) if self:loseLife("soos") then return end self.player:startSpecialAnimation( PORTAL_PLAYER_ANIM_FRAMES, PORTAL_PLAYER_ANIM_DELAY, {mode = "disappear", start_hidden = false} ) end end function Game:checkGnomeCollision() if DEV.mode and DEV.disable_hostile_collisions then return end if not self.gnomes or not self.player then return end if self.player:isHidden() or self.player:isPlayingSpecialAnimation() then return end local px, py, pw, ph = self.player:getBounds() for _, gnome in ipairs(self.gnomes) do if not gnome.removed and not gnome.blown then local gx, gy, gw, gh = gnome:getBounds() if rects_overlap(px, py, pw, ph, gx, gy, gw, gh) then play_sfx_on_channel(AUDIO.player_death.id, AUDIO.player_death.channel) self:loseLife() self.player:startSpecialAnimation( PORTAL_PLAYER_ANIM_FRAMES, PORTAL_PLAYER_ANIM_DELAY, {mode = "disappear", start_hidden = false} ) return end end end end function Game:drawEnemy() if DEV.mode and DEV.disable_enemy then return end if not self.enemy then return end if self:isMazeEnemyPaused() then return end self.enemy:draw(self.camera_px, self.camera_py) end function Game:isEnemyInCamera() if not self.enemy then return false end local enemy_cx = self.enemy.x + ENEMY_HALF_W local enemy_cy = self.enemy.y + ENEMY_HALF_H local enemy_chunk_x = math.floor(enemy_cx / SCREEN_W) local enemy_chunk_y = math.floor(enemy_cy / SCREEN_H) return enemy_chunk_x == self.camera_chunk_x and enemy_chunk_y == self.camera_chunk_y end function Game:updateEnemyAudio() if (self.pickup_audio_timer or 0) > 0 then if self.enemy then self.enemy.sfx_timer = 0 end return end if not self.enemy then return end if self:isMazeEnemyPaused() then self.enemy.sfx_timer = 0 return end if not self:isEnemyInCamera() then self.enemy.sfx_timer = 0 return end if self.enemy.mode == "chase" and self.enemy.moved_this_frame then self.enemy.sfx_timer = (self.enemy.sfx_timer or 0) + 1 if self.enemy.sfx_timer >= AUDIO.enemy.period then self.enemy.sfx_timer = 0 play_sfx_on_channel(AUDIO.enemy.id, AUDIO.enemy.channel) end else self.enemy.sfx_timer = 0 end end function Game:shouldSuppressFootsteps() if (self.pickup_audio_timer or 0) > 0 then return true end if not self.enemy then return false end if self:isMazeEnemyPaused() then return false end if not self:isEnemyInCamera() then return false end return self.enemy.mode == "chase" end function Game:playPickupSfx() self.pickup_audio_timer = AUDIO.pickup.hold_frames or 24 play_sfx_on_channel(AUDIO.pickup.id, AUDIO.pickup.channel) end function Game:updateGnomes() if not self.gnomes then return end for _, gnome in ipairs(self.gnomes) do gnome:update(self.player) end end function Game:drawGnomes() if not self.gnomes then return end for _, gnome in ipairs(self.gnomes) do gnome:draw(self.camera_px, self.camera_py) end end function Game:getPowerupOccupancy(skip_powerup) local occupied = {} if not self.powerups then return occupied end for _, powerup in ipairs(self.powerups) do if powerup ~= skip_powerup and powerup:isActive() then local key = marker_tile_key(powerup.tile_x, powerup.tile_y) occupied[key] = true end end return occupied end function Game:updatePowerups() if not self.powerups then return end local player_tile = self:getPlayerTile() for _, powerup in ipairs(self.powerups) do local occupied = self:getPowerupOccupancy(powerup) powerup:update(self.walkable_tiles, occupied, player_tile) end end function Game:drawPowerups() if not self.powerups then return end for _, powerup in ipairs(self.powerups) do powerup:draw(self.camera_px, self.camera_py) end end function Game:drawBillArenaPowerup() local boss = self.bill_boss if not boss or not boss.arena_powerup or not boss.arena_powerup:isActive() then return end boss.arena_powerup:draw(self.camera_px, self.camera_py) end function Game:drawMicrophone() if not self.microphone then return end self.microphone:draw(self.camera_px, self.camera_py) end function Game:drawGrapplingHookPickup() if not self.grappling_hook or not self.grappling_hook.active then return end spr( BILL_BOSS.hook.sprite, self.grappling_hook.x - self.camera_px, self.grappling_hook.y - self.camera_py, 0, 1, 0, 0, BILL_BOSS.hook.w, BILL_BOSS.hook.h ) end function Game:drawBillBoss() local boss = self.bill_boss if not boss or boss.phase == "inactive" then return end if boss.bill.visible and boss.phase ~= "ending" then local bill = boss.bill local sprite = BILL_BOSS.sprites.neutral local flip = 0 if bill.dir == "left" then sprite = BILL_BOSS.sprites.side flip = 1 elseif bill.dir == "right" then sprite = BILL_BOSS.sprites.side elseif bill.dir == "up" then sprite = BILL_BOSS.sprites.up elseif bill.dir == "down" then sprite = BILL_BOSS.sprites.down end local visible = true if bill.invulnerable_timer and bill.invulnerable_timer > 0 and (bill.invulnerable_timer % 2) == 0 then visible = false end local bob_y = get_float_bob_offset((self.run_frames or 0) + (boss.timer or 0), BILL_BOSS.bob_period or 96, 2) if visible then spr( sprite, math.floor(bill.x + 0.5) - self.camera_px, math.floor(bill.y + bob_y + 0.5) - self.camera_py, 0, 1, flip, 0, 2, 2 ) end if (boss.shockwave_timer or 0) > 0 and type(circb) == "function" then local shockwave = BILL_BOSS.shockwave or {} local total_frames = math.max(1, shockwave.frames or 18) local progress = 1 - ((boss.shockwave_timer or 0) / total_frames) local radius = math.max(2, math.floor((shockwave.max_radius_px or 26) * progress + 0.5)) local cx = math.floor(bill.x + TILE_SIZE - self.camera_px + 0.5) local cy = math.floor(bill.y + bob_y + TILE_SIZE - self.camera_py + 0.5) circb(cx, cy, radius, shockwave.color or 12) if radius > 2 then circb(cx, cy, radius - 1, shockwave.color or 12) end end if bill.zap then local x1 = math.floor(bill.x + (BILL_ZAP_VISUAL.eye_offset_x or 8) - self.camera_px + 0.5) local y1 = math.floor(bill.y + bob_y + (BILL_ZAP_VISUAL.eye_offset_y or 7) - self.camera_py + 0.5) local x2 = math.floor(bill.zap.x - self.camera_px + 0.5) local y2 = math.floor(bill.zap.y - self.camera_py + 0.5) local color = BILL_ZAP_VISUAL.color or 2 % local thickness = math.max(1, BILL_ZAP_VISUAL.thickness or 1) if type(line) == "function" then line(x1, y1, x2, y2, color) local dx = x2 - x1 local dy = y2 - y1 if math.abs(dx) >= math.abs(dy) then for offset = 1, thickness do line(x1, y1 - offset, x2, y2 - offset, color) line(x1, y1 + offset, x2, y2 + offset, color) end else for offset = 1, thickness do line(x1 - offset, y1, x2 - offset, y2, color) line(x1 + offset, y1, x2 + offset, y2, color) end end end if type(circ) == "function" then circ(x2, y2, 1, 15) end end end if boss.phase == "ending" then if boss.ending_form == "bill" and boss.bill and boss.bill.visible then local bill_sprite = BILL_BOSS.sprites.neutral local bill_flip = 0 if boss.bill.dir == "left" then bill_sprite = BILL_BOSS.sprites.side bill_flip = 1 elseif boss.bill.dir == "right" then bill_sprite = BILL_BOSS.sprites.side elseif boss.bill.dir == "up" then bill_sprite = BILL_BOSS.sprites.up elseif boss.bill.dir == "down" then bill_sprite = BILL_BOSS.sprites.down end if ((boss.timer or 0) % 6) < 3 then spr( bill_sprite, math.floor(boss.bill.x + 0.5) - self.camera_px, math.floor(boss.bill.y + get_float_bob_offset((self.run_frames or 0) + (boss.timer or 0), BILL_BOSS.bob_period or 96, 2) + 0.5) - self.camera_py, 0, 1, bill_flip, 0, 2, 2 ) end else local soos_sprite = boss.ending_form == "pac" and BILL_BOSS.ending.pac_sprite or BILL_BOSS.ending.normal_sprite spr(soos_sprite, boss.soos_x - self.camera_px, boss.soos_y - self.camera_py, 0, 1, 0, 0, 2, 2) if (boss.dialogue_index or 0) >= 4 then spr(BILL_BOSS.ending.mabel_sprite, boss.mabel_x - self.camera_px, boss.mabel_y - self.camera_py, 0, 1, 0, 0, 2, 2) end end elseif boss.phase == "soos" or boss.phase == "outro" then spr( BILL_BOSS.soos.sprite, boss.soos_x - self.camera_px, boss.soos_y - self.camera_py, 0, 1, 0, 0, BILL_BOSS.soos.w, BILL_BOSS.soos.h ) end end function Game:drawBillBossOverlay() local boss = self.bill_boss if not boss or boss.phase == "inactive" then return end local flash = false if boss.phase == "intro" and boss.flash_timer and boss.flash_timer > 0 then flash = true if boss.bill and boss.bill.visible and type(line) == "function" then local bill_cx = math.floor(boss.bill.x + TILE_SIZE - self.camera_px) local bill_cy = math.floor(boss.bill.y + TILE_SIZE - self.camera_py) local player_cx = math.floor(self.player.x + PLAYER_FOOT_OFFSET_X - self.camera_px) local player_cy = math.floor(self.player.y + PLAYER_FOOT_OFFSET_Y - self.camera_py) line(bill_cx, bill_cy, player_cx, player_cy, 12) end elseif boss.flash_timer and boss.flash_timer > 0 then flash = true end if boss.phase == "fight" and boss.bill and boss.bill.attack_state == "aim" and type(line) == "function" then local bill_cx = math.floor(boss.bill.x + TILE_SIZE - self.camera_px) local bill_cy = math.floor(boss.bill.y + TILE_SIZE - self.camera_py) local player_cx = math.floor(self.player.x + PLAYER_FOOT_OFFSET_X - self.camera_px) local player_cy = math.floor(self.player.y + PLAYER_FOOT_OFFSET_Y - self.camera_py) line(bill_cx, bill_cy, player_cx, player_cy, 14) end if flash and type(rect) == "function" then rect(0, 0, SCREEN_W, SCREEN_H, 15) end if boss.phase == "pre_intro" then local entry = BILL_BOSS.pre_intro_dialogue and BILL_BOSS.pre_intro_dialogue[boss.dialogue_index or 1] if entry then local box_x = 10 local box_y = SCREEN_H - (CUTSCENE.dialogue_box_h or 22) - 6 local box_w = SCREEN_W - 20 local box_h = CUTSCENE.dialogue_box_h or 22 local display_lines = {} local raw_lines = entry.lines or {} if raw_lines[1] then local first_line = string.format("%s: %s", entry.speaker or "PHIL", raw_lines[1]) local wrapped_first = wrap_intro_dialogue_line(first_line, 34) for _, visual_line in ipairs(wrapped_first) do display_lines[#display_lines + 1] = visual_line end for i = 2, #raw_lines do local wrapped = wrap_intro_dialogue_line(raw_lines[i], 34) for _, visual_line in ipairs(wrapped) do display_lines[#display_lines + 1] = visual_line end end end rect(box_x, box_y, box_w, box_h, 0) rectb(box_x, box_y, box_w, box_h, 15) for i = 1, math.min(CUTSCENE.dialogue_max_lines or 3, #display_lines) do if display_lines[i] and display_lines[i] ~= "" then print(display_lines[i], box_x + 5, box_y + 4 + (i - 1) * 7, 15) end end print("A/SPACE", box_x + box_w - 46, box_y + box_h - 7, 12) end elseif boss.phase == "ending" then local entry = BILL_BOSS.ending.dialogue and BILL_BOSS.ending.dialogue[boss.dialogue_index or 1] if entry then local box_x = 10 local box_y = SCREEN_H - (CUTSCENE.dialogue_box_h or 22) - 6 local box_w = SCREEN_W - 20 local box_h = CUTSCENE.dialogue_box_h or 22 local display_lines = {} local raw_lines = entry.lines or {} if raw_lines[1] then local first_line = string.format("%s: %s", entry.speaker or "PIPPER", raw_lines[1]) local wrapped_first = wrap_intro_dialogue_line(first_line, 34) for _, visual_line in ipairs(wrapped_first) do display_lines[#display_lines + 1] = visual_line end for i = 2, #raw_lines do local wrapped = wrap_intro_dialogue_line(raw_lines[i], 34) for _, visual_line in ipairs(wrapped) do display_lines[#display_lines + 1] = visual_line end end end rect(box_x, box_y, box_w, box_h, 0) rectb(box_x, box_y, box_w, box_h, 15) for i = 1, math.min(CUTSCENE.dialogue_max_lines or 3, #display_lines) do if display_lines[i] and display_lines[i] ~= "" then print(display_lines[i], box_x + 5, box_y + 4 + (i - 1) * 7, 15) end end print("A/SPACE", box_x + box_w - 46, box_y + box_h - 7, 12) end elseif boss.phase == "soos" then local box_x = 32 local box_y = SCREEN_H - 30 local box_w = SCREEN_W - 64 local box_h = 22 rect(box_x, box_y, box_w, box_h, 0) rectb(box_x, box_y, box_w, box_h, 15) print(BILL_BOSS.soos.text, box_x + 8, box_y + 8, 15) print("A / SPACE", box_x + 8, box_y + 14, 12) end end function Game:drawLeafBlower() if not self.leaf_blower then return end self.leaf_blower:draw(self.camera_px, self.camera_py) end function Game:drawRayGun() if not self.ray_gun then return end self.ray_gun:draw(self.camera_px, self.camera_py) end function Game:updateHoodedFigure() if not self.hooded_figure then return end self.hooded_figure:update(self.player, self.camera_px, self.camera_py) end function Game:drawHoodedFigure() if not self.hooded_figure then return end self.hooded_figure:draw(self.camera_px, self.camera_py) end function Game:drawJournals() if not self.journals then return end for _, journal in ipairs(self.journals) do if not journal.collected then spr( journal.sprite, journal.tile_x * TILE_SIZE - self.camera_px, journal.tile_y * TILE_SIZE - self.camera_py, 0, 1, 0, 0, JOURNAL_SPRITE_W, JOURNAL_SPRITE_H ) end end end function Game:checkPowerupCollection() if not self.powerups or not self.player then return end if self.player:isHidden() or self.player:isPlayingSpecialAnimation() then return end local px, py, pw, ph = self.player:getBounds() for _, powerup in ipairs(self.powerups) do if powerup:isActive() then local ox, oy, ow, oh = powerup:getBounds() if rects_overlap(px, py, pw, ph, ox, oy, ow, oh) then self:onPowerupCollected(powerup) end end end end function Game:checkBillArenaPowerupCollection() local boss = self.bill_boss if not boss or not boss.arena_powerup or not boss.arena_powerup:isActive() or not self.player then return end if self.player:isHidden() or self.player:isPlayingSpecialAnimation() then return end local px, py, pw, ph = self.player:getBounds() local ox, oy, ow, oh = boss.arena_powerup:getBounds() if rects_overlap(px, py, pw, ph, ox, oy, ow, oh) then self:onPowerupCollected(boss.arena_powerup) boss.arena_powerup.active = false boss.arena_powerup.respawn_timer = 0 end end function Game:checkJournalCollection() if not self.journals or not self.player then return end if self.player:isHidden() or self.player:isPlayingSpecialAnimation() then return end local foot_px = self.player.x + PLAYER_FOOT_OFFSET_X local foot_py = self.player.y + PLAYER_FOOT_OFFSET_Y local tile_x, tile_y = pixel_to_tile(foot_px, foot_py) if tile_x == nil or tile_y == nil then return end for _, journal in ipairs(self.journals) do if not journal.collected and journal.tile_x == tile_x and journal.tile_y == tile_y then self:onJournalCollected(journal) end end end function Game:checkLeafBlowerCollection() if not self.leaf_blower or self.leaf_blower:isCollected() then return end if not self.player or self.player:isHidden() or self.player:isPlayingSpecialAnimation() then return end local px, py, pw, ph = self.player:getBounds() local bx, by, bw, bh = self.leaf_blower:getBounds() if rects_overlap(px, py, pw, ph, bx, by, bw, bh) then self.leaf_blower:collect() self.has_leaf_blower = true self:selectInventoryItem(ITEMS.leaf_blower) self:clearLightBeam() self:playPickupSfx() end end function Game:checkMicrophoneCollection() if not self.microphone or self.microphone:isCollected() then return end if not self.player or self.player:isHidden() or self.player:isPlayingSpecialAnimation() then return end local px, py, pw, ph = self.player:getBounds() local mx, my, mw, mh = self.microphone:getBounds() if rects_overlap(px, py, pw, ph, mx, my, mw, mh) then self.microphone:collect() self.has_microphone = true self:selectInventoryItem(ITEMS.microphone) self:playPickupSfx() end end function Game:checkGrapplingHookCollection() if not self.grappling_hook or not self.grappling_hook.active or not self.player then return end if not self:isBillBossArenaPhase() or self.player:isHidden() or self.player:isPlayingSpecialAnimation() then return end local px, py, pw, ph = self.player:getBounds() local gx = self.grappling_hook.x local gy = self.grappling_hook.y local gw = BILL_BOSS.hook.w * TILE_SIZE local gh = BILL_BOSS.hook.h * TILE_SIZE if rects_overlap(px, py, pw, ph, gx, gy, gw, gh) then self.grappling_hook.active = false self.has_grappling_hook = true self:selectInventoryItem(ITEMS.grappling_hook) self:playPickupSfx() end end function Game:checkRayGunCollection() if not self.ray_gun or self.ray_gun:isCollected() then return end if not self.player or self.player:isHidden() or self.player:isPlayingSpecialAnimation() then return end local px, py, pw, ph = self.player:getBounds() local rx, ry, rw, rh = self.ray_gun:getBounds() if rects_overlap(px, py, pw, ph, rx, ry, rw, rh) then self.ray_gun:collect() self.has_ray_gun = true self:selectInventoryItem(ITEMS.ray_gun) self:playPickupSfx() end end function Game:checkHoodedFigureHit() if not self.hooded_figure or self.hooded_figure:isDefeated() then return end if not self.light_beam or not self.light_beam.segments then return end local hx, hy, hw, hh = self.hooded_figure:getBounds() for _, segment in ipairs(self.light_beam.segments) do local sx = segment.x1 local sy = segment.y1 local sw = math.max(1, segment.x2 - segment.x1 + 1) local sh = math.max(1, segment.y2 - segment.y1 + 1) if rects_overlap(hx, hy, hw, hh, sx, sy, sw, sh) then local dropped_journal = self.hooded_figure:onShot(self.player) if dropped_journal then self.journals[#self.journals + 1] = dropped_journal end return end end end function Game:onJournalCollected(journal) if journal.collected then return end journal.collected = true if journal.story_trigger then self:playPickupSfx() self:startStoryEvent() return end self.collected_journals = (self.collected_journals or 0) + 1 self:playPickupSfx() if self.collected_journals >= JOURNAL_COUNT then self:onAllJournalsCollected() end end function Game:onAllJournalsCollected() self:startBillIntro() end function Game:onPowerupCollected(powerup) if not powerup then return end powerup:deactivate() self.active_powerup_timer = POWERUP_DURATION_FRAMES self.powerup_overlay_timer = 0 self.player:applySpeedBoost(POWERUP_SPEED_MULTIPLIER) end function Game:updatePowerupEffects() if self.active_powerup_timer and self.active_powerup_timer > 0 then self.active_powerup_timer = self.active_powerup_timer - 1 self.powerup_overlay_timer = (self.powerup_overlay_timer + 1) % (POWERUP_OVERLAY_PERIOD * #POWERUP_OVERLAY_COLORS) if self.active_powerup_timer <= 0 then self.active_powerup_timer = 0 self.player:clearSpeedBoost() end end end function Game:drawPowerupOverlay() -- Flashing overlay disabled. return end function Game:updatePlayerTrail() self.player_trail = self.player_trail or {} for i = #self.player_trail, 1, -1 do local node = self.player_trail[i] node.life = node.life - 1 if node.life <= 0 then table.remove(self.player_trail, i) end end if self.active_powerup_timer and self.active_powerup_timer > 0 then self.player_trail_timer = (self.player_trail_timer or 0) + 1 if (self.player_trail_timer % PLAYER_TRAIL_SPAWN_INTERVAL) == 0 then local frame, flip = self.player:getCurrentSprite() local node = { x = self.player.x, y = self.player.y, frame = frame, flip = flip, life = PLAYER_TRAIL_LIFETIME } self.player_trail[#self.player_trail + 1] = node if #self.player_trail > PLAYER_TRAIL_MAX then table.remove(self.player_trail, 1) end end else self.player_trail_timer = 0 end end function Game:drawPlayerTrail() if not self.player_trail then return end for _, node in ipairs(self.player_trail) do local ratio = math.max(0, node.life / PLAYER_TRAIL_LIFETIME) local color_index = math.max(1, math.min(#PLAYER_TRAIL_COLORS, math.ceil(ratio * #PLAYER_TRAIL_COLORS))) local color = PLAYER_TRAIL_COLORS[color_index] or PLAYER_TRAIL_COLORS[#PLAYER_TRAIL_COLORS] draw_sprite_silhouette( node.frame, node.x - self.camera_px, node.y - self.camera_py, node.flip, color ) end end function Game:respawnPlayerIfNeeded() if not self.player:needsRespawn() then return end if self:isBillBossArenaPhase() then self:setPlayerFootTile(BILL_BOSS.player_spawn_foot_tile_x, BILL_BOSS.player_spawn_foot_tile_y) if self.player.setAppearanceByIndex then self.player:setAppearanceByIndex(self.player_appearance_index or 1) end self.player.hidden = true self.player:clearRespawnFlag() local frames = PORTAL_PLAYER_ANIM_FRAMES_REVERSED local delay = PORTAL_PLAYER_ANIM_DELAY self.player:startSpecialAnimation(frames, delay, {mode = "reappear", start_hidden = true}) self:updateCamera(true) return end self.player:respawnAtSpawn() if self.player.setAppearanceByIndex then self.player:setAppearanceByIndex(self.player_appearance_index or 1) end self.player.hidden = true self.player:clearRespawnFlag() local frames = PORTAL_PLAYER_ANIM_FRAMES_REVERSED local delay = PORTAL_PLAYER_ANIM_DELAY self.player:startSpecialAnimation(frames, delay, {mode = "reappear", start_hidden = true}) self:respawnEnemyAwayFromPlayer() end function Game:startSoosDeathMessage() if not SOOS_GAME_OVER_LINES or #SOOS_GAME_OVER_LINES == 0 then return end self.soos_death_message_index = ((self.soos_death_message_index or 0) % #SOOS_GAME_OVER_LINES) + 1 self.soos_death_message_line = SOOS_GAME_OVER_LINES[self.soos_death_message_index] self.soos_death_message_timer = SOOS_DEATH.message_frames end function Game:updateSoosDeathMessage() if not self.soos_death_message_timer or self.soos_death_message_timer <= 0 then return end self.soos_death_message_timer = self.soos_death_message_timer - 1 end function Game:drawSoosDeathMessage() local line = self.soos_death_message_line if not line then return end if self.show_game_over then if self.game_over_source ~= "soos" then return end elseif (self.soos_death_message_timer or 0) <= 0 then return end local box_w = SCREEN_W - 20 local box_h = 14 local box_x = 10 local box_y = SCREEN_H - 24 rect(box_x, box_y, box_w, box_h, 0) rectb(box_x, box_y, box_w, box_h, 15) print(line, box_x + 5, box_y + 4, 15) end function Game:loseLife(source) self.lives = math.max(0, (self.lives or 0) - 1) self:advancePlayerAppearance() self:calmGnomes() if source == "soos" then self:startSoosDeathMessage() end if (self.lives or 0) <= 0 then self:clearItemAction() if self.player and self.player.setMovementLocked then self.player:setMovementLocked(false) end self.game_over_source = source or "generic" self.game_over_timer = 0 self:beginResults("game_over") return true end return false end -- Front-end helpers (splash + start) --------------------------------- function Game:isAnyButtonDown() if type(key) == "function" and key(INPUT_KEYS.space) then return true end if type(btn) ~= "function" then return false end for i = 0, 7 do if btn(i) then return true end end return false end -- Edge-triggered button press helper with optional release gating. function Game:anyButtonPressed() if self.waiting_for_input_release then if self:isAnyButtonDown() then return false end self.waiting_for_input_release = false end if type(btnp) == "function" then for i = 0, 7 do if btnp(i) then return true end end end return false end function Game:setFrontendMusic(mode) if type(music) ~= "function" then return end if self.frontend_music_mode == mode then return end if mode == "title" then music(FRONTEND.music.track_id or 0, FRONTEND.music.title_frame or 0, 0, true) else music(-1) end self.frontend_music_mode = mode end function Game:startFrontendStartTransition() self.frontend_start_transition = { phase = "finish_current", timer = FRONTEND.music.title_finish_frames or 30 } end function Game:handleFrontendStartTransition() local transition = self.frontend_start_transition if not transition then return false end self:drawStartScreen() self.frontend_timer = (self.frontend_timer or 0) + 1 transition.timer = math.max(0, (transition.timer or 0) - 1) if transition.timer > 0 then return true end if transition.phase == "finish_current" then if type(music) == "function" then music(FRONTEND.music.track_id or 0, FRONTEND.music.ending_frame or 3, 0, false) end self.frontend_music_mode = "title_ending" transition.phase = "finish_ending" transition.timer = FRONTEND.music.ending_finish_frames or 30 return true end if type(music) == "function" then music(-1) end self.frontend_music_mode = nil self.frontend_start_transition = nil self:startIntroCutscene() return true end -- Centered copy block for the splash text. function Game:drawSplashScreen() local small_scale = 1 local large_scale = 2 local small_w = #FRONTEND.splash.text_small * 6 * small_scale local large_w = #FRONTEND.splash.text_large * 6 * large_scale local prompt_w = #FRONTEND.splash.prompt * 6 local small_x = math.floor((SCREEN_W - small_w) / 2) local large_x = math.floor((SCREEN_W - large_w) / 2) local prompt_x = math.floor((SCREEN_W - prompt_w) / 2) local center_y = math.floor(SCREEN_H / 2) local small_y = center_y - 18 local large_y = center_y - 6 print(FRONTEND.splash.text_small, small_x, small_y, FRONTEND.splash.small_color, false, small_scale) print(FRONTEND.splash.text_large, large_x, large_y, FRONTEND.splash.large_color, false, large_scale) print(FRONTEND.splash.prompt, prompt_x, center_y + 18, FRONTEND.splash.prompt_color or 15) end -- Returns true while we are still showing the splash screen. function Game:handleSplashScreen() if not self.show_splash then return false end self:setFrontendMusic("title") cls(FRONTEND.splash.bg_color or 0) self:drawSplashScreen() self.frontend_timer = (self.frontend_timer or 0) + 1 if self:isConfirmPressed() then self.show_splash = false self.show_start = true self.waiting_for_input_release = true end return true end function Game:resetIntroCutscene() local actors = {} for id, spec in pairs(CUTSCENE.actors or {}) do actors[id] = { id = id, x = 0, y = 0, target_x = nil, target_y = nil, speed = CUTSCENE.walk_speed_px or 1, dir = "neutral", anim_dir = "neutral", anim_index = 1, anim_timer = 0, visible = true } self:setIntroActorBottomTile(actors[id], spec.tile_x, spec.bottom_y) if id == "bill" or id == "pac_soos" or id == "stan" then actors[id].visible = false end end self.intro_cutscene = { phase = "idle", scroll_y = 0, timer = 0, step_index = 1, step_started = false, action_timer = 0, flash_timer = 0, dialogue_sfx_step_index = 0, dialogue_sfx_stop_timer = 0, actors = actors } end function Game:setIntroActorBottomTile(actor, tile_x, bottom_y) if not actor then return end actor.x = tile_x * TILE_SIZE actor.y = (bottom_y - 1) * TILE_SIZE end function Game:moveIntroActorToBottomTile(actor, tile_x, bottom_y, speed) if not actor then return end actor.target_x = tile_x * TILE_SIZE actor.target_y = (bottom_y - 1) * TILE_SIZE actor.speed = speed or CUTSCENE.walk_speed_px or 1 end function Game:isIntroActorMoving(actor) return actor and actor.target_x ~= nil and actor.target_y ~= nil end function Game:getIntroDialogueSpeaker(step) if not step or step.type ~= "say" then return nil end local lines = step.lines or {} for _, line_text in ipairs(lines) do if line_text and line_text ~= "" then local label = string.match(line_text, "^([A-Z%- ]+):") if label == "PIPPER" then return "dipper" end if label == "FABEL" then return "mabel" end if label == "ZOOS" then return "soos" end if label == "PAC-ZOOS" then return "pac_soos" end if label == "UNCLE MAN" then return "stan" end if label == "PHIL" then return "bill" end end end return nil end wrap_intro_dialogue_line = function(line_text, max_chars) local wrapped = {} if not line_text or line_text == "" then return wrapped end max_chars = math.max(8, max_chars or 34) local current = "" for word in string.gmatch(line_text, "%S+") do if current == "" then current = word elseif (#current + 1 + #word) <= max_chars then current = current .. " " .. word else wrapped[#wrapped + 1] = current current = word end end if current ~= "" then wrapped[#wrapped + 1] = current end return wrapped end function Game:getIntroDialogueChunk() local scene = self.intro_cutscene if not scene or scene.phase ~= "scene" then return nil end local script = CUTSCENE.script or {} local step = script[scene.step_index] if not step or step.type ~= "say" then return nil end local speaker = self:getIntroDialogueSpeaker(step) local max_lines = CUTSCENE.dialogue_max_lines or 3 local lines = {} local step_count = 0 local index = scene.step_index while index <= #script and #lines < max_lines do local next_step = script[index] if not next_step or next_step.type ~= "say" then break end local next_speaker = self:getIntroDialogueSpeaker(next_step) if speaker and next_speaker and next_speaker ~= speaker then break end if not speaker then speaker = next_speaker end local preview = {} for _, existing in ipairs(lines) do preview[#preview + 1] = existing end for _, line_text in ipairs(next_step.lines or {}) do local wrapped = wrap_intro_dialogue_line(line_text, 34) for _, visual_line in ipairs(wrapped) do preview[#preview + 1] = visual_line if #preview > max_lines then break end end if #preview > max_lines then break end end if #preview > max_lines and #lines > 0 then break end step_count = step_count + 1 lines = preview if #lines >= max_lines then break end index = index + 1 end return { lines = lines, speaker = speaker, step_count = math.max(1, step_count) } end get_float_bob_offset = function(timer, period, amplitude) period = math.max(1, period or 30) amplitude = amplitude or 0 if amplitude == 0 then return 0 end local angle = ((timer or 0) % period) / period * math.pi * 2 return math.floor(math.sin(angle) * amplitude + 0.5) end function Game:updateIntroActor(actor) if not actor or not actor.visible then return end local moving = false if actor.target_x ~= nil and actor.target_y ~= nil then local dx = actor.target_x - actor.x local dy = actor.target_y - actor.y local dist = math.sqrt(dx * dx + dy * dy) if dist <= (actor.speed or 1) or dist < 0.001 then actor.x = actor.target_x actor.y = actor.target_y actor.target_x = nil actor.target_y = nil else actor.x = actor.x + dx / dist * (actor.speed or 1) actor.y = actor.y + dy / dist * (actor.speed or 1) moving = true if math.abs(dx) >= math.abs(dy) then actor.dir = dx < 0 and "left" or "right" else actor.dir = dy < 0 and "up" or "down" end end end local spec = CUTSCENE.actors and CUTSCENE.actors[actor.id] if not spec then return end local talking = actor.talking == true if spec.type == "player" or spec.type == "enemy" then local frame_bank = PLAYER_ANIMATION_FRAMES local flip_bank = PLAYER_DIRECTION_FLIPS if spec.type == "player" then local appearance = PLAYER_RESPAWN_APPEARANCES[spec.appearance or 1] if appearance then frame_bank = appearance.frames or frame_bank flip_bank = appearance.flips or flip_bank end else frame_bank = ENEMY_ANIMATION_FRAMES flip_bank = ENEMY_DIRECTION_FLIPS end local draw_dir = "neutral" if spec.type == "enemy" then draw_dir = actor.dir or "right" else draw_dir = moving and (actor.dir or "right") or "neutral" end local frames = frame_bank[draw_dir] or frame_bank.neutral or frame_bank.right if actor.anim_dir ~= draw_dir then actor.anim_dir = draw_dir actor.anim_index = 1 actor.anim_timer = 0 end local should_cycle = moving or (talking and draw_dir == "neutral") if frames and #frames > 1 and should_cycle then actor.anim_timer = actor.anim_timer + 1 if actor.anim_timer >= (CUTSCENE.actor_anim_delay or PLAYER_ANIM_FRAME_DELAY) then actor.anim_timer = 0 actor.anim_index = actor.anim_index + 1 if actor.anim_index > #frames then actor.anim_index = 1 end end else actor.anim_index = 1 actor.anim_timer = 0 end actor.flip = flip_bank[draw_dir] or 0 elseif spec.type == "static_anim" or spec.type == "soos_head" then actor.anim_timer = actor.anim_timer + 1 if talking and actor.anim_timer >= (CUTSCENE.actor_anim_delay or PLAYER_ANIM_FRAME_DELAY) then actor.anim_timer = 0 actor.anim_index = actor.anim_index + 1 local frame_total = 2 if actor.anim_index > frame_total then actor.anim_index = 1 end elseif not talking then actor.anim_index = 1 actor.anim_timer = 0 end else if not moving and actor.dir == nil then actor.dir = "neutral" end end end function Game:drawIntroActor(actor, offset_y) if not actor or not actor.visible then return end local spec = CUTSCENE.actors and CUTSCENE.actors[actor.id] if not spec then return end local draw_x = math.floor(actor.x - CUTSCENE.scene.tile_x * TILE_SIZE + 0.5) local draw_y = math.floor(actor.y - CUTSCENE.scene.tile_y * TILE_SIZE + offset_y + 0.5) if spec.type == "player" then local appearance = PLAYER_RESPAWN_APPEARANCES[spec.appearance or 1] local frames = appearance and appearance.frames or PLAYER_ANIMATION_FRAMES local dir = actor.anim_dir or "neutral" local frame_list = frames[dir] or frames.neutral or PLAYER_ANIMATION_FRAMES.neutral local frame = frame_list[actor.anim_index] or frame_list[1] local flips = appearance and appearance.flips or PLAYER_DIRECTION_FLIPS local flip = flips[dir] or 0 spr(frame, draw_x, draw_y, 0, 1, flip, 0, 2, 2) elseif spec.type == "enemy" then local dir = actor.anim_dir or "neutral" local frame_list = ENEMY_ANIMATION_FRAMES[dir] or ENEMY_ANIMATION_FRAMES.neutral local frame = frame_list[actor.anim_index] or frame_list[1] local flip = ENEMY_DIRECTION_FLIPS[dir] or 0 spr(frame, draw_x, draw_y, 0, 1, flip, 0, 2, 2) elseif spec.type == "bill" then local dir = actor.dir or "neutral" local frame = BILL_BOSS.sprites.neutral local flip = 0 if dir == "left" or dir == "right" then frame = BILL_BOSS.sprites.side flip = dir == "left" and 1 or 0 elseif dir == "up" then frame = BILL_BOSS.sprites.up elseif dir == "down" then frame = BILL_BOSS.sprites.down end local intro_timer = self.intro_cutscene and self.intro_cutscene.timer or 0 local bob_y = get_float_bob_offset(intro_timer, BILL_BOSS.bob_period or 96, 2) spr(frame, draw_x, draw_y + bob_y, 0, 1, flip, 0, 2, 2) elseif spec.type == "soos_head" then local side_pose = actor.pose == "side" local frames = side_pose and spec.side_frames or spec.front_frames local frame = frames[(actor.anim_index or 1)] or frames[1] local flip = 0 if side_pose and actor.dir == "left" then flip = 1 end spr(frame, draw_x, draw_y, 0, 1, flip, 0, 2, 2) elseif spec.type == "static_anim" then local frames = spec.frames or {spec.sprite} local frame = frames[(actor.anim_index or 1)] or frames[1] local flip = actor.dir == "left" and 1 or 0 spr(frame, draw_x, draw_y, 0, 1, flip, 0, 2, 2) else local flip = actor.dir == "left" and 1 or 0 spr(spec.sprite, draw_x, draw_y, 0, 1, flip, 0, 2, 2) end end function Game:startIntroCutscene() self:resetIntroCutscene() self.show_start = false self.show_intro_cutscene = true if self.intro_cutscene then self.intro_cutscene.phase = "scroll" self.intro_cutscene.scroll_y = 0 end end function Game:advanceIntroCutsceneStep() local scene = self.intro_cutscene if not scene then return end scene.step_index = (scene.step_index or 1) + 1 scene.step_started = false scene.action_timer = 0 scene.zap_target = nil end function Game:beginIntroCutsceneAction(action) local scene = self.intro_cutscene if not scene then return end local actors = scene.actors or {} if action == "walk_left" then self:moveIntroActorToBottomTile(actors.dipper, CUTSCENE.offscreen_left_tile_x, CUTSCENE.actors.dipper.bottom_y, CUTSCENE.run_speed_px) self:moveIntroActorToBottomTile(actors.mabel, CUTSCENE.offscreen_left_tile_x - 2, CUTSCENE.actors.mabel.bottom_y, CUTSCENE.run_speed_px) self:moveIntroActorToBottomTile(actors.soos, CUTSCENE.soos_shift_tile_x, CUTSCENE.actors.soos.bottom_y, CUTSCENE.walk_speed_px) elseif action == "bill_appear" then scene.action_timer = CUTSCENE.flash_frames or 12 scene.flash_timer = scene.action_timer actors.bill.visible = false self:setIntroActorBottomTile(actors.bill, CUTSCENE.bill_tile_x, CUTSCENE.bill_bottom_y) actors.bill.dir = "right" elseif action == "transform_soos" then scene.action_timer = CUTSCENE.flash_frames or 12 scene.flash_timer = scene.action_timer scene.zap_target = "soos" actors.pac_soos.visible = false elseif action == "bill_exit" then scene.action_timer = CUTSCENE.flash_frames or 12 scene.flash_timer = scene.action_timer elseif action == "return_kids" then actors.dipper.visible = true actors.mabel.visible = true if actors.pac_soos then actors.pac_soos.pose = "side" actors.pac_soos.dir = "left" end self:setIntroActorBottomTile(actors.dipper, CUTSCENE.offscreen_left_tile_x, CUTSCENE.actors.dipper.bottom_y) self:setIntroActorBottomTile(actors.mabel, CUTSCENE.offscreen_left_tile_x - 2, CUTSCENE.actors.mabel.bottom_y) self:moveIntroActorToBottomTile(actors.dipper, CUTSCENE.actors.dipper.tile_x, CUTSCENE.actors.dipper.bottom_y, CUTSCENE.run_speed_px) self:moveIntroActorToBottomTile(actors.mabel, CUTSCENE.actors.mabel.tile_x, CUTSCENE.actors.mabel.bottom_y, CUTSCENE.run_speed_px) elseif action == "stan_enter" then actors.stan.visible = true self:setIntroActorBottomTile(actors.stan, CUTSCENE.actors.stan.tile_x, CUTSCENE.actors.stan.bottom_y) self:moveIntroActorToBottomTile(actors.stan, CUTSCENE.stan_target_tile_x, CUTSCENE.stan_target_bottom_y, CUTSCENE.walk_speed_px) elseif action == "ui_flash" then scene.action_timer = CUTSCENE.ui_flash_frames or 18 scene.flash_timer = scene.action_timer elseif action == "start_game" then self:finishIntroCutscene() end end function Game:updateIntroCutsceneAction(action) local scene = self.intro_cutscene if not scene then return false end local actors = scene.actors or {} if action == "walk_left" then if not self:isIntroActorMoving(actors.dipper) and not self:isIntroActorMoving(actors.mabel) and not self:isIntroActorMoving(actors.soos) then actors.dipper.visible = false actors.mabel.visible = false actors.soos.dir = "neutral" return true end return false elseif action == "bill_appear" then scene.action_timer = math.max(0, (scene.action_timer or 0) - 1) if scene.action_timer <= math.floor((CUTSCENE.flash_frames or 12) / 2) then actors.bill.visible = true end return scene.action_timer <= 0 elseif action == "transform_soos" then scene.action_timer = math.max(0, (scene.action_timer or 0) - 1) if scene.action_timer <= math.floor((CUTSCENE.flash_frames or 12) / 2) and not actors.pac_soos.visible then actors.soos.visible = false actors.bill.visible = false actors.pac_soos.visible = true self:setIntroActorBottomTile(actors.pac_soos, CUTSCENE.pac_soos_tile_x, CUTSCENE.pac_soos_bottom_y) actors.pac_soos.dir = "right" actors.pac_soos.pose = "front" scene.zap_target = nil scene.flash_timer = CUTSCENE.transform_flash_hold_frames or 24 end return scene.action_timer <= 0 elseif action == "bill_exit" then scene.action_timer = math.max(0, (scene.action_timer or 0) - 1) if scene.action_timer <= math.floor((CUTSCENE.flash_frames or 12) / 2) then actors.bill.visible = false end return scene.action_timer <= 0 elseif action == "return_kids" then return not self:isIntroActorMoving(actors.dipper) and not self:isIntroActorMoving(actors.mabel) elseif action == "stan_enter" then return not self:isIntroActorMoving(actors.stan) elseif action == "ui_flash" then scene.action_timer = math.max(0, (scene.action_timer or 0) - 1) return scene.action_timer <= 0 elseif action == "start_game" then return true end return false end function Game:updateIntroCutscene() local scene = self.intro_cutscene if not scene then return end scene.timer = (scene.timer or 0) + 1 if scene.dialogue_sfx_stop_timer and scene.dialogue_sfx_stop_timer > 0 then scene.dialogue_sfx_stop_timer = scene.dialogue_sfx_stop_timer - 1 if scene.dialogue_sfx_stop_timer <= 0 and type(sfx) == "function" then sfx(-1) end end if scene.flash_timer and scene.flash_timer > 0 then scene.flash_timer = scene.flash_timer - 1 end if scene.phase == "scroll" then scene.scroll_y = math.min(SCREEN_H, (scene.scroll_y or 0) + (CUTSCENE.scene.scroll_speed_px or 2)) for _, actor in pairs(scene.actors or {}) do self:updateIntroActor(actor) end if scene.scroll_y >= SCREEN_H then scene.phase = "scene" scene.step_index = 1 scene.step_started = false end return elseif scene.phase ~= "scene" then return end local step = CUTSCENE.script and CUTSCENE.script[scene.step_index] if not step then self:finishIntroCutscene() return end local chunk = self:getIntroDialogueChunk() for _, actor in pairs(scene.actors or {}) do actor.talking = false end if chunk and chunk.speaker and scene.actors and scene.actors[chunk.speaker] then scene.actors[chunk.speaker].talking = true end for _, actor in pairs(scene.actors or {}) do self:updateIntroActor(actor) end if not scene.step_started then scene.step_started = true if step.type == "action" then self:beginIntroCutsceneAction(step.action) end end if step.type == "say" then if scene.dialogue_sfx_step_index ~= (scene.step_index or 0) then play_sfx_on_channel(CUTSCENE.dialogue_sfx_id or 2) scene.dialogue_sfx_step_index = scene.step_index or 0 scene.dialogue_sfx_stop_timer = CUTSCENE.dialogue_sfx_stop_frames or 18 end if self:isConfirmPressed() then scene.step_index = (scene.step_index or 1) + ((chunk and chunk.step_count) or 1) scene.step_started = false scene.action_timer = 0 end elseif step.type == "action" then scene.dialogue_sfx_step_index = 0 scene.dialogue_sfx_stop_timer = 0 if type(sfx) == "function" then sfx(-1) end if self:updateIntroCutsceneAction(step.action) then self:advanceIntroCutsceneStep() end end end function Game:finishIntroCutscene() self.show_intro_cutscene = false self:setFrontendMusic(nil) self.objective_prompt_timer = 0 self.post_intro_page = 1 self.post_intro_lock_timer = CUTSCENE.post_intro and CUTSCENE.post_intro.hold_frames or 60 if self.intro_cutscene then self.intro_cutscene.phase = "done" end self:updateCamera(true) end function Game:updatePostIntroModal() if not self.post_intro_page then return false end if self.post_intro_lock_timer and self.post_intro_lock_timer > 0 then self.post_intro_lock_timer = self.post_intro_lock_timer - 1 return true end if self:isConfirmPressed() then self.post_intro_page = (self.post_intro_page or 1) + 1 if self.post_intro_page > #(CUTSCENE.post_intro and CUTSCENE.post_intro.pages or {}) then self.post_intro_page = nil end end return true end function Game:drawIntroTitleOverlay(offset_y) local title = CUTSCENE.title local small_scale = 1 local large_scale = 2 local game_scale = 2 local small_top_w = #title.small_top * 6 * small_scale local large_w = #title.large_title * 6 * large_scale local small_bottom_w = #title.small_bottom * 6 * small_scale local game_w = #title.game_title * 6 * game_scale print(title.small_top, math.floor((SCREEN_W - small_top_w) / 2), 18 + offset_y, title.small_color, false, small_scale) print(title.large_title, math.floor((SCREEN_W - large_w) / 2), 34 + offset_y, title.large_color, false, large_scale) print(title.small_bottom, math.floor((SCREEN_W - small_bottom_w) / 2), 54 + offset_y, title.small_color, false, small_scale) print(title.game_title, math.floor((SCREEN_W - game_w) / 2), 80 + offset_y, title.game_title_color, false, game_scale) for i, option_text in ipairs(title.options or {}) do local option_w = #option_text * 6 local option_x = math.floor((SCREEN_W - option_w) / 2) local option_y = (title.option_y or 112) + (i - 1) * (title.option_gap or 10) + offset_y local selected = (self.start_menu_index or 1) == i local blink_on = math.floor((self.frontend_timer or 0) / math.max(1, title.option_blink_frames or 15)) % 2 == 0 local color = title.prompt_color if selected then color = blink_on and 15 or 0 end print(option_text, option_x, option_y, color) end end function Game:drawIntroDialogueBox(offset_y) local scene = self.intro_cutscene if not scene or scene.phase ~= "scene" then return end local chunk = self:getIntroDialogueChunk() if not chunk then return end local box_x = 10 local box_y = SCREEN_H - (CUTSCENE.dialogue_box_h or 22) - 6 + offset_y local box_w = SCREEN_W - 20 local box_h = CUTSCENE.dialogue_box_h or 22 rect(box_x, box_y, box_w, box_h, 0) rectb(box_x, box_y, box_w, box_h, 15) local lines = chunk.lines or {} for i = 1, math.min(CUTSCENE.dialogue_max_lines or 3, #lines) do if lines[i] and lines[i] ~= "" then print(lines[i], box_x + 5, box_y + 4 + (i - 1) * 7, 15) end end print("A/SPACE", box_x + box_w - 46, box_y + box_h - 7, 12) end function Game:drawIntroSceneViewport(offset_y) map(CUTSCENE.scene.tile_x, CUTSCENE.scene.tile_y, SCREEN_TILES_W, SCREEN_TILES_H, 0, offset_y, 0) local scene = self.intro_cutscene if scene and scene.actors then self:drawIntroActor(scene.actors.dipper, offset_y) self:drawIntroActor(scene.actors.mabel, offset_y) self:drawIntroActor(scene.actors.soos, offset_y) self:drawIntroActor(scene.actors.pac_soos, offset_y) self:drawIntroActor(scene.actors.bill, offset_y) self:drawIntroActor(scene.actors.stan, offset_y) end if scene and scene.actors and scene.zap_target and type(line) == "function" then local bill_actor = scene.actors.bill local target_actor = scene.actors[scene.zap_target] if bill_actor and bill_actor.visible and target_actor and target_actor.visible then local bill_x = math.floor(bill_actor.x - CUTSCENE.scene.tile_x * TILE_SIZE + TILE_SIZE + 0.5) local bill_y = math.floor(bill_actor.y - CUTSCENE.scene.tile_y * TILE_SIZE + offset_y + TILE_SIZE + get_float_bob_offset(scene.timer or 0, BILL_BOSS.bob_period or 96, 2) + 0.5) local target_x = math.floor(target_actor.x - CUTSCENE.scene.tile_x * TILE_SIZE + TILE_SIZE + 0.5) local target_y = math.floor(target_actor.y - CUTSCENE.scene.tile_y * TILE_SIZE + offset_y + TILE_SIZE + 0.5) line(bill_x, bill_y, target_x, target_y, 12) if type(circ) == "function" then circ(target_x, target_y, 2, 15) end end end if scene and scene.flash_timer and scene.flash_timer > 0 then local flash_color = (scene.flash_timer % 4) < 2 and 15 or 12 rect(0, offset_y, SCREEN_W, SCREEN_H, flash_color) end self:drawIntroDialogueBox(offset_y) end -- Draw the main title + instruction prompt between splash and gameplay. function Game:drawStartScreen() cls(FRONTEND.start_bg_color or 0) map(CUTSCENE.title.tile_x, CUTSCENE.title.tile_y, SCREEN_TILES_W, SCREEN_TILES_H, 0, 0, 0) self:drawIntroTitleOverlay(0) end -- Returns true while the title screen is waiting for input. function Game:handleStartScreen() if not self.show_start then return false end self:setFrontendMusic("title") self:drawStartScreen() self.frontend_timer = (self.frontend_timer or 0) + 1 if self.waiting_for_input_release then if self:isAnyButtonDown() then return true end self.waiting_for_input_release = false end if type(btnp) == "function" then if btnp(0) then self.start_menu_index = math.max(1, (self.start_menu_index or 1) - 1) elseif btnp(1) then self.start_menu_index = math.min(#(CUTSCENE.title.options or {}), (self.start_menu_index or 1) + 1) end end if self:isConfirmPressed() then if (self.start_menu_index or 1) == 1 then self:startFrontendStartTransition() else self.show_start = false self.show_high_scores = true self.waiting_for_input_release = true end end return true end function Game:handleIntroCutscene() if not self.show_intro_cutscene then return false end local scene = self.intro_cutscene if not scene then return true end if type(btnp) == "function" and btnp(0) then if type(sfx) == "function" then sfx(-1) end self:finishIntroCutscene() return true end cls(0) if scene.phase == "scroll" then map(CUTSCENE.title.tile_x, CUTSCENE.title.tile_y, SCREEN_TILES_W, SCREEN_TILES_H, 0, -scene.scroll_y, 0) map(CUTSCENE.scene.tile_x, CUTSCENE.scene.tile_y, SCREEN_TILES_W, SCREEN_TILES_H, 0, SCREEN_H - scene.scroll_y, 0) self:drawIntroTitleOverlay(-scene.scroll_y) if scene.scroll_y > 0 then local viewport_y = SCREEN_H - scene.scroll_y if scene.actors then self:drawIntroActor(scene.actors.dipper, viewport_y) self:drawIntroActor(scene.actors.mabel, viewport_y) self:drawIntroActor(scene.actors.soos, viewport_y) end end else self:drawIntroSceneViewport(0) end self:updateIntroCutscene() return true end function Game:formatElapsedTime(seconds) local minutes = math.floor((seconds or 0) / 60) local secs = math.max(0, math.floor(seconds or 0) % 60) return string.format("%02d:%02d", minutes, secs) end function Game:drawHighScoreRows(scores, start_x, start_y) for i = 1, HIGH_SCORE_SYSTEM.limit do local entry = scores and scores[i] or nil local initials = "AAA" local score = 0 local color = 12 if entry then initials = self:initialsToString(entry.initials) score = entry.score or 0 if entry.pending then color = 15 end end print(string.format("%d.%s %05d", i, initials, score), start_x, start_y + (i - 1) * 8, color) end end function Game:drawHighScoresScreen() cls(0) print("HIGH SCORES", math.floor((SCREEN_W - (#"HIGH SCORES" * 6 * 2)) / 2), 14, 12, false, 2) self:drawHighScoreRows(self.high_scores, 66, 40) print("PRESS A / SPACE", math.floor((SCREEN_W - (#"PRESS A / SPACE" * 6)) / 2), 120, 14) end function Game:handleHighScoresScreen() if not self.show_high_scores then return false end self:setFrontendMusic("title") self.frontend_start_transition = nil self:drawHighScoresScreen() if self:isConfirmPressed() or self:anyButtonPressed() then self.show_high_scores = false self.show_start = true self.waiting_for_input_release = true end return true end function Game:drawResultsScreen() local results = self.results_data if not results then return end cls(0) local title = results.result == "win" and "YOU WIN!" or "GAME OVER" print(title, math.floor((SCREEN_W - (#title * 6 * 2)) / 2), 6, 12, false, 2) print(string.format("SCORE %05d", results.final_score), 12, 28, 15) print(string.format("PATH %05d", results.base_score), 12, 38, 12) print(string.format("BOOKS %05d", results.journal_bonus), 12, 46, 12) print(string.format("LIVES %05d", results.life_bonus), 12, 54, 12) print(string.format("TIME %s", self:formatElapsedTime(results.time_seconds)), 12, 62, 12) print(string.format("BONUS %05d", results.time_bonus), 12, 70, 12) print("HIGH SCORES", 140, 28, 14) self:drawHighScoreRows(self:getDisplayedHighScores(), 140, 38) if results.qualifies and not results.saved then local initials = self:cloneInitials(results.initials) print("NEW HIGH SCORE", 12, 88, 15) print(self:initialsToString(initials), 12, 98, 15, false, 2) local cursor_x = 12 + ((results.initials_index or 1) - 1) * 12 rectb(cursor_x - 1, 97, 10, 14, 15) print("U/D LETTER L/R SLOT A/SPACE SAVE", 12, 116, 12) else print("PRESS A / SPACE", 12, 116, 14) end end function Game:handleResultsScreen() if not self.show_results then return false end local results = self.results_data self:drawResultsScreen() if not results then return true end if self.waiting_for_input_release then if self:isAnyButtonDown() then return true end self.waiting_for_input_release = false end if results.qualifies and not results.saved then if type(btnp) == "function" then if btnp(2) then results.initials_index = math.max(1, (results.initials_index or 1) - 1) elseif btnp(3) then results.initials_index = math.min(3, (results.initials_index or 1) + 1) elseif btnp(0) or btnp(1) then local idx = results.initials_index or 1 local chars = self:cloneInitials(results.initials) local byte = string.byte(chars[idx] or "A") local delta = btnp(0) and -1 or 1 local value = (byte - string.byte("A") + delta) % 26 chars[idx] = string.char(string.byte("A") + value) results.initials = chars end end if self:isConfirmPressed() then self:commitResultHighScore() self.waiting_for_input_release = true end elseif self:isConfirmPressed() then self:resetWorld() self.show_splash = false self.show_start = true self.waiting_for_input_release = true end return true end function Game:drawObjectivePrompt() if self.post_intro_page then local page = CUTSCENE.post_intro and CUTSCENE.post_intro.pages and CUTSCENE.post_intro.pages[self.post_intro_page] if not page then return end local lines = page.lines or {} local box_w = 156 local box_h = 18 + math.max(1, #lines) * 8 local box_x = math.floor((SCREEN_W - box_w) / 2) local box_y = math.floor((SCREEN_H - box_h) / 2) rect(box_x, box_y, box_w, box_h, 0) rectb(box_x, box_y, box_w, box_h, 15) local text_x = box_x + 8 if page.sprite then spr(page.sprite, box_x + 8, box_y + 8, 0, 1, 0, 0, 1, 1) text_x = box_x + 22 end for i, line_text in ipairs(lines) do print(line_text, text_x, box_y + 8 + (i - 1) * 8, 15) end if (self.post_intro_lock_timer or 0) <= 0 then local prompt = "A/SPACE" print(prompt, math.floor((SCREEN_W - #prompt * 6) / 2), box_y + box_h - 9, 12) end return end if not self.objective_prompt_timer or self.objective_prompt_timer <= 0 then return end local box_w = 148 local box_h = 18 local box_x = math.floor((SCREEN_W - box_w) / 2) local box_y = 12 rect(box_x, box_y, box_w, box_h, 0) rectb(box_x, box_y, box_w, box_h, 15) spr(CUTSCENE.objective.sprite, box_x + 6, box_y + 5, 0, 1, 0, 0, 1, 1) print(CUTSCENE.objective.text, box_x + 20, box_y + 6, 15) end -- Held item action helpers ------------------------------------------- function Game:isItemActionHeld() if type(btn) == "function" and btn(INPUT_BUTTONS.action) then return true end if not INPUT_KEYS or not INPUT_KEYS.space then return false end if type(key) == "function" and key(INPUT_KEYS.space) then return true end if type(keyp) == "function" and keyp(INPUT_KEYS.space) then return true end return false end function Game:isConfirmPressed() if type(btnp) == "function" and btnp(INPUT_BUTTONS.action) then return true end if not INPUT_KEYS or not INPUT_KEYS.space then return false end if type(keyp) == "function" and keyp(INPUT_KEYS.space) then return true end return false end function Game:isInventoryKeyPressed(key_code) if not key_code or type(keyp) ~= "function" then return false end return keyp(key_code) and true or false end function Game:isDevBossHotkeyPressed() if not DEV.mode or not DEV.enable_boss_hotkey then return false end if not INPUT_KEYS or not INPUT_KEYS.dev_boss then return false end if type(keyp) ~= "function" then return false end return keyp(INPUT_KEYS.dev_boss) and true or false end function Game:hasInventoryItem(item_id) if item_id == ITEMS.ray_gun then return self.has_ray_gun and true or false elseif item_id == ITEMS.leaf_blower then return self.has_leaf_blower and true or false elseif item_id == ITEMS.microphone then return self.has_microphone and true or false elseif item_id == ITEMS.grappling_hook then return self.has_grappling_hook and true or false end return false end function Game:selectInventoryItem(item_id) if not self:hasInventoryItem(item_id) then return false end self:clearItemAction() self.equipped_item = item_id return true end function Game:cycleInventoryItem(direction) local slots = INVENTORY_UI.slots local current = direction > 0 and 0 or 1 for i, slot in ipairs(slots) do if slot.item_id == self.equipped_item then current = i; break end end for step = 1, #slots do local index = ((current - 1 + direction * step) % #slots) + 1 local item_id = slots[index].item_id if self:hasInventoryItem(item_id) then if item_id ~= self.equipped_item then self:selectInventoryItem(item_id) end return end end end function Game:updateInventorySelection() -- Do not let item selection cancel a cutscene or hit-recovery lock. if self.player:isHidden() or self.player:isPlayingSpecialAnimation() or self.post_intro_page then return end local story = self.story_event if story and (story.state == "intro" or story.state == "sing") then return end local boss = self.bill_boss if boss and boss.phase ~= "inactive" and (boss.phase ~= "fight" or (boss.player_hit_timer or 0) > 0) then return end if type(btnp) == "function" then if btnp(INPUT_BUTTONS.previous_item) then self:cycleInventoryItem(-1) return elseif btnp(INPUT_BUTTONS.next_item) then self:cycleInventoryItem(1) return end end for _, slot in ipairs(INVENTORY_UI.slots) do if self:isInventoryKeyPressed(slot.key) then self:selectInventoryItem(slot.item_id) return end end end function Game:buildBeamSegments(dir, origin_tx, origin_ty, max_length) local segments = {} local dir_info = MOVEMENT_DIR_LOOKUP[dir] or MOVEMENT_DIR_LOOKUP.down if not dir_info then return segments end local half_tile = math.floor(TILE_SIZE / 2) local half_line = math.max(1, math.floor((BEAM.thickness_px or 2) / 2)) local tx = origin_tx local ty = origin_ty max_length = max_length or BEAM.tile_length for _ = 1, max_length do tx = tx + dir_info.dx ty = ty + dir_info.dy if not is_path_tile(tx, ty) then break end local cx, cy = tile_center_px(tx, ty) local rect if dir_info.dx ~= 0 then rect = { x1 = cx - half_tile, y1 = cy - half_line, x2 = cx + half_tile - 1, y2 = cy + half_line } else rect = { x1 = cx - half_line, y1 = cy - half_tile, x2 = cx + half_line, y2 = cy + half_tile - 1 } end segments[#segments + 1] = rect rect.tx = tx rect.ty = ty end return segments end local function get_beam_sprite_rotation(dir) if dir == "down" then return 1 end if dir == "left" then return 2 end if dir == "up" then return 3 end return 0 end get_cardinal_dir_from_vector = function(dx, dy) if math.abs(dx or 0) >= math.abs(dy or 0) then return (dx or 0) < 0 and "left" or "right" end return (dy or 0) < 0 and "up" or "down" end build_sprite_chain_positions = function(x1, y1, x2, y2, spacing_px) local positions = {} local dx = (x2 or 0) - (x1 or 0) local dy = (y2 or 0) - (y1 or 0) local dist = math.sqrt(dx * dx + dy * dy) local spacing = math.max(1, spacing_px or TILE_SIZE) if dist < 0.001 then positions[1] = {x = x1 or 0, y = y1 or 0} return positions end local steps = math.max(1, math.floor(dist / spacing)) for i = 0, steps do local t = i / steps positions[#positions + 1] = { x = (x1 or 0) + dx * t, y = (y1 or 0) + dy * t } end return positions end function Game:drawSpriteChain(config, x1, y1, x2, y2, dir) if not config then return end local positions = build_sprite_chain_positions(x1, y1, x2, y2, config.spacing_px) if #positions == 0 then return end dir = dir or get_cardinal_dir_from_vector((x2 or 0) - (x1 or 0), (y2 or 0) - (y1 or 0)) local rotation = get_beam_sprite_rotation(dir) local start_flip = 0 local start_rotation = rotation local start_sprite = config.start_sprite if config.start_side_sprite or config.start_down_sprite then if dir == "left" or dir == "right" then start_sprite = config.start_side_sprite or config.start_sprite start_rotation = 0 if dir == "left" then start_flip = 1 end else start_sprite = config.start_down_sprite or config.start_sprite start_rotation = dir == "up" and 2 or 0 end end for i, pos in ipairs(positions) do local sprite_id = config.middle_sprite local flip = 0 local rotate = rotation if i == 1 then sprite_id = start_sprite or config.middle_sprite flip = start_flip rotate = start_rotation elseif i == #positions then sprite_id = config.end_sprite or config.middle_sprite end spr( sprite_id, math.floor((pos.x or 0) - 4 - self.camera_px + 0.5), math.floor((pos.y or 0) - 4 - self.camera_py + 0.5), 0, 1, flip, rotate, 1, 1 ) end end function Game:startLightBeam() if not self.player then return end local player_tile = self:getPlayerTile() if not player_tile then self:clearLightBeam() return end local dir = (self.player.getFacingDir and self.player:getFacingDir()) or self.player.dir or "down" if not dir or dir == "neutral" then dir = "down" end local segments = self:buildBeamSegments(dir, player_tile.tx, player_tile.ty) if #segments == 0 then self:clearLightBeam() return end self.light_beam = self.light_beam or {} self.light_beam.dir = dir self.light_beam.origin_tx = player_tile.tx self.light_beam.origin_ty = player_tile.ty self.light_beam.segments = segments if self.player.setMovementLocked then self.player:setMovementLocked(true) end if self.player.setBeamVisual then self.player:setBeamVisual(true, ITEMS.ray_gun) end end function Game:clearLightBeam() self.light_beam = nil end function Game:startLeafBlowerBlast() if not self.player then return end local player_tile = self:getPlayerTile() if not player_tile then self:clearLeafBlowerBlast() return end local dir = (self.player.getFacingDir and self.player:getFacingDir()) or self.player.dir or "down" if not dir or dir == "neutral" then dir = "down" end local length = 1 local segments = self:buildBeamSegments(dir, player_tile.tx, player_tile.ty, length) if #segments == 0 then self:clearLeafBlowerBlast() return end self.leaf_blower_blast = { dir = dir, origin_tx = player_tile.tx, origin_ty = player_tile.ty, length = length, grow_timer = 0, segments = segments } if self.player.setMovementLocked then self.player:setMovementLocked(true) end if self.player.setBeamVisual then self.player:setBeamVisual(true, ITEMS.leaf_blower) end end function Game:clearLeafBlowerBlast() self.leaf_blower_blast = nil end function Game:clearItemAction() self:clearLightBeam() self:clearLeafBlowerBlast() self.grapple_shot = nil if self.player then if self.player.setMovementLocked then self.player:setMovementLocked(false) end if self.player.setBeamVisual then self.player:setBeamVisual(false) end end end function Game:getGrappleOrigin() if not self.player then return 0, 0 end local facing = (self.player.getFacingDir and self.player:getFacingDir()) or self.player.dir or "right" local offset_x = GRAPPLE_VISUAL.origin_offset_x or PLAYER_FOOT_OFFSET_X local offset_y = GRAPPLE_VISUAL.origin_offset_y or 7 if facing == "left" then offset_x = GRAPPLE_VISUAL.origin_flip_x or offset_x elseif facing == "down" then offset_x = GRAPPLE_VISUAL.origin_down_x or offset_x offset_y = GRAPPLE_VISUAL.origin_down_y or offset_y end return self.player.x + offset_x, self.player.y + offset_y end function Game:startGrappleShot() if not self.player or not self.has_grappling_hook or self.grapple_shot or not self:isBillBossArenaPhase() then return end local dir = (self.player.getFacingDir and self.player:getFacingDir()) or self.player.dir or "right" local dir_info = MOVEMENT_DIR_LOOKUP[dir] or MOVEMENT_DIR_LOOKUP.right local start_x, start_y = self:getGrappleOrigin() local speed = BILL_BOSS.hook.speed or 4 local life = math.max(1, math.floor(((BILL_BOSS.hook.range_tiles or 6) * TILE_SIZE) / speed)) self.grapple_shot = { x = start_x, y = start_y, start_x = start_x, start_y = start_y, vx = (dir_info.dx or 1) * speed, vy = (dir_info.dy or 0) * speed, speed = speed, life = life, returning = false } if self.player and self.player.setBeamVisual then self.player:setBeamVisual(true, ITEMS.grappling_hook) end end function Game:updateGrappleShot() if not self.grapple_shot then return end local shot = self.grapple_shot if not shot.returning then shot.x = shot.x + shot.vx shot.y = shot.y + shot.vy shot.life = shot.life - 1 local bx, by, bw, bh = self:getBillBounds() if bx and self.bill_boss and self.bill_boss.phase == "fight" then if shot.x >= bx and shot.x <= (bx + bw) and shot.y >= by and shot.y <= (by + bh) then self:damageBill() shot.returning = true end end if shot.life <= 0 then shot.returning = true end end if shot.returning then if not self.player then self.grapple_shot = nil if self.player and self.player.setBeamVisual then self.player:setBeamVisual(false) end return end local target_x, target_y = self:getGrappleOrigin() local dx = target_x - shot.x local dy = target_y - shot.y local dist = math.sqrt(dx * dx + dy * dy) if dist <= (shot.speed or (BILL_BOSS.hook.speed or 4)) or dist < 0.001 then self.grapple_shot = nil if self.player and self.player.setBeamVisual then self.player:setBeamVisual(false) end return end local speed = shot.speed or (BILL_BOSS.hook.speed or 4) shot.x = shot.x + dx / dist * speed shot.y = shot.y + dy / dist * speed end end function Game:drawGrappleShot() if not self.grapple_shot then return end local shot = self.grapple_shot self:drawSpriteChain( GRAPPLE_VISUAL, shot.start_x, shot.start_y, shot.x, shot.y, get_cardinal_dir_from_vector(shot.vx, shot.vy) ) end function Game:handleItemActionInput() if not self.player or self.player:isHidden() then self:clearItemAction() return end if self.bill_boss and (self.bill_boss.phase == "pre_intro" or self.bill_boss.phase == "intro" or self.bill_boss.phase == "arena_spawn" or self.bill_boss.phase == "death" or self.bill_boss.phase == "ending" or self.bill_boss.phase == "soos" or self.bill_boss.phase == "outro") then self:clearLightBeam() self:clearLeafBlowerBlast() self.grapple_shot = nil if self.player and self.player.setBeamVisual then self.player:setBeamVisual(false) end return end if self:tryStartZombieSong() then self:clearLightBeam() self:clearLeafBlowerBlast() self.grapple_shot = nil if self.player and self.player.setBeamVisual then self.player:setBeamVisual(false) end return end local equipped_item = self.equipped_item if equipped_item == ITEMS.grappling_hook then if self.has_grappling_hook and self:isConfirmPressed() and not self.grapple_shot then self:startGrappleShot() end self:clearLightBeam() self:clearLeafBlowerBlast() if self.player and self.player.setBeamVisual and not self.grapple_shot then self.player:setBeamVisual(false) end return end if not equipped_item or not self:isItemActionHeld() then self:clearItemAction() return end if equipped_item == ITEMS.ray_gun and self.has_ray_gun then if not self.light_beam then self:startLightBeam() end self:clearLeafBlowerBlast() self.grapple_shot = nil elseif equipped_item == ITEMS.leaf_blower and self.has_leaf_blower then if not self.leaf_blower_blast then self:startLeafBlowerBlast() end self:clearLightBeam() self.grapple_shot = nil elseif equipped_item == ITEMS.microphone and self.has_microphone then self:clearLightBeam() self:clearLeafBlowerBlast() self.grapple_shot = nil else self:clearItemAction() end end function Game:updateItemAction() if self.bill_boss and (self.bill_boss.phase == "pre_intro" or self.bill_boss.phase == "intro" or self.bill_boss.phase == "arena_spawn" or self.bill_boss.phase == "death" or self.bill_boss.phase == "ending" or self.bill_boss.phase == "soos" or self.bill_boss.phase == "outro") then self:clearLightBeam() self:clearLeafBlowerBlast() self.grapple_shot = nil if self.player and self.player.setBeamVisual then self.player:setBeamVisual(false) end return end if self.story_event and self.story_event.state == "sing" then self:clearLightBeam() self:clearLeafBlowerBlast() self.grapple_shot = nil if self.player and self.player.setBeamVisual then self.player:setBe )amVisual(false) end return end if self.equipped_item == ITEMS.ray_gun then self:updateLightBeam() elseif self.equipped_item == ITEMS.leaf_blower then self:updateLeafBlowerBlast() elseif self.equipped_item == ITEMS.grappling_hook then self:updateGrappleShot() elseif self.equipped_item == ITEMS.microphone then self:clearLightBeam() self:clearLeafBlowerBlast() self.grapple_shot = nil else self:clearItemAction() return end end function Game:updateLightBeam() if not self.light_beam then return end local player_tile = self:getPlayerTile() if not player_tile then self:clearLightBeam() return end local dir = self.light_beam.dir if self.player and self.player.getFacingDir then dir = self.player:getFacingDir() end local segments = self:buildBeamSegments(dir, player_tile.tx, player_tile.ty) if #segments == 0 then self:clearLightBeam() return end self.light_beam.dir = dir self.light_beam.origin_tx = player_tile.tx self.light_beam.origin_ty = player_tile.ty self.light_beam.segments = segments end function Game:updateLeafBlowerBlast() if not self.leaf_blower_blast then return end local player_tile = self:getPlayerTile() if not player_tile then self:clearLeafBlowerBlast() return end local dir = self.leaf_blower_blast.dir if self.player and self.player.getFacingDir then dir = self.player:getFacingDir() end local blast = self.leaf_blower_blast blast.grow_timer = (blast.grow_timer or 0) + 1 if blast.grow_timer >= (LEAF_BLOWER_ITEM.gust_grow_frames or 1) then blast.grow_timer = 0 blast.length = (blast.length or 1) + 1 if blast.length > (LEAF_BLOWER_ITEM.gust_max_length or 5) then blast.length = 1 end end local segments = self:buildBeamSegments(dir, player_tile.tx, player_tile.ty, blast.length) if #segments == 0 then self:clearLeafBlowerBlast() return end blast.dir = dir blast.origin_tx = player_tile.tx blast.origin_ty = player_tile.ty blast.segments = segments end function Game:checkLeafBlowerGnomeHit() if not self.gnomes or not self.leaf_blower_blast or not self.leaf_blower_blast.segments then return end local dir = self.leaf_blower_blast.dir or "right" for _, gnome in ipairs(self.gnomes) do if not gnome.removed and not gnome.blown then local gx, gy, gw, gh = gnome:getBounds() for _, segment in ipairs(self.leaf_blower_blast.segments) do local sx = segment.tx * TILE_SIZE local sy = segment.ty * TILE_SIZE if rects_overlap(gx, gy, gw, gh, sx, sy, TILE_SIZE, TILE_SIZE) then gnome:blowAway(dir) break end end end end end function Game:drawItemAction() if self.equipped_item == ITEMS.ray_gun then self:drawLightBeam() elseif self.equipped_item == ITEMS.leaf_blower then self:drawLeafBlowerBlast() end end function Game:drawLightBeam() if not self.light_beam or not self.light_beam.segments then return end local segments = self.light_beam.segments local rotation = get_beam_sprite_rotation(self.light_beam.dir) for idx, segment in ipairs(segments) do local sprite_id = BEAM.middle_sprite if idx == 1 then sprite_id = BEAM.start_sprite elseif idx == #segments then sprite_id = BEAM.end_sprite end local px = segment.tx * TILE_SIZE - self.camera_px local py = segment.ty * TILE_SIZE - self.camera_py + BEAM.draw_offset_y spr(sprite_id, px, py, 0, 1, 0, rotation, 1, 1) end end function Game:drawLeafBlowerBlast() if not self.leaf_blower_blast or not self.leaf_blower_blast.segments then return end local rotation = get_beam_sprite_rotation(self.leaf_blower_blast.dir) for _, segment in ipairs(self.leaf_blower_blast.segments) do local px = segment.tx * TILE_SIZE - self.camera_px local py = segment.ty * TILE_SIZE - self.camera_py + LEAF_BLOWER_ITEM.gust_draw_offset_y spr(LEAF_BLOWER_ITEM.gust_sprite, px, py, 0, 1, 0, rotation, 1, 1) end end function Game:drawVictoryScreen() local title_scale = 2 local prompt_scale = 1 local title_w = #FRONTEND.victory.title * 6 * title_scale local prompt_w = #FRONTEND.victory.prompt * 6 * prompt_scale local title_x = math.floor((SCREEN_W - title_w) / 2) local prompt_x = math.floor((SCREEN_W - prompt_w) / 2) local center_y = math.floor(SCREEN_H / 2) local title_y = center_y - 10 local prompt_y = center_y + 8 print(FRONTEND.victory.title, title_x, title_y, FRONTEND.victory.title_color, false, title_scale) print(FRONTEND.victory.prompt, prompt_x, prompt_y, FRONTEND.victory.prompt_color, false, prompt_scale) end function Game:drawGameOverScreen() local title = "GAME OVER" local prompt = "Press any button" local title_scale = 2 local prompt_scale = 1 local title_w = #title * 6 * title_scale local prompt_w = #prompt * 6 * prompt_scale local title_x = math.floor((SCREEN_W - title_w) / 2) local prompt_x = math.floor((SCREEN_W - prompt_w) / 2) local center_y = math.floor(SCREEN_H / 2) print(title, title_x, center_y - 10, 12, false, title_scale) print(prompt, prompt_x, center_y + 8, 14, false, prompt_scale) self:drawSoosDeathMessage() end function Game:handleGameOverScreen() if not self.show_game_over then return false end cls(0) self.game_over_timer = (self.game_over_timer or 0) + 1 self:drawGameOverScreen() if self:anyButtonPressed() then self.show_game_over = false self:resetWorld() self.show_splash = false self.show_start = true self.waiting_for_input_release = true end return true end function Game:handleVictoryScreen() if not self.show_victory then return false end cls(FRONTEND.victory.bg_color or 0) self:drawVictoryScreen() if self:anyButtonPressed() then self.show_victory = false self:resetWorld() self.show_splash = false self.show_start = true self.waiting_for_input_release = true end return true end -- Master simulation tick called from TIC(). -- Early returns keep splash/start screens from updating the world. function Game:tick() if self:handleSplashScreen() then return end if self:handleFrontendStartTransition() then return end if self:handleStartScreen() then return end if self:handleIntroCutscene() then return end if self:handleHighScoresScreen() then return end if self:handleResultsScreen() then return end if self:handleGameOverScreen() then return end if self:handleVictoryScreen() then return end local post_intro_modal_active = self:updatePostIntroModal() if not post_intro_modal_active then self.run_frames = (self.run_frames or 0) + 1 if self.pickup_audio_timer and self.pickup_audio_timer > 0 then self.pickup_audio_timer = self.pickup_audio_timer - 1 end if self.objective_prompt_timer and self.objective_prompt_timer > 0 then self.objective_prompt_timer = self.objective_prompt_timer - 1 end self:updateSoosDeathMessage() if self:isDevBossHotkeyPressed() and self.bill_boss and self.bill_boss.phase == "inactive" then self:startBillIntro() end if self.player and self.player.setMovementLocked then local story = self.story_event if story and (story.state == "intro" or story.state == "sing") then self.player:setMovementLocked(true) end end if self.player then self.player.footsteps_enabled = not self:shouldSuppressFootsteps() end self.player:update() self:checkBottomlessPit() self:updatePowerups() self:updateGnomes() self:updateHoodedFigure() self:updateStoryEvent() self:updateBillBoss() if self.show_game_over then return end self:respawnPlayerIfNeeded() if self.enemy and not (DEV.mode and DEV.disable_enemy) and not self:isMazeEnemyPaused() then self.enemy:update(self.player) end if not self.player:isHidden() and not self.player:isPlayingSpecialAnimation() then self:collectMarkerUnderPlayer() self:checkPowerupCollection() self:checkBillArenaPowerupCollection() self:checkJournalCollection() self:checkRayGunCollection() self:checkLeafBlowerCollection() self:checkMicrophoneCollection() self:checkGrapplingHookCollection() end self:checkEnemyCollision() self:checkGnomeCollision() self:updatePowerupEffects() self:updatePlayerTrail() self:updateInventorySelection() self:handleItemActionInput() self:updateItemAction() self:checkLeafBlowerGnomeHit() self:checkHoodedFigureHit() self:updateCamera() self:updateEnemyAudio() end -- Clear background, draw the current map slice, then render entities. local shake_x = 0 local shake_y = 0 if self.bill_boss and self.bill_boss.phase == "death" then local amount = BILL_BOSS.shake_px or 0 shake_x = math.random(-amount, amount) shake_y = math.random(-amount, amount) elseif self.story_event and (self.story_event.state == "siege" or self.story_event.state == "sing") then local amount = STORY_EVENT.shack.shake_px or 0 shake_x = math.random(-amount, amount) shake_y = math.random(-amount, amount) end if type(camera) == "function" then camera(shake_x, shake_y) end cls(6) map(self.camera_tile_x, self.camera_tile_y, SCREEN_TILES_W, SCREEN_TILES_H, 0, 0, 0) self:drawEnemy() self:drawGnomes() self:drawPowerups() self:drawBillArenaPowerup() self:drawMicrophone() self:drawGrapplingHookPickup() self:drawRayGun() self:drawHoodedFigure() self:drawLeafBlower() self:drawStoryEvent() self:drawBillBoss() self:drawJournals() draw_path_markers( self.camera_tile_x, self.camera_tile_y, self.camera_px, self.camera_py, self.collected_markers ) self:drawPlayerTrail() self:drawItemAction() local draw_player = true if self.bill_boss and (self.bill_boss.player_invulnerable_timer or 0) > 0 then draw_player = ((self.bill_boss.player_invulnerable_timer % 4) < 2) end if draw_player then self.player:draw(self.camera_px, self.camera_py, self.equipped_item) end if self.equipped_item == ITEMS.grappling_hook then self:drawGrappleShot() end self:drawPowerupOverlay() self:drawHUD() self:drawObjectivePrompt() self:drawStoryDialogue() self:drawSoosDeathMessage() self:drawBillBossOverlay() if type(camera) == "function" then camera(0, 0) end end local game = Game.new() function TIC() game:tick() end