|
|
@@ -2,13 +2,9 @@ package model.state;
|
|
|
|
|
|
import java.awt.Color;
|
|
|
import java.awt.Graphics2D;
|
|
|
-import java.awt.event.ActionEvent;
|
|
|
-import java.awt.event.ActionListener;
|
|
|
import java.awt.event.KeyEvent;
|
|
|
import java.util.ArrayList;
|
|
|
-import java.util.Iterator;
|
|
|
-
|
|
|
-import javax.swing.Timer;
|
|
|
+import java.util.ListIterator;
|
|
|
|
|
|
import main.Window;
|
|
|
import model.GameStateManager;
|
|
|
@@ -18,24 +14,21 @@ import model.objects.ShootingLine;
|
|
|
import resources.image.Images;
|
|
|
import resources.image.Images.ImageType;
|
|
|
|
|
|
-public class PlayState extends State implements ActionListener{
|
|
|
+public class PlayState extends State{
|
|
|
|
|
|
Player player;
|
|
|
- ArrayList<ShootingLine> lines;
|
|
|
+ ShootingLine line;
|
|
|
ArrayList<Ball> balls;
|
|
|
|
|
|
- Timer t;
|
|
|
int direction;
|
|
|
|
|
|
public PlayState(GameStateManager gsm) {
|
|
|
super("play", gsm);
|
|
|
|
|
|
player = new Player(Images.getImage(ImageType.PLAYER1), 40, 550);
|
|
|
- lines = new ArrayList<ShootingLine>();
|
|
|
balls = new ArrayList<Ball>();
|
|
|
- balls.add(new Ball(5, 400, Color.red, 50, 50));
|
|
|
+ balls.add(new Ball(5, 400, Color.red, 300, 50,-1));
|
|
|
|
|
|
- t = new Timer(1000/5, this);
|
|
|
}
|
|
|
|
|
|
/* INIT AND EXIT */
|
|
|
@@ -68,28 +61,45 @@ public class PlayState extends State implements ActionListener{
|
|
|
player.setX(32);
|
|
|
}
|
|
|
player.update();
|
|
|
- for(Ball b: balls){
|
|
|
- b.update();
|
|
|
+ if(line != null){
|
|
|
+ line.setLength(line.getLength()+1);
|
|
|
+ if(line.getMaxHeight() <0){
|
|
|
+ line = null;
|
|
|
+ }
|
|
|
}
|
|
|
- Iterator<ShootingLine> i = lines.iterator();
|
|
|
- while(i.hasNext()){
|
|
|
- ShootingLine l = i.next();
|
|
|
- if(l.getMaxHeight() < 0){
|
|
|
- i.remove();
|
|
|
+
|
|
|
+ //collision detection
|
|
|
+ ListIterator<Ball> b = balls.listIterator();
|
|
|
+ while(b.hasNext()){
|
|
|
+ Ball bal = b.next();
|
|
|
+ if(line != null && bal.hitLine(line)){ // line - ball
|
|
|
+ line = null;
|
|
|
+ b.remove();
|
|
|
+ b.add(new Ball(bal.getSize()-1, 400, bal.getColor(), bal.getX(), bal.getY(), -1));
|
|
|
+ b.add(new Ball(bal.getSize()-1, 400, bal.getColor(), bal.getX(), bal.getY(), 1));
|
|
|
+ }else if(bal.hitPlayer(player)){
|
|
|
+ try {
|
|
|
+ throw new Exception("YOU DIED MOTHERFUCKER");
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }else if(bal.getSize() <= 0 ){
|
|
|
+ b.remove();
|
|
|
}else{
|
|
|
- l.setLength(l.getLength() +1);
|
|
|
+ bal.update();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void paint(Graphics2D g2d) {
|
|
|
g2d.drawString("Press ESC to stop the game", Window.WIDTH/2, Window.HEIGHT/2);
|
|
|
g2d.drawImage(Images.getImage(ImageType.BACKGROUND),null,0,0);
|
|
|
-
|
|
|
|
|
|
- for(ShootingLine l:lines){
|
|
|
- l.paint(g2d);
|
|
|
+ if(line != null){
|
|
|
+ line.paint(g2d);
|
|
|
}
|
|
|
for(Ball b: balls){
|
|
|
b.paint(g2d);
|
|
|
@@ -110,8 +120,8 @@ public class PlayState extends State implements ActionListener{
|
|
|
direction = -1;
|
|
|
break;
|
|
|
case KeyEvent.VK_SPACE:
|
|
|
- if(lines.size() == 0){
|
|
|
- lines.add(new ShootingLine((int)player.getX()+player.getWidth()/2, 600));
|
|
|
+ if(line == null){
|
|
|
+ line = new ShootingLine((int)player.getX()+player.getWidth()/2, 600);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -129,11 +139,5 @@ public class PlayState extends State implements ActionListener{
|
|
|
}
|
|
|
}
|
|
|
@Override
|
|
|
- public void keyTyped(KeyEvent e) {}
|
|
|
-
|
|
|
- @Override
|
|
|
- public void actionPerformed(ActionEvent arg0) {
|
|
|
- // TODO Auto-generated method stub
|
|
|
-
|
|
|
- }
|
|
|
+ public void keyTyped(KeyEvent e) {};
|
|
|
}
|