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 1
sssssssssssssssssd
sssssssssssssssssd
sssssssssssssssssd 1 1 1 1 1
sssssssssssssssssd 1
sssssssssssssssssd
sssssssssssssssssd qe qe
sssssssssssssssssd ad ! ad
sssssssssssssssssd qwe qwe atwwwrd
sssssssssssssssssd asd asd zxxxxxc
sssssssssssssssssd 1 zxc zxc
sssssssssssssssssd
sssssssssssssssssd qwwwwwwwwe
sssssssssssssssssd qrsssssssstwe
sssssssssssssssssd qwwrssssssssssstwe !!! qwe +
sssssssssssssssssd w 1
sssssssssssssssssd w
sssssssssssssssssd w qe qe
sssssssssssssssssd w ad ! ad
sssssssssssssssssd w qwe qwe atwwwrd
sssssssssssssssssd w asd asd zxxxxxc
sssssssssssssssssd w 1 zxc zxc
sssssssssssssssssd qe
sssssssssssssssssd ad qwwwwwwwwe
sssssssssssssssssd ad qrsssssssstwe
sssssssssssssssssd ad qwwrssssssssssstwe !!! qwe +
ssssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrsssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrstwwwwww3

View File

@ -1,4 +1,4 @@
2wwwwwwwwwwwwwwwwe
wwwwwwwwwwwwwwwwwe
sssssssssssssssssd
sssssssssssssssssd 1
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[][][] 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 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
public void draw(Graphics g, int playerFrame, int enemyFrame){
background.draw(g);
for(int i=0; i<map.size(); i++){
map.get(i).draw(g);
//Don't want to draw off screen items
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++){
enemy.get(i).draw(g, enemyFrame);
}
playerFrameCounter += player.draw(g, playerFrame);
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
@ -147,8 +157,12 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//handles all collision detection and responds accordingly
public void checkCollision() {
player.isGrounded = false;
for (Tile i : map) {
i.update();
for(int i=0; i<map.length; i++){
for(int j=0; j<map[0].length; j++){
if(map[i][j]!=null) {
map[i][j].update();
}
}
}
for (NonPlayer n: enemy) {
n.update();

View File

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

View File

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

View File

@ -2,9 +2,14 @@ import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
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
/*
1: Normal Grass
@ -17,61 +22,72 @@ public class MapReader {
!: Slime
Grass:
*/
public static void inputMap(ArrayList<Tile> map, ArrayList<NonPlayer>enemy , String filePath) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
enemy.clear();
map.clear();
String file = FileManager.readFile(LevelManager.filePath);
int x = -GamePanel.GAME_WIDTH/2 + Tile.length;
int y = 0;
public static void inputMap(String filePath) throws IOException, SpriteException, UnsupportedAudioFileException, LineUnavailableException {
x = 0;
y = 0;
GamePanel.enemy.clear();
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++){
if(file.charAt(i)=='\n'){
y+=Tile.length;
x= -GamePanel.GAME_WIDTH/2;
y+=1;
x=0;
}
else if(file.charAt(i)=='1'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grass.png")));
TileX = x*Tile.length - (GamePanel.GAME_WIDTH/2);
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'){
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'){
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'){
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'){
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'){
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'){
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'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassCenter.png")));
map.get(map.size()-1).collision = false;
newTile("img/tiles/terrain/grassCenter.png");
GamePanel.map[x][y].collision = false;
} 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'){
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'){
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'){
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'){
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'){
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'){
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'){
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'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/boxes/box.png")));
map.get(map.size()-1).collision = false;
newTile("img/tiles/boxes/box.png");
GamePanel.map[x][y].collision = false;
} 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)=='+') {
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/boxes/finish.png")));
map.get(map.size()-1).isFinish = true;
newTile("img/tiles/boxes/finish.png");
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){
if(tile==null){return false;}
if(!tile.collision){
return false;
}
@ -88,14 +89,22 @@ public class Player extends GenericSprite {
public boolean canUpdate(double x, double y){
boolean canUpdate = true;
for(int i=0; i<GamePanel.map.size(); i++) {
if(collide(GamePanel.map.get(i),this.x+x,this.y+y)){
if(GamePanel.map.get(i).isFinish){
LevelManager.nextLevel();
return true;
int lowX = Math.max(0, ((GamePanel.camera.x+GamePanel.GAME_WIDTH)/Tile.length)-4);
int highX = Math.min(lowX + 8, GamePanel.map.length);
int lowY = Math.max(0,(this.y/Tile.length)-6);
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 != 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;
@ -138,9 +147,10 @@ public class Player extends GenericSprite {
GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity;
} else if(canUpdate(0,yVelocity)){
y = y + (int) yVelocity;
xVelocity*=0.75;
} else if(canUpdate(xVelocity,0)){
GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity;
xVelocity*=0.2;
}
if(rightPressed && xVelocity<5){
@ -157,7 +167,7 @@ public class Player extends GenericSprite {
}
yVelocity = -10;
}
xVelocity*=0.9;
xVelocity*=0.93;
if(!isGrounded) {
yVelocity += 0.3;
if(downPressed){