Browse Source

Some code optimalisation

jancoow 10 năm trước cách đây
mục cha
commit
eae94dc699

+ 2 - 2
src/model/GameModel.java

@@ -9,8 +9,8 @@ import main.Window;
 
 public class GameModel implements ActionListener {
 	
-	Timer t;
-	GameStateManager gsm;
+	private Timer t;
+	private GameStateManager gsm;
 	
 	public GameModel(GameStateManager gsm)
 	{

+ 15 - 21
src/model/objects/Ball.java

@@ -11,8 +11,8 @@ public class Ball {
 	private int size, bounceheight;
 	private Ellipse2D.Double bal;
 
-	double rx = 100, ry = 100; // position
-	double vx = 1, vy = 1; // velocity
+	double rx, ry; 			// position
+	double vx = 1, vy = 1; 	// velocity
 
 	public Ball(int size, int bounceheight, Color color, int x, int y, int direction) {
 		this.color = color;
@@ -24,6 +24,16 @@ public class Ball {
 		bal = new Ellipse2D.Double(x, y, this.size, this.size);
 	}
 
+	public boolean hitLine(ShootingLine l){
+		return bal.intersects(l.getX(), l.getY()-l.getHeight(), l.getWidth(), l.getHeight());
+	}
+	
+	public boolean hitPlayer(Player p){
+		return bal.intersects(p.getX(),p.getY(),p.getWidth(),p.getHeigth());
+	}
+	
+	//** Drawing and Calculating **//
+	
 	public void paint(Graphics2D g2d) {
 		g2d.setColor(color);
 		g2d.fill(bal);
@@ -46,7 +56,9 @@ public class Ball {
 		setX((int) rx);
 		setY((int) ry);
 	}
-
+	
+	//** Getters and Setters **//
+	
 	public int getSize() {
 		return size/20;
 	}
@@ -82,28 +94,10 @@ public class Ball {
 	public void setY(int y) {
 		bal.setFrame(getX(), y, getWidth(), getHeight());
 	}
-
-	public void setWidth(int w) {
-		bal.setFrame(getX(), getY(), w, getHeight());
-	}
-
-	public void setHeight(int h) {
-		bal.setFrame(getX(), getY(), getWidth(), h);
-	}
 	
 	public void setDirection(int d){
 		if(d == -1 || d == 1){
 			vx = d;
 		}
 	}
-	
-	public boolean hitLine(ShootingLine l){
-		return bal.intersects(l.getX(), l.getY()-l.getHeight(), l.getWidth(), l.getHeight());
-	}
-	
-	public boolean hitPlayer(Player p){
-		return bal.intersects(p.getX(),p.getY(),p.getWidth(),p.getHeigth());
-	}
-	
-
 }

+ 3 - 3
src/model/objects/DrawObject.java

@@ -10,8 +10,7 @@ public class DrawObject {
 	protected Point2D position;
 	protected double scale;
 	protected double rotation;
-	
-	BufferedImage image;
+	protected BufferedImage image;
 	
 	public DrawObject(BufferedImage image)
 	{
@@ -38,7 +37,8 @@ public class DrawObject {
 		return tx;
 	}
 	
-	/* HELPER FUNCTIONS */
+	/* Helper Functions */
+	
 	public void setX(double x)
 	{
 		position.setLocation(x, position.getY());

+ 12 - 16
src/model/objects/Player.java

@@ -5,14 +5,11 @@ import java.util.ArrayList;
 
 public class Player extends DrawObject {
 
-	private int score;
-	private int health;
+	private int score, health, speed = 10;
 	private ArrayList<PowerUp> powerups;
 	
 	private long lastMovement;
-	private int lastX;
-	private int lastDirection;
-	private int frame;
+	private int lastDirection, frame;
 	private BufferedImage spriteimage;
 	
 	public Player(BufferedImage image, int x, int y) {
@@ -23,26 +20,25 @@ public class Player extends DrawObject {
 	}
 	
 	public void update(){
-		if(System.currentTimeMillis() - lastMovement >= 200){
+		if(System.currentTimeMillis() - lastMovement >= 100){	//If the player didn't move for 200ms, look forward
 			image = spriteimage.getSubimage(38, 0, 40, 54);
 			frame = 0;
-		}else{
+		}else{ 													//Look into the right walking direction
 			frame++;
 			frame %= 1000;
 			image = spriteimage.getSubimage(2+(((frame / 4) % 4) * 38), 54*lastDirection, 38, 50);
 		}
 	}
 	
-	public void setX(double x)
-	{
-		lastX = (int) super.getX();
-		super.setX(x);
-		lastDirection = x < lastX ? 1 : 2;
+	public void walkLeft(){
+		setX(getX()-speed);
+		lastDirection = 1;
 		lastMovement = System.currentTimeMillis();
 	}
-	public void setY(double y)
-	{
-		super.setY(y);
+	
+	public void walkRight(){
+		setX(getX()+speed);
+		lastDirection = 2;
+		lastMovement = System.currentTimeMillis();
 	}
-
 }

+ 6 - 2
src/model/objects/ShootingLine.java

@@ -15,6 +15,7 @@ public class ShootingLine {
 	}
 
 	public void setLength(int length){
+		this.length = length;
 		line = new Path2D.Double();
 		line.moveTo(startx, starty);
 		int tempy = starty;
@@ -22,15 +23,18 @@ public class ShootingLine {
 			line.curveTo(startx + 10, tempy- 10, startx - 10, tempy - 20, startx, tempy-30);
 			tempy -= 30;
 		}
-		this.length = length;
 	}
 	
+	//** Painting **//
+
 	public void paint(Graphics2D g2d)
 	{
 		g2d.draw(line);
 		g2d.drawLine(startx, getMaxHeight(), startx+8, getMaxHeight()+15);
 		g2d.drawLine(startx, getMaxHeight(), startx-8, getMaxHeight()+15);
-	}
+	}	
+	
+	//** Getters and Setters **//
 	
 	public int getLength(){
 		return length;

+ 8 - 9
src/model/state/PlayState.java

@@ -16,11 +16,10 @@ import resources.image.Images.ImageType;
 
 public class PlayState extends State{
 	
-	Player player;
-	ShootingLine line;
-	ArrayList<Ball> balls;
-	
-	int direction;
+	private Player player;
+	private ShootingLine line;
+	private ArrayList<Ball> balls;
+	private int direction;
 	
 	public PlayState(GameStateManager gsm) {
 		super("play", gsm);
@@ -28,7 +27,6 @@ public class PlayState extends State{
 		player = new Player(Images.getImage(ImageType.PLAYER1), 40, 550);
 		balls = new ArrayList<Ball>();
 		balls.add(new Ball(5, 400, Color.red, 300, 50,-1));
-		
 	}
 
 	/* INIT AND EXIT */
@@ -48,9 +46,9 @@ public class PlayState extends State{
 	@Override
 	public void update() {		
 		if(direction == 1)
-			player.setX(player.getX()+10);
+			player.walkRight();
 		else if(direction == -1)
-			player.setX(player.getX()-10);
+			player.walkLeft();
 		
 		if(player.getX() + 30 + player.getWidth() >= Window.WIDTH)
 			{
@@ -63,7 +61,7 @@ public class PlayState extends State{
 		player.update();
 		if(line != null){
 			line.setLength(line.getLength()+1);
-			if(line.getMaxHeight() <0){
+			if(line.getMaxHeight() < 0){ 		//Line hit the ceiling, so "remove" it
 				line = null;
 			}
 		}
@@ -130,6 +128,7 @@ public class PlayState extends State{
 		switch(e.getKeyCode())
 		{
 		case KeyEvent.VK_RIGHT:
+			direction = 0;
 		case KeyEvent.VK_LEFT:
 			direction = 0;
 			break;