Changed collisions to 2d array, fixed collision checking for sprites

master
Chara1236 2022-06-07 22:12:40 -04:00
parent b47b3cec95
commit 8f3f35a4af
7 changed files with 119 additions and 66 deletions

View File

@ -1,18 +1,18 @@
2wwwwwwwwwwwwwwwwe wwwwwwwwwwwwwwwwwe
sssssssssssssssssd sssssssssssssssssd
sssssssssssssssssd 1 sssssssssssssssssd 1
sssssssssssssssssd sssssssssssssssssd
sssssssssssssssssd sssssssssssssssssd
sssssssssssssssssd 1 1 1 1 1 sssssssssssssssssd 1 1 1 1 1
sssssssssssssssssd 1 sssssssssssssssssd w 1
sssssssssssssssssd sssssssssssssssssd w
sssssssssssssssssd qe qe sssssssssssssssssd w qe qe
sssssssssssssssssd ad ! ad sssssssssssssssssd w ad ! ad
sssssssssssssssssd qwe qwe atwwwrd sssssssssssssssssd w qwe qwe atwwwrd
sssssssssssssssssd asd asd zxxxxxc sssssssssssssssssd w asd asd zxxxxxc
sssssssssssssssssd 1 zxc zxc sssssssssssssssssd w 1 zxc zxc
sssssssssssssssssd sssssssssssssssssd qe
sssssssssssssssssd qwwwwwwwwe sssssssssssssssssd ad qwwwwwwwwe
sssssssssssssssssd qrsssssssstwe sssssssssssssssssd ad qrsssssssstwe
sssssssssssssssssd qwwrssssssssssstwe !!! qwe + sssssssssssssssssd ad qwwrssssssssssstwe !!! qwe +
ssssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrsssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrstwwwwww3 ssssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrsssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrstwwwwww3

View File

@ -1,4 +1,4 @@
2wwwwwwwwwwwwwwwwe wwwwwwwwwwwwwwwwwe
sssssssssssssssssd sssssssssssssssssd
sssssssssssssssssd 1 sssssssssssssssssd 1
sssssssssssssssssd sssssssssssssssssd

View File

@ -39,7 +39,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11]; public static BufferedImage[][][] playerSpriteArray = new BufferedImage[2][2][11];
public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3]; public static BufferedImage[][][] slimeSpriteArray = new BufferedImage[2][2][3];
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 ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>(); public static ArrayList<NonPlayer>enemy = new ArrayList<NonPlayer>();
public static StickyBomb b; public static StickyBomb b;
@ -123,15 +125,23 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//call the draw methods in each class to update positions as things move //call the draw methods in each class to update positions as things move
public void draw(Graphics g, int playerFrame, int enemyFrame){ public void draw(Graphics g, int playerFrame, int enemyFrame){
background.draw(g); background.draw(g);
for(int i=0; i<map.size(); i++){ //Don't want to draw off screen items
map.get(i).draw(g); int xMin = Math.max(0,((GamePanel.camera.x+GAME_WIDTH)/Tile.length)-(GAME_WIDTH/(2*Tile.length))-5);
int xMax = Math.min(map.length, 7+xMin + GAME_WIDTH/Tile.length);
for(int i=xMin; i<xMax; i++){
for(int j=0; j<map[0].length; j++){
if(map[i][j]!=null) {
map[i][j].draw(g);
}
}
} }
for(int i=0; i<enemy.size(); i++){ for(int i=0; i<enemy.size(); i++){
enemy.get(i).draw(g, enemyFrame); enemy.get(i).draw(g, enemyFrame);
} }
playerFrameCounter += player.draw(g, playerFrame); playerFrameCounter += player.draw(g, playerFrame);
b.draw(g); b.draw(g);
g.drawString(camera.x+" "+player.y,100,100); 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 //call the move methods in other classes to update positions
@ -147,8 +157,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//handles all collision detection and responds accordingly //handles all collision detection and responds accordingly
public void checkCollision() { public void checkCollision() {
player.isGrounded = false; player.isGrounded = false;
for (Tile i : map) { for(int i=0; i<map.length; i++){
i.update(); for(int j=0; j<map[0].length; j++){
if(map[i][j]!=null) {
map[i][j].update();
}
}
} }
for (NonPlayer n: enemy) { for (NonPlayer n: enemy) {
n.update(); n.update();

View File

@ -77,12 +77,25 @@ public class GenericSprite extends Rectangle{
} }
return false; return false;
} }
public boolean canUpdate(double x, double y){ public boolean canUpdate(double x, double y){
boolean canUpdate = true; boolean canUpdate = true;
for(Tile i: GamePanel.map){ int lowX = Math.max(0, (this.x+GamePanel.GAME_WIDTH/2)/Tile.length-4);
if(collide(i,this.x+x,this.y+y)){ int highX = Math.min(lowX + 8, GamePanel.map.length);
canUpdate = false; int lowY = Math.max(0,(this.y/Tile.length)-6);
break; int highY = Math.min(lowY + 12, GamePanel.map[0].length);
for(int i=lowX; i<highX; i++) {
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) {
LevelManager.nextLevel();
return true;
}
canUpdate = false;
break;
}
}
} }
} }
return canUpdate; return canUpdate;

View File

@ -16,11 +16,11 @@ public class LevelManager {
filePath = "saves/Level1.txt"; filePath = "saves/Level1.txt";
} else if(level == 2){ } else if(level == 2){
xSpawn = 200; xSpawn = 200;
ySpawn = 100; ySpawn = 0;
filePath = "saves/Level2.txt"; filePath = "saves/Level2.txt";
} }
try { try {
MapReader.inputMap(GamePanel.map, GamePanel.enemy,filePath); MapReader.inputMap(filePath);
} catch (IOException | SpriteException e) { } catch (IOException | SpriteException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch (UnsupportedAudioFileException e) { } catch (UnsupportedAudioFileException e) {

View File

@ -2,9 +2,14 @@ import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException; import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.*; import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner; import java.util.Scanner;
public class MapReader { public class MapReader {
private static int x = 0;
private static int y = 0;
private static int TileX = 0;
private static int TileY = 0;
//Input game map //Input game map
/* /*
1: Normal Grass 1: Normal Grass
@ -17,61 +22,72 @@ public class MapReader {
!: Slime !: Slime
Grass: Grass:
*/ */
public static void inputMap(ArrayList<Tile> map, ArrayList<NonPlayer>enemy , String filePath) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
enemy.clear(); public static void inputMap(String filePath) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
map.clear(); x = 0;
String file = FileManager.readFile(LevelManager.filePath); y = 0;
int x = -GamePanel.GAME_WIDTH/2 + Tile.length; GamePanel.enemy.clear();
int y = 0; for(int i=0; i<GamePanel.map.length; i++){
Arrays.fill(GamePanel.map[i], null);
}
String file = FileManager.readFile(filePath);
for(int i=0; i<file.length(); i++){ for(int i=0; i<file.length(); i++){
if(file.charAt(i)=='\n'){ if(file.charAt(i)=='\n'){
y+=Tile.length; y+=1;
x= -GamePanel.GAME_WIDTH/2; x=0;
} }
else if(file.charAt(i)=='1'){ TileX = x*Tile.length - (GamePanel.GAME_WIDTH/2);
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grass.png"))); if(y==0){
TileX += Tile.length;
}
TileY = y*Tile.length;
if(file.charAt(i)=='1'){
newTile("img/tiles/terrain/grass.png");
} else if(file.charAt(i)=='2'){ } else if(file.charAt(i)=='2'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassLeft.png"))); newTile("img/tiles/terrain/grassLeft.png");
} else if(file.charAt(i)=='3'){ } else if(file.charAt(i)=='3'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassRight.png"))); newTile("img/tiles/terrain/grassRight.png");
} else if(file.charAt(i)=='q'){ } else if(file.charAt(i)=='q'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassTopLeft.png"))); newTile("img/tiles/terrain/grassTopLeft.png");
} else if(file.charAt(i)=='w'){ } else if(file.charAt(i)=='w'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassMid.png"))); newTile("img/tiles/terrain/grassMid.png");
} else if(file.charAt(i)=='e'){ } else if(file.charAt(i)=='e'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassTopRight.png"))); newTile("img/tiles/terrain/grassTopRight.png");
} else if(file.charAt(i)=='a'){ } else if(file.charAt(i)=='a'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassMiddleLeft.png"))); newTile("img/tiles/terrain/grassMiddleLeft.png");
} else if(file.charAt(i)=='s'){ } else if(file.charAt(i)=='s'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassCenter.png"))); newTile("img/tiles/terrain/grassCenter.png");
map.get(map.size()-1).collision = false; GamePanel.map[x][y].collision = false;
} else if(file.charAt(i)=='d'){ } else if(file.charAt(i)=='d'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassMiddleRight.png"))); newTile("img/tiles/terrain/grassMiddleRight.png");
} else if(file.charAt(i)=='z'){ } else if(file.charAt(i)=='z'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassBottomLeft.png"))); newTile("img/tiles/terrain/grassBottomLeft.png");
} else if(file.charAt(i)=='x'){ } else if(file.charAt(i)=='x'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassBottomMiddle.png"))); newTile("img/tiles/terrain/grassBottomMiddle.png");
} else if(file.charAt(i)=='c'){ } else if(file.charAt(i)=='c'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassBottomRight.png"))); newTile("img/tiles/terrain/grassBottomRight.png");
} else if(file.charAt(i)=='r'){ } else if(file.charAt(i)=='r'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/cornerTopLeft.png"))); newTile("img/tiles/terrain/cornerTopLeft.png");
} else if(file.charAt(i)=='t'){ } else if(file.charAt(i)=='t'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/cornerTopRight.png"))); newTile("img/tiles/terrain/cornerTopRight.png");
} else if(file.charAt(i)=='f'){ } else if(file.charAt(i)=='f'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/cornerBottomLeft.png"))); newTile("img/tiles/terrain/cornerBottomLeft.png");
} else if(file.charAt(i)=='g'){ } else if(file.charAt(i)=='g'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/cornerBottomRight.png"))); newTile("img/tiles/terrain/cornerBottomRight.png");
} else if(file.charAt(i)=='b'){ } else if(file.charAt(i)=='b'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/boxes/box.png"))); newTile("img/tiles/boxes/box.png");
map.get(map.size()-1).collision = false; GamePanel.map[x][y].collision = false;
} else if(file.charAt(i)=='!'){ } else if(file.charAt(i)=='!'){
enemy.add(new NonPlayer(x, y, GamePanel.slimeSpriteArray, 50, 28, 100)); GamePanel.enemy.add(new NonPlayer(TileX, TileY, GamePanel.slimeSpriteArray, 50, 28, 100));
} else if(file.charAt(i)=='+') { } else if(file.charAt(i)=='+') {
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/boxes/finish.png"))); newTile("img/tiles/boxes/finish.png");
map.get(map.size()-1).isFinish = true; GamePanel.map[x][y].isFinish = true;
} }
x+=Tile.length; x+=1;
} }
} }
public static void newTile(String filePath) throws IOException, SpriteException {
GamePanel.map[x][y]=(new SingleTile(TileX,TileY, GamePanel.getImage(filePath)));
}
} }

View File

@ -76,6 +76,7 @@ public class Player extends GenericSprite {
} }
private boolean collide(Tile tile, double x, double y){ private boolean collide(Tile tile, double x, double y){
if(tile==null){return false;}
if(!tile.collision){ if(!tile.collision){
return false; return false;
} }
@ -88,14 +89,22 @@ public class Player extends GenericSprite {
public boolean canUpdate(double x, double y){ public boolean canUpdate(double x, double y){
boolean canUpdate = true; boolean canUpdate = true;
for(int i=0; i<GamePanel.map.size(); i++) { int lowX = Math.max(0, ((GamePanel.camera.x+GamePanel.GAME_WIDTH)/Tile.length)-4);
if(collide(GamePanel.map.get(i),this.x+x,this.y+y)){ int highX = Math.min(lowX + 8, GamePanel.map.length);
if(GamePanel.map.get(i).isFinish){ int lowY = Math.max(0,(this.y/Tile.length)-6);
LevelManager.nextLevel(); int highY = Math.min(lowY + 12, GamePanel.map[0].length);
return true; for(int i=lowX; i<highX; i++) {
for (int j = lowY; j < highY; j++) {
if (GamePanel.map != null) {
if (collide(GamePanel.map[i][j], this.x + x, this.y + y)) {
if (GamePanel.map[i][j].isFinish) {
LevelManager.nextLevel();
return true;
}
canUpdate = false;
break;
}
} }
canUpdate = false;
break;
} }
} }
return canUpdate; return canUpdate;
@ -138,9 +147,10 @@ public class Player extends GenericSprite {
GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity; GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity;
} else if(canUpdate(0,yVelocity)){ } else if(canUpdate(0,yVelocity)){
y = y + (int) yVelocity; y = y + (int) yVelocity;
xVelocity*=0.75;
} else if(canUpdate(xVelocity,0)){ } else if(canUpdate(xVelocity,0)){
GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity; GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity;
xVelocity*=0.2;
} }
if(rightPressed && xVelocity<5){ if(rightPressed && xVelocity<5){
@ -157,7 +167,7 @@ public class Player extends GenericSprite {
} }
yVelocity = -10; yVelocity = -10;
} }
xVelocity*=0.9; xVelocity*=0.93;
if(!isGrounded) { if(!isGrounded) {
yVelocity += 0.3; yVelocity += 0.3;
if(downPressed){ if(downPressed){