Finished Collision Physics

master
Chara1236 2022-06-05 11:53:03 -04:00
parent 1421495745
commit e8a1dea2d6
5 changed files with 92 additions and 43 deletions

View File

@ -5,13 +5,14 @@
00010000 00010000
00010000 00010000
00010000 00010000
00010000 00010100
00000000 00000000
00000000 00000000
00000000 10000001
00000000 0000000011000100000000000101010100110100
00000000 0000000000000000000000000000000000000000000101000000111111111111111111111
00000000 100000000000000000000000000000000000000000011000001000000000000000000000
00000000 100000000000000000000000000000000000000000011000001000000000000000000000
00000000 10000000000000000000010000bb00001000000000010000000000000000000000000
23333334 10000000000000000000000000b000000000000000010000000000000011111111111111111
2333333333333333333333333333333333333333333333341111111111

View File

@ -66,10 +66,6 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
} }
player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, 'W', 'A', 'S', 'D', spriteArray); //create a player controlled player, set start location to middle of screenk player = new Player(GAME_WIDTH/2, GAME_HEIGHT/2, 'W', 'A', 'S', 'D', spriteArray); //create a player controlled player, set start location to middle of screenk
// the height of 35 is set because it is half of the original tile height (i.e., 70px) // the height of 35 is set because it is half of the original tile height (i.e., 70px)
map.add(new SingleTile(1000, 500, box));
map.add(new SingleTile(700, 400, boxCoin));
map.add(new SingleTile(1000, 300, boxCoin));
map.add(new Tile(700, 200));
this.setFocusable(true); //make everything in this class appear on the screen this.setFocusable(true); //make everything in this class appear on the screen
this.addKeyListener(this); //start listening for keyboard input this.addKeyListener(this); //start listening for keyboard input
@ -99,10 +95,11 @@ 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 frame){ public void draw(Graphics g, int frame){
background.draw(g); background.draw(g);
frameCounter += player.draw(g, frame);
for(Tile i: map){ for(int i=0; i<map.size(); i++){
i.draw(g); map.get(i).draw(g);
} }
frameCounter += player.draw(g, frame);
g.drawString(camera.x+" "+map.get(0).x,100,100); g.drawString(camera.x+" "+map.get(0).x,100,100);
} }
@ -117,7 +114,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{
player.isGrounded = false; player.isGrounded = false;
for (Tile i : map) { for (Tile i : map) {
i.collide(player); i.update();
} }
//force player to remain on screen //force player to remain on screen
if (player.y <= 0) { if (player.y <= 0) {

View File

@ -12,12 +12,12 @@ public class MapReader {
*/ */
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(filePath);
int x = 0; int x = -GamePanel.WIDTH*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=0; x= -GamePanel.WIDTH*Tile.length;
} }
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")));
@ -27,6 +27,9 @@ public class MapReader {
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassMid.png"))); map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/terrain/grassMid.png")));
} else if(file.charAt(i)=='4'){ } 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)=='b'){
map.add(new SingleTile(x,y, GamePanel.getImage("img/tiles/boxes/box.png")));
map.get(map.size()-1).collision = false;
} }
x+=Tile.length; x+=Tile.length;
} }

View File

@ -53,33 +53,84 @@ public class Player extends GenericSprite {
downPressed = true; downPressed = true;
} }
move();
} }
// stops moving paddle when key is released // stops moving paddle when key is released
public void keyReleased(KeyEvent e) { public void keyReleased(KeyEvent e) {
if(e.getKeyChar() == 'd'){ if(e.getKeyChar() == 'd'){
rightPressed = false; rightPressed = false;
move();
} }
if(e.getKeyChar() == 'a'){ if(e.getKeyChar() == 'a'){
leftPressed = false; leftPressed = false;
move();
} }
if(e.getKeyChar() == 'w'){ if(e.getKeyChar() == 'w'){
upPressed = false; upPressed = false;
move();
} }
if(e.getKeyChar() == 's'){ if(e.getKeyChar() == 's'){
downPressed = false; downPressed = false;
move();
} }
} }
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){
return false;
}
if(x+PLAYER_WIDTH>tile.realX&&x<tile.realX+Tile.length&&y-tile.y<Tile.length&&tile.y-y<PLAYER_HEIGHT){
return true;
}
return false;
}
// calls parent // calls parent
private 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(){
y = y + (int)yVelocity;
GamePanel.camera.x = GamePanel.camera.x + (int)xVelocity; if(!canUpdate(xVelocity, 0)){
int updateAmount = 0;
if(xVelocity>0){
while(canUpdate(updateAmount, 0)){
updateAmount++;
}
GamePanel.camera.x+=updateAmount-1;
} else if(xVelocity<0){
while(canUpdate(updateAmount, 0)){
updateAmount--;
}
GamePanel.camera.x+=updateAmount+1;
}
xVelocity = 0;
} 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(xVelocity, yVelocity)) {
y = y + (int) yVelocity;
GamePanel.camera.x = GamePanel.camera.x + (int) xVelocity;
}
if(rightPressed){ if(rightPressed){
xVelocity+=1; xVelocity+=1;
} }
@ -87,11 +138,17 @@ public class Player extends GenericSprite {
xVelocity -= 1; xVelocity -= 1;
} }
if(upPressed&isGrounded){ if(upPressed&isGrounded){
y-=1;
isGrounded = false;
if(canUpdate(0,-8)) {
jump.start(); jump.start();
}
yVelocity = -10; yVelocity = -10;
} }
xVelocity*=0.9; xVelocity*=0.9;
yVelocity+=0.3; if(!isGrounded) {
yVelocity += 0.3;
}
capSpeed(); capSpeed();
} }

View File

@ -4,28 +4,19 @@ import java.awt.*;
public class Tile { public class Tile {
public int x; public int x;
public int y; public int y;
public boolean collision;
public int realX;
public static final int length = 35; public static final int length = 35;
public Tile(int x, int y){ public Tile(int x, int y){
collision = true;
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
public void update(){
realX = x-GamePanel.camera.x;
}
//Actions when tile interacts with sprites
public void collide(GenericSprite s){
int realX = x-GamePanel.camera.x;
if(s.x+s.WIDTH>realX&s.x<realX+length&&s.y+s.HEIGHT>=y&&s.y<=y+length){
if(s.x+s.WIDTH>realX+20&&s.x<realX+length-20&&y-s.y-s.HEIGHT<4){
s.isGrounded = true;
s.yVelocity = 0;
s.y = y-s.HEIGHT;
}
if(s.x+s.WIDTH<realX+length/2){
GamePanel.camera.x = x-s.WIDTH-GamePanel.GAME_WIDTH/2;
} else if(s.x>realX+length-length/2){
GamePanel.camera.x = x+length-GamePanel.GAME_WIDTH/2;
}
}
}
public void draw(Graphics g){ public void draw(Graphics g){
g.setColor(Color.black); g.setColor(Color.black);
g.fillRect(x-GamePanel.camera.x, y, length, length); g.fillRect(x-GamePanel.camera.x, y, length, length);