Compare commits

..

7 Commits

23 changed files with 273 additions and 97 deletions

BIN
assets/npc/bigrock.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 896 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

BIN
assets/npc/oldman_up_1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

BIN
assets/npc/oldman_up_2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

View File

@@ -1,15 +0,0 @@
package de.miaurizius.jgame2d.core;
public class AssetSetter {
GamePanel panel;
public AssetSetter(GamePanel panel) {
this.panel = panel;
}
public void setObject() {
}
}

View File

@@ -1,5 +1,11 @@
package de.miaurizius.jgame2d.core;
import de.miaurizius.jgame2d.core.enums.GameState;
import de.miaurizius.jgame2d.core.handlers.AssetSetter;
import de.miaurizius.jgame2d.core.handlers.CollisionHandler;
import de.miaurizius.jgame2d.core.handlers.KeyHandler;
import de.miaurizius.jgame2d.core.handlers.Sound;
import de.miaurizius.jgame2d.entity.Entity;
import de.miaurizius.jgame2d.entity.Player;
import de.miaurizius.jgame2d.object.SuperObject;
import de.miaurizius.jgame2d.tile.TileManager;
@@ -30,7 +36,7 @@ public class GamePanel extends JPanel implements Runnable {
int FPS = 60;
// SYSTEM
TileManager tileM = new TileManager(this);
public TileManager tileM = new TileManager(this);
KeyHandler keyH = new KeyHandler(this);
Sound se = new Sound();
Sound music = new Sound();
@@ -42,6 +48,7 @@ public class GamePanel extends JPanel implements Runnable {
// ENTITY AND OBJECT
public Player player = new Player(this, keyH);
public SuperObject[] obj = new SuperObject[10];
public Entity[] npc = new Entity[10];
// GAME STATE
public GameState gameState;
@@ -56,6 +63,7 @@ public class GamePanel extends JPanel implements Runnable {
public void setupGame() {
assetSetter.setObject();
assetSetter.setNPC();
playMusic(0); //Play main theme
gameState = GameState.PLAY;
}
@@ -98,6 +106,7 @@ public class GamePanel extends JPanel implements Runnable {
switch(gameState) {
case PLAY:
player.update();
for(Entity entity : npc) if(entity != null) entity.update();
break;
case PAUSE:
break;
@@ -112,9 +121,10 @@ public class GamePanel extends JPanel implements Runnable {
long drawStart = 0;
drawStart = System.nanoTime();
//Draw all components
// COMPONENTS
tileM.draw(graphics2d);
for (SuperObject superObject : obj) if (superObject != null) superObject.draw(graphics2d, this);
for(Entity npc : npc) if(npc != null) npc.draw(graphics2d);
player.draw(graphics2d);
ui.draw(graphics2d);

View File

@@ -1,21 +1,14 @@
package de.miaurizius.jgame2d.core;
import de.miaurizius.jgame2d.core.enums.GameState;
import java.awt.*;
import java.text.DecimalFormat;
public class UI {
GamePanel panel;
Graphics graphics2d;
Font arial_40, arial_80B;
DecimalFormat df = new DecimalFormat("#0.00");
public boolean messageOn = false;
public String message;
public boolean gameFinished = false;
int msgC = 0;
double playTime;
public UI(GamePanel panel) {
this.panel = panel;
@@ -23,27 +16,24 @@ public class UI {
arial_80B = new Font("Arial", Font.BOLD, 80);
}
public void showMessage(String text) {
message = text;
messageOn = true;
}
public void draw(Graphics graphics2d) {
this.graphics2d = graphics2d;
graphics2d.setFont(arial_40);
graphics2d.setColor(Color.white);
if(panel.gameState == null) return;
switch (panel.gameState) {
case PLAY:
case GameState.PLAY:
break;
case PAUSE:
case GameState.PAUSE:
drawPauseScreen();
break;
}
}
public void drawPauseScreen() {
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 80));
String text = "PAUSED";
int y = panel.screenHeight / 2;
graphics2d.drawString(text, getCenteredX(text), y);

View File

@@ -1,4 +1,4 @@
package de.miaurizius.jgame2d.core;
package de.miaurizius.jgame2d.core.enums;
public enum Direction {

View File

@@ -1,4 +1,4 @@
package de.miaurizius.jgame2d.core;
package de.miaurizius.jgame2d.core.enums;
public enum GameState {

View File

@@ -0,0 +1,24 @@
package de.miaurizius.jgame2d.core.handlers;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.entity.OldManNPC;
public class AssetSetter {
GamePanel panel;
public AssetSetter(GamePanel panel) {
this.panel = panel;
}
public void setObject() {
}
public void setNPC() {
panel.npc[0] = new OldManNPC(panel);
panel.npc[0].worldX = panel.tileSize*21;
panel.npc[0].worldY = panel.tileSize*21;
}
}

View File

@@ -1,5 +1,6 @@
package de.miaurizius.jgame2d.core;
package de.miaurizius.jgame2d.core.handlers;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.entity.Entity;
import de.miaurizius.jgame2d.object.SuperObject;
@@ -105,4 +106,89 @@ public class CollisionHandler {
return index;
}
//NPC OR MONSTER COLLISION
public int checkEntity(Entity entity, Entity[] target) {
int index = 999;
int c = -1;
for(Entity e : target) {
c++;
if (e != null) {
entity.solidArea.x += entity.worldX;
entity.solidArea.y += entity.worldY;
e.solidArea.x += e.worldX;
e.solidArea.y += e.worldY;
switch (entity.direction) {
case UP:
entity.solidArea.y -= entity.speed;
if (entity.solidArea.intersects(e.solidArea)) {
entity.collisionOn = true;
index = c;
}
break;
case DOWN:
entity.solidArea.y += entity.speed;
if (entity.solidArea.intersects(e.solidArea)) {
entity.collisionOn = true;
index = c;
}
break;
case LEFT:
entity.solidArea.x -= entity.speed;
if (entity.solidArea.intersects(e.solidArea)) {
entity.collisionOn = true;
index = c;
}
break;
case RIGHT:
entity.solidArea.x += entity.speed;
if (entity.solidArea.intersects(e.solidArea)) {
entity.collisionOn = true;
index = c;
}
break;
}
entity.solidArea.x = entity.solidAreaDefaultX;
entity.solidArea.y = entity.solidAreaDefaultY;
e.solidArea.x = e.solidAreaDefaultX;
e.solidArea.y = e.solidAreaDefaultY;
}
}
return index;
}
public void checkPlayer(Entity entity) {
entity.solidArea.x += entity.worldX;
entity.solidArea.y += entity.worldY;
panel.player.solidArea.x += panel.player.worldX;
panel.player.solidArea.y += panel.player.worldY;
switch (entity.direction) {
case UP:
entity.solidArea.y -= entity.speed;
if (entity.solidArea.intersects(panel.player.solidArea)) entity.collisionOn = true;
break;
case DOWN:
entity.solidArea.y += entity.speed;
if (entity.solidArea.intersects(panel.player.solidArea)) entity.collisionOn = true;
break;
case LEFT:
entity.solidArea.x -= entity.speed;
if (entity.solidArea.intersects(panel.player.solidArea)) entity.collisionOn = true;
break;
case RIGHT:
entity.solidArea.x += entity.speed;
if (entity.solidArea.intersects(panel.player.solidArea)) entity.collisionOn = true;
break;
}
entity.solidArea.x = entity.solidAreaDefaultX;
entity.solidArea.y = entity.solidAreaDefaultY;
panel.player.solidArea.x = panel.player.solidAreaDefaultX;
panel.player.solidArea.y = panel.player.solidAreaDefaultY;
}
}

View File

@@ -1,4 +1,7 @@
package de.miaurizius.jgame2d.core;
package de.miaurizius.jgame2d.core.handlers;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.GameState;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

View File

@@ -1,4 +1,6 @@
package de.miaurizius.jgame2d.core;
package de.miaurizius.jgame2d.core.handlers;
import de.miaurizius.jgame2d.core.Boot;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
@@ -7,7 +9,6 @@ import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Sound {
@@ -32,7 +33,7 @@ public class Sound {
clip = AudioSystem.getClip();
clip.open(ais);
} catch (Exception e) {
e.printStackTrace();
Boot.logger.log(Level.SEVERE, "Could not load Sound File: " + soundURL[i], e);
}
}

View File

@@ -1,23 +1,86 @@
package de.miaurizius.jgame2d.entity;
import de.miaurizius.jgame2d.core.Direction;
import de.miaurizius.jgame2d.core.Boot;
import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.Utility;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
public class Entity {
GamePanel panel;
public int worldX, worldY;
public int speed;
public BufferedImage up1, up2, down1, down2, left1, left2, right1, right2;
public Direction direction;
public int spriteCounter = 0;
public int spriteNum = 1;
public Rectangle solidArea;
public Rectangle solidArea = new Rectangle(0, 0, 48, 48);
public int solidAreaDefaultX, solidAreaDefaultY;
public boolean collisionOn = false;
public int actionLock = 0;
public Entity(GamePanel panel) {
this.panel = panel;
}
public void setAction() {}
public void update() {
setAction();
collisionOn = false;
panel.collisionH.checkTile(this);
panel.collisionH.checkObject(this, false);
panel.collisionH.checkPlayer(this);
if(!collisionOn) {
switch (direction) {
case UP -> worldY -= speed;
case DOWN -> worldY += speed;
case LEFT ->worldX -= speed;
case RIGHT -> worldX += speed;
}
}
spriteCounter++;
if(spriteCounter > 12) {
if(spriteNum == 1) spriteNum = 2;
else if(spriteNum == 2) spriteNum = 1;
else spriteNum = 0;
}
}
public void draw(Graphics graphics2d) {
int screenX = worldX - panel.player.worldX + panel.player.screenX;
int screenY = worldY - panel.player.worldY + panel.player.screenY;
if(worldX + panel.tileSize > panel.player.worldX - panel.player.screenX &&
worldX - panel.tileSize < panel.player.worldX + panel.player.screenX &&
worldY + panel.tileSize > panel.player.worldY - panel.player.screenY &&
worldY - panel.tileSize < panel.player.worldY + panel.player.screenY
) {
BufferedImage image = switch (direction) {
case UP -> (spriteNum == 1) ? up1 : up2;
case DOWN -> (spriteNum == 1) ? down1 : down2;
case LEFT -> (spriteNum == 1) ? left1 : left2;
case RIGHT -> (spriteNum == 1) ? right1 : right2;
};
graphics2d.drawImage(image, screenX, screenY, panel.tileSize, panel.tileSize, null);
}
}
public BufferedImage initEntitySprites(String name) {
try {
return Utility.scaleImage(ImageIO.read(new FileInputStream("assets/" + name + ".png")), panel.tileSize, panel.tileSize);
} catch (IOException e) {
Boot.logger.log(Level.SEVERE, "Could not load entity-image", e);
}
return null;
}
}

View File

@@ -0,0 +1,42 @@
package de.miaurizius.jgame2d.entity;
import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.GamePanel;
import java.util.Random;
public class OldManNPC extends Entity {
public OldManNPC(GamePanel panel) {
super(panel);
direction = Direction.DOWN;
speed = 1;
getImage();
}
public void getImage() {
up1 = initEntitySprites("npc/oldman_up_1");
up2 = initEntitySprites("npc/oldman_up_2");
down1 = initEntitySprites("npc/oldman_down_1");
down2 = initEntitySprites("npc/oldman_down_2");
left1 = initEntitySprites("npc/oldman_left_1");
left2 = initEntitySprites("npc/oldman_left_2");
right1 = initEntitySprites("npc/oldman_right_1");
right2 = initEntitySprites("npc/oldman_right_2");
}
public void setAction() {
actionLock++;
if(actionLock != 120) return; //lock action for x frames
Random rand = new Random();
int i = rand.nextInt(100)+1; //Generate number between 1 and 100
if(i <= 25) direction = Direction.UP;
if(i > 25 && i <= 50) direction = Direction.DOWN;
if(i > 50 && i <= 75) direction = Direction.LEFT;
if(i > 75) direction = Direction.RIGHT;
actionLock = 0;
}
}

View File

@@ -1,24 +1,21 @@
package de.miaurizius.jgame2d.entity;
import de.miaurizius.jgame2d.core.*;
import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.handlers.KeyHandler;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.logging.Level;
public class Player extends Entity {
GamePanel panel;
KeyHandler keyH;
public final int screenX;
public final int screenY;
public int hasKey = 0;
public Player(GamePanel panel, KeyHandler keyH) {
this.panel = panel;
super(panel);
this.keyH = keyH;
screenX = panel.screenWidth/2 - panel.tileSize/2;
@@ -44,27 +41,18 @@ public class Player extends Entity {
}
public void getPlayerImage() {
up1 = initPlayerImage("boy_up_1");
up2 = initPlayerImage("boy_up_2");
down1 = initPlayerImage("boy_down_1");
down2 = initPlayerImage("boy_down_2");
left1 = initPlayerImage("boy_left_1");
left2 = initPlayerImage(("boy_left_2"));
right1 = initPlayerImage(("boy_right_1"));
right2 = initPlayerImage(("boy_right_2"));
}
public BufferedImage initPlayerImage(String name) {
try {
return Utility.scaleImage(ImageIO.read(new FileInputStream("assets/player/" + name + ".png")), panel.tileSize, panel.tileSize);
} catch (IOException e) {
Boot.logger.log(Level.SEVERE, "Could not load player-image", e);
}
return null;
up1 = initEntitySprites("player/boy_up_1");
up2 = initEntitySprites("player/boy_up_2");
down1 = initEntitySprites("player/boy_down_1");
down2 = initEntitySprites("player/boy_down_2");
left1 = initEntitySprites("player/boy_left_1");
left2 = initEntitySprites("player/boy_left_2");
right1 = initEntitySprites("player/boy_right_1");
right2 = initEntitySprites("player/boy_right_2");
}
public void update() {
//Move player
// MOVEMENT
if(keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed) {
if(keyH.upPressed) direction = Direction.UP;
else if(keyH.downPressed) direction = Direction.DOWN;
@@ -74,9 +62,15 @@ public class Player extends Entity {
// CHECK TILE COLLISION
collisionOn = false;
panel.collisionH.checkTile(this);
// CHECK OBJECT COLLISION
int objIndex = panel.collisionH.checkObject(this, true);
pickObject(objIndex);
// CHECK ENTITY COLLISION
int npcIndex = panel.collisionH.checkEntity(this, panel.npc);
interactNPC(npcIndex);
if(!collisionOn) {
switch (direction) {
case UP -> worldY -= speed;
@@ -97,34 +91,12 @@ public class Player extends Entity {
public void pickObject(int index) {
if(index == 999) return;
switch(panel.obj[index].name.toLowerCase()) {
case "key":
panel.playSE(1);
hasKey++;
panel.obj[index] = null;
panel.ui.showMessage("You got a key!");
break;
case "door":
if(hasKey > 0) {
panel.playSE(3);
panel.obj[index] = null;
hasKey--;
panel.ui.showMessage("You opened the door!");
}
else panel.ui.showMessage("You need a key!");
break;
case "boots":
panel.playSE(2);
speed += 1;
panel.obj[index] = null;
panel.ui.showMessage("Speed up!");
break;
case "chest":
panel.ui.gameFinished = true;
panel.stopMusic();
panel.playSE(4);
break;
}
}
public void interactNPC(int index) {
if(index == 999) return;
System.out.println("npc collision detected");
}
public void draw(Graphics2D graphics2d) {