Fixing Collisions, added level manager

master
Chara1236 2022-06-05 17:33:24 -04:00
parent 76d914955f
commit f5b809473a
19 changed files with 152 additions and 45 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 963 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 953 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 937 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 942 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 993 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 965 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1002 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 844 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@ -1,18 +1,18 @@
10000000 2wwwwwwwwwwwwwwwwe
01101101 sssssssssssssssssd
00000000 sssssssssssssssssd 1
00000000 sssssssssssssssssd
00010000 sssssssssssssssssd
00010000 sssssssssssssssssd 1 1 1 1 1
00010000 sssssssssssssssssd 1
00010100 sssssssssssssssssd
00000000 sssssssssssssssssd
00000000 sssssssssssssssssd
10000001 sssssssssssssssssd qwe qwe
0000000011000100000000000101010100110100 sssssssssssssssssd asd asd
0000000000000000000000000000000000000000000101000000111111111111111111111 sssssssssssssssssd 1 zxc zxc
100000000000000000000000000000000000000000011000001000000000000000000000 sssssssssssssssssd
100000000000000000000000000000000000000000011000001000000000000000000000 sssssssssssssssssd qwwwwwwwwe
10000000000000000000010000bb00001000000000010000000000000000000000000 sssssssssssssssssd qrsssssssstwe
10000000000000000000000000b000000000000000010000000000000011111111111111111 sssssssssssssssssd qwwrssssssssssstwe qwe
2333333333333333333333333333333333333333333333341111111111 ssssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrsssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrstwwwwww3

18
saves/Level2.txt Normal file
View File

@ -0,0 +1,18 @@
2wwwwwwwwwwwwwwwwe
sssssssssssssssssd
sssssssssssssssssd 1
sssssssssssssssssd
sssssssssssssssssd
sssssssssssssssssd 1 1 1 1 1
sssssssssssssssssd 1
sssssssssssssssssd
sssssssssssssssssd
sssssssssssssssssd
sssssssssssssssssd qwe qwe
sssssssssssssssssd asd asd
sssssssssssssssssd bbb 1 zxc zxc
sssssssssssssssssdbbbbbbbbbbbbbbbbbbbbbbb
sssssssssssssssssd qwwwwwwwwe
sssssssssssssssssd qrsssssssstwe
sssssssssssssssssd bbb qwwrssssssssssstwe qwe
ssssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrsssssssssssssssstwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwrstwwwwww3

View File

@ -138,10 +138,8 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
for (Tile i : map) { for (Tile i : map) {
i.update(); i.update();
} }
//force player to remain on screen //force player to remain on screen (For the most part)
if (player.y <= 0) {
player.y = 0;
}
if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) { if (player.y >= GAME_HEIGHT - Player.PLAYER_HEIGHT) {
player.y = GAME_HEIGHT - Player.PLAYER_HEIGHT; player.y = GAME_HEIGHT - Player.PLAYER_HEIGHT;
player.yVelocity = 0; player.yVelocity = 0;
@ -165,6 +163,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
//run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen //run() method is what makes the game continue running without end. It calls other methods to move objects, check for collision, and update the screen
public void run(){ public void run(){
LevelManager.setLevel(1);
try { try {
MapReader.inputMap(map, "saves/Level1.txt"); MapReader.inputMap(map, "saves/Level1.txt");
} catch (IOException | SpriteException e) { } catch (IOException | SpriteException e) {

19
src/LevelManager.java Normal file
View File

@ -0,0 +1,19 @@
public class LevelManager {
public static int level = 1;
public static int xSpawn = 0;
public static int ySpawn = 600;
public static String filePath;
public static void setLevel(int level){
LevelManager.level = level;
if(level == 1){
xSpawn = 0;
ySpawn = 600;
filePath = "saves/Level1.txt";
} else if(level == 2){
xSpawn = 0;
ySpawn = 600;
filePath = "saves/Level2.txt";
}
}
}

View File

@ -6,27 +6,56 @@ public class MapReader {
//Input game map //Input game map
/* /*
1: Normal Grass 1: Normal Grass
2: Left Grass 2: Left Grass:
3: Middle Grass 3: Right Grass:
4: Right Grass Grass Tiling:
qwe
asd
zxc
!: Slime
Grass:
*/ */
public static void inputMap(ArrayList<Tile> map, String filePath) throws IOException, SpriteException { public static void inputMap(ArrayList<Tile> map, String filePath) throws IOException, SpriteException {
String file = FileManager.readFile(filePath); String file = FileManager.readFile(LevelManager.filePath);
int x = -GamePanel.WIDTH*Tile.length; int x = -GamePanel.GAME_WIDTH/2 + Tile.length;
int y = 0; int y = 0;
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+=Tile.length;
x= -GamePanel.WIDTH*Tile.length; x= -GamePanel.GAME_WIDTH/2;
} }
else if(file.charAt(i)=='1'){ else if(file.charAt(i)=='1'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grass.png"))); map.add(new SingleTile(x,y, GamePanel.getImage("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"))); map.add(new SingleTile(x,y, GamePanel.getImage("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/grassMid.png")));
} else if(file.charAt(i)=='4'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassRight.png"))); map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassRight.png")));
} else if(file.charAt(i)=='q'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassTopLeft.png")));
} else if(file.charAt(i)=='w'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassMid.png")));
} else if(file.charAt(i)=='e'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassTopRight.png")));
} else if(file.charAt(i)=='a'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassMiddleLeft.png")));
} else if(file.charAt(i)=='s'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassCenter.png")));
} else if(file.charAt(i)=='d'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassMiddleRight.png")));
} else if(file.charAt(i)=='z'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassBottomLeft.png")));
} else if(file.charAt(i)=='x'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassBottomMiddle.png")));
} else if(file.charAt(i)=='c'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassBottomRight.png")));
} else if(file.charAt(i)=='r'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/cornerTopLeft.png")));
} else if(file.charAt(i)=='t'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/cornerTopRight.png")));
} else if(file.charAt(i)=='f'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/cornerBottomLeft.png")));
} else if(file.charAt(i)=='g'){
map.add(new SingleTile(x,y, GamePanel.getImage("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"))); map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/boxes/box.png")));
map.get(map.size()-1).collision = false; map.get(map.size()-1).collision = false;

View File

@ -31,11 +31,51 @@ public class NonPlayer extends GenericSprite {
xVelocity = 3; xVelocity = 3;
} }
private boolean collide(Tile tile, double x, double y){
if(!tile.collision){
return false;
}
if(x+npcWidth>tile.x&&x<tile.x+Tile.length&&y-tile.y<Tile.length&&tile.y-y<npcHeight){
return true;
}
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;
}
}
return canUpdate;
}
public void move(){ public void move(){
x += (int)xVelocity; if(!canUpdate(xVelocity, 0)){
y += (int)yVelocity; xVelocity*=-1;
}
if(!canUpdate(0, yVelocity)){
if(yVelocity>0){
while(canUpdate(0,1)){
y+=1;
}
isGrounded = true;
} else if(yVelocity<0){
while(canUpdate(0,-1)){
y-=1;
}
}
yVelocity = 0;
}
if(canUpdate(0, yVelocity)) {
y = y + (int) yVelocity;
x = x + (int) xVelocity;
}
if(!isGrounded) {
yVelocity += 0.3; yVelocity += 0.3;
}
capSpeed(); capSpeed();
} }

View File

@ -12,8 +12,8 @@ import java.io.IOException;
public class Player extends GenericSprite { public class Player extends GenericSprite {
public final int SPEED = 5; public final int SPEED = 5;
public static final int PLAYER_WIDTH = 72; public static final int PLAYER_WIDTH = 64;
public static final int PLAYER_HEIGHT = 97; public static final int PLAYER_HEIGHT = 94;
public int lastXDirection, lastYDirection, lastFrame; public int lastXDirection, lastYDirection, lastFrame;
public int upKey, downKey, rightKey, leftKey; public int upKey, downKey, rightKey, leftKey;
@ -72,11 +72,6 @@ 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.realX-x>PLAYER_WIDTH&&x-tile.realX<Tile.length){
// System.out.println("Ligma");
// return true;
// }
// System.out.println("")
if(!tile.collision){ if(!tile.collision){
return false; return false;
} }
@ -87,7 +82,7 @@ public class Player extends GenericSprite {
} }
// calls parent // calls parent
private boolean canUpdate(double x, double y){ public boolean canUpdate(double x, double y){
boolean canUpdate = true; boolean canUpdate = true;
for(Tile i: GamePanel.map){ for(Tile i: GamePanel.map){
if(collide(i,this.x+x,this.y+y)){ if(collide(i,this.x+x,this.y+y)){
@ -99,6 +94,9 @@ public class Player extends GenericSprite {
} }
public void move(){ public void move(){
if(canUpdate(xVelocity, 0)&&canUpdate(0, yVelocity)&&!canUpdate(xVelocity, yVelocity)){
GamePanel.camera.x += -Math.signum(xVelocity);
}
if(!canUpdate(xVelocity, 0)){ if(!canUpdate(xVelocity, 0)){
int updateAmount = 0; int updateAmount = 0;
if(xVelocity>0){ if(xVelocity>0){
@ -113,13 +111,14 @@ public class Player extends GenericSprite {
GamePanel.camera.x+=updateAmount+1; GamePanel.camera.x+=updateAmount+1;
} }
xVelocity = 0; xVelocity = 0;
} if(!canUpdate(0, yVelocity)){ }
if(!canUpdate(0, yVelocity)){
if(yVelocity>0){ if(yVelocity>0){
while(canUpdate(0,1)){ while(canUpdate(0,1)){
y+=1; y+=1;
} }
isGrounded = true; isGrounded = true;
} else if(yVelocity>0){ } else if(yVelocity<0){
while(canUpdate(0,-1)){ while(canUpdate(0,-1)){
y-=1; y-=1;
} }
@ -148,6 +147,9 @@ public class Player extends GenericSprite {
xVelocity*=0.9; xVelocity*=0.9;
if(!isGrounded) { if(!isGrounded) {
yVelocity += 0.3; yVelocity += 0.3;
if(downPressed){
yVelocity+=1;
}
} }
capSpeed(); capSpeed();
} }
@ -155,13 +157,13 @@ public class Player extends GenericSprite {
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) {
g.drawImage(spriteArray[lastXDirection][lastYDirection][0], x, y, null); g.drawImage(spriteArray[lastXDirection][lastYDirection][0], x-6, y, null);
return 0; return 0;
} else { } else {
lastXDirection = (int)(Math.signum(xVelocity) + 1) / 2; lastXDirection = (int)(Math.signum(xVelocity) + 1) / 2;
lastYDirection = (int)(Math.signum(yVelocity) + 1) / 2; lastYDirection = (int)(Math.signum(yVelocity) + 1) / 2;
lastFrame = frame; lastFrame = frame;
g.drawImage(spriteArray[lastXDirection][lastYDirection][frame], x, y, null); g.drawImage(spriteArray[lastXDirection][lastYDirection][frame], x-6, y, null);
return 1; return 1;
} }
} }