From f6f8ee1ac8a8e2b16022a5f02731fdf43d879ae2 Mon Sep 17 00:00:00 2001 From: Ryan Tse Date: Wed, 9 Mar 2016 20:44:11 -0800 Subject: [PATCH] Complete rewrite of grid level data loading and introduce a mega-awesome game level editor. --- assets/levels/level1.data | Bin 0 -> 700 bytes assets/levels/level2.data | Bin 0 -> 700 bytes assets/levels/level3.data | Bin 0 -> 700 bytes assets/levels/level4.data | Bin 0 -> 700 bytes assets/levels/level5.data | Bin 0 -> 700 bytes build.xml | 8 + .../ucsb/cs56/projects/games/pacman/Grid.java | 224 ++++-------------- .../pacman/editor/GridDataConversion.java | 66 ++++++ .../pacman/editor/PacManLevelEditor.java | 29 ++- 9 files changed, 137 insertions(+), 190 deletions(-) create mode 100644 assets/levels/level1.data create mode 100644 assets/levels/level2.data create mode 100644 assets/levels/level3.data create mode 100644 assets/levels/level4.data create mode 100644 assets/levels/level5.data create mode 100644 src/edu/ucsb/cs56/projects/games/pacman/editor/GridDataConversion.java diff --git a/assets/levels/level1.data b/assets/levels/level1.data new file mode 100644 index 0000000000000000000000000000000000000000..f9f8b3ac7ed674e8f6c13ad3d1841db3e866641d GIT binary patch literal 700 zcma)3u}Z{15PdEvXl0VStt?EVu}yuUpwsB`Qr1A6tas=J#7wqsIneR{4)(W$Y7$3@UguC>J0< u=#;0-gA%=bOEPT{_1U>Hp5?KRJ)Q>z;UAcPBYZFH!r-aoI`MJ;AL|eD!8YIk literal 0 HcmV?d00001 diff --git a/assets/levels/level2.data b/assets/levels/level2.data new file mode 100644 index 0000000000000000000000000000000000000000..14bd7a2d97296108851a2cae86e2e0e90283c35c GIT binary patch literal 700 zcmZ`$yH3ME5F9Ll1YH6~*h)i>u0g;LAR%hX2%*EaLLwzteh7Mgf{&n}<7+4>5j_<< zx7pY(@X2@kntjZ^ePg?>a8l&0YqR>^W%b#)E33yvku|lO`DIbN(q~J*a#z(nzx0g< zPOFR0kJkv%HTGs=y?dJH&BGMCLCGbiK{UO2E&+_%it^8k+xfbE|BV2ew~n!oRL=OH zKgA)E;G0O2M6_7mSWv1HM@b5Zk6*lwU>qdwAiTWdkSWEH9J`6uNOsZ1Za_g8i7yS3 zWu>misv05*v-C3&JzjUIQ96Sm*qQ!Ai_Wdwa7L{S$;O(d^|6%6C)u!RlmTO>vv)Y< v#dzr3HIHTLYg(!6O7k1r^xCeh)8SqiL60)4Yz-sKLl(F#-jlFFy*IW$@d`Hh literal 0 HcmV?d00001 diff --git a/assets/levels/level3.data b/assets/levels/level3.data new file mode 100644 index 0000000000000000000000000000000000000000..882ae210e638e5e047aca8b0daa3b9bcb4cb0dcf GIT binary patch literal 700 zcmY+ByGjF55QfJE1+B7fB5p`yFQ*ap1q89S3=-De5F;X_#1`81Z6VsGVq zXELa>oINxD{Bu1opR(CDayYO0(3kCXDBI(cP&fCt^RjEhqFBw_P#5K@xC`gavN|ie zLgc79eSdpSB)O2Cg`%^EW!2qGWjh*`aym(-myflGqAXxi)yo&4Q&9qNEh{vO+ ziqkbHpUpum2an@aG6pOc%QX_ZL`)-=9dOWzMCf<8-rH}dwkw+kmQ^tg%f!(v?iG}o ztW%o-hI>;P#9DcrA=3`XKhWy1{Il#fgX`|gBbN=?JCfU8A^o!n@{g3mD_8`zEF(KH Od=mF#ua&Pc_5A?g$~n#e literal 0 HcmV?d00001 diff --git a/assets/levels/level4.data b/assets/levels/level4.data new file mode 100644 index 0000000000000000000000000000000000000000..238b547ec5f6430178f5a931b28b0f170793b231 GIT binary patch literal 700 zcmZ4UmVvdnh(RYcrBtspxj0ELx!BZ9ub?PDD>b>KST8*>H?>%=ATc>NF;CCEC^N++ zu_Td!!MMo$?)Di>3{0L3TCkT9bngD`^-UMRpI$RNNVffWh>Wk67nK?G<8HUQEKQ6YfC7Kr@7*n;dY zggcP^B8ucGgkL}^VBrSyml#kCRRxR(GZ~}{CI$&HNwiRv!B7Dd00RLS14tvxfY~j? mAO;L&n2Usb>KST8*>H?>%=ATc>NF;CCEC^N++ zu_Td!!MMo$?)Di>3{0L3TCkT9bnP>L8J%pimXiXmwgWDvjtMX;!lU_d|t222&mB0z@|R3XVAgT)q@ zdy(A;QHJhPRQo_`B!MnO^$o-{RI`xHfvHC|3!wre4&gymK*CcBXbUJT7=Re$2V@l> mHb|`~5W`5^DujWmA+7_t9by{9A0TsOffyu?98MrpsR97o$1(r_ literal 0 HcmV?d00001 diff --git a/build.xml b/build.xml index 74de4cd..5cc4ba5 100644 --- a/build.xml +++ b/build.xml @@ -38,6 +38,14 @@ + + + + + + + + diff --git a/src/edu/ucsb/cs56/projects/games/pacman/Grid.java b/src/edu/ucsb/cs56/projects/games/pacman/Grid.java index c89a7a4..4f9ceb9 100644 --- a/src/edu/ucsb/cs56/projects/games/pacman/Grid.java +++ b/src/edu/ucsb/cs56/projects/games/pacman/Grid.java @@ -2,6 +2,7 @@ import java.awt.*; import java.util.Arrays; +import java.io.*; /** * Class representing the map layout @@ -15,114 +16,15 @@ public class Grid public int fruitCounter = 0; public int x; public int y; - final int MAX_LEVEL = 5; + /* check this link to implement the ghost AI movement at intersection. Revise the level 1 data to classic pacman for intersection detection http://gameinternals.com/post/2072558330/understanding-pac-man-ghost-behavior */ - final short leveldata1[][] = new short[][]{ - {19, 26, 26, 18, 26, 26, 26, 22, 0, 19, 26, 26, 26, 18, 26, 26, 22}, - {21, 0, 0, 21, 0, 0, 0, 21, 0, 21, 0, 0, 0, 21, 0, 0, 21}, - {17, 26, 26, 16, 26, 18, 26, 24, 26, 24, 26, 18, 26, 16, 26, 26, 20}, - {25, 26, 26, 20, 0, 25, 26, 22, 0, 19, 26, 28, 0, 17, 26, 26, 28}, - { 0, 0, 0, 21, 0, 0, 0, 21, 0, 21, 0, 0, 0, 21, 0, 0, 0}, - { 0, 0, 0, 21, 0, 19, 26, 24, 26, 24, 26, 22, 0, 21, 0, 0, 0}, - {26, 26, 26, 16, 26, 20, 0, 0, 0, 0, 0, 17, 26, 16, 26, 26, 26}, - { 0, 0, 0, 21, 0, 17, 26, 26, 26, 26, 26, 20, 0, 21, 0, 0, 0}, - { 0, 0, 0, 21, 0, 21, 0, 0, 0, 0, 0, 21, 0, 21, 0, 0, 0}, - {19, 26, 26, 16, 26, 24, 26, 22, 0, 19, 26, 24, 26, 16, 26, 26, 22}, - {21, 0, 0, 21, 0, 0, 0, 21, 0, 21, 0, 0, 0, 21, 0, 0, 21}, - {25, 22, 0, 21, 0, 0, 0, 17, 2, 20, 0, 0, 0, 21, 0, 19, 28}, // "2" in this line stands for where the pacman spawn - { 0, 21, 0, 17, 26, 26, 18, 24, 24, 24, 18, 26, 26, 20, 0, 21, 0}, - {19, 24, 26, 28, 0, 0, 25, 18, 26, 18, 28, 0, 0, 25, 26, 24, 22}, - {21, 0, 0, 0, 0, 0, 0, 21, 0, 21, 0, 0, 0, 0, 0, 0, 21}, - {25, 26, 26, 26, 26, 26, 26, 24, 26, 24, 26, 26, 26, 26, 26, 26, 28}, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - }; - - final short leveldata2[][] = new short[][]{ - {19, 26, 26, 18, 26, 18, 26, 18, 26, 18, 26, 18, 26, 18, 26, 26, 22}, //1 - {25, 26, 18, 28, 0, 25, 22, 21, 0, 21, 19, 28, 0, 25, 18, 26, 28}, //2 - { 0, 0, 17, 22, 0, 0, 21, 21, 0, 21, 21, 0, 0, 19, 20, 0, 0}, //3 - { 0, 0, 21, 25, 26, 26, 24, 24, 26, 24, 24, 26, 26, 28, 21, 0, 0}, //4 - {26, 26, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 26, 26}, //5 - { 0, 0, 21, 0, 0, 19, 26, 26, 26, 26, 26, 22, 0, 0, 21, 0, 0}, //6 - { 0, 0, 25, 18, 26, 20, 0, 0, 0, 0, 0, 17, 26, 18, 28, 0, 0}, //7 - { 0, 0, 0, 21, 0, 21, 0, 0, 0, 0, 0, 21, 0, 21, 0, 0, 0}, //8 - { 0, 0, 0, 21, 0, 25, 26, 18, 26, 18, 26, 28, 0, 21, 0, 0, 0}, //9 - {26, 22, 0, 21, 0, 0, 0, 21, 0, 21, 0, 0, 0, 21, 0, 19, 26}, //10 - { 0, 17, 26, 20, 0, 0, 19, 20, 0, 17, 22, 0, 0, 17, 26, 20, 0}, //11 - { 0, 21, 0, 21, 0, 0, 21, 25, 26, 28, 21, 0, 0, 21, 0, 21, 0}, //12 - { 0, 21, 0, 17, 26, 26, 20, 0, 0, 0, 17, 26, 26, 20, 0, 21, 0}, //13 - {19, 28, 0, 21, 0, 0, 17, 22, 0, 19, 20, 0, 0, 21, 0, 25, 22}, //14 - {21, 0, 0, 21, 0, 0, 21, 21, 0, 21, 21, 0, 0, 21, 0, 0, 21}, //15 - {17, 26, 26, 20, 0, 0, 21, 21, 0, 21, 21, 0, 0, 17, 26, 26, 20}, //16 - {25, 26, 26, 24, 26, 26, 28, 25, 26, 28, 25, 26, 26, 24, 26, 26, 28} //17 - - }; - - final short leveldata3[][] = new short[][]{ - {19, 26, 26, 18, 26, 22, 0, 19, 26, 22, 0, 19, 26, 18, 26, 26, 22}, //1 - {21, 0, 0, 21, 0, 17, 26, 20, 0, 17, 26, 20, 0, 21, 0, 0, 21}, //2 - {21, 0, 0, 21, 0, 21, 0, 17, 26, 20, 0, 21, 0, 21, 0, 0, 21}, //3 - {25, 26, 26, 20, 0, 21, 0, 21, 0, 21, 0, 21, 0, 17, 26, 26, 28}, //4 - { 0, 0, 0, 17, 26, 24, 26, 20, 0, 17, 26, 24, 26, 20, 0, 0, 0}, //5 - { 0, 19, 18, 20, 0, 0, 0, 21, 0, 21, 0, 0, 0, 17, 18, 22, 0}, //6 - { 0, 21, 21, 21, 0, 19, 26, 24, 26, 24, 26, 22, 0, 21, 21, 21, 0}, //7 - {26, 28, 21, 21, 0, 21, 0, 0, 0, 0, 0, 21, 0, 21, 21, 25, 26}, //8 - { 0, 0, 21, 25, 26, 24, 26, 18, 26, 18, 26, 24, 26, 28, 21, 0, 0}, //9 - {26, 26, 20, 19, 26, 26, 26, 20, 0, 17, 26, 26, 26, 22, 17, 26, 26}, //10 - { 0, 0, 21, 21, 0, 0, 0, 21, 0, 21, 0, 0, 0, 21, 21, 0, 0}, //11 - {19, 22, 21, 21, 0, 19, 18, 24, 26, 24, 18, 22, 0, 21, 21, 19, 22}, //12 - {21, 21, 17, 16, 26, 20, 25, 22, 0, 19, 28, 17, 26, 16, 20, 21, 21}, //13 - {21, 21, 21, 21, 0, 21, 0, 21, 0, 21, 0, 21, 0, 21, 21, 21, 21}, //14 - {21, 25, 20, 21, 0, 17, 22, 21, 0, 21, 19, 20, 0, 21, 17, 28, 21}, //15 - {21, 0, 21, 25, 22, 21, 17, 28, 0, 25, 20, 21, 19, 28, 21, 0, 21}, //16 - {25, 26, 28, 0, 25, 28, 25, 26, 26, 26, 28, 25, 28, 0, 25, 26, 28} //17 - }; - - final short leveldata4[][] = new short[][]{ - {19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 16}, - {17, 16, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 16, 16, 16, 16}, - {17, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 16, 16, 16}, - {17, 16, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 16, 16, 16, 16}, - {17, 16, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 16, 16, 16, 16}, - {17, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 17, 16, 16, 16}, - {17, 16, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 16, 16, 16, 16}, - {17, 16, 24, 24, 24, 24, 24, 16, 24, 24, 24, 24, 24, 16, 16, 16, 16}, - {17, 20, 0, 0, 0, 0, 0, 21, 0, 0, 0, 0, 0, 17, 16, 16, 16}, - {17, 20, 0, 19, 18, 18, 18, 16, 18, 18, 18, 22, 0, 17, 16, 16, 16}, - {17, 20, 0, 17, 16, 16, 16, 16, 16, 16, 16, 20, 0, 17, 16, 16, 16}, - {17, 20, 0, 25, 24, 24, 24, 24, 24, 24, 24, 28, 0, 17, 16, 16, 16}, - {17, 20, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 17, 16, 16, 16}, - {17, 16, 18, 18, 18, 18, 22, 16, 19, 18, 18, 18, 18, 16, 16, 16, 16}, - {25, 24, 24, 24, 24, 24, 28, 16, 25, 24, 24, 24, 24, 24, 24, 24, 16}, - {18, 18, 18, 18, 18, 18, 18, 16, 18, 18, 18, 18, 18, 18, 18, 18, 16}, - {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16} - }; - - final short leveldata5[][] = new short[][]{ - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - { 0, 0, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 22, 0, 0, 0}, - { 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 0, 0, 0}, - { 0, 0, 17, 16, 16, 16, 24, 24, 24, 24, 24, 24, 16, 20, 0, 0, 0}, - { 0, 0, 17, 16, 16, 20, 0, 0, 0, 0, 0, 0, 17, 20, 0, 0, 0}, - { 0, 0, 17, 16, 16, 20, 0, 0, 0, 0, 0, 0, 25, 28, 0, 0, 0}, - { 0, 0, 17, 16, 16, 16, 18, 18, 18, 18, 22, 0, 0, 0, 0, 0, 0}, - { 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 20, 0, 0, 0, 0, 0, 0}, - { 0, 0, 25, 24, 16, 16, 16, 16, 16, 16, 16, 18, 18, 18, 22, 0, 0}, - { 0, 0, 0, 0, 17, 16, 16, 16, 16, 16, 24, 16, 16, 16, 20, 0, 0}, - { 0, 0, 0, 0, 17, 16, 16, 16, 16, 20, 0, 17, 16, 16, 20, 0, 0}, - {19, 18, 18, 26, 16, 24, 24, 0, 24, 28, 0, 17, 16, 16, 20, 0, 0}, - {17, 16, 20, 0, 21, 0, 0, 21, 0, 0, 0, 17, 16, 16, 20, 0, 0}, - {17, 16, 20, 0, 21, 0, 19, 16, 22, 0, 0, 25, 24, 24, 28, 0, 0}, - {25, 24, 28, 0, 29, 0, 25, 24, 28, 0, 0, 0, 0, 0, 0, 0, 0}, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, - { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} - }; short[][] screenData; + short[][][] levelsData; Color mazeColor, dotColor, fruitColor; /** @@ -133,6 +35,29 @@ public Grid() { mazeColor = new Color(5, 100, 5); dotColor = new Color(192, 192, 0); fruitColor = new Color(255,0,0); + + String[] loadableLevels = {"level1.data", "level2.data", "level3.data", "level4.data", "level5.data"}; + this.levelsData = new short[loadableLevels.length][1][1]; + for(int i = 0; i < loadableLevels.length; i++) { + GridData level = loadLevel("assets/levels/"+loadableLevels[i]); + levelsData[i] = level.get2DGridData(); + } + } + + public GridData loadLevel(String asset_path) { + try { + InputStream input_stream = getClass().getResourceAsStream(asset_path); + //System.out.println(input_stream); + ObjectInputStream object_input_stream = new ObjectInputStream(input_stream); + GridData data = (GridData)object_input_stream.readObject(); + return data; + } catch (Exception e) { + e.printStackTrace(); + System.out.println(e); + System.out.println("Failed to load level data assets."); + System.exit(2); + } + return null; } /** @@ -144,7 +69,7 @@ public Grid() { public boolean checkMaze() { for (int i = 0; i < Board.NUMBLOCKS; i++) { for (int j = 0; j < Board.NUMBLOCKS; j++) { - if ((screenData[i][j] & 16) != 0) + if ((screenData[i][j] & GridData.GRID_CELL_PELLET) != 0) return false; } } @@ -160,7 +85,7 @@ public int getPelletNum() { int numOfPellet = 0; for (int i = 0; i < Board.NUMBLOCKS; i++) { for (int j = 0; j < Board.NUMBLOCKS; j++) { - if ((screenData[i][j] & 16) != 0) + if ((screenData[i][j] & GridData.GRID_CELL_PELLET) != 0) numOfPellet++; } } @@ -176,25 +101,8 @@ public int getPelletNumForMap(int numBoardsCleared) { int numOfPellet = 0; for (int i = 0; i < Board.NUMBLOCKS; i++) { for (int j = 0; j < Board.NUMBLOCKS; j++) { - if (numBoardsCleared % MAX_LEVEL == 0){ - if ((leveldata1[i][j] & 16) != 0) - numOfPellet++; - } - else if (numBoardsCleared % MAX_LEVEL == 1) { - if ((leveldata2[i][j] & 16) != 0) - numOfPellet++; - } - else if (numBoardsCleared % MAX_LEVEL == 2) { - if ((leveldata3[i][j] & 16) != 0) - numOfPellet++; - } - else if (numBoardsCleared % MAX_LEVEL == 3) { - if ((leveldata4[i][j] & 16) != 0) - numOfPellet++; - } - else if (numBoardsCleared % MAX_LEVEL == 4){ - if ((leveldata5[i][j] & 16) != 0) - numOfPellet++; + if((this.levelsData[numBoardsCleared % this.levelsData.length][i][j] & GridData.GRID_CELL_PELLET) != 0) { + numOfPellet++; } } } @@ -206,16 +114,7 @@ else if (numBoardsCleared % MAX_LEVEL == 4){ */ public void levelInit(int numBoardsCleared) { for (int i = 0; i < Board.NUMBLOCKS; i++) { - if (numBoardsCleared % 3 == 0) - screenData[i] = Arrays.copyOf(leveldata1[i], Board.NUMBLOCKS); - else if (numBoardsCleared % MAX_LEVEL == 1) - screenData[i] = Arrays.copyOf(leveldata2[i], Board.NUMBLOCKS); - else if (numBoardsCleared % MAX_LEVEL == 2) - screenData[i] = Arrays.copyOf(leveldata3[i], Board.NUMBLOCKS); - else if (numBoardsCleared % MAX_LEVEL == 3) - screenData[i] = Arrays.copyOf(leveldata4[i], Board.NUMBLOCKS); - else if (numBoardsCleared % MAX_LEVEL == 4) - screenData[i] = Arrays.copyOf(leveldata5[i], Board.NUMBLOCKS); + screenData[i] = Arrays.copyOf(this.levelsData[numBoardsCleared % this.levelsData.length][i], Board.NUMBLOCKS); } } @@ -258,60 +157,19 @@ public void incrementFruit(int numBoardsCleared) { if (fruitCounter > 100) { fruitCounter = 0; this.randomBlock(); - if (numBoardsCleared % MAX_LEVEL == 0) { - while (true) { - { - if (((screenData[this.x][this.y] & 16) == 0) && (leveldata1[this.x][this.y] & 16) != 0) { - screenData[this.x][this.y] = (short) (screenData[this.x][this.y] | 32); - break; - } - this.randomBlock(); - } - } - } else if (numBoardsCleared % MAX_LEVEL == 1) - while (true) { - { - if (((screenData[this.x][this.y] & 16) == 0) && (leveldata2[this.x][this.y] & 16) != 0) { - screenData[this.x][this.y] = (short) (screenData[this.x][this.y] | 32); - break; - } - this.randomBlock(); - } - } - else if (numBoardsCleared % MAX_LEVEL == 2) - while (true) { - { - if (((screenData[this.x][this.y] & 16) == 0) && (leveldata3[this.x][this.y] & 16) != 0) { - screenData[this.x][this.y] = (short) (screenData[this.x][this.y] | 32); - break; - } - this.randomBlock(); - } + while(true) { + if (((screenData[this.x][this.y] & GridData.GRID_CELL_PELLET) == 0) && (this.levelsData[numBoardsCleared % this.levelsData.length][this.x][this.y] & GridData.GRID_CELL_PELLET) != 0) { + screenData[this.x][this.y] = (short) (screenData[this.x][this.y] | GridData.GRID_CELL_FRUIT); + break; } - else if (numBoardsCleared % MAX_LEVEL == 3) - while (true) { - { - if (((screenData[this.x][this.y] & 16) == 0) && (leveldata4[this.x][this.y] & 16) != 0) { - screenData[this.x][this.y] = (short) (screenData[this.x][this.y] | 32); - break; - } - this.randomBlock(); - } - } - else if (numBoardsCleared % MAX_LEVEL == 4) - while (true) { - { - if (((screenData[this.x][this.y] & 16) == 0) && (leveldata5[this.x][this.y] & 16) != 0) { - screenData[this.x][this.y] = (short) (screenData[this.x][this.y] | 32); - break; - } - this.randomBlock(); - } - } - } else + this.randomBlock(); + } + } else { fruitCounter++; - }else + } + }else { return; + } } diff --git a/src/edu/ucsb/cs56/projects/games/pacman/editor/GridDataConversion.java b/src/edu/ucsb/cs56/projects/games/pacman/editor/GridDataConversion.java new file mode 100644 index 0000000..5be0719 --- /dev/null +++ b/src/edu/ucsb/cs56/projects/games/pacman/editor/GridDataConversion.java @@ -0,0 +1,66 @@ +package edu.ucsb.cs56.projects.games.pacman.editor; + +import java.io.*; +import java.util.*; +import edu.ucsb.cs56.projects.games.pacman.GridData; + +/* + This file implements a method of converting the old 2D array of level grid data + into a serialized file of GridData. This file is left here for historical reasons + as this system is no longer used and is succeeded by the level editor. + */ + +public class GridDataConversion { + public static void main(String[] args) { + short convertData[][] = new short[][]{ + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { 0, 0, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 22, 0, 0, 0}, + { 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 20, 0, 0, 0}, + { 0, 0, 17, 16, 16, 16, 24, 24, 24, 24, 24, 24, 16, 20, 0, 0, 0}, + { 0, 0, 17, 16, 16, 20, 0, 0, 0, 0, 0, 0, 17, 20, 0, 0, 0}, + { 0, 0, 17, 16, 16, 20, 0, 0, 0, 0, 0, 0, 25, 28, 0, 0, 0}, + { 0, 0, 17, 16, 16, 16, 18, 18, 18, 18, 22, 0, 0, 0, 0, 0, 0}, + { 0, 0, 17, 16, 16, 16, 16, 16, 16, 16, 20, 0, 0, 0, 0, 0, 0}, + { 0, 0, 25, 24, 16, 16, 16, 16, 16, 16, 16, 18, 18, 18, 22, 0, 0}, + { 0, 0, 0, 0, 17, 16, 16, 16, 16, 16, 24, 16, 16, 16, 20, 0, 0}, + { 0, 0, 0, 0, 17, 16, 16, 16, 16, 20, 0, 17, 16, 16, 20, 0, 0}, + {19, 18, 18, 26, 16, 24, 24, 0, 24, 28, 0, 17, 16, 16, 20, 0, 0}, + {17, 16, 20, 0, 21, 0, 0, 21, 0, 0, 0, 17, 16, 16, 20, 0, 0}, + {17, 16, 20, 0, 21, 0, 19, 16, 22, 0, 0, 25, 24, 24, 28, 0, 0}, + {25, 24, 28, 0, 29, 0, 25, 24, 28, 0, 0, 0, 0, 0, 0, 0, 0}, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} + }; + + // Determine the size of the grid. + int grid_width = convertData[0].length; + int grid_height = convertData.length; + + // Currently, the code presumes that the width and length + // of the board are the same. + if(grid_height != grid_width) { + System.out.println("Abort. Unsupported grid dimensions."); + System.exit(1); + } + + int grid_data_size = grid_width*grid_height; + + // Flatten the data into a 1D array. + short[] grid_data = new short[grid_data_size]; + for(int i = 0; i < grid_data_size; i++) { + grid_data[i] = convertData[i / grid_width][i % grid_width]; + } + + // Store the data into a serializable object. + GridData grid_data_out = new GridData(grid_width, grid_data); + + // Dump the grid data object into file. + try { + FileOutputStream grid_data_out_file = new FileOutputStream("level_out.data"); + ObjectOutputStream grid_data_object_out = new ObjectOutputStream(grid_data_out_file); + grid_data_object_out.writeObject(grid_data_out); + } catch (Exception e) { + System.out.println(e); + } + } +} \ No newline at end of file diff --git a/src/edu/ucsb/cs56/projects/games/pacman/editor/PacManLevelEditor.java b/src/edu/ucsb/cs56/projects/games/pacman/editor/PacManLevelEditor.java index ce1bd36..a9ef968 100644 --- a/src/edu/ucsb/cs56/projects/games/pacman/editor/PacManLevelEditor.java +++ b/src/edu/ucsb/cs56/projects/games/pacman/editor/PacManLevelEditor.java @@ -30,10 +30,12 @@ public class PacManLevelEditor extends JFrame { private short[][] grid_data; private Point current_grid_selection; private String save_path; + private boolean level_edited; public PacManLevelEditor() { initComponents(); this.save_path = ""; + this.level_edited = false; } public static void main(String[] args) { @@ -49,6 +51,8 @@ public void newLevel() { this.grid_data = new short[17][17]; this.panel_grid_display.updateGrid(this.grid_data); this.panel_grid_display.repaint(); + this.menu_file_save.setEnabled(true); + this.menu_file_save_as.setEnabled(true); } public void loadLevel() { @@ -67,13 +71,8 @@ public void loadLevel() { this.panel_grid_display.repaint(); this.save_path = selectedFile.getAbsolutePath(); - - for(int i = 0; i < this.grid_data.length; i++) { - for(int j = 0; j < this.grid_data[i].length; j++) { - System.out.print(" " + this.grid_data[i][j]); - } - System.out.print("\n"); - } + this.menu_file_save.setEnabled(true); + this.menu_file_save_as.setEnabled(true); } catch (Exception e) { e.printStackTrace(); System.out.println(e); @@ -114,6 +113,7 @@ public boolean saveLevel() { FileOutputStream grid_data_out_file = new FileOutputStream(selectedFile); ObjectOutputStream grid_data_object_out = new ObjectOutputStream(grid_data_out_file); grid_data_object_out.writeObject(grid_data_out); + this.level_edited = false; } catch (Exception e) { e.printStackTrace(); System.out.println(e); @@ -205,10 +205,12 @@ private void initComponents() { menu_file.addSeparator(); menu_file_save.setText("Save"); + menu_file_save.setEnabled(false); menu_file_save.addActionListener(menu_listener); menu_file.add(menu_file_save); menu_file_save_as.setText("Save As..."); + menu_file_save_as.setEnabled(false); menu_file_save_as.addActionListener(menu_listener); menu_file.add(menu_file_save_as); @@ -348,8 +350,20 @@ class MenuActionListener implements ActionListener { public void actionPerformed(ActionEvent e) { PacManLevelEditor parent = PacManLevelEditor.this; if(e.getSource() == parent.menu_file_new) { + if(parent.level_edited) { + int response = JOptionPane.showConfirmDialog(parent, "There are unsaved edits to this file. Are you sure you wish to discard changes?", "Unsaved Changes", JOptionPane.YES_NO_OPTION); + if(response != JOptionPane.YES_OPTION) { + return; + } + } parent.newLevel(); } else if(e.getSource() == parent.menu_file_load) { + if(parent.level_edited) { + int response = JOptionPane.showConfirmDialog(parent, "There are unsaved edits to this file. Are you sure you wish to discard changes?", "Unsaved Changes", JOptionPane.YES_NO_OPTION); + if(response != JOptionPane.YES_OPTION) { + return; + } + } parent.loadLevel(); } else if(e.getSource() == parent.menu_file_save) { parent.saveLevel(); @@ -367,6 +381,7 @@ public void actionPerformed(ActionEvent e) { for(int i = 0; i < buttons.length; i++) { if(e.getSource() == buttons[i]) { + parent.level_edited = true; if(buttons[i].isSelected()) { parent.grid_data[parent.current_grid_selection.y][parent.current_grid_selection.x] |= bits[i]; } else {