Compare commits
7 Commits
ff82d8f7e6
...
1f614916b5
| Author | SHA1 | Date | |
|---|---|---|---|
|
1f614916b5
|
|||
|
b294eb49c8
|
|||
|
c4d6539db7
|
|||
|
1c23a1528e
|
|||
|
d2025b0c80
|
|||
|
73e17a56fc
|
|||
|
c3aa7ea540
|
BIN
assets/npc/bigrock.png
Normal file
|
After Width: | Height: | Size: 896 B |
BIN
assets/npc/merchant_down_1.png
Normal file
|
After Width: | Height: | Size: 6.0 KiB |
BIN
assets/npc/merchant_down_2.png
Normal file
|
After Width: | Height: | Size: 6.0 KiB |
BIN
assets/npc/oldman_down_1.png
Normal file
|
After Width: | Height: | Size: 5.9 KiB |
BIN
assets/npc/oldman_down_2.png
Normal file
|
After Width: | Height: | Size: 5.9 KiB |
BIN
assets/npc/oldman_left_1.png
Normal file
|
After Width: | Height: | Size: 5.9 KiB |
BIN
assets/npc/oldman_left_2.png
Normal file
|
After Width: | Height: | Size: 6.0 KiB |
BIN
assets/npc/oldman_right_1.png
Normal file
|
After Width: | Height: | Size: 5.9 KiB |
BIN
assets/npc/oldman_right_2.png
Normal file
|
After Width: | Height: | Size: 5.9 KiB |
BIN
assets/npc/oldman_up_1.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
BIN
assets/npc/oldman_up_2.png
Normal file
|
After Width: | Height: | Size: 5.8 KiB |
@@ -1,15 +0,0 @@
|
|||||||
package de.miaurizius.jgame2d.core;
|
|
||||||
|
|
||||||
public class AssetSetter {
|
|
||||||
|
|
||||||
GamePanel panel;
|
|
||||||
|
|
||||||
public AssetSetter(GamePanel panel) {
|
|
||||||
this.panel = panel;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setObject() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,11 @@
|
|||||||
package de.miaurizius.jgame2d.core;
|
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.entity.Player;
|
||||||
import de.miaurizius.jgame2d.object.SuperObject;
|
import de.miaurizius.jgame2d.object.SuperObject;
|
||||||
import de.miaurizius.jgame2d.tile.TileManager;
|
import de.miaurizius.jgame2d.tile.TileManager;
|
||||||
@@ -30,7 +36,7 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
int FPS = 60;
|
int FPS = 60;
|
||||||
|
|
||||||
// SYSTEM
|
// SYSTEM
|
||||||
TileManager tileM = new TileManager(this);
|
public TileManager tileM = new TileManager(this);
|
||||||
KeyHandler keyH = new KeyHandler(this);
|
KeyHandler keyH = new KeyHandler(this);
|
||||||
Sound se = new Sound();
|
Sound se = new Sound();
|
||||||
Sound music = new Sound();
|
Sound music = new Sound();
|
||||||
@@ -42,6 +48,7 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
// ENTITY AND OBJECT
|
// ENTITY AND OBJECT
|
||||||
public Player player = new Player(this, keyH);
|
public Player player = new Player(this, keyH);
|
||||||
public SuperObject[] obj = new SuperObject[10];
|
public SuperObject[] obj = new SuperObject[10];
|
||||||
|
public Entity[] npc = new Entity[10];
|
||||||
|
|
||||||
// GAME STATE
|
// GAME STATE
|
||||||
public GameState gameState;
|
public GameState gameState;
|
||||||
@@ -56,6 +63,7 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
|
|
||||||
public void setupGame() {
|
public void setupGame() {
|
||||||
assetSetter.setObject();
|
assetSetter.setObject();
|
||||||
|
assetSetter.setNPC();
|
||||||
playMusic(0); //Play main theme
|
playMusic(0); //Play main theme
|
||||||
gameState = GameState.PLAY;
|
gameState = GameState.PLAY;
|
||||||
}
|
}
|
||||||
@@ -98,6 +106,7 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
switch(gameState) {
|
switch(gameState) {
|
||||||
case PLAY:
|
case PLAY:
|
||||||
player.update();
|
player.update();
|
||||||
|
for(Entity entity : npc) if(entity != null) entity.update();
|
||||||
break;
|
break;
|
||||||
case PAUSE:
|
case PAUSE:
|
||||||
break;
|
break;
|
||||||
@@ -112,9 +121,10 @@ public class GamePanel extends JPanel implements Runnable {
|
|||||||
long drawStart = 0;
|
long drawStart = 0;
|
||||||
drawStart = System.nanoTime();
|
drawStart = System.nanoTime();
|
||||||
|
|
||||||
//Draw all components
|
// COMPONENTS
|
||||||
tileM.draw(graphics2d);
|
tileM.draw(graphics2d);
|
||||||
for (SuperObject superObject : obj) if (superObject != null) superObject.draw(graphics2d, this);
|
for (SuperObject superObject : obj) if (superObject != null) superObject.draw(graphics2d, this);
|
||||||
|
for(Entity npc : npc) if(npc != null) npc.draw(graphics2d);
|
||||||
player.draw(graphics2d);
|
player.draw(graphics2d);
|
||||||
ui.draw(graphics2d);
|
ui.draw(graphics2d);
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,14 @@
|
|||||||
package de.miaurizius.jgame2d.core;
|
package de.miaurizius.jgame2d.core;
|
||||||
|
|
||||||
|
import de.miaurizius.jgame2d.core.enums.GameState;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.text.DecimalFormat;
|
|
||||||
|
|
||||||
public class UI {
|
public class UI {
|
||||||
|
|
||||||
GamePanel panel;
|
GamePanel panel;
|
||||||
Graphics graphics2d;
|
Graphics graphics2d;
|
||||||
Font arial_40, arial_80B;
|
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) {
|
public UI(GamePanel panel) {
|
||||||
this.panel = panel;
|
this.panel = panel;
|
||||||
@@ -23,27 +16,24 @@ public class UI {
|
|||||||
arial_80B = new Font("Arial", Font.BOLD, 80);
|
arial_80B = new Font("Arial", Font.BOLD, 80);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void showMessage(String text) {
|
|
||||||
message = text;
|
|
||||||
messageOn = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void draw(Graphics graphics2d) {
|
public void draw(Graphics graphics2d) {
|
||||||
this.graphics2d = graphics2d;
|
this.graphics2d = graphics2d;
|
||||||
graphics2d.setFont(arial_40);
|
graphics2d.setFont(arial_40);
|
||||||
graphics2d.setColor(Color.white);
|
graphics2d.setColor(Color.white);
|
||||||
|
|
||||||
|
if(panel.gameState == null) return;
|
||||||
switch (panel.gameState) {
|
switch (panel.gameState) {
|
||||||
case PLAY:
|
case GameState.PLAY:
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case PAUSE:
|
case GameState.PAUSE:
|
||||||
drawPauseScreen();
|
drawPauseScreen();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void drawPauseScreen() {
|
public void drawPauseScreen() {
|
||||||
|
graphics2d.setFont(graphics2d.getFont().deriveFont(Font.PLAIN, 80));
|
||||||
String text = "PAUSED";
|
String text = "PAUSED";
|
||||||
int y = panel.screenHeight / 2;
|
int y = panel.screenHeight / 2;
|
||||||
graphics2d.drawString(text, getCenteredX(text), y);
|
graphics2d.drawString(text, getCenteredX(text), y);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package de.miaurizius.jgame2d.core;
|
package de.miaurizius.jgame2d.core.enums;
|
||||||
|
|
||||||
public enum Direction {
|
public enum Direction {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package de.miaurizius.jgame2d.core;
|
package de.miaurizius.jgame2d.core.enums;
|
||||||
|
|
||||||
public enum GameState {
|
public enum GameState {
|
||||||
|
|
||||||
24
src/de/miaurizius/jgame2d/core/handlers/AssetSetter.java
Normal 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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.entity.Entity;
|
||||||
import de.miaurizius.jgame2d.object.SuperObject;
|
import de.miaurizius.jgame2d.object.SuperObject;
|
||||||
|
|
||||||
@@ -105,4 +106,89 @@ public class CollisionHandler {
|
|||||||
return index;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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.KeyEvent;
|
||||||
import java.awt.event.KeyListener;
|
import java.awt.event.KeyListener;
|
||||||
@@ -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.AudioInputStream;
|
||||||
import javax.sound.sampled.AudioSystem;
|
import javax.sound.sampled.AudioSystem;
|
||||||
@@ -7,7 +9,6 @@ import java.io.File;
|
|||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
public class Sound {
|
public class Sound {
|
||||||
|
|
||||||
@@ -32,7 +33,7 @@ public class Sound {
|
|||||||
clip = AudioSystem.getClip();
|
clip = AudioSystem.getClip();
|
||||||
clip.open(ais);
|
clip.open(ais);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
Boot.logger.log(Level.SEVERE, "Could not load Sound File: " + soundURL[i], e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,23 +1,86 @@
|
|||||||
package de.miaurizius.jgame2d.entity;
|
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.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class Entity {
|
public class Entity {
|
||||||
|
|
||||||
|
GamePanel panel;
|
||||||
public int worldX, worldY;
|
public int worldX, worldY;
|
||||||
public int speed;
|
public int speed;
|
||||||
|
|
||||||
public BufferedImage up1, up2, down1, down2, left1, left2, right1, right2;
|
public BufferedImage up1, up2, down1, down2, left1, left2, right1, right2;
|
||||||
public Direction direction;
|
public Direction direction;
|
||||||
|
|
||||||
public int spriteCounter = 0;
|
public int spriteCounter = 0;
|
||||||
public int spriteNum = 1;
|
public int spriteNum = 1;
|
||||||
|
public Rectangle solidArea = new Rectangle(0, 0, 48, 48);
|
||||||
public Rectangle solidArea;
|
|
||||||
public int solidAreaDefaultX, solidAreaDefaultY;
|
public int solidAreaDefaultX, solidAreaDefaultY;
|
||||||
public boolean collisionOn = false;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
42
src/de/miaurizius/jgame2d/entity/OldManNPC.java
Normal 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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,24 +1,21 @@
|
|||||||
package de.miaurizius.jgame2d.entity;
|
package de.miaurizius.jgame2d.entity;
|
||||||
|
|
||||||
import de.miaurizius.jgame2d.core.*;
|
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.*;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.*;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
public class Player extends Entity {
|
public class Player extends Entity {
|
||||||
|
|
||||||
GamePanel panel;
|
|
||||||
KeyHandler keyH;
|
KeyHandler keyH;
|
||||||
|
|
||||||
public final int screenX;
|
public final int screenX;
|
||||||
public final int screenY;
|
public final int screenY;
|
||||||
public int hasKey = 0;
|
|
||||||
|
|
||||||
public Player(GamePanel panel, KeyHandler keyH) {
|
public Player(GamePanel panel, KeyHandler keyH) {
|
||||||
this.panel = panel;
|
super(panel);
|
||||||
this.keyH = keyH;
|
this.keyH = keyH;
|
||||||
|
|
||||||
screenX = panel.screenWidth/2 - panel.tileSize/2;
|
screenX = panel.screenWidth/2 - panel.tileSize/2;
|
||||||
@@ -44,27 +41,18 @@ public class Player extends Entity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void getPlayerImage() {
|
public void getPlayerImage() {
|
||||||
up1 = initPlayerImage("boy_up_1");
|
up1 = initEntitySprites("player/boy_up_1");
|
||||||
up2 = initPlayerImage("boy_up_2");
|
up2 = initEntitySprites("player/boy_up_2");
|
||||||
down1 = initPlayerImage("boy_down_1");
|
down1 = initEntitySprites("player/boy_down_1");
|
||||||
down2 = initPlayerImage("boy_down_2");
|
down2 = initEntitySprites("player/boy_down_2");
|
||||||
left1 = initPlayerImage("boy_left_1");
|
left1 = initEntitySprites("player/boy_left_1");
|
||||||
left2 = initPlayerImage(("boy_left_2"));
|
left2 = initEntitySprites("player/boy_left_2");
|
||||||
right1 = initPlayerImage(("boy_right_1"));
|
right1 = initEntitySprites("player/boy_right_1");
|
||||||
right2 = initPlayerImage(("boy_right_2"));
|
right2 = initEntitySprites("player/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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
//Move player
|
// MOVEMENT
|
||||||
if(keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed) {
|
if(keyH.upPressed || keyH.downPressed || keyH.leftPressed || keyH.rightPressed) {
|
||||||
if(keyH.upPressed) direction = Direction.UP;
|
if(keyH.upPressed) direction = Direction.UP;
|
||||||
else if(keyH.downPressed) direction = Direction.DOWN;
|
else if(keyH.downPressed) direction = Direction.DOWN;
|
||||||
@@ -74,9 +62,15 @@ public class Player extends Entity {
|
|||||||
// CHECK TILE COLLISION
|
// CHECK TILE COLLISION
|
||||||
collisionOn = false;
|
collisionOn = false;
|
||||||
panel.collisionH.checkTile(this);
|
panel.collisionH.checkTile(this);
|
||||||
|
|
||||||
|
// CHECK OBJECT COLLISION
|
||||||
int objIndex = panel.collisionH.checkObject(this, true);
|
int objIndex = panel.collisionH.checkObject(this, true);
|
||||||
pickObject(objIndex);
|
pickObject(objIndex);
|
||||||
|
|
||||||
|
// CHECK ENTITY COLLISION
|
||||||
|
int npcIndex = panel.collisionH.checkEntity(this, panel.npc);
|
||||||
|
interactNPC(npcIndex);
|
||||||
|
|
||||||
if(!collisionOn) {
|
if(!collisionOn) {
|
||||||
switch (direction) {
|
switch (direction) {
|
||||||
case UP -> worldY -= speed;
|
case UP -> worldY -= speed;
|
||||||
@@ -97,34 +91,12 @@ public class Player extends Entity {
|
|||||||
|
|
||||||
public void pickObject(int index) {
|
public void pickObject(int index) {
|
||||||
if(index == 999) return;
|
if(index == 999) return;
|
||||||
switch(panel.obj[index].name.toLowerCase()) {
|
|
||||||
case "key":
|
}
|
||||||
panel.playSE(1);
|
|
||||||
hasKey++;
|
public void interactNPC(int index) {
|
||||||
panel.obj[index] = null;
|
if(index == 999) return;
|
||||||
panel.ui.showMessage("You got a key!");
|
System.out.println("npc collision detected");
|
||||||
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 draw(Graphics2D graphics2d) {
|
public void draw(Graphics2D graphics2d) {
|
||||||
|
|||||||