/* *-------------------------------------- * Program Name: MazeDash * Author: matkeller19 * License: * Description: *-------------------------------------- */ #include #include #include #include #include #include /* Standard headers (recommended) */ #include #include #include #include #include #include "gfx/gfx.h" #include "key_helper.h" typedef struct{ bool unlocked; uint8_t mapData[49]; bool twoSnakes; uint8_t numKeys; uint8_t toFill; uint8_t s1x, s1y, s2x, s2y; uint8_t portalLinks[2]; bool canPortal; }data_t; data_t level; typedef struct{ bool canMove; uint8_t lastDir, moveDir; uint8_t col; uint8_t row; bool teleportNext; bool moveOpposite; }snake_t; int canMoveVals[] = {2, 3}; int portalDispLoc[2][2] = {{50, 0}, {50, 0}}; int levelsPerWorld[] = {20, 30, 40, 40, 40, 40, 40, 40}; snake_t snake1; snake_t snake2; gfx_tilemap_t tilemap; gfx_sprite_t * map; extern unsigned char tilemap_map_compressed[]; gfx_sprite_t *tileset_tiles[24]; gfx_sprite_t *HomeIcon, *NextLevelIcon, *portalOverlay, *RestartIcon, *SettingsIcon, *ExitIcon, *ContinueIcon; void setMoveDir(); bool checkIfCanMove(snake_t * snake, uint8_t val); void render(int sWorld, int sLevel); void moveUntilStopped(snake_t * snake); void shake(snake_t * snake); bool levelSelect(int *sLevel,int *sWorld); int levelTransition( bool clear, int world, int level); int findMaxUnlocked(int sWorld); void loadLevel(int sWorld, int sLevel); int homescreen(); void runGame(); int main(void) { bool no = false; int i; gfx_sprite_t *tmp_ptr; ti_var_t file; srand(rtc_Time()); gfx_Begin(); for (i = 0; i < sizeof(tileset_tiles) / sizeof(gfx_sprite_t*); ++i) { tmp_ptr = gfx_MallocSprite(16, 16); zx7_Decompress(tmp_ptr, TempTiles_tiles_compressed[i]); tileset_tiles[i] = tmp_ptr; } HomeIcon = gfx_MallocSprite(HomeIcon_width, HomeIcon_height); ExitIcon = gfx_MallocSprite(ExitIcon_width, ExitIcon_height); NextLevelIcon = gfx_MallocSprite(NextLevelIcon_width, NextLevelIcon_height); portalOverlay = gfx_MallocSprite(portalOverlay_width, portalOverlay_height); RestartIcon = gfx_MallocSprite(RestartIcon_width, RestartIcon_height); SettingsIcon = gfx_MallocSprite(SettingsIcon_width, SettingsIcon_height); ContinueIcon = gfx_MallocSprite(ContinueIcon_width, ContinueIcon_height); zx7_Decompress(HomeIcon, HomeIcon_compressed); zx7_Decompress(ExitIcon, ExitIcon_compressed); zx7_Decompress(NextLevelIcon, NextLevelIcon_compressed); zx7_Decompress(portalOverlay, portalOverlay_compressed); zx7_Decompress(RestartIcon, RestartIcon_compressed); zx7_Decompress(SettingsIcon, SettingsIcon_compressed); zx7_Decompress(ContinueIcon, ContinueIcon_compressed); tilemap.tiles = tileset_tiles; tilemap.type_width = gfx_tile_32_pixel; tilemap.type_height = gfx_tile_32_pixel; tilemap.tile_height = 16; tilemap.tile_width = 16; tilemap.draw_height = 7; tilemap.draw_width = 7; tilemap.height = 7; tilemap.width = 7; tilemap.y_loc = 0; tilemap.x_loc = 0; gfx_SetDrawBuffer(); map = gfx_MallocSprite(7*16,7*16); gfx_SetPalette(world1_palette, sizeof_world1_palette, 0); if(!(file = ti_Open("MDLVLS", "r"))){ ti_Close(file); gfx_FillScreen(0); gfx_SetTextFGColor(18); gfx_PrintStringXY("APPVAR NOT FOUND", (LCD_WIDTH - gfx_GetStringWidth("APPVAR NOT FOUND")) / 2, LCD_HEIGHT / 2); gfx_PrintStringXY("Please add MDLVLS.8xv", (LCD_WIDTH - gfx_GetStringWidth("Please add MDLVLS.8xv")) / 2, LCD_HEIGHT / 2 + 12); gfx_SwapDraw(); while(!os_GetCSC()); free(map); free(tmp_ptr); gfx_End(); return 0; } gfx_SetTransparentColor(20); kb_scan_edge(); while(true){ bool choice = true; switch(homescreen()){ case 0: gfx_End(); free(map); free(tmp_ptr); file = ti_Open("MDLVLS", "r"); ti_SetArchiveStatus(true, file); ti_Close(file); return 0; case 1: runGame(); break; case 2: gfx_SetColor(2); gfx_FillRectangle_NoClip(LCD_WIDTH/4, LCD_HEIGHT/3, LCD_WIDTH/2, LCD_HEIGHT/3); gfx_SetColor(11); gfx_FillRectangle_NoClip(LCD_WIDTH/4 + 4, LCD_HEIGHT/3 + 4, LCD_WIDTH/2 - 8, LCD_HEIGHT/3 - 8); gfx_SetTextScale(2,2); gfx_SetTextFGColor(2); gfx_PrintStringXY("Options", 108, LCD_HEIGHT/3 + 8); gfx_SetTextScale(1, 1); gfx_ScaledTransparentSprite_NoClip(RestartIcon, (LCD_WIDTH - HomeIcon_width * 2)/2 - 32, (LCD_HEIGHT/3 + 32), 2, 2); gfx_ScaledTransparentSprite_NoClip(HomeIcon, (LCD_WIDTH - HomeIcon_width * 2)/2 + 32, (LCD_HEIGHT/3 + 32), 2, 2); gfx_PrintStringXY("Reset", 110, 140); gfx_PrintStringXY("Back", 178, 140); while(true){ kb_scan_edge(); gfx_SetColor(11); gfx_Rectangle_NoClip((LCD_WIDTH - HomeIcon_width * 2)/2 - 36 + 64 * choice, (LCD_HEIGHT/3 + 32), 42, 40); if(kb_Rising[7] & kb_Left || (kb_Rising[7] & kb_Right)) choice = !choice; if(kb_Rising[6] & kb_Clear) break; if(kb_Rising[1] & kb_2nd){ if(!choice){ choice = true; gfx_SetColor(2); gfx_FillRectangle_NoClip(LCD_WIDTH/4, LCD_HEIGHT/3, LCD_WIDTH/2, LCD_HEIGHT/3); gfx_SetColor(11); gfx_FillRectangle_NoClip(LCD_WIDTH/4 + 4, LCD_HEIGHT/3 + 4, LCD_WIDTH/2 - 8, LCD_HEIGHT/3 - 8); gfx_PrintStringXY("Are you sure?", 113, LCD_HEIGHT/3 + 8); gfx_PrintStringXY("This cannot be undone.", 84, LCD_HEIGHT/3 + 24); gfx_PrintStringXY("Yes", 116, 132); gfx_PrintStringXY("No", 184, 132); while(true){ kb_scan_edge(); gfx_SetColor(11); gfx_Rectangle_NoClip(112 + 64 * choice, 128, 32, 16); if(kb_Rising[7] & kb_Left || (kb_Rising[7] & kb_Right)) choice = !choice; if(kb_Rising[1] & kb_2nd){ if(!choice){ ti_Close(file); file = ti_Open("MDLVLS","r+"); for(i = 1; i < 289; i++){ ti_Seek(50*i, 0, file); ti_Write(&no, sizeof(no), 1, file); } ti_Close(file); } break; } if(kb_Rising[6] & kb_Clear) break; gfx_SetColor(2); gfx_Rectangle_NoClip(112 + 64 * choice, 128, 32, 16); gfx_SwapDraw(); gfx_BlitScreen(); } } break; } gfx_SetColor(2); gfx_Rectangle_NoClip((LCD_WIDTH - HomeIcon_width * 2)/2 - 36 + 64 * choice, (LCD_HEIGHT/3 + 32), 42, 40); gfx_SwapDraw(); gfx_BlitScreen(); } break; } } } void setMoveDir(snake_t * snake){ snake->moveDir = 4; if((kb_Rising[7] & kb_Up)) snake->moveDir = 0; if((kb_Rising[7] & kb_Right)) snake->moveDir = 1; if((kb_Rising[7] & kb_Down)) snake->moveDir = 2; if((kb_Rising[7] & kb_Left)) snake->moveDir = 3; } void moveUntilStopped(snake_t * snake){ int i = gfx_GetTileMapped(&tilemap, snake->col + (snake->moveDir == 1) - (snake->moveDir == 3), snake->row - (snake->moveDir == 0) + (snake->moveDir == 2)); switch (snake->moveDir){ case 0: if(snake->lastDir == 4) gfx_SetTileMapped(&tilemap,snake->col,snake->row,7); if(snake->lastDir == 0 || snake->lastDir == 2) gfx_SetTileMapped(&tilemap,snake->col,snake->row,6); if(snake->lastDir == 1) gfx_SetTileMapped(&tilemap,snake->col,snake->row,12); if(snake->lastDir == 3) gfx_SetTileMapped(&tilemap,snake->col,snake->row,11); snake->lastDir = 0; snake->row--; break; case 1: if(snake->lastDir == 4) gfx_SetTileMapped(&tilemap,snake->col,snake->row,9); if(snake->lastDir == 1 || snake->lastDir == 3) gfx_SetTileMapped(&tilemap,snake->col,snake->row,5); if(snake->lastDir == 0) gfx_SetTileMapped(&tilemap,snake->col,snake->row,14); if(snake->lastDir == 2) gfx_SetTileMapped(&tilemap,snake->col,snake->row,11); snake->lastDir = 1; snake->col++; break; case 2: if(snake->lastDir == 4) gfx_SetTileMapped(&tilemap,snake->col,snake->row,8); if(snake->lastDir == 0 || snake->lastDir == 2) gfx_SetTileMapped(&tilemap,snake->col,snake->row,6); if(snake->lastDir == 1) gfx_SetTileMapped(&tilemap,snake->col,snake->row,13); if(snake->lastDir == 3) gfx_SetTileMapped(&tilemap,snake->col,snake->row,14); snake->lastDir = 2; snake->row++; break; case 3: if(snake->lastDir == 4) gfx_SetTileMapped(&tilemap,snake->col,snake->row,10); if(snake->lastDir == 1 || snake->lastDir == 3) gfx_SetTileMapped(&tilemap,snake->col,snake->row,5); if(snake->lastDir == 0) gfx_SetTileMapped(&tilemap,snake->col,snake->row,13); if(snake->lastDir == 2) gfx_SetTileMapped(&tilemap,snake->col,snake->row,12); snake->lastDir = 3; snake->col--; break; } level.toFill--; if(i > 14 && i < 19){ snake->moveDir = i - 15; } if(snake->teleportNext){ gfx_SetTileMapped(&tilemap,snake->col,snake->row,5 + (snake->moveDir % 2 == 0)); portalDispLoc[0][0] = 7 * snake->row + snake->col; portalDispLoc[0][1] = snake->moveDir; portalDispLoc[1][0] = (*portalDispLoc[0] == level.portalLinks[0] ? level.portalLinks[1] : level.portalLinks[0]); portalDispLoc[1][1] = (snake->moveDir + 2) % 4; snake->row = portalDispLoc[1][0] / 7; snake->col = portalDispLoc[1][0] % 7; level.toFill--; snake->teleportNext = false; } gfx_SetTileMapped(&tilemap,snake->col,snake->row,4); } bool checkIfCanMove(snake_t * snake, uint8_t val){ int i = 2; if((snake->moveDir == 2 && snake->row == 6) || (snake->moveDir == 0 && snake->row == 0) || (snake->moveDir == 1 && snake->col == 6) || (snake->moveDir == 3 && snake->col == 0)) return false; if(!snake->moveOpposite && (snake->moveDir + 2) % 4 == snake->lastDir){ return false; } if(val == 2 || val == 3) { snake->moveOpposite = true; return true; } if(val == 22 || val == 23){ if(!--level.numKeys){ for(i = 0; i < 49; i++){ if(gfx_GetTileMapped(&tilemap, i % 7, i / 7) == 21){ gfx_SetTileMapped(&tilemap, i % 7, i / 7, 3); if(i / 7 > 0){ int temp; temp = gfx_GetTileMapped(&tilemap, i % 7, i / 7 - 1); gfx_SetTileMapped(&tilemap, i % 7, i / 7, (temp == 0 || temp == 1 || temp == 19) ? 3 : 2); } if(i / 7 < 6){ if(gfx_GetTileMapped(&tilemap, i % 7, i / 7 + 1) == 3) gfx_SetTileMapped(&tilemap, i % 7, i / 7 + 1, 2); } } } } snake->moveOpposite = true; return true; } if(val == 19){ if(snake->row == 0)gfx_SetTileMapped(&tilemap, snake->col + (snake->moveDir == 1) - (snake->moveDir == 3), snake->row - (snake->moveDir == 0) + (snake->moveDir == 2), 3); if(snake->row > 0){ i = gfx_GetTileMapped(&tilemap, snake->col + (snake->moveDir == 1) - (snake->moveDir == 3), snake->row - 1 - (snake->moveDir == 0) + (snake->moveDir == 2)); gfx_SetTileMapped(&tilemap,snake->col + (snake->moveDir == 1) - (snake->moveDir == 3),snake->row - (snake->moveDir == 0) + (snake->moveDir == 2),(i == 0 || i == 1 || i == 19 || i == 21) ? 3 : 2); } if(snake->row < 6) { i = gfx_GetTileMapped(&tilemap, snake->col + (snake->moveDir == 1) - (snake->moveDir == 3), snake->row + 1 - (snake->moveDir == 0) + (snake->moveDir == 2)); if(i == 3) gfx_SetTileMapped(&tilemap, snake->col + (snake->moveDir == 1) - (snake->moveDir == 3), snake->row + 1 - (snake->moveDir == 0) + (snake->moveDir == 2), 2); } snake->moveOpposite = true; return false; } if(val > 14 && val < 19) { snake->moveOpposite = true; return (val - 15 != (snake->moveDir + 2) % 4); } if(val == 20 && level.canPortal){ level.canPortal = false; snake->teleportNext = true; snake->moveOpposite = false; return true; } if((snake->moveDir == 0 && snake->row == 0 ) || (snake->moveDir == 1 && snake->col == 6 ) || (snake->moveDir == 2 && snake->row == 6 ) || (snake->moveDir == 3 && snake->col == 0 )) { snake->moveOpposite = true; return false; } return false; } void render(int sWorld, int sLevel){ int i; char temp1[8]; char temp2[8]; sprintf(temp1, "WORLD0%d", sWorld); sprintf(temp2, "LEVEL%d%d", sLevel / 10, sLevel % 10); gfx_SetColor(11); gfx_Tilemap(&tilemap, 0, 0); switch(snake1.lastDir){ case 0: gfx_HorizLine(snake1.col* 16 + 1, snake1.row * 16 + 15, 14); break; case 1: gfx_VertLine(snake1.col* 16, snake1.row * 16 + 1, 14); break; case 2: gfx_HorizLine(snake1.col* 16 + 1, snake1.row * 16, 14); break; case 3: gfx_VertLine(snake1.col* 16 + 15, snake1.row * 16 + 1, 14); break; } if(level.twoSnakes){ switch(snake2.lastDir){ case 0: gfx_HorizLine(snake2.col* 16 + 1, snake2.row * 16 + 15, 14); break; case 1: gfx_VertLine(snake2.col* 16, snake2.row * 16 + 1, 14); break; case 2: gfx_HorizLine(snake2.col* 16 + 1, snake2.row * 16, 14); break; case 3: gfx_VertLine(snake2.col* 16 + 15, snake2.row * 16 + 1, 14); break; } } if(portalDispLoc[0][0] != 50){ gfx_RotatedTransparentSprite_NoClip(portalOverlay, (portalDispLoc[0][0] % 7) * 16, portalDispLoc[0][0] / 7 * 16, 64 * portalDispLoc[0][1]); gfx_RotatedTransparentSprite_NoClip(portalOverlay, (portalDispLoc[1][0] % 7) * 16, portalDispLoc[1][0] / 7 * 16, 64 * portalDispLoc[1][1]); } gfx_GetSprite(map, 0, 0); gfx_FillScreen(0); gfx_ScaledSprite_NoClip(map, LCD_WIDTH/2-7*16,LCD_HEIGHT/2-7*16,2,2); gfx_SetTextFGColor(11); gfx_SetTextScale(3, 3); for(i = 0; i < 7; i++){ gfx_SetTextXY(12, LCD_HEIGHT/2-7*16 + 28 * (i + (i > 4))); gfx_PrintChar(temp1[i]); gfx_SetTextXY(284, LCD_HEIGHT/2-7*16 + 28 * (i + (i > 4))); gfx_PrintChar(temp2[i]); } gfx_SwapDraw(); } int levelTransition(bool clear, int world, int level){ int i, selectedItem = ((world == 8 && level == 40 && clear) ? 1 : 2); if(clear){ for(i = 0; i < LCD_HEIGHT / 2; i+=4){ gfx_SetColor(11); gfx_FillRectangle_NoClip(0, i, LCD_WIDTH, 4); gfx_FillRectangle_NoClip(0, LCD_HEIGHT - 4 - i, LCD_WIDTH, 4); gfx_SetColor(2); gfx_HorizLine(0,i + 4, 320); gfx_HorizLine(0,LCD_HEIGHT - 5 - i, 320); gfx_SwapDraw(); gfx_BlitScreen(); } } i = ((world == 8 && level == 40 && clear) ? 2 : 3); gfx_SetColor(2); gfx_FillRectangle_NoClip(LCD_WIDTH/4, LCD_HEIGHT/3, LCD_WIDTH/2, LCD_HEIGHT/3); gfx_SetColor(11); gfx_FillRectangle_NoClip(LCD_WIDTH/4 + 4, LCD_HEIGHT/3 + 4, LCD_WIDTH/2 - 8, LCD_HEIGHT/3 - 8); gfx_SetTextScale(2,2); gfx_SetTextFGColor(2); gfx_PrintStringXY("Level", 123, LCD_HEIGHT/3 + 8); if(clear){ gfx_PrintStringXY("Complete", 100, LCD_HEIGHT/3 + 32); gfx_TransparentSprite_NoClip(ExitIcon, (LCD_WIDTH - HomeIcon_width)/2 - 32, (LCD_HEIGHT/3 + 54)); gfx_TransparentSprite_NoClip(HomeIcon, (LCD_WIDTH - HomeIcon_width)/2, (LCD_HEIGHT/3 + 54)); if(!(world == 8 && level == 40))gfx_TransparentSprite_NoClip(NextLevelIcon, (LCD_WIDTH - HomeIcon_width)/2 + 32, (LCD_HEIGHT/3 + 54)); }else{ gfx_PrintStringXY("Paused", 112, LCD_HEIGHT/3 + 32); gfx_TransparentSprite_NoClip(HomeIcon, (LCD_WIDTH - HomeIcon_width)/2 - 32, (LCD_HEIGHT/3 + 54)); gfx_TransparentSprite_NoClip(RestartIcon, (LCD_WIDTH - HomeIcon_width)/2, (LCD_HEIGHT/3 + 54)); gfx_TransparentSprite_NoClip(ContinueIcon, (LCD_WIDTH - HomeIcon_width)/2 + 32, (LCD_HEIGHT/3 + 54)); } gfx_SwapDraw(); gfx_BlitScreen(); do{ kb_scan_edge(); gfx_SetColor(11); gfx_Rectangle_NoClip((LCD_WIDTH - HomeIcon_width)/2 - 34 + 32 * selectedItem, (LCD_HEIGHT/3 + 52), 20, 20); if(kb_Rising[7] & kb_Left) selectedItem = (selectedItem + (i == 2 ? 1 : 2)) % i; if(kb_Rising[7] & kb_Right) selectedItem = (selectedItem + 1) % i; if(kb_Rising[1] & kb_2nd) return selectedItem; if(kb_Rising[6] & kb_Clear) return 0; gfx_SetColor(2); gfx_Rectangle_NoClip((LCD_WIDTH - HomeIcon_width)/2 - 34 + 32 * selectedItem, (LCD_HEIGHT/3 + 52), 20, 20); gfx_SwapDraw(); gfx_BlitScreen(); }while(true); } bool levelSelect(int *sLevel, int *sWorld){ int numCols = levelsPerWorld[*sWorld - 1] / 5, i; int prevWorld = 0; int maxUnlocked = findMaxUnlocked(*sWorld); do{ kb_scan_edge(); if(prevWorld != *sWorld){ gfx_FillScreen(0); gfx_SetColor(11); gfx_SetTextFGColor(2); gfx_SetTextScale(1, 1); if(*sWorld > 1)gfx_PrintStringXY("^ Previous World ^", 98, 36); if(*sWorld < 8)gfx_PrintStringXY("v Next World v", 114, 225); numCols = levelsPerWorld[*sWorld - 1] / 5; for(i = 0; i < levelsPerWorld[*sWorld - 1]; i++){ gfx_FillRectangle(LCD_WIDTH / 2 - (numCols - 1.25 - 0.5 * ((numCols - 4) / 2)) * 24 + 36 * (i % numCols), LCD_HEIGHT / 2 - 68 + 36 * (i / numCols), 24, 24); if(maxUnlocked > i){ gfx_SetTextXY(LCD_WIDTH / 2 - (numCols - 1.25 - 0.5 * ((numCols - 4) / 2)) * 24 + 36 * (i % numCols) + 5, LCD_HEIGHT / 2 - 68 + 36 * (i / numCols) + 8); gfx_PrintInt(i + 1, 2); } } gfx_SetColor(2); gfx_SetTextFGColor(11); gfx_SetTextScale(3,3); gfx_PrintStringXY("World ", 78, 8); gfx_PrintInt(*sWorld, 2); gfx_SetTextFGColor(2); gfx_SetTextScale(1, 1); prevWorld = *sWorld; } gfx_SetColor(0); if(maxUnlocked)gfx_Rectangle_NoClip(LCD_WIDTH / 2 - (numCols - 1.25 - 0.5 * ((numCols - 4) / 2)) * 24 + 36 * ((*sLevel - 1) % numCols) - 1, LCD_HEIGHT / 2 - 69 + 36 * ((*sLevel - 1) / numCols), 26, 26); if((kb_Rising[7] & kb_Left) && *sLevel > 1 && maxUnlocked) *sLevel = *sLevel - 1; if((kb_Rising[7] & kb_Right) && *sLevel < maxUnlocked) *sLevel = *sLevel + 1; if(kb_Rising[6] & kb_Clear) return false; if(kb_Rising[7] & kb_Down){ if(*sLevel + numCols > maxUnlocked){ if(*sWorld != 8){ *sWorld = *sWorld + 1; maxUnlocked = findMaxUnlocked(*sWorld); *sLevel = 1; } }else{ *sLevel = *sLevel + numCols; } } if(kb_Rising[7] & kb_Up){ if(*sLevel - numCols < 1 && *sWorld > 1){ *sWorld = *sWorld - 1; maxUnlocked = findMaxUnlocked(*sWorld); *sLevel = maxUnlocked; }else if (*sLevel - numCols > 0){ *sLevel = *sLevel - numCols; } } if(kb_Rising[1] & kb_2nd && maxUnlocked) return true; gfx_SetColor(2); if(maxUnlocked)gfx_Rectangle_NoClip(LCD_WIDTH / 2 - (numCols - 1.25 - 0.5 * ((numCols - 4) / 2)) * 24 + 36 * ((*sLevel - 1) % numCols) - 1, LCD_HEIGHT / 2 - 69 + 36 * ((*sLevel - 1) / numCols), 26, 26); gfx_SwapDraw(); switch(*sWorld){ case 1: gfx_SetPalette(world1_palette, sizeof_world1_palette, 0); break; case 2: gfx_SetPalette(world2_palette, sizeof_world2_palette, 0); break; case 3: gfx_SetPalette(world3_palette, sizeof_world2_palette, 0); break; case 4: gfx_SetPalette(world4_palette, sizeof_world2_palette, 0); break; case 5: gfx_SetPalette(world5_palette, sizeof_world2_palette, 0); break; case 6: gfx_SetPalette(world6_palette, sizeof_world2_palette, 0); break; case 7: gfx_SetPalette(world7_palette, sizeof_world2_palette, 0); break; case 8: gfx_SetPalette(world8_palette, sizeof_world2_palette, 0); break; } gfx_BlitScreen(); }while(true); } int findMaxUnlocked(int sWorld){ int maxUnlocked = -1; int worldJump = 0; int i; ti_var_t unlockFinder; data_t tempLevel; for(i = 1; i < sWorld; i++){ worldJump += levelsPerWorld[i - 1]; } unlockFinder = ti_Open("MDLVLS","r+"); ti_Seek(worldJump * 50, 0, unlockFinder); do{ ti_Read(&tempLevel, 50, 1, unlockFinder); maxUnlocked++; } while (tempLevel.unlocked && maxUnlocked < levelsPerWorld[sWorld - 1]); ti_Close(unlockFinder); return maxUnlocked; } void loadLevel(int sworld, int slevel){ int levelOffset = 0, i; ti_var_t file; for(i = 1; i < sworld; i++){ levelOffset += levelsPerWorld[i - 1]; } levelOffset += slevel - 1; file = ti_Open("MDLVLS","r+"); ti_Seek(levelOffset * 50, 0, file); ti_Read(&level.unlocked, 1, 1, file); ti_Read(&level.mapData, 49, 1, file); ti_Close(file); level.twoSnakes = false; level.s1x = 7; level.s1y = 7; level.s2x = 7; level.s2y = 7; level.numKeys = 0; level.toFill = 0; level.portalLinks[0] = 50; level.portalLinks[1] = 50; level.canPortal = true; for(i = 0; i < 49; i++){ int tile = level.mapData[i]; if(tile == 2 || tile == 3 || tile > 14) level.toFill++; if(tile == 4){ if(level.s1x == 7){ level.s1x = i % 7; level.s1y = i / 7; }else{ level.twoSnakes = true; level.s2x = i % 7; level.s2y = i / 7; } } if(tile == 22 || tile == 23) level.numKeys++; if(tile == 20){ if(level.portalLinks[0] == 50){ level.portalLinks[0] = i; }else{ level.portalLinks[1] = i; } } } snake1.col = level.s1x; snake1.row = level.s1y; snake2.col = level.s2x; snake2.row = level.s2y; snake1.lastDir = 4; snake2.lastDir = 4; snake1.moveOpposite = true; snake2.moveOpposite = true; tilemap.map = level.mapData; portalDispLoc[0][0] = 50; portalDispLoc[1][0] = 50; switch(sworld){ case 1: gfx_SetPalette(world1_palette, sizeof_world1_palette, 0); break; case 2: gfx_SetPalette(world2_palette, sizeof_world2_palette, 0); break; case 3: gfx_SetPalette(world3_palette, sizeof_world2_palette, 0); break; case 4: gfx_SetPalette(world4_palette, sizeof_world2_palette, 0); break; case 5: gfx_SetPalette(world5_palette, sizeof_world2_palette, 0); break; case 6: gfx_SetPalette(world6_palette, sizeof_world2_palette, 0); break; case 7: gfx_SetPalette(world7_palette, sizeof_world2_palette, 0); break; case 8: gfx_SetPalette(world8_palette, sizeof_world2_palette, 0); break; } } int homescreen(){ int i; gfx_FillScreen(0); for(i = 0; i < 10; i++){ gfx_ScaledSprite_NoClip(tileset_tiles[1], rand() % 9 * 32 + 16, rand() % 7 * 32, 2, 2); } for(i = 0; i < 5; i++){ gfx_ScaledSprite_NoClip(tileset_tiles[3], 80 + i * 32, 96, 2, 2); } gfx_ScaledSprite_NoClip(tileset_tiles[2], 80 + 64, 96 + 32, 2, 2); gfx_ScaledSprite_NoClip(tileset_tiles[2], 80 + 64, 96 + 64, 2, 2); gfx_ScaledSprite_NoClip(tileset_tiles[4], 80 + 64, 96, 2, 2); gfx_SetTextFGColor(2); gfx_SetTextScale(3,3); gfx_PrintStringXY("MazeDash!",57, 33); gfx_PrintStringXY("CE", 135, 71); gfx_SetTextFGColor(11); gfx_PrintStringXY("MazeDash!",58, 32); gfx_PrintStringXY("CE", 136, 70); gfx_SwapDraw(); gfx_SetPalette(world1_palette, sizeof_world1_palette, 0); gfx_BlitScreen(); do{ kb_scan_edge(); gfx_SetColor(2); gfx_SetTextScale(1,1); gfx_ScaledTransparentSprite_NoClip(ExitIcon, (LCD_WIDTH - NextLevelIcon_width * 2) / 2 - 66, (LCD_HEIGHT - ExitIcon_height * 2) / 2 - 6, 2, 2); gfx_FillTriangle((LCD_WIDTH) / 2 - 12, (LCD_HEIGHT) / 2 + 46, (LCD_WIDTH) / 2 - 12, (LCD_HEIGHT) / 2 + 70, (LCD_WIDTH) / 2 + 12, LCD_HEIGHT / 2 + 58); gfx_ScaledTransparentSprite_NoClip(SettingsIcon, (LCD_WIDTH - NextLevelIcon_width * 2) / 2 + 66, (LCD_HEIGHT - NextLevelIcon_height * 2) / 2 - 6, 2, 2); gfx_SetColor(11); if(kb_Rising[7] & kb_Left){ gfx_ScaledSprite_NoClip(tileset_tiles[10], 80 + 64, 96, 2, 2); gfx_ScaledSprite_NoClip(tileset_tiles[4], 80 + 32, 96, 2, 2); gfx_VertLine_NoClip(80 + 62, 98, 28); gfx_VertLine_NoClip(80 + 63, 98, 28); gfx_SwapDraw(); gfx_BlitScreen(); delay(80); gfx_ScaledSprite_NoClip(tileset_tiles[5], 80 + 32, 96, 2, 2); gfx_ScaledSprite_NoClip(tileset_tiles[4], 80, 96, 2, 2); gfx_VertLine_NoClip(80 + 30, 98, 28); gfx_VertLine_NoClip(80 + 31, 98, 28); gfx_SwapDraw(); gfx_BlitScreen(); delay(80); return 0; } if(kb_Rising[7] & kb_Down){ gfx_ScaledSprite_NoClip(tileset_tiles[8], 80 + 64, 96, 2, 2); gfx_ScaledSprite_NoClip(tileset_tiles[4], 80 + 64, 96 + 32, 2, 2); gfx_HorizLine_NoClip(80 + 64 + 2, 96 + 32, 28); gfx_HorizLine_NoClip(80 + 64 + 2, 96 + 33, 28); gfx_SwapDraw(); gfx_BlitScreen(); delay(80); gfx_ScaledSprite_NoClip(tileset_tiles[6], 80 + 64, 96 + 32, 2, 2); gfx_ScaledSprite_NoClip(tileset_tiles[4], 80 + 64, 96 + 64, 2, 2); gfx_HorizLine_NoClip(80 + 64 + 2, 96 + 64, 28); gfx_HorizLine_NoClip(80 + 64 + 2, 96 + 65, 28); gfx_SwapDraw(); gfx_BlitScreen(); delay(80); return 1; } if(kb_Rising[7] & kb_Right){ gfx_ScaledSprite_NoClip(tileset_tiles[9], 80 + 64, 96, 2, 2); gfx_ScaledSprite_NoClip(tileset_tiles[4], 80 + 64 + 32, 96, 2, 2); gfx_VertLine_NoClip(80 + 64 + 32, 98, 28); gfx_VertLine_NoClip(80 + 65 + 32, 98, 28); gfx_SwapDraw(); gfx_BlitScreen(); delay(80); gfx_ScaledSprite_NoClip(tileset_tiles[5], 80 + 96, 96, 2, 2); gfx_ScaledSprite_NoClip(tileset_tiles[4], 80 + 128, 96, 2, 2); gfx_VertLine_NoClip(80 + 128, 98, 28); gfx_VertLine_NoClip(80 + 129, 98, 28); gfx_SwapDraw(); gfx_BlitScreen(); delay(80); return 2; } gfx_SwapDraw(); } while(!(kb_Rising[6] & kb_Clear)); return 0; } void runGame(){ ti_var_t file; int selectedWorld = 1, selectedLevel = 1; if(!levelSelect(&selectedLevel, &selectedWorld)){ return; } loadLevel(selectedWorld, selectedLevel); while(true){ kb_scan_edge(); setMoveDir(&snake1); if(kb_Rising[7]){ if(level.twoSnakes) setMoveDir(&snake2); snake1.canMove = snake2.canMove = true; snake1.canMove = checkIfCanMove(&snake1, gfx_GetTileMapped(&tilemap, snake1.col + (snake1.moveDir == 1) - (snake1.moveDir == 3), snake1.row - (snake1.moveDir == 0) + (snake1.moveDir == 2))); snake2.canMove = checkIfCanMove(&snake2, gfx_GetTileMapped(&tilemap, snake2.col + (snake2.moveDir == 1) - (snake2.moveDir == 3), snake2.row - (snake2.moveDir == 0) + (snake2.moveDir == 2))); while(snake1.canMove || (level.twoSnakes && snake2.canMove)){ if(snake1.canMove){ moveUntilStopped(&snake1); if((snake1.col == 0 && snake1.moveDir == 3) || (snake1.col == 6 && snake1.moveDir == 1) || (snake1.row == 0 && snake1.moveDir == 0) || (snake1.row == 6 && snake1.moveDir == 2)){ snake1.canMove = false; }else{ snake1.canMove = checkIfCanMove(&snake1, gfx_GetTileMapped(&tilemap, snake1.col + (snake1.moveDir == 1) - (snake1.moveDir == 3), snake1.row - (snake1.moveDir == 0) + (snake1.moveDir == 2))); } } if(level.twoSnakes && snake2.canMove){ moveUntilStopped(&snake2); snake2.canMove = checkIfCanMove(&snake2, gfx_GetTileMapped(&tilemap, snake2.col + (snake2.moveDir == 1) - (snake2.moveDir == 3), snake2.row - (snake2.moveDir == 0) + (snake2.moveDir == 2))); } render(selectedWorld, selectedLevel); } } render(selectedWorld, selectedLevel); if(!level.toFill) { int action = levelTransition(true, selectedWorld, selectedLevel), temp = 0, i; for(i = 1; i < selectedWorld; i++){ temp += levelsPerWorld[i - 1]; } temp += selectedLevel; file = ti_Open("MDLVLS","r+"); ti_Seek(temp * 50, 0, file); ti_Write((bool*) 1, sizeof(bool), 1, file); ti_Close(file); switch (action){ case 0: return; case 1: if(!levelSelect(&selectedLevel, &selectedWorld)){ return; } selectedLevel--; case 2: selectedLevel++; if(selectedLevel > levelsPerWorld[selectedWorld - 1]){ selectedWorld++; selectedLevel = 1; } loadLevel(selectedWorld, selectedLevel); // switch(selectedWorld){ // case 1: // gfx_SetPalette(world1_palette, sizeof_world1_palette, 0); // break; // case 2: // gfx_SetPalette(world2_palette, sizeof_world2_palette, 0); // break; // } // break; } } if(kb_Rising[6] & kb_Clear){ int action; action = levelTransition(false, selectedWorld, selectedLevel); switch(action){ case 0: if(!levelSelect(&selectedLevel, &selectedWorld)){ return; } case 1: loadLevel(selectedWorld, selectedLevel); break; case 2: break; } } } }