Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 16th, 2010 | Syntax: Java | Size: 6.76 KB | Hits: 26 | Expires: Never
Copy text to clipboard
  1. package Project;
  2.  
  3. /*********************************************************************
  4.  *
  5.  * Copyright (C) 2008,  Blekinge Institute of Technology
  6.  *
  7.  * Filename:      PlayerShip.java
  8.  * Author:        Simon Sönnby <sso@bth.se>
  9.  * Description:   Player ship
  10.  *
  11.  ********************************************************************/
  12. import javax.microedition.lcdui.Image;
  13. import javax.microedition.lcdui.game.*;
  14. import java.util.*;
  15.  
  16. public class enemyShip extends Ship
  17. {
  18.     private static final int MAX_NR_OF_BULLETS = 20;
  19.  
  20.     private Bullet[] mBullets;
  21.  
  22.     private int mNrOfBullets;       //Current amount of bullets
  23.     private int mStartNrOfLifes;
  24.     private int mNrOfLifes;
  25.  
  26.     /**
  27.      * PlayerShip constructor
  28.      * @param lmgr LayerManager that handles all the sprites
  29.      * @param img Image of the ship
  30.      * @param frameWidth Width of the ship frame
  31.      * @param frameHeight Height of the ship frame
  32.      * @param startSpeed Start speed of the ship
  33.      * @param nrOfLifes Number of lifes the player has
  34.      * @param bulletImg Image of the bullet
  35.      * @param bulletSpeed Speed of the bullet
  36.      * @param bulletDmg Damage of the bullet
  37.      * @param nrOfBullets Number of bullets (Ammunition)
  38.      */
  39.     public enemyShip(LayerManager lmgr, Image img,       int frameWidth,  int frameHeight, int startSpeed, int nrOfLifes,
  40.                                          Image bulletImg, int bulletSpeed, int bulletDmg,   int nrOfBullets)
  41.     {
  42.         super(img, frameWidth, frameHeight, startSpeed);
  43.         mStartNrOfLifes = nrOfLifes;
  44.         mNrOfBullets    = nrOfBullets;
  45.  
  46.         mBullets = new Bullet[ MAX_NR_OF_BULLETS ];
  47.         for(int i = 0; i < MAX_NR_OF_BULLETS; ++i)
  48.         {
  49.             mBullets[i] = new Bullet(bulletImg, bulletSpeed, bulletDmg);
  50.             lmgr.append( mBullets[i] );
  51.         }
  52.  
  53.         lmgr.append( this );
  54.  
  55.         reset();
  56.     }
  57.  
  58.     /**
  59.      * Shoot a bullet
  60.      */
  61.     public void reset()
  62.     {
  63.         super.restart();
  64.         mNrOfLifes = mStartNrOfLifes;
  65.     }
  66.    
  67.     public void shoot()
  68.     {
  69.         if( !mBullets[0].isAlive() )
  70.         {
  71.             mBullets[0].setSpeed(7);
  72.             mBullets[0].shoot(getX() + (int)(getWidth() * 0.5), getY()+15);
  73.         }
  74.     }
  75.  
  76.     public void secShoot()
  77.     {
  78.         int i=0;
  79.             if( !mBullets[i].isAlive() )
  80.             {
  81.                 mBullets[i].setSpeed(8);
  82.                 mBullets[i].shoot(getX() + (int)(getWidth() * 0.9), getY()+35);  
  83.             }
  84.             if( !mBullets[i+1].isAlive() )
  85.             {
  86.                 mBullets[i+1].setSpeed(8);
  87.                 mBullets[i+1].shoot(getX() + (int)(getWidth() * 0.7), getY()+35);
  88.             }
  89.             if( !mBullets[i+2].isAlive() )
  90.             {
  91.                 mBullets[i+2].setSpeed(8);
  92.                 mBullets[i+2].shoot(getX() + (int)(getWidth() * 0.5), getY()+35);
  93.             }
  94.             if( !mBullets[i+3].isAlive() )
  95.             {
  96.                 mBullets[i+3].setSpeed(8);
  97.                 mBullets[i+3].shoot(getX() + (int)(getWidth() * 0.3), getY()+35);
  98.             }
  99.             if( !mBullets[i+4].isAlive() )
  100.             {
  101.                 mBullets[i+4].setSpeed(8);
  102.                 mBullets[i+4].shoot(getX() + (int)(getWidth() * 0.1), getY()+35);
  103.             }
  104.     }
  105.  
  106.     public void increaseAmmoPack()
  107.     {
  108.         if( mNrOfBullets < MAX_NR_OF_BULLETS )
  109.             mNrOfBullets++;
  110.     }
  111.  
  112.     public void clearBullets()
  113.     {
  114.         for(int i=0;i<this.mNrOfBullets;i++)
  115.         {
  116.             this.mBullets[i].destroy();
  117.             mBullets[i].setPosition(-100, -100);
  118.         }
  119.     }
  120.  
  121.     /**
  122.      * Decreases the mNrOfLifes
  123.      */
  124.     public void kill()
  125.     {
  126.         mNrOfLifes--;
  127.     }
  128.  
  129.     /**
  130.      * Return number of lifes left
  131.      * @return mNrOfLifes
  132.      */
  133.     public int getNrOfLifes()
  134.     {
  135.         return mNrOfLifes;
  136.     }
  137.  
  138.     /*
  139.      * Check if the param collides with any bullet
  140.      * @param obj Sprite to check collision with
  141.      * @return True if there is a collision, else return false
  142.      */
  143.     public boolean collisionWithBullet(Sprite obj)
  144.     {
  145.         boolean collision = false;
  146.  
  147.         for(int i = 0; i < mNrOfBullets; ++i)
  148.         {
  149.             if( mBullets[i].isAlive() && mBullets[i].collidesWith(obj, true))
  150.             {
  151.                 collision = true;
  152.                 mBullets[i].destroy();
  153.                 mBullets[i].setPosition(-100, -100);
  154.             }
  155.         }
  156.         return collision;
  157.     }
  158.  
  159.     /**
  160.      * Update
  161.      */
  162.     public void update()
  163.     {  
  164.         super.update();
  165.  
  166.         for(int i = 0; i < mNrOfBullets; ++i)
  167.         {
  168.             if( mBullets[i].isAlive() )
  169.             {
  170.                 mBullets[i].update();
  171.                
  172.                 if( ((int)mBullets[i].getY() + mBullets[i].getHeight()) > 230) //Off screen
  173.                     mBullets[i].destroy();
  174.             }
  175.         }
  176.     }
  177.  
  178.     public void update2()
  179.     {
  180.         int i = 0;
  181.         super.update();
  182.  
  183.         if( mBullets[i].isAlive() )
  184.         {
  185.            mBullets[i].update2();
  186.            
  187.  
  188.            if( ((int)mBullets[i].getY() + mBullets[i].getHeight()) > 230) //Off screen
  189.                 mBullets[i].destroy();
  190.         }
  191.        
  192.         if( mBullets[i+4].isAlive() )
  193.         {
  194.             mBullets[i+4].update1();
  195.              
  196.             if( ((int)mBullets[i+4].getY() + mBullets[i+4].getHeight()) > 230) //Off screen
  197.                 mBullets[i+4].destroy();
  198.         }
  199.  
  200.         if(mBullets[i+1].isAlive() && mBullets[i+2].isAlive() && mBullets[i+3].isAlive())
  201.         {
  202.             mBullets[i+1].update();
  203.             mBullets[i+2].update();
  204.             mBullets[i+3].update();
  205.             for(i=1;i<4;i++)
  206.             {
  207.                 if( ((int)mBullets[i].getY() + mBullets[i].getHeight()) > 230) //Off screen
  208.                     mBullets[i].destroy();
  209.             }
  210.         }
  211.     }
  212.  
  213.     public void MoveEnemyShipUpdate(int x, int y)
  214.     {
  215.         super.update();
  216.         Random r = new Random();
  217.         int random = r.nextInt(2)+1;
  218.  
  219.         if(random == 1)
  220.         {
  221.             moveShip(3, 0);
  222.         }
  223.         else
  224.             if(random == 2)
  225.         {
  226.             moveShip(-3, 0);
  227.         }
  228.     }
  229.  
  230.     public void MoveEnemyShipUpdateHard(int x, int y)
  231.     {
  232.         super.update();
  233.         Random r = new Random();
  234.         int random = r.nextInt(4)+1;
  235.  
  236.         if(random == 1)
  237.         {
  238.             moveShip(1, 0);
  239.         }
  240.         else
  241.             if(random == 2)
  242.         {
  243.             moveShip(-1, 0);
  244.         }
  245.         else
  246.             if(random == 3)
  247.         {
  248.              moveShip(0, 1);
  249.         }
  250.         else
  251.             if(random == 4)
  252.         {
  253.             moveShip(0, -1);
  254.         }
  255.     }
  256. }