Improving Particles

master
bob 2022-06-08 14:32:31 -04:00
parent 6af212ab1c
commit a43a199c15
8 changed files with 60 additions and 15 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 195 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 B

View File

@ -42,6 +42,8 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//public static ArrayList<Tile>map = new ArrayList<Tile>(); //public static ArrayList<Tile>map = new ArrayList<Tile>();
public static Tile[][]map = new Tile[300][18]; public static Tile[][]map = new Tile[300][18];
public static ArrayList<Tile>particleTiles = new ArrayList<Tile>();
public static ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>(); public static ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>();
public static ArrayList<Particle>particles = new ArrayList<Particle>(); public static ArrayList<Particle>particles = new ArrayList<Particle>();
@ -142,10 +144,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
playerFrameCounter += player.draw(g, playerFrame); playerFrameCounter += player.draw(g, playerFrame);
b.draw(g); b.draw(g);
for(int i=0; i<particles.size(); i++){ for(int i=0; i<particles.size(); i++){
particles.get(i).draw(g); if(i<particles.size()) {
particles.get(i).lifeSpan--; particles.get(i).draw(g);
if(particles.get(i).lifeSpan<=0){ particles.get(i).lifeSpan--;
particles.remove(i); if (particles.get(i).lifeSpan <= 0) {
particles.remove(i);
}
} }
} }
g.drawString(camera.x+" "+((camera.x+GAME_WIDTH)/Tile.length),100,100); g.drawString(camera.x+" "+((camera.x+GAME_WIDTH)/Tile.length),100,100);
@ -154,14 +158,14 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//call the move methods in other classes to update positions //call the move methods in other classes to update positions
//this method is constantly called from run(). By doing this, movements appear fluid and natural. If we take this out the movements appear sluggish and laggy //this method is constantly called from run(). By doing this, movements appear fluid and natural. If we take this out the movements appear sluggish and laggy
public void move(){ public void move() throws IOException {
player.move(); player.move();
for (NonPlayer n: enemy) { for (NonPlayer n: enemy) {
n.move(); n.move();
} }
b.move(); b.move();
for (Particle p: particles) { for(int i=0; i<particles.size(); i++){
p.move(); particles.get(i).move();
} }
} }
@ -225,8 +229,17 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//only move objects around and update screen if enough time has passed //only move objects around and update screen if enough time has passed
if(delta >= 1){ if(delta >= 1){
move(); try {
move();
} catch (IOException e) {
throw new RuntimeException(e);
}
checkCollision(); checkCollision();
try {
updateParticle();
} catch (IOException e) {
throw new RuntimeException(e);
}
repaint(); repaint();
if (playerFrameCounter > 5) { if (playerFrameCounter > 5) {
// increment sprite image to be used and keeps it below 12 // increment sprite image to be used and keeps it below 12
@ -238,6 +251,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
} }
} }
public void updateParticle() throws IOException {
for(Tile t: particleTiles){
particles.add(new Particle(t.realX+Tile.length/2,t.y+Tile.length/2 ,GlobalState.randInt(-3,3),GlobalState.randInt(-3,3),GlobalState.randInt(1,5),"img/particles/LavaParticle.png"));
}
}
//if a key is pressed, we'll send it over to the Player class for processing //if a key is pressed, we'll send it over to the Player class for processing
public void keyPressed(KeyEvent e){ public void keyPressed(KeyEvent e){
player.keyPressed(e); player.keyPressed(e);

View File

@ -6,6 +6,7 @@ In 2D GUI, basically everything is a rectangle even if it doesn't look like it!
*/ */
import java.awt.*; import java.awt.*;
import java.awt.event.*; import java.awt.event.*;
import java.io.IOException;
public class GenericSprite extends Rectangle{ public class GenericSprite extends Rectangle{
@ -22,6 +23,8 @@ public class GenericSprite extends Rectangle{
public boolean upPressed= false; public boolean upPressed= false;
public boolean downPressed = false; public boolean downPressed = false;
public boolean isGrounded = false; public boolean isGrounded = false;
public boolean isPlayer = false;
//constructor creates ball at given location with given dimensions //constructor creates ball at given location with given dimensions
// TODO: reverse order of height and width // TODO: reverse order of height and width
public GenericSprite(int x, int y, int height, int width){ public GenericSprite(int x, int y, int height, int width){
@ -51,7 +54,7 @@ public class GenericSprite extends Rectangle{
} }
public void move(){ public void move() throws IOException {
} }
@ -88,7 +91,7 @@ public class GenericSprite extends Rectangle{
for (int j = lowY; j < highY; j++) { for (int j = lowY; j < highY; j++) {
if (GamePanel.map[i][j] != null) { if (GamePanel.map[i][j] != null) {
if (collide(GamePanel.map[i][j], this.x + x, this.y + y)) { if (collide(GamePanel.map[i][j], this.x + x, this.y + y)) {
if (GamePanel.map[i][j].isFinish) { if (GamePanel.map[i][j].isFinish&&isPlayer) {
LevelManager.nextLevel(); LevelManager.nextLevel();
return true; return true;
} }

View File

@ -1,3 +1,6 @@
public class GlobalState { public class GlobalState {
public static final int second = 10; public static final int second = 10;
public static int randInt(int low, int high){
return (int)(Math.random()*(high-low+1))+low;
}
} }

View File

@ -28,6 +28,7 @@ public class MapReader {
x = 0; x = 0;
y = 0; y = 0;
GamePanel.enemy.clear(); GamePanel.enemy.clear();
GamePanel.particleTiles.clear();
for(int i=0; i<GamePanel.map.length; i++){ for(int i=0; i<GamePanel.map.length; i++){
Arrays.fill(GamePanel.map[i], null); Arrays.fill(GamePanel.map[i], null);
} }
@ -90,6 +91,7 @@ public class MapReader {
} else if(file.charAt(i)=='l'){ } else if(file.charAt(i)=='l'){
newTile("img/tiles/terrain/lava.png"); newTile("img/tiles/terrain/lava.png");
GamePanel.map[x][y].kills = true; GamePanel.map[x][y].kills = true;
GamePanel.particleTiles.add(GamePanel.map[x][y]);
} }
x+=1; x+=1;
} }

View File

@ -1,4 +1,6 @@
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
public class Particle extends GenericSprite{ public class Particle extends GenericSprite{
public static final int small = 3; public static final int small = 3;
@ -9,17 +11,22 @@ public class Particle extends GenericSprite{
public int yVelocity; public int yVelocity;
public int lifeSpan = 10; public int lifeSpan = 10;
public Particle(int x, int y, int xVelocity, int yVelocity){
super(x,y,(int)(Math.random()*(big-small+1))+3, (int)(Math.random()*(big-small+1))+3); public BufferedImage sprite;
public Particle(int x, int y, int xVelocity, int yVelocity, int length, String filePath) throws IOException {
super(x,y,length, length);
this.xVelocity = xVelocity; this.xVelocity = xVelocity;
this.yVelocity = yVelocity; this.yVelocity = yVelocity;
sprite = GamePanel.getImage(filePath);
} }
public void move(){ public void move(){
x+=xVelocity; x+=xVelocity;
y+=yVelocity; y+=yVelocity;
yVelocity+=0.5;
} }
public void draw(Graphics g){ public void draw(Graphics g){
g.drawRect(x-GamePanel.camera.x,y,WIDTH,HEIGHT); g.drawImage(sprite,x-GamePanel.camera.x,y,WIDTH,HEIGHT, null);
} }
} }

View File

@ -34,6 +34,7 @@ public class Player extends GenericSprite {
this.rightKey = rightKey; this.rightKey = rightKey;
spriteArray = sprites; spriteArray = sprites;
alive = true; alive = true;
isPlayer = true;
} }
@ -113,7 +114,7 @@ public class Player extends GenericSprite {
} }
return canUpdate; return canUpdate;
} }
public void move(){ public void move() throws IOException {
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){ if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
GamePanel.camera.x += -Math.signum(xVelocity); GamePanel.camera.x += -Math.signum(xVelocity);
@ -159,13 +160,16 @@ public class Player extends GenericSprite {
if(rightPressed){ if(rightPressed){
if(isGrounded){ if(isGrounded){
GamePanel.particles.add(new Particle(x+GamePanel.camera.x+WIDTH/2,(int)(y+HEIGHT*0.8),3,3)); addParticle(-1);
} }
if(xVelocity<5) { if(xVelocity<5) {
xVelocity += 1; xVelocity += 1;
} }
} }
if(leftPressed) { if(leftPressed) {
if(isGrounded){
addParticle(1);
}
if(xVelocity>-5) { if(xVelocity>-5) {
xVelocity -= 1; xVelocity -= 1;
} }
@ -197,6 +201,13 @@ public class Player extends GenericSprite {
GamePanel.camera.x = LevelManager.xSpawn; GamePanel.camera.x = LevelManager.xSpawn;
y = LevelManager.ySpawn; y = LevelManager.ySpawn;
} }
public void addParticle(int x) throws IOException {
if(GlobalState.randInt(1,3)==3) {
GamePanel.particles.add(new Particle(this.x + GamePanel.camera.x + WIDTH / 2 + GlobalState.randInt(-PLAYER_WIDTH / 2, PLAYER_WIDTH / 2)
, (int) (y + HEIGHT * 0.95), GlobalState.randInt(-2, 2) + x, GlobalState.randInt(-4, 1), GlobalState.randInt(1, 7), "img/particles/GrassParticle.png"));
}
}
public int draw(Graphics g, int frame) { public int draw(Graphics g, int frame) {
frame %= spriteArray[0][0].length; frame %= spriteArray[0][0].length;
if (!upPressed && !downPressed && !leftPressed && !rightPressed) { if (!upPressed && !downPressed && !leftPressed && !rightPressed) {