Exemple #1
0
 public WorldView(
     Asteroids<World, Ship, Asteroid, Bullet> game, World world, Ship player1, Ship player2) {
   this.game = game;
   this.facade = game.getFacade();
   this.world = world;
   this.player1 = player1;
   this.player2 = player2;
   this.timer = new Timer(TIMER_DELAY, this);
   setBackground(Color.BLACK);
   ClassLoader loader = WorldView.class.getClassLoader();
   background =
       Toolkit.getDefaultToolkit()
           .getImage(loader.getResource("asteroids/resources/game-background.jpg"));
   background =
       background.getScaledInstance(game.getWidth(), game.getHeight(), Image.SCALE_DEFAULT);
   if (player1 != null) {
     int size = (int) (2 * facade.getShipRadius(player1));
     Image image =
         Toolkit.getDefaultToolkit()
             .getImage(loader.getResource("asteroids/resources/deathstar.png"))
             .getScaledInstance(size, size, Image.SCALE_DEFAULT);
     visualizations.put(player1, new ShipVisualization(Color.RED, player1, image));
   }
   if (player2 != null) {
     int size = (int) (2 * facade.getShipRadius(player2));
     Image image =
         Toolkit.getDefaultToolkit()
             .getImage(loader.getResource("asteroids/resources/sphere.png"))
             .getScaledInstance(size, size, Image.SCALE_DEFAULT);
     visualizations.put(player2, new ShipVisualization(Color.GREEN, player2, image));
   }
   addKeyListener(this);
 }
Exemple #2
0
 @Override
 public void keyPressed(KeyEvent e) {
   switch (e.getKeyCode()) {
     case KeyEvent.VK_ESCAPE:
       timer.stop();
       game.showMenu();
     case THRUSTER_P1:
       facade.setThrusterActive(player1, true);
       break;
     case LEFT_P1:
       player1_angle = Math.PI / 20;
       break;
     case RIGHT_P1:
       player1_angle = -Math.PI / 20;
       break;
     case FIRE_P1:
       player1_fire = true;
       break;
     case THRUSTER_P2:
       if (player2 != null) facade.setThrusterActive(player2, true);
       break;
     case LEFT_P2:
       if (player2 != null) player2_angle = Math.PI / 20;
       break;
     case RIGHT_P2:
       if (player2 != null) player2_angle = -Math.PI / 20;
       break;
     case FIRE_P2:
       if (player2 != null) player2_fire = true;
       break;
   }
 }
 @Test
 public void testMoveVertical() {
   Worm worm = facade.createWorm(world, 1, 1.5, Math.PI / 2, 0.5, "Test");
   facade.move(worm);
   assertEquals(1, facade.getX(worm), EPS);
   assertEquals(2.0, facade.getY(worm), EPS);
 }
 @Test
 public void testMoveHorizontal() {
   Worm worm = facade.createWorm(world, 1, 2, 0, 1, "Test");
   facade.move(worm);
   assertEquals(2, facade.getX(worm), EPS);
   assertEquals(2, facade.getY(worm), EPS);
 }
 private Set<IShip> initModel(IFacade facade) {
   Set<IShip> ships = new HashSet<IShip>();
   ships.add(facade.createShip(200, 400, 0, 0, 50, 0));
   ships.add(facade.createShip(700, 400, 0, 0, 50, Math.PI));
   ships.add(facade.createShip(450, 600, 0, 0, 75, -Math.PI / 2));
   return ships;
 }
 @Test
 public void testMoveVertical() {
   Worm worm = facade.createWorm(0, 0, Math.PI / 2, 1, "Test");
   facade.move(worm, 5);
   assertEquals(0, facade.getX(worm), EPS);
   assertEquals(5, facade.getY(worm), EPS);
 }
Exemple #7
0
 @Override
 public void objectCollision(Object entity1, Object entity2, double x, double y) {
   if ((facade.isBullets(entity1) && !facade.isBullets(entity2))
       || (facade.isBullets(entity2) && !facade.isBullets(entity1))) {
     game.getSound().play("explosion");
     explosions.add(new Explosion(x, facade.getWorldHeight(world) - y));
   }
 }
 @Test
 public void testMoveVerticalAlongTerrain() {
   // . . X
   // . w X
   World world =
       facade.createWorld(
           3.0, 2.0, new boolean[][] {{true, true, false}, {true, true, false}}, random);
   Worm worm = facade.createWorm(world, 1.5, 0.5, Math.PI / 2 - 10 * 0.0175, 0.5, "Test");
   facade.move(worm);
   assertEquals(1.5, facade.getX(worm), EPS);
   assertEquals(1.0, facade.getY(worm), EPS);
 }
Exemple #9
0
 @Override
 protected void paintComponent(Graphics g) {
   super.paintComponent(g);
   Graphics2D g2d = (Graphics2D) g;
   g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
   g2d.drawImage(background, 0, 0, null);
   g2d.setColor(Color.WHITE);
   for (Ship ship : facade.getShips(world)) {
     if (!visualizations.containsKey(ship)) {
       visualizations.put(ship, new ShipVisualization(Color.BLUE, ship, null));
     }
     visualizations.get(ship).draw(g2d);
   }
   for (Asteroid asteroid : facade.getAsteroids(world)) {
     if (!visualizations.containsKey(asteroid)) {
       visualizations.put(asteroid, new AsteroidVisualization(asteroid));
     }
     visualizations.get(asteroid).draw(g2d);
   }
   for (Bullet bullet : facade.getBullets(world)) {
     if (!visualizations.containsKey(bullet)) {
       Ship ship = facade.getBulletSource(bullet);
       visualizations.put(
           bullet, new BulletVisualization(visualizations.get(ship).getColor(), bullet));
     }
     visualizations.get(bullet).draw(g2d);
   }
   for (Explosion explosion : explosions) {
     explosion.draw(g2d);
   }
   if (msg != null) {
     g2d.setColor(Color.WHITE);
     g2d.setFont(g2d.getFont().deriveFont(40f));
     drawCenteredString(g2d, msg);
     g2d.setFont(g2d.getFont().deriveFont(20f));
     drawCenteredString(g2d, "Press ESC to continue ...", getHeight() / 3 * 2);
   }
 }
Exemple #10
0
 @Override
 public void keyReleased(KeyEvent e) {
   switch (e.getKeyCode()) {
     case THRUSTER_P1:
       facade.setThrusterActive(player1, false);
       break;
     case LEFT_P1:
       player1_angle = 0;
       break;
     case RIGHT_P1:
       player1_angle = 0;
       break;
     case THRUSTER_P2:
       if (player2 != null) facade.setThrusterActive(player2, false);
       break;
     case LEFT_P2:
       if (player2 != null) player2_angle = 0;
       break;
     case RIGHT_P2:
       if (player2 != null) player2_angle = 0;
       break;
   }
 }
 @Test
 public void testFall() {
   // . X .
   // . w .
   // . . .
   // X X X
   World world =
       facade.createWorld(
           3.0,
           4.0,
           new boolean[][] {
             {true, false, true}, {true, true, true},
             {true, true, true}, {false, false, false}
           },
           random);
   Worm worm = facade.createWorm(world, 1.5, 2.5, (3.0 * Math.PI) / 2.0, 0.5, "Test");
   assertFalse(facade.canFall(worm));
   facade.move(worm);
   assertTrue(facade.canFall(worm));
   facade.fall(worm);
   assertEquals(1.5, facade.getX(worm), EPS);
   assertTrue(1.5 <= facade.getY(worm) && facade.getY(worm) <= 1.55);
 }
Exemple #12
0
 @Override
 public void actionPerformed(ActionEvent e) {
   long now = System.currentTimeMillis();
   long millisSinceLastEvolve = now - timeAfterLastEvolve;
   timeAfterLastEvolve = now;
   if (player1_angle != 0) {
     facade.turn(player1, player1_angle);
   }
   if (player2 != null && player2_angle != 0) {
     facade.turn(player2, player2_angle);
   }
   if (player1_fire && facade.getShips(world).contains(player1)) {
     player1_fire = false;
     facade.fireBullet(player1);
     game.getSound().play("torpedo");
   }
   if (player2 != null && player2_fire && facade.getShips(world).contains(player2)) {
     player2_fire = false;
     facade.fireBullet(player2);
     game.getSound().play("torpedo");
   }
   facade.evolve(world, millisSinceLastEvolve / 1000., this);
   Iterator<Explosion> iter = explosions.iterator();
   while (iter.hasNext()) {
     boolean done = iter.next().evolve(millisSinceLastEvolve / 1000.);
     if (done) iter.remove();
   }
   boolean player1Alive = facade.getShips(world).contains(player1);
   if (player1Alive) {
     if (player2 != null && !facade.getShips(world).contains(player2)) {
       timer.stop();
       msg = "Player 1 wins!";
     } else if (facade.getAsteroids(world).isEmpty() && player2 == null) {
       timer.stop();
       msg = "You win!";
     }
   } else {
     timer.stop();
     if (player2 == null || !facade.getShips(world).contains(player2)) {
       msg = "Asteroids win!";
     } else {
       msg = "Player 2 wins!";
     }
   }
   repaint();
 }
 @Test
 public void testMaximumActionPoints() {
   Worm worm = facade.createWorm(world, 1, 2, 0, 1, "Test");
   assertEquals(4448, facade.getMaxActionPoints(worm));
 }
 @Test(expected = ModelException.class)
 public void testJumpException() {
   Worm worm = facade.createWorm(0, 0, 3 * Math.PI / 2, 1, "Test");
   facade.jump(worm);
 }