Skip to content

Commit

Permalink
Clean up Refactoring
Browse files Browse the repository at this point in the history
Code Clean up and refactoring
  • Loading branch information
michailmarkou1995 committed Aug 20, 2021
1 parent fc8e022 commit 44993ae
Show file tree
Hide file tree
Showing 22 changed files with 190 additions and 1,190 deletions.
3 changes: 3 additions & 0 deletions 3DGameProgramming/Minefront/src/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: com.mime.minefront.Display

Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public void render() {
g.drawImage(resource.loadImage("/textures/mini_game.png"), 0, 0, resource.getWidth(), resource.getHeight(), null);
g.fillRect((int) x, (int) y, 32, 32);
g.setFont(new Font("Verdana", 0, 50));
// RGB color check Text Follow Align
if (resource.getColorHex((int) x, (int) y) == 0xFF6A00) {
int xx = (int) x;
int yy = (int) y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.io.*;
import java.util.Properties;

/**
* XML configuration store
*/
public class Configuration {

Properties properties = new Properties();
Expand All @@ -24,7 +27,7 @@ public void saveConfiguration(String key, int value) {
}
}

//define path so many config files in future e.g. load player location not in same as res settings
// define path so many config files in future e.g. load player location not in same as res settings
public void loadConfiguration(String path) {//String key
try {
InputStream read = new FileInputStream(path);
Expand All @@ -45,6 +48,5 @@ public void loadConfiguration(String path) {//String key
public void setResolution(int width, int height) {
Display.WIDTH = width;
Display.HEIGHT = height;

}
}
34 changes: 25 additions & 9 deletions 3DGameProgramming/Minefront/src/com/mime/minefront/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import java.awt.image.DataBufferInt;
import java.io.Serial;

/**
* <h2>Main Game Class Run</h2>
*/
public class Display extends Canvas implements Runnable {
public static final String TITLE = "Minefront Pre-Alpha 0.05";
@Serial
Expand Down Expand Up @@ -125,7 +128,7 @@ public synchronized void stop() {
}
}

/*? used anywhere really?*/
// pause thread for wait
public synchronized void paused() {
try {
wait();
Expand All @@ -139,6 +142,7 @@ public synchronized void paused() {
* <h2>Game Menu Pause Thread to Continue on "esc"</h2>
*/
public synchronized void continued() {
// return the Thread flow back here
notify();
input.key[KeyEvent.VK_ESCAPE] = false;
}
Expand Down Expand Up @@ -216,8 +220,10 @@ public void run() {
}
}

// Display Graphics
public void render() {
if (!PlayerController.not_paused) {
// Image buffer creation "pre-show" in Display
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
Expand All @@ -239,9 +245,8 @@ public void render() {

screen.render(game);

for (int i = 0; i < getGameWidth() * getGameHeight(); i++) {
pixels[i] = screen.PIXELS[i];
}
if (getGameWidth() * getGameHeight() >= 0)
System.arraycopy(screen.PIXELS, 0, pixels, 0, getGameWidth() * getGameHeight());

g = bs.getDrawGraphics();
g.drawImage(img, 0, 0, getGameWidth(), getGameHeight(), null);
Expand All @@ -255,19 +260,25 @@ public void render() {

//update method
private void tick() {

// input keys update
input.tick();
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
for (int i = 0; i < input.key.length; i++) {
if (input.key[i]) {
//System.out.println(i); System.out.println(Arrays.toString(input.key));
}
}

// for (int i = 0; i < input.key.length; i++) {
// if (input.key[i]) {
// //System.out.println(i); System.out.println(Arrays.toString(input.key));
// }
// }

// update logic method
game.tick();

// Update Mouse Position Coordinates (translate X, Y)
int newX = InputHandler.MouseX;
int newY = InputHandler.MouseY;
int winX = InputHandler.WindowX;
Expand All @@ -287,6 +298,7 @@ deltaY is (oldY - newY) at the power of 2
your mouseMotionListener can also store the time in milli if you want a really exact "speed"
*/
// Mouse Speed calc #1
double mouseSpeedX_temp = Math.abs((oldX - newX)); /*Vector X then SQRT this !*/
double mouseSpeedY_temp = Math.abs((oldY - newY));/*Vector Y then SQRT this !*/
mouseSpeed = (int) Math.sqrt(mouseSpeedX_temp * mouseSpeedX_temp + mouseSpeedY_temp * mouseSpeedY_temp);
Expand All @@ -298,13 +310,15 @@ deltaY is (oldY - newY) at the power of 2
e.printStackTrace();
}

// Window Loop Mouse over
if (newX < 0 || newX > WIDTH) robot.mouseMove(winX + 500, winY + 500);
if (newY < 0 || newY > HEIGHT) robot.mouseMove(winX + 500, winY + 500);
if (newX < 0) robot.mouseMove(winX + getGameWidth() - 20, winY + newY);
if (newX >= getGameWidth() - 20) robot.mouseMove(winX + 20, winY + newY);
if (newY < 15) robot.mouseMove(winX + newX, winY + getGameHeight() - 20);
if (newY >= getGameHeight() - 60) robot.mouseMove(winX + newX, winY + 40);

// Player UP and Down Rotation till a "Natural human neck" degree
if (newY < oldY && PlayerController.rotationUp <= 2.8) {
PlayerController.turnUpM = true;
}
Expand All @@ -322,6 +336,7 @@ deltaY is (oldY - newY) at the power of 2
PlayerController.turnDownM = false;
}

// Turn left and Right Camera First Person
if (newX > oldX) {
PlayerController.turnRightM = true;
}
Expand All @@ -333,6 +348,7 @@ deltaY is (oldY - newY) at the power of 2
PlayerController.turnLeftM = true;
}

// Mouse Speed calc #2 just testing
double mouseSpeedX_temp = Math.abs((oldX - newX));
double mouseSpeedY_temp = Math.abs((oldY - newY));
MouseSpeed = (int) Math.sqrt(mouseSpeedX_temp * mouseSpeedX_temp + mouseSpeedY_temp * mouseSpeedY_temp);
Expand Down

This file was deleted.

20 changes: 10 additions & 10 deletions 3DGameProgramming/Minefront/src/com/mime/minefront/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,28 @@
import com.mime.minefront.input.PlayerController;
import com.mime.minefront.level.Level;

/**
* <p>Main Game Logic Setup</p>
*/
public class Game {

public int time;
public PlayerController controls; /* controls == PLAYER kinda*/
//public PlayerController controls_Player2;/**use ArrayList instead of creating new Players./mobs */
/*public PlayerController[] players;*///if dies remove from array for memory save //you cant do it this way because array is not DYNAMIC no index out of bounds
//public PlayerController controls_Player2; /**use ArrayList instead of creating new Players./mobs */
/*public PlayerController[] players;*/ // if dies remove from array for memory save // you cant do it this way because array is not DYNAMIC no index out of bounds
public Level level;
public boolean jumpIf;
ThreadCheck threadCheck;
ThreadTest tt1;
HealthRecoveryThread tt1;

public Game(InputHandler input) {
controls = new PlayerController(input);
level = new Level(16, 16);
level.addEntity(controls);//which is player !
threadCheck = new ThreadCheck();
level.addEntity(controls); // which is player !

tt1 = new ThreadTest();
tt1 = new HealthRecoveryThread();
}

public void tick() {//boolean[] key
public void tick() {
time++;
level.update();//Old Player.tick
level.update(); // Old Player.tick
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.mime.minefront;

public class ThreadTest extends Thread {//extends Thread implements Runnable
public class HealthRecoveryThread extends Thread {//extends Thread implements Runnable

public final Object lock = this;
public boolean pause = false;
public double health;

// Press H to gradually increase health
public void recoverHealth() {
for (double i = 0; i <= 100; i = i + 0.1) {
health += i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.awt.*;
import java.io.Serial;

/**
* @deprecated Applet usage to run on browser plugin support
*/
public class MinefrontApplet extends Applet {

@Serial
Expand Down
21 changes: 11 additions & 10 deletions 3DGameProgramming/Minefront/src/com/mime/minefront/RunGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

/**
* <p>Main game Setup Window</p>
*/
public class RunGame {

static Display game;
Expand All @@ -17,33 +20,31 @@ public RunGame() {
Cursor blank = Toolkit.getDefaultToolkit().createCustomCursor(cursor, new Point(0, 0), "blank");
//Display game = new Display();
game = new Display();
//JFrame frame = new JFrame();//if here and not in Game class THEN not able to run in on WINDOW FPS LABEL
// JFrame frame = new JFrame(); // if here and not in Game class THEN not able to run in on WINDOW FPS LABEL
// frame = new JFrame();
// frame.add(game);
// frame.setResizable(false);
// frame.setTitle(TITLE);
// frame.pack();
// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// //frame.setSize(WIDTH,HEIGHT);// remove if setPreffered is set on Constructor
// //frame.setSize(WIDTH,HEIGHT); // remove if setPreffered is set on Constructor
// frame.setLocationRelativeTo(null);
// frame.setVisible(true);
game.frame = new JFrame();
game.frame.add(game);
game.frame.setResizable(false);
game.frame.setTitle(Display.TITLE);//if not here delay to appear a little
game.frame.setTitle(Display.TITLE); // if not here delay to appear a little

//game.frame.pack();
game.frame.setSize(Display.getGameWidth(), Display.getGameHeight());

//game.frame.getContentPane().setCursor(blank);

//game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
//frame.setSize(WIDTH,HEIGHT);// remove if setPreffered is set on Constructor
//frame.setSize(WIDTH,HEIGHT); // remove if setPreffered is set on Constructor
game.frame.setLocationRelativeTo(null);
Display.WindowLocation = game.frame.getLocation();
game.frame.addComponentListener(game.input);//new InputHandler()
Expand All @@ -61,16 +62,16 @@ public RunGame(Point WindowLocation) {
game.frame = new JFrame();
game.frame.add(game);
game.frame.setResizable(false);
game.frame.setTitle(Display.TITLE);//if not here delay to appear a little
game.frame.setTitle(Display.TITLE); // if not here delay to appear a little
game.frame.setSize(Display.getGameWidth(), Display.getGameHeight());
game.frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
});
game.frame.setLocationRelativeTo(null);
WindowLocation = game.frame.getLocation();//wrong HERE <--------- Window is from Display.WindowLocation
game.frame.addComponentListener(game.input);//new InputHandler()
WindowLocation = game.frame.getLocation();
game.frame.addComponentListener(game.input);
game.frame.setVisible(true);
game.start();
stopMenuThread();
Expand All @@ -80,7 +81,7 @@ public static void getDispose() {
game.frame.dispose();
}

//back to menu hold/kill
// back to menu hold/kill
public static Display getGameInstance() {
return RunGame.game;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

//Main JFrame here for main method to be called
// Main JFrame here for main method to be called
public class RunMiniGame {

static Color game, game1;
static Color game;

public RunMiniGame() {
BufferedImage cursor = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Expand All @@ -30,24 +30,16 @@ public RunMiniGame() {
public void windowClosing(WindowEvent windowEvent) {
//System.exit(0);
Display.setLauncherInstance(new Launcher(0));
//RunMiniGame.getDispose();
//RunMiniGame.getGameInstance(RunGame.getGameInstance()).stop();
}
});
game.frame.setLocationRelativeTo(null);
//game.frame.addComponentListener(game.input);
game.frame.setVisible(true);

Launcher.getDispose();
game.start();
//game1.start(); //if thread start is not in Constructor problem of Instances of new Thread is not possible
stopMenuThread();
}

public static Color getGameInstance() {
return RunMiniGame.game;
}

private void stopMenuThread() {
Display.getLauncherInstance().stopMenu();
}
Expand Down
Loading

0 comments on commit 44993ae

Please sign in to comment.