Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Agzam4 authored Nov 18, 2021
1 parent ce2b396 commit 280604c
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 49 deletions.
Binary file added The Light2++ v1.2.0.jar
Binary file not shown.
99 changes: 64 additions & 35 deletions src/JLight2.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.UIDefaults;
Expand All @@ -20,10 +21,13 @@
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFileChooser;

import game.JBlocksBox;
import javax.swing.JSpinner;
Expand Down Expand Up @@ -79,7 +83,7 @@ public JLight2() {

UIDefaults ui = UIManager.getLookAndFeel().getDefaults();

for ( Object o : ui.keySet() ){
for ( Object o : ui.keySet()) {
System.out.println(o.toString());
}

Expand Down Expand Up @@ -190,15 +194,6 @@ public JLight2() {
game.clear();
});


JMenuItem save = getJMenuItem("Save", KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_DOWN_MASK));
file.add(save);

save.addActionListener(e -> {
save(pathString);
});

JMenuItem saveAs = getJMenuItem("Save As", KeyStroke.getKeyStroke(KeyEvent.VK_S,
InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
file.add(saveAs);
Expand All @@ -212,17 +207,23 @@ public JLight2() {
file.add(open);

open.addActionListener(e -> {
FileDialog fd = new FileDialog(new JFrame());
fd.setVisible(true);
File[] f = fd.getFiles();
if(f.length > 0) {
pathString = fd.getFiles()[0].getAbsolutePath();
game.load(pathString);
try {
pathString = pathString.substring(0, pathString.lastIndexOf('.'));
} catch (StringIndexOutOfBoundsException e2) {
Thread t = new Thread(() -> {
// game.pause();
FileDialog fd = new FileDialog(new JFrame());
fd.setVisible(true);
fd.setFilenameFilter((dir, name) -> name.endsWith(".txt"));
String filename = fd.getFile();
if (filename != null) {
pathString = filename;
game.load(pathString);
try {
pathString = pathString.substring(0, pathString.lastIndexOf('.'));
} catch (StringIndexOutOfBoundsException e2) {
}
}
}
// game.resume();
});
t.start();
});


Expand All @@ -236,6 +237,7 @@ public JLight2() {
game.clear();
});


// ctrl + p

// game.addKeyListener(new KeyListener() {
Expand All @@ -262,37 +264,64 @@ public JLight2() {
// }
// }
// });
file.add(getJSeparator());

JMenuItem saveAndExit = getJMenuItem("Save & Exit", KeyStroke.getKeyStroke(KeyEvent.VK_E,
InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
file.add(saveAndExit);

saveAndExit.addActionListener(e -> {
setVisible(false);
if(pathString == null) {
saveAs(true);
}else {
game.save(getPathString());
System.exit(0);
}
});

Thread title = new Thread(() -> {
while(true) {
setTitle(frametitle + " - " + getPathString() + (game.isSaved() ? "" : "*"));
setTitle(frametitle + " / " + getPathString() + (game.isSaved() ? "" : "*"));
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
});
title.start();
}


private void saveAs() {
game.pause();
FileDialog fd = new FileDialog(new JFrame());
fd.setFile(getPathString());
fd.setMode(FileDialog.SAVE);
fd.setVisible(true);
File[] f = fd.getFiles();
if(f.length > 0) {
pathString = fd.getFiles()[0].getAbsolutePath();
save(pathString);
}

game.resume();
saveAs(true);
}

private void saveAs(boolean exit) {
Thread t = new Thread(() -> {
FileDialog fd = new FileDialog(new JFrame());
fd.setFile(getPathString() + ".game");
fd.setMode(FileDialog.SAVE);
fd.setVisible(true);
File[] f = fd.getFiles();
if(f.length > 0) {
pathString = fd.getFiles()[0].getAbsolutePath();
save(pathString);
}
fd.dispose();
if(exit) {
System.exit(0);
}
});
t.start();
}


private void save(String path) {
if(path == null) {
saveAs();
}else {
game.save(path);
game.save(getPathString());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/JMyPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void go() {
start = System.nanoTime();
update();
draw((Graphics2D) getGraphics());
wait = 100-(System.nanoTime()-start)/1_000_000;
wait = 75-(System.nanoTime()-start)/1_000_000;
if(wait < 1) wait = 1;
try {
Thread.sleep(wait);
Expand Down Expand Up @@ -142,9 +142,9 @@ private void setBlock() {
public void update() {
game.update();
if(isMousePressed) {
if(updateCount%2 == 0) {
// if(updateCount%2 == 0) {
setBlock();
}
// }
}else {
updateCount = 1;
}
Expand Down
18 changes: 13 additions & 5 deletions src/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@

public class Game implements Serializable {

private static final long serialVersionUID = 0L;

public static int BlockSize = 10;
public static double scale = 0.1;
public static transient double scale = 0.1;
boolean isViewMode;

public static final Color WHITE = new Color(255,255,255);
Expand All @@ -46,7 +48,7 @@ public class Game implements Serializable {
public static final Color PINK = new Color(255,0,255);
public static final Color CRIMSON = new Color(255,0,100);

public enum Blocks {
public static enum Blocks {
AIR (new Block(Color.BLACK, true)),
WALL (new Block(BROWN, false)),

Expand Down Expand Up @@ -155,7 +157,7 @@ public static String[] getNames(Class<? extends Enum<?>> e) {


private Block[][] blocks;
private Block[][] newBlocks;
private transient Block[][] newBlocks;

int[][] r,g,b;
int[][] wr,wg,wb; // wires
Expand Down Expand Up @@ -228,6 +230,10 @@ public void update() { // TODO: update
generateCaves();
isNeedCreateCaves = false;
}

if(newBlocks == null) {
newBlocks = new Block[width][height];
}

// blocks -> newBlocks
for (int y = 0; y < height; y++) {
Expand All @@ -247,7 +253,7 @@ public void update() { // TODO: update

if(!isStopped) {
// update water
for (int y = 0; y < height; y++) {
for (int y = height-1; y > -1; y--) {
for (int x = 0; x < width; x++) {
if(!blocks[x][y].isMechanism()) {
blocks[x][y].update(this, x, y);
Expand Down Expand Up @@ -526,12 +532,14 @@ public void valve(int x, int y, int px, int py) {
}
}

public void copyWater(int x, int y, int px, int py) {
public boolean copyWater(int x, int y, int px, int py) {
if(checkMoveBlock(x, y, px, py)) {
if(blocks[x][y].isWater()) {
newBlocks[x+px][y+py] = new Water(blocks[x][y].getLightColor());
return true;
}
}
return false;
}

public void removeWater(int x, int y) {
Expand Down
19 changes: 13 additions & 6 deletions src/mechanism/Generator.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@
public class Generator extends Mechanism {

private static final long serialVersionUID = 7L;
private int updateCount = 0;

@Override
public void update(Game game, int x, int y) {
game.copyWater(x-1,y,2,0);
game.copyWater(x+1,y,-2,0);

game.copyWater(x,y+1,0,-2);
game.copyWater(x,y-1,0,+2);
boolean b = false;
if(updateCount%2 == 0) {
b = game.copyWater(x-1,y,2,0) ||
game.copyWater(x+1,y,-2,0) ||
game.copyWater(x,y+1,0,-2) ||
game.copyWater(x,y-1,0,+2);
updateCount++;
}
if(!b) {
updateCount = 0;
}
super.update(game, x, y);
}

@Override
public void draw(Graphics2D g, Game game, int x, int y) {
g.setColor(game.getLightColor(x, y));
Expand Down

0 comments on commit 280604c

Please sign in to comment.