Advertisement
raiyanshadow

Ship.java

Apr 14th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.98 KB | None | 0 0
  1. package br.ol.spaceinvaders.obj;
  2.  
  3.  
  4. import br.ol.spaceinvaders.SpaceInvadersGame;
  5. import br.ol.spaceinvaders.SpaceInvadersGame.State;
  6. import br.ol.spaceinvaders.SpaceInvadersObj;
  7. import br.ol.spaceinvaders.core.Keyboard;
  8. import java.awt.event.KeyEvent;
  9. import java.awt.geom.Rectangle2D;
  10.  
  11.  
  12. public class Ship extends SpaceInvadersObj {
  13.    
  14.     public ShipShot shipShot;
  15.     public boolean hit;
  16.     public long hitTime;
  17.  
  18.     public Ship(SpaceInvadersGame game, ShipShot shipShot) {
  19.         super(game);
  20.         this.shipShot = shipShot;
  21.     }
  22.  
  23.     @Override
  24.     public void init() {
  25.         x = 360 / 2 - 13;
  26.         y = 290;
  27.         collider = new Rectangle2D.Double(x, y, 26, 16);
  28.         loadFrames("ship.png", "ship_destroyed_0.png", "ship_destroyed_1.png");
  29.     }
  30.  
  31.     @Override
  32.     public void updatePlaying() {
  33.         if (!visible) {
  34.             return;
  35.         }
  36.  
  37.         if (Keyboard.keyPressed[KeyEvent.VK_LEFT]) {
  38.             x -= 4;
  39.         }
  40.         else if (Keyboard.keyPressed[KeyEvent.VK_RIGHT]) {
  41.             x += 4;
  42.         }
  43.         // limit x movement
  44.         x = x < 10 ? 10 : x;    
  45.         x = x > 324 ? 324 : x;
  46.        
  47.         if (Keyboard.keyPressed[KeyEvent.VK_SPACE] && shipShot.canShoot()) {
  48.             shipShot.shoot(x + 10, y);
  49.         }
  50.     }
  51.  
  52.     @Override
  53.     public void updateClear() {
  54.         yield:
  55.         while (true) {
  56.             switch (instructionPointer) {
  57.                 case 0:
  58.                     waitTime = System.currentTimeMillis();
  59.                     instructionPointer = 1;
  60.                 case 1:
  61.                     if (System.currentTimeMillis() - waitTime < 1500){
  62.                         break yield;
  63.                     }
  64.                     game.nextGame();
  65.                     break yield;
  66.             }
  67.         }
  68.     }
  69.    
  70.     @Override
  71.     public void updateHit(){
  72.         yield:
  73.         while (true){
  74.             switch (instructionPointer){
  75.                 case 0:
  76.                     waitTime = System.currentTimeMillis();
  77.                     instructionPointer = 1;
  78.                 case 1:
  79.                     frame = frames[1 + ((int) (System.nanoTime() * 0.00000001) % 2)];
  80.                     if (System.currentTimeMillis() - waitTime < 1000){
  81.                         break yield;
  82.                     }
  83.                     visible = false;
  84.                     waitTime = System.currentTimeMillis();
  85.                     instructionPointer = 2;
  86.                 case 2:
  87.                     if (System.currentTimeMillis() - waitTime < 1500){
  88.                         break yield;   
  89.                     }
  90.                     frame = frames[0];
  91.                     game.lives--;
  92.                     if (game.lives == 0){
  93.                         game.setState(State.GAME_OVER);
  94.                     }
  95.                     else{
  96.                         game.setState(State.PLAYING);
  97.                         visible = true;
  98.                     }
  99.                     break yield;
  100.                 }
  101.             }      
  102.         }
  103.        
  104.         // broadcast message
  105.        
  106.         @Override
  107.         public void stateChanged(){
  108.             if (game.state == State.TITLE){
  109.                 x = 360 / 2 - 13;
  110.             }
  111.             else if (game.state == State.READY){
  112.                 visible = true;
  113.             }
  114.             else if (game.state == State.HIT || game.state == State.CLEAR){
  115.                 instructionPointer = 0;
  116.             }
  117.             else if (game.state = SpaceInvadersGame.State.GAME_OVER){
  118.                 visible = false;
  119.             }
  120.         }
  121.        
  122.         public void hit(){
  123.             hit = true;
  124.             hitTime = System.currentTimeMillis;
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement