Skip to content

Commit

Permalink
Complete rewrite of grid level data loading and introduce a mega-awes…
Browse files Browse the repository at this point in the history
…ome game level editor.
  • Loading branch information
ryantse committed Mar 10, 2016
1 parent ac86bc7 commit f6f8ee1
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 190 deletions.
Binary file added assets/levels/level1.data
Binary file not shown.
Binary file added assets/levels/level2.data
Binary file not shown.
Binary file added assets/levels/level3.data
Binary file not shown.
Binary file added assets/levels/level4.data
Binary file not shown.
Binary file added assets/levels/level5.data
Binary file not shown.
8 changes: 8 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
<java classpath="build" classname="${projectPackage}.PacMan" fork="true"/>
</target>

<target name="run-convert" depends="compile" description="run Pacman level conversion">
<java classpath="build" classname="${projectPackage}.editor.GridDataConversion" fork="true"/>
</target>

<target name="run-editor" depends="compile" description="run Pacman lavel editor">
<java classpath="build" classname="${projectPackage}.editor.PacManLevelEditor" fork="true"/>
</target>

<target name="run-args" depends="compile" description="run Pacman game">
<java classpath="build" classname="${projectPackage}.PacMan" fork="true">
<arg value="${arg0}"/>
Expand Down
224 changes: 41 additions & 183 deletions src/edu/ucsb/cs56/projects/games/pacman/Grid.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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() {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand All @@ -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 {
Expand Down

0 comments on commit f6f8ee1

Please sign in to comment.