Skip to content

Commit

Permalink
Made some changes to the project structure and changed the cam movement
Browse files Browse the repository at this point in the history
  • Loading branch information
Pegacraft committed Oct 27, 2020
1 parent 5124d16 commit 6365725
Show file tree
Hide file tree
Showing 21 changed files with 69 additions and 298 deletions.
12 changes: 12 additions & 0 deletions .idea/artifacts/Age_engine_jar.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 30 additions & 2 deletions src/main/java/engine/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import engine.listeners.Keyboard;
import engine.listeners.Mouse;
import engine.rendering.Graphics;

import javax.swing.*;
import java.awt.*;
Expand Down Expand Up @@ -105,6 +106,33 @@ public Display setBackgroundColor(Color bgColor) {
return this;
}

/**
* makes the window resizable
*/
public Display makeResizable(boolean b){
frame.setResizable(b);
return this;
}

/**
* Sets the width of the window
* @param width The value you want the width to be set to
*/
public Display setWidth(int width){
this.width = width;
frame.setSize(this.width, this.height);
return this;
}
/**
* Sets the height of the window
* @param height The value you want the height to be set to
*/
public Display setHeight(int height){
this.height = height;
frame.setSize(this.width, this.height);
return this;
}

/**
* set the window's title to a given string
*
Expand Down Expand Up @@ -158,14 +186,14 @@ public BufferStrategy bs() {
* @return Returns the screen width.
*/
public int getWidth() {
return canvas.getWidth();
return width;
}

/**
* @return Returns the screen height.
*/
public int getHeight() {
return canvas.getHeight();
return height;
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/engine/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import engine.editor.EditScene;
import engine.editor.SettingsScene;
import engine.loops.Loop;
import engine.rendering.Graphics;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -15,6 +16,7 @@ public class Game {
private static final Map<String, Display> displays = new HashMap<>();
private static final Map<String, Scene> scenes = new HashMap<>();
private static Loop loop;
private static int FPS = 60;

private Game() {
throw new IllegalStateException("Utility class");
Expand All @@ -26,6 +28,7 @@ private Game() {
public static void start() {
loop = new Loop();
loop.start();
loop.setFrameRate(FPS);
}

/**
Expand All @@ -35,7 +38,9 @@ public static void start() {
* @param frameRate The frame rate you want.
*/
public static void setFrameRate(int frameRate) {
loop.setFrameRate(frameRate);
FPS = frameRate;
if (loop != null)
loop.setFrameRate(frameRate);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/engine/editor/EditScene.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import engine.Game;
import engine.Scene;
import engine.editor.menu.Button;
import engine.mechanics.Button;
import engine.editor.menu.Sprite;
import engine.listeners.MouseButtons;
import engine.mechanics.*;
Expand Down Expand Up @@ -37,7 +37,7 @@ public void init() {
g.fillRect(1100, 451, 1280, 720);
});
TickBox t = new TickBox(1190, 650, 30, 30, this);
Button b = new Button(1130, 500, 100, 40)
Button b = new Button(1130, 500, 100, 40, this)
.addEvent(MouseButtons.LEFT_DOWN, this::mouseHandler)
.addEvent(MouseButtons.RIGHT_DOWN, this::mouseHandler)
.addEvent(MouseButtons.MIDDLE_DOWN, this::mouseHandler)
Expand All @@ -47,7 +47,7 @@ public void init() {
.setTextColor(Color.red)
.setFontSize(15)
.setFont("JhengHei UI");
Button settings = new Button(1130, 600, 100, 40)
Button settings = new Button(1130, 600, 100, 40, this)
.addEvent(MouseButtons.LEFT_DOWN, e -> display.attachScene("Settings"))
.setColor(Color.GREEN)
.setText("Settings")
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/engine/editor/SettingsScene.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package engine.editor;

import engine.Scene;
import engine.editor.menu.Button;
import engine.mechanics.Button;
import engine.listeners.MouseButtons;
import engine.mechanics.TextBox;

Expand All @@ -23,7 +23,7 @@ public void init() {
.setMatcher("[0-9]*")
.setMaxValue(4)
.setText(String.valueOf(gridHeight));
Button back = new Button(20, 20, 100, 40)
Button back = new Button(20, 20, 100, 40, this)
.addEvent(MouseButtons.LEFT_DOWN, e -> {
gridWidth = Integer.parseInt(gridWidthBox.getText());
gridHeight = Integer.parseInt(gridHeightBox.getText());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/engine/listeners/Mouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ public Point getMousePos() {
}

private double scaledX(int x) {
return (x / ((double) (display.getWidth()) / 1280));
return (x / ((double) (display.getWidth()) / Graphics.stdWidth));
}

private double scaledY(int y) {
return (y / ((double) (display.getHeight()) / 720));
return (y / ((double) (display.getHeight()) / Graphics.stdHeight));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package engine.editor.menu;
package engine.mechanics;

import engine.Entity;
import engine.Game;
import engine.Scene;
import engine.listeners.Mouse;
import engine.listeners.MouseButtons;
import engine.mechanics.Hitbox;

import java.awt.*;
import java.awt.event.MouseEvent;
Expand All @@ -16,7 +15,7 @@
import static engine.rendering.Graphics.g;

public class Button implements Entity {
private final Mouse mouseListener = Game.getScene("EditScene").mouseListener;
private final Mouse mouseListener;
private final Hitbox h;
private final Map<MouseButtons, Predicate<MouseEvent>> events = new EnumMap<>(MouseButtons.class);
private final int x;
Expand All @@ -32,7 +31,8 @@ public class Button implements Entity {
private int fontSize = 13;
private String fontFace = "Comic Sans MS";

public Button(int x, int y, int width, int height) {
public Button(int x, int y, int width, int height, Scene scene) {
this.mouseListener = scene.mouseListener;
this.x = x;
this.y = y;
this.width = width;
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/engine/rendering/Graphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class Graphics {
public static Graphics2D g;
private static int xOffset = 0;
private static int yOffset = 0;
public static double stdWidth = 1280d;
public static double stdHeight = 720d;

private Graphics() {
throw new IllegalStateException("Utility class");
Expand All @@ -31,9 +33,9 @@ public static void graphicsLoop() {
Game.getDisplays().values().forEach(e -> {
BufferStrategy bs = e.bs();
g = (Graphics2D) bs.getDrawGraphics();
g.setColor(Color.white);
g.setColor(e.getCanvas().getBackground());
g.fillRect(0, 0, e.getWidth(), e.getHeight());
g.scale(e.getWidth() / 1280d, e.getHeight() / 720d);
g.scale(e.getWidth() / stdWidth, e.getHeight() / stdHeight);
Graphics.g.translate(xOffset, yOffset);
Scene s = Game.getScene(e.getAttachedScene());
if (s != null) {
Expand All @@ -53,12 +55,12 @@ public static void graphicsLoop() {
* @param y The y offset
*/
public static void moveCam(int x, int y) {
xOffset = -x + 640;
yOffset = -y + 360;
xOffset = x;
yOffset = y;
}

/**
* @return Returns the current offset
* @return Returns the current camera position
*/
public static Point getCamPos() {
return new Point(xOffset, yOffset);
Expand Down
1 change: 0 additions & 1 deletion src/main/java/engine/rendering/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public static BufferedImage load(String path) {
return image;
} catch (IOException | IllegalArgumentException e) {
e.printStackTrace();
System.exit(1);
return null;
}
}
Expand Down
26 changes: 0 additions & 26 deletions src/main/java/game/Launcher.java

This file was deleted.

27 changes: 0 additions & 27 deletions src/main/java/game/TestObject.java

This file was deleted.

41 changes: 0 additions & 41 deletions src/main/java/game/TestScene.java

This file was deleted.

25 changes: 0 additions & 25 deletions src/main/java/game/test/GameScene.java

This file was deleted.

Loading

0 comments on commit 6365725

Please sign in to comment.