Improving Particles
parent
6af212ab1c
commit
a43a199c15
Binary file not shown.
After Width: | Height: | Size: 195 B |
Binary file not shown.
After Width: | Height: | Size: 128 B |
|
@ -42,6 +42,8 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
|||
//public static ArrayList<Tile>map = new ArrayList<Tile>();
|
||||
|
||||
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<Particle>particles = new ArrayList<Particle>();
|
||||
|
@ -142,26 +144,28 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
|
|||
playerFrameCounter += player.draw(g, playerFrame);
|
||||
b.draw(g);
|
||||
for(int i=0; i<particles.size(); i++){
|
||||
if(i<particles.size()) {
|
||||
particles.get(i).draw(g);
|
||||
particles.get(i).lifeSpan--;
|
||||
if(particles.get(i).lifeSpan<=0){
|
||||
if (particles.get(i).lifeSpan <= 0) {
|
||||
particles.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
g.drawString(camera.x+" "+((camera.x+GAME_WIDTH)/Tile.length),100,100);
|
||||
g.drawString(b.x+" "+((b.x+GamePanel.GAME_WIDTH)/Tile.length-4),100,200);
|
||||
}
|
||||
|
||||
//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
|
||||
public void move(){
|
||||
public void move() throws IOException {
|
||||
player.move();
|
||||
for (NonPlayer n: enemy) {
|
||||
n.move();
|
||||
}
|
||||
b.move();
|
||||
for (Particle p: particles) {
|
||||
p.move();
|
||||
for(int i=0; i<particles.size(); i++){
|
||||
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
|
||||
if(delta >= 1){
|
||||
try {
|
||||
move();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
checkCollision();
|
||||
try {
|
||||
updateParticle();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
repaint();
|
||||
if (playerFrameCounter > 5) {
|
||||
// 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
|
||||
public void keyPressed(KeyEvent e){
|
||||
player.keyPressed(e);
|
||||
|
|
|
@ -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.event.*;
|
||||
import java.io.IOException;
|
||||
|
||||
public class GenericSprite extends Rectangle{
|
||||
|
||||
|
@ -22,6 +23,8 @@ public class GenericSprite extends Rectangle{
|
|||
public boolean upPressed= false;
|
||||
public boolean downPressed = false;
|
||||
public boolean isGrounded = false;
|
||||
|
||||
public boolean isPlayer = false;
|
||||
//constructor creates ball at given location with given dimensions
|
||||
// TODO: reverse order of height and 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++) {
|
||||
if (GamePanel.map[i][j] != null) {
|
||||
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();
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
public class GlobalState {
|
||||
public static final int second = 10;
|
||||
public static int randInt(int low, int high){
|
||||
return (int)(Math.random()*(high-low+1))+low;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ public class MapReader {
|
|||
x = 0;
|
||||
y = 0;
|
||||
GamePanel.enemy.clear();
|
||||
GamePanel.particleTiles.clear();
|
||||
for(int i=0; i<GamePanel.map.length; i++){
|
||||
Arrays.fill(GamePanel.map[i], null);
|
||||
}
|
||||
|
@ -90,6 +91,7 @@ public class MapReader {
|
|||
} else if(file.charAt(i)=='l'){
|
||||
newTile("img/tiles/terrain/lava.png");
|
||||
GamePanel.map[x][y].kills = true;
|
||||
GamePanel.particleTiles.add(GamePanel.map[x][y]);
|
||||
}
|
||||
x+=1;
|
||||
}
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.IOException;
|
||||
|
||||
public class Particle extends GenericSprite{
|
||||
public static final int small = 3;
|
||||
|
@ -9,17 +11,22 @@ public class Particle extends GenericSprite{
|
|||
public int yVelocity;
|
||||
|
||||
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.yVelocity = yVelocity;
|
||||
sprite = GamePanel.getImage(filePath);
|
||||
}
|
||||
public void move(){
|
||||
x+=xVelocity;
|
||||
y+=yVelocity;
|
||||
yVelocity+=0.5;
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ public class Player extends GenericSprite {
|
|||
this.rightKey = rightKey;
|
||||
spriteArray = sprites;
|
||||
alive = true;
|
||||
isPlayer = true;
|
||||
}
|
||||
|
||||
|
||||
|
@ -113,7 +114,7 @@ public class Player extends GenericSprite {
|
|||
}
|
||||
return canUpdate;
|
||||
}
|
||||
public void move(){
|
||||
public void move() throws IOException {
|
||||
|
||||
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
|
||||
GamePanel.camera.x += -Math.signum(xVelocity);
|
||||
|
@ -159,13 +160,16 @@ public class Player extends GenericSprite {
|
|||
|
||||
if(rightPressed){
|
||||
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) {
|
||||
xVelocity += 1;
|
||||
}
|
||||
}
|
||||
if(leftPressed) {
|
||||
if(isGrounded){
|
||||
addParticle(1);
|
||||
}
|
||||
if(xVelocity>-5) {
|
||||
xVelocity -= 1;
|
||||
}
|
||||
|
@ -197,6 +201,13 @@ public class Player extends GenericSprite {
|
|||
GamePanel.camera.x = LevelManager.xSpawn;
|
||||
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) {
|
||||
frame %= spriteArray[0][0].length;
|
||||
if (!upPressed && !downPressed && !leftPressed && !rightPressed) {
|
||||
|
|
Loading…
Reference in New Issue