Skip to content

Commit

Permalink
Reworked Animation handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Pegacraft committed Mar 25, 2021
1 parent 1b9595c commit ae5b9a5
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 162 deletions.
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.

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

import javax.swing.*;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.image.BufferStrategy;

/**
Expand Down Expand Up @@ -68,8 +69,6 @@ private void createWindow() {
*/
public Display setFullScreen(boolean fullScreen) {
this.fullScreen = fullScreen;
String backup = attachedScene;
attachedScene = null;
frame.dispose();
if (fullScreen) {
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
Expand All @@ -80,9 +79,9 @@ public Display setFullScreen(boolean fullScreen) {
frame.setLocation(100, 100);
frame.setUndecorated(false);
}

frame.setVisible(true);
this.attachScene(backup);
frame.toFront();
frame.requestFocus();
return this;
}

Expand Down Expand Up @@ -159,13 +158,15 @@ public Display setTitle(String name) {
*/
public Display attachScene(String alias) {
//runs the init in a loaded scene
Game.getScene(alias).display = this;
Game.getScene(alias).keyListener = this.keyListener;
Game.getScene(alias).mouseListener = this.mouseListener;
Scene scene = Game.getScene(alias);
scene.display = this;
scene.keyListener = this.keyListener;
scene.mouseListener = this.mouseListener;
this.mouseListener.clear();
Game.getScenes().get(alias).getObjectList().clear();
Game.getScenes().get(alias).init();
this.keyListener.clear();
scene.getObjectList().clear();
this.attachedScene = alias;
scene.init();
return this;
}

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/engine/listeners/Keyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public void addListener(Integer keyCode, Consumer<KeyEvent> function, boolean bl
});
}

/**
* clears all keyboard events
*/
public void clear() {
onKeyPress.clear();
}

/**
* Use this method to check if a key is held down
*
Expand Down
71 changes: 0 additions & 71 deletions src/main/java/engine/rendering/Animation.java

This file was deleted.

80 changes: 0 additions & 80 deletions src/main/java/engine/rendering/FrameAnimation.java

This file was deleted.

1 change: 0 additions & 1 deletion src/main/java/engine/rendering/Graphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public static void graphicsLoop() {
} catch (ConcurrentModificationException ignore) {
}
}
Animation.animationLoop();
bs.show();
g.dispose();
} catch (IllegalStateException ignore) {
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/engine/rendering/TileSheet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package engine.rendering;

import java.awt.image.BufferedImage;
import java.util.ArrayList;

/**
* This class is used to extract the sub images out of an tile sheet.
*/
public class TileSheet {

public enum READ_DIRECTION {
UP_DOWN,
LEFT_RIGHT;
}

BufferedImage sheet;
int width, height, xImages, yImages;
READ_DIRECTION read_direction;
ArrayList<BufferedImage> subImages = new ArrayList<>();

/**
* @param sheet the tile sheet you want to process
* @param xImages how many images are in the x dimension
* @param yImages how many images are in the y dimension
* @param read_direction in witch direction the tile sheet should be read
*/
public TileSheet(BufferedImage sheet, int xImages, int yImages, READ_DIRECTION read_direction) {
this.sheet = sheet;
this.width = sheet.getWidth();
this.height = sheet.getHeight();
this.xImages = xImages;
this.yImages = yImages;
this.read_direction = read_direction;
createSubImages();
}

void createSubImages() {
switch (read_direction) {

case UP_DOWN -> {
for (int x = 0; x < xImages; x++) {
for (int y = 0; y < yImages; y++) {
subImages.add(sheet.getSubimage(x * (width / xImages), y * (height / yImages), width / xImages, height / yImages));
}
}
}
case LEFT_RIGHT -> {
for (int y = 0; y < yImages; y++) {
for (int x = 0; x < xImages; x++) {
subImages.add(sheet.getSubimage(x * (width / xImages), y * (height / yImages), width / xImages, height / yImages));
}
}
}
}
}

/**
* @return Returns an arrayList of the sub images.
*/
public ArrayList<BufferedImage> getSubImages() {
return subImages;
}
}
Loading

0 comments on commit ae5b9a5

Please sign in to comment.