diff --git a/src/Camera.java b/src/Camera.java new file mode 100644 index 0000000..5f84198 --- /dev/null +++ b/src/Camera.java @@ -0,0 +1,7 @@ + +public class Camera { + public int x; + public Camera(int x){ + this.x = x; + } +} diff --git a/src/GamePanel.java b/src/GamePanel.java index 3aef5ad..ea9bedd 100644 --- a/src/GamePanel.java +++ b/src/GamePanel.java @@ -34,7 +34,9 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ public static ArrayListmap = new ArrayList(); + public static Camera camera; public GamePanel(){ + camera = new Camera(0); for (int i = 0; i < 11; i++) { try { BufferedImage sprite = getImage(String.format("img/walk/p1_walk%s.png", String.format("%1$2s", i+1).replace(' ', '0'))); @@ -85,7 +87,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener{ for(Tile i: map){ i.draw(g); } - + g.drawString(camera.x+" "+map.get(0).x,100,100); } //call the move methods in other classes to update positions diff --git a/src/GenericSprite.java b/src/GenericSprite.java index abffe35..ce3c20d 100644 --- a/src/GenericSprite.java +++ b/src/GenericSprite.java @@ -29,44 +29,13 @@ public class GenericSprite extends Rectangle{ //updates the direction of the ball based on user input //if the keyboard input isn't any of the options (d, a, w, s), then nothing happens public void keyPressed(KeyEvent e){ - if(e.getKeyChar() == 'd'){ - rightPressed = true; - } - if(e.getKeyChar() == 'a'){ - leftPressed = true; - - } - if(e.getKeyChar() == 'w'){ - upPressed = true; - - } - if(e.getKeyChar() == 's'){ - downPressed = true; - - } - move(); } //called from GamePanel when any key is released (no longer being pressed down) //Makes the ball stop moving in that direction public void keyReleased(KeyEvent e){ - if(e.getKeyChar() == 'd'){ - rightPressed = false; - move(); - } - if(e.getKeyChar() == 'a'){ - leftPressed = false; - move(); - } - if(e.getKeyChar() == 'w'){ - upPressed = false; - move(); - } - if(e.getKeyChar() == 's'){ - downPressed = false; - move(); - } + } //called from GamePanel whenever a mouse click is detected @@ -78,20 +47,6 @@ public class GenericSprite extends Rectangle{ public void move(){ - y = y + (int)yVelocity; - x = x + (int)xVelocity; - if(rightPressed){ - xVelocity+=1; - } - if(leftPressed) { - xVelocity -= 1; - } - if(upPressed&isGrounded){ - yVelocity = -10; - } - xVelocity*=0.9; - yVelocity+=0.3; - capSpeed(); } public void capSpeed(){ diff --git a/src/Level1 b/src/Level1 new file mode 100644 index 0000000..b228584 --- /dev/null +++ b/src/Level1 @@ -0,0 +1,5 @@ +10000000 +01101101 +00000000 +00000000 +00010000 \ No newline at end of file diff --git a/src/MapReader.java b/src/MapReader.java new file mode 100644 index 0000000..0be82a3 --- /dev/null +++ b/src/MapReader.java @@ -0,0 +1,8 @@ +import java.io.File; +import java.util.ArrayList; + +public class MapReader { + public static void inputMap(ArrayList map, File file){ + + } +} diff --git a/src/Player.java b/src/Player.java index 533abcc..d8e4858 100644 --- a/src/Player.java +++ b/src/Player.java @@ -32,17 +32,69 @@ public class Player extends GenericSprite { // moves paddle when key is pressed public void keyPressed(KeyEvent e) { - super.keyPressed(e); + if(e.getKeyChar() == 'd'){ + rightPressed = true; + + } + if(e.getKeyChar() == 'a'){ + leftPressed = true; + + } + if(e.getKeyChar() == 'w'){ + upPressed = true; + + } + if(e.getKeyChar() == 's'){ + downPressed = true; + + } + move(); } // stops moving paddle when key is released public void keyReleased(KeyEvent e) { - super.keyReleased(e); + if(e.getKeyChar() == 'd'){ + rightPressed = false; + move(); + } + if(e.getKeyChar() == 'a'){ + leftPressed = false; + move(); + } + if(e.getKeyChar() == 'w'){ + upPressed = false; + move(); + } + if(e.getKeyChar() == 's'){ + downPressed = false; + move(); + } } // calls parent - public void move() { - super.move(); + public void move(){ + y = y + (int)yVelocity; + GamePanel.camera.x = GamePanel.camera.x + (int)xVelocity; + if(rightPressed){ + xVelocity+=1; + } + if(leftPressed) { + xVelocity -= 1; + } + if(upPressed&isGrounded){ + yVelocity = -10; + } + xVelocity*=0.9; + yVelocity+=0.3; + capSpeed(); + } + + public void capSpeed(){ + if(xVelocity>speedCap){ + xVelocity = speedCap; + } else if(xVelocity<-1*speedCap) { + xVelocity = -1*speedCap; + } } public int draw(Graphics g, int frame) { diff --git a/src/Tile.java b/src/Tile.java index 62e667f..5be1474 100644 --- a/src/Tile.java +++ b/src/Tile.java @@ -11,21 +11,22 @@ public class Tile { //Actions when tile interacts with sprites public void collide(GenericSprite s){ - if(s.x+s.WIDTH>x&&s.x=y&&s.y<=y+length){ - if(s.x+s.WIDTH>x+20&&s.xrealX&s.x=y&&s.y<=y+length){ + if(s.x+s.WIDTH>realX+20&&s.xx+length-length/2){ - s.x = x+length; + if(s.x+s.WIDTHrealX+length-length/2){ + GamePanel.camera.x = x+length-GamePanel.GAME_WIDTH/2; } } } public void draw(Graphics g){ g.setColor(Color.black); - g.fillRect(x, y, length, length); + g.fillRect(x-GamePanel.camera.x, y, length, length); } }