diff --git a/saves/Level1.txt b/saves/Level1.txt index 3879e91..27d0633 100644 --- a/saves/Level1.txt +++ b/saves/Level1.txt @@ -5,13 +5,14 @@ 00010000 00010000 00010000 -00010000 +00010100 00000000 00000000 -00000000 -00000000 -00000000 -00000000 -00000000 -00000000 -23333334 \ No newline at end of file +10000001 +0000000011000100000000000101010100110100 +0000000000000000000000000000000000000000000101000000111111111111111111111 +100000000000000000000000000000000000000000011000001000000000000000000000 +100000000000000000000000000000000000000000011000001000000000000000000000 +10000000000000000000010000bb00001000000000010000000000000000000000000 +10000000000000000000000000b000000000000000010000000000000011111111111111111 +2333333333333333333333333333333333333333333333341111111111 \ No newline at end of file diff --git a/src/GamePanel.java b/src/GamePanel.java index 8918f46..bb788f6 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -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 // 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.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 public void draw(Graphics g, int frame){ background.draw(g); - frameCounter += player.draw(g, frame); - for(Tile i: map){ - i.draw(g); + + for(int i=0; i map, String filePath) throws IOException, SpriteException { String file = FileManager.readFile(filePath); - int x = 0; + int x = -GamePanel.WIDTH*Tile.length; int y = 0; for(int i=0; iPLAYER_WIDTH&&x-tile.realXtile.realX&&x0){ + 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){ xVelocity+=1; } @@ -87,11 +138,17 @@ public class Player extends GenericSprite { xVelocity -= 1; } if(upPressed&isGrounded){ - jump.start(); + y-=1; + isGrounded = false; + if(canUpdate(0,-8)) { + jump.start(); + } yVelocity = -10; } xVelocity*=0.9; - yVelocity+=0.3; + if(!isGrounded) { + yVelocity += 0.3; + } capSpeed(); } diff --git a/src/Tile.java b/src/Tile.java index ade3e1f..66242cb 100644 --- a/src/Tile.java +++ b/src/Tile.java @@ -4,28 +4,19 @@ import java.awt.*; public class Tile { public int x; public int y; + + public boolean collision; + public int realX; public static final int length = 35; public Tile(int x, int y){ + collision = true; this.x = x; this.y = y; } - - //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=y&&s.y<=y+length){ - if(s.x+s.WIDTH>realX+20&&s.xrealX+length-length/2){ - GamePanel.camera.x = x+length-GamePanel.GAME_WIDTH/2; - } - } + public void update(){ + realX = x-GamePanel.camera.x; } + public void draw(Graphics g){ g.setColor(Color.black); g.fillRect(x-GamePanel.camera.x, y, length, length);