Compare commits

...

6 Commits

10 changed files with 185 additions and 24 deletions

View File

@@ -6,12 +6,14 @@ import java.util.logging.Logger;
public class Boot {
public static final Logger logger = Logger.getLogger("JDGame2D");
public static JFrame window;
static void main() {
JFrame window = new JFrame();
window = new JFrame();
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
window.setResizable(false);
window.setTitle("JGame2D");
window.setUndecorated(true);
GamePanel gamePanel = new GamePanel();
window.add(gamePanel);

View File

@@ -10,6 +10,7 @@ import de.miaurizius.jgame2d.tile.interactive.InteractiveTile;
import javax.sound.sampled.Clip;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.logging.Level;
@@ -19,12 +20,18 @@ public class GamePanel extends JPanel implements Runnable {
// SCREEN SETTINGS
final int originalTileSize = 16; //16x16 tile
final int scale = 3;
public final int maxScreenCol = 16;
public final int tileSize = originalTileSize * scale; //48x48 tile
public final int maxScreenCol = 20;
public final int maxScreenRow = 12;
public final int screenWidth = tileSize * maxScreenCol; // 768 pixels
public final int screenWidth = tileSize * maxScreenCol; // 960 pixels
public final int screenHeight = tileSize * maxScreenRow; // 576 pixels
// FULLSCREEN SETTINGS
int fScreenWidth = screenWidth;
int fScreenHeight = screenHeight;
BufferedImage tempScreen;
Graphics2D fg2;
// WORLD SETTINGS
public final int maxWorldCol = 50;
public final int maxWorldRow = 50;
@@ -52,6 +59,7 @@ public class GamePanel extends JPanel implements Runnable {
public Entity[] monster = new Entity[20];
public InteractiveTile[] iTile = new InteractiveTile[50];
public ArrayList<Entity> projectileList = new ArrayList<>();
public ArrayList<Entity> particleList = new ArrayList<>();
ArrayList<Entity> entityList = new ArrayList<>();
// GAME STATE
@@ -87,7 +95,9 @@ public class GamePanel extends JPanel implements Runnable {
if(delta >= 1) {
update();
repaint();
//repaint(); //since the new drawing method
drawTempScreen();
drawScreen();
delta--;
drawCount++;
}
@@ -122,29 +132,33 @@ public class GamePanel extends JPanel implements Runnable {
else projectileList.remove(i);
}
}
for(int i = 0; i < particleList.size(); i++) {
Entity m = particleList.get(i);
if(m != null) {
if(m.alive) m.update();
else particleList.remove(i);
}
}
for(Entity entity : iTile) if(entity != null) entity.update();
break;
case PAUSE:
break;
}
}
public void paintComponent(Graphics graphics) {
super.paintComponent(graphics);
Graphics2D graphics2d = (Graphics2D) graphics;
public void drawTempScreen() {
// DEBUG
long drawStart;
drawStart = System.nanoTime();
// TITLE SCREEN
if(gameState == GameState.TITLE) {
ui.draw(graphics2d);
ui.draw(fg2);
return;
}
// GAME
tileM.draw(graphics2d);
for(Entity entity : iTile) if(entity != null) entity.draw(graphics2d);
tileM.draw(fg2);
for(Entity entity : iTile) if(entity != null) entity.draw(fg2);
// ENTITY RENDER SYSTEM
entityList.add(player);
@@ -152,11 +166,12 @@ public class GamePanel extends JPanel implements Runnable {
for(Entity entity : obj) if(entity != null) entityList.add(entity);
for(Entity entity : monster) if(entity != null) entityList.add(entity);
for(Entity entity : projectileList) if(entity != null) entityList.add(entity);
for(Entity entity : particleList) if(entity != null) entityList.add(entity);
entityList.sort(Comparator.comparingInt(o -> o.worldY));
for(Entity entity : entityList) entity.draw(graphics2d);
for(Entity entity : entityList) entity.draw(fg2);
entityList.clear();
ui.draw(graphics2d);
ui.draw(fg2);
long drawEnd = System.nanoTime();
long passed = drawEnd - drawStart;
@@ -164,17 +179,19 @@ public class GamePanel extends JPanel implements Runnable {
// DEBUG
if(keyH.debug) {
int start = 350;
graphics2d.setColor(Color.white);
graphics2d.drawString("Draw Time: " + passed, 10, start);
graphics2d.drawString("FPS: " + fpsMeasure, 10, start+tileSize);
graphics2d.drawString("Invincible: " + player.invincibleCount, 10, start+tileSize*2);
graphics2d.drawString("X, Y: " + player.worldX+", "+player.worldY, 10, start+tileSize*3);
graphics2d.drawString("Col, Row: " + (player.worldX+player.solidArea.x)/tileSize+", "+(player.worldY+player.solidArea.y)/tileSize, 10, start+tileSize*4);
fg2.setColor(Color.white);
fg2.drawString("Draw Time: " + passed, 10, start);
fg2.drawString("FPS: " + fpsMeasure, 10, start+tileSize);
fg2.drawString("Invincible: " + player.invincibleCount, 10, start+tileSize*2);
fg2.drawString("X, Y: " + player.worldX+", "+player.worldY, 10, start+tileSize*3);
fg2.drawString("Col, Row: " + (player.worldX+player.solidArea.x)/tileSize+", "+(player.worldY+player.solidArea.y)/tileSize, 10, start+tileSize*4);
Boot.logger.log(Level.FINE, "Draw Time: " + passed);
}
// END
graphics.dispose();
}
public void drawScreen() {
Graphics fg = getGraphics();
fg.drawImage(tempScreen, 0, 0, fScreenWidth, fScreenHeight, null);
fg.dispose();
}
// MUSIC
@@ -202,6 +219,19 @@ public class GamePanel extends JPanel implements Runnable {
assetSetter.setMonster();
assetSetter.setITiles();
gameState = GameState.TITLE;
tempScreen = new BufferedImage(screenWidth, screenHeight, BufferedImage.TYPE_INT_RGB);
fg2 = (Graphics2D) tempScreen.getGraphics();
setFullscreen();
}
public void setFullscreen() {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
gd.setFullScreenWindow(Boot.window);
fScreenWidth = Boot.window.getWidth();
fScreenHeight = Boot.window.getHeight();
}
}

View File

@@ -113,7 +113,7 @@ public class UI {
}
public void drawCharStats() {
// DRAW FRAME
final int frameX = panel.tileSize;
final int frameX = panel.tileSize*2;
final int frameY = panel.tileSize;
final int frameWidth = panel.tileSize*5;
final int frameHeight = panel.tileSize*10;
@@ -160,7 +160,7 @@ public class UI {
}
public void drawInventory() {
// DRAW FRAME
int frameX = panel.tileSize*9;
int frameX = panel.tileSize*12;
int frameY = panel.tileSize;
int frameWidth = panel.tileSize*6;
int frameHeight = panel.tileSize*5;

View File

@@ -5,6 +5,7 @@ import de.miaurizius.jgame2d.core.enums.Direction;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.Utility;
import de.miaurizius.jgame2d.core.enums.EntityType;
import de.miaurizius.jgame2d.entity.particle.Particle;
import de.miaurizius.jgame2d.entity.projectile.Projectile;
import javax.imageio.ImageIO;
@@ -214,6 +215,35 @@ public class Entity {
}
}
// PARTICLE SETUP
public Color getParticleColor() {
return null;
}
public int getParticleSize() {
return -1;
}
public int getParticleSpeed() {
return -1;
}
public int getParticleMaxLife() {
return -1;
}
public void generateParticle(Entity generator, Entity target) {
Color color = generator.getParticleColor();
int size = generator.getParticleSize();
int speed = generator.getParticleSpeed();
int maxLife = generator.getParticleMaxLife();
Particle p1 = new Particle(panel, target, color, size, speed, maxLife, -2, -1);
Particle p2 = new Particle(panel, target, color, size, speed, maxLife, 2, -1);
Particle p3 = new Particle(panel, target, color, size, speed, maxLife, -2, 1);
Particle p4 = new Particle(panel, target, color, size, speed, maxLife, 2, 1);
panel.particleList.add(p1);
panel.particleList.add(p2);
panel.particleList.add(p3);
panel.particleList.add(p4);
}
// SETTING THINGS UP
BufferedImage parseSprite() {
return switch (direction) {

View File

@@ -225,6 +225,7 @@ public class Player extends Entity {
panel.iTile[index].playSE();
panel.iTile[index].life--;
panel.iTile[index].invincible = true;
generateParticle(panel.iTile[index], panel.iTile[index]);
if(panel.iTile[index].life == 0) panel.iTile[index] = panel.iTile[index].getDestroyedForm();
}

View File

@@ -0,0 +1,50 @@
package de.miaurizius.jgame2d.entity.particle;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.entity.Entity;
import java.awt.*;
public class Particle extends Entity {
GamePanel panel;
Entity generator;
Color color;
int size;
int xd, yd;
public Particle(GamePanel panel, Entity generator, Color color, int size, int speed, int maxLife, int xd, int yd) {
super(panel);
this.panel = panel;
this.generator = generator;
this.color = color;
this.size = size;
this.speed = speed;
this.maxLife = maxLife;
this.xd = xd;
this.yd = yd;
life = maxLife;
int offset = (panel.tileSize/2) - (size/2);
worldX = generator.worldX + offset;
worldY = generator.worldY + offset;
}
@Override
public void update() {
life--;
if(life < maxLife/3) yd++;
worldX += xd*speed;
worldY += yd*speed;
alive = (life != 0);
}
@Override
public void draw(Graphics2D graphics2d) {
int screenX = worldX - panel.player.worldX + panel.player.screenX;
int screenY = worldY - panel.player.worldY + panel.player.screenY;
graphics2d.setColor(color);
graphics2d.fillRect(screenX, screenY, size, size);
}
}

View File

@@ -28,6 +28,7 @@ public class Projectile extends Entity {
int monsterIndex = panel.collisionH.checkEntity(this, panel.monster);
if(monsterIndex != 999) {
panel.player.damageMonster(monsterIndex, attack);
generateParticle(user.projectile, panel.monster[monsterIndex]);
alive = false;
}
}
@@ -35,6 +36,7 @@ public class Projectile extends Entity {
if(user.type == EntityType.MONSTER) {
if(!panel.player.invincible && panel.collisionH.checkPlayer(this)) {
damagePlayer(attack);
generateParticle(user.projectile, panel.player);
alive = false;
}
}

View File

@@ -3,6 +3,8 @@ package de.miaurizius.jgame2d.entity.projectile;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.EntityType;
import java.awt.*;
public class RockObj extends Projectile {
GamePanel panel;
@@ -33,4 +35,17 @@ public class RockObj extends Projectile {
right2 = initEntitySprites(defaultSprite);
}
public Color getParticleColor() {
return new Color(40, 50,0);
}
public int getParticleSize() {
return 10; //in pixels
}
public int getParticleSpeed() {
return 1;
}
public int getParticleMaxLife() {
return 20;
}
}

View File

@@ -4,6 +4,8 @@ import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.core.enums.EntityType;
import de.miaurizius.jgame2d.entity.Entity;
import java.awt.*;
public class DryTreeTI extends InteractiveTile{
GamePanel panel;
@@ -31,4 +33,16 @@ public class DryTreeTI extends InteractiveTile{
public InteractiveTile getDestroyedForm() {
return new TrunkIT(panel, worldX/panel.tileSize, worldY/panel.tileSize);
}
public Color getParticleColor() {
return new Color(65, 50,30);
}
public int getParticleSize() {
return 6; //in pixels
}
public int getParticleSpeed() {
return 1;
}
public int getParticleMaxLife() {
return 20;
}
}

View File

@@ -3,6 +3,8 @@ package de.miaurizius.jgame2d.tile.interactive;
import de.miaurizius.jgame2d.core.GamePanel;
import de.miaurizius.jgame2d.entity.Entity;
import java.awt.*;
public class InteractiveTile extends Entity {
GamePanel panel;
@@ -14,6 +16,7 @@ public class InteractiveTile extends Entity {
}
// GENERAL
@Override
public void update() {
// INVINCIBLE COUNTER
if(!invincible) return;
@@ -23,6 +26,20 @@ public class InteractiveTile extends Entity {
invincibleCount = 0;
}
}
@Override
public void draw(Graphics2D 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
) {
graphics2D.drawImage(down1, screenX, screenY, null);
}
}
// PRE-CONFIGURED
public void playSE() {}