package Project;
/*********************************************************************
*
* Copyright (C) 2008, Blekinge Institute of Technology
*
* Filename: PlayerShip.java
* Author: Simon Sönnby <sso@bth.se>
* Description: Player ship
*
********************************************************************/
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.*;
import java.util.*;
public class enemyShip extends Ship
{
private static final int MAX_NR_OF_BULLETS = 20;
private Bullet[] mBullets;
private int mNrOfBullets; //Current amount of bullets
private int mStartNrOfLifes;
private int mNrOfLifes;
/**
* PlayerShip constructor
* @param lmgr LayerManager that handles all the sprites
* @param img Image of the ship
* @param frameWidth Width of the ship frame
* @param frameHeight Height of the ship frame
* @param startSpeed Start speed of the ship
* @param nrOfLifes Number of lifes the player has
* @param bulletImg Image of the bullet
* @param bulletSpeed Speed of the bullet
* @param bulletDmg Damage of the bullet
* @param nrOfBullets Number of bullets (Ammunition)
*/
public enemyShip(LayerManager lmgr, Image img, int frameWidth, int frameHeight, int startSpeed, int nrOfLifes,
Image bulletImg, int bulletSpeed, int bulletDmg, int nrOfBullets)
{
super(img, frameWidth, frameHeight, startSpeed);
mStartNrOfLifes = nrOfLifes;
mNrOfBullets = nrOfBullets;
mBullets = new Bullet[ MAX_NR_OF_BULLETS ];
for(int i = 0; i < MAX_NR_OF_BULLETS; ++i)
{
mBullets[i] = new Bullet(bulletImg, bulletSpeed, bulletDmg);
lmgr.append( mBullets[i] );
}
lmgr.append( this );
reset();
}
/**
* Shoot a bullet
*/
public void reset()
{
super.restart();
mNrOfLifes = mStartNrOfLifes;
}
public void shoot()
{
if( !mBullets[0].isAlive() )
{
mBullets[0].setSpeed(7);
mBullets[0].shoot(getX() + (int)(getWidth() * 0.5), getY()+15);
}
}
public void secShoot()
{
int i=0;
if( !mBullets[i].isAlive() )
{
mBullets[i].setSpeed(8);
mBullets[i].shoot(getX() + (int)(getWidth() * 0.9), getY()+35);
}
if( !mBullets[i+1].isAlive() )
{
mBullets[i+1].setSpeed(8);
mBullets[i+1].shoot(getX() + (int)(getWidth() * 0.7), getY()+35);
}
if( !mBullets[i+2].isAlive() )
{
mBullets[i+2].setSpeed(8);
mBullets[i+2].shoot(getX() + (int)(getWidth() * 0.5), getY()+35);
}
if( !mBullets[i+3].isAlive() )
{
mBullets[i+3].setSpeed(8);
mBullets[i+3].shoot(getX() + (int)(getWidth() * 0.3), getY()+35);
}
if( !mBullets[i+4].isAlive() )
{
mBullets[i+4].setSpeed(8);
mBullets[i+4].shoot(getX() + (int)(getWidth() * 0.1), getY()+35);
}
}
public void increaseAmmoPack()
{
if( mNrOfBullets < MAX_NR_OF_BULLETS )
mNrOfBullets++;
}
public void clearBullets()
{
for(int i=0;i<this.mNrOfBullets;i++)
{
this.mBullets[i].destroy();
mBullets[i].setPosition(-100, -100);
}
}
/**
* Decreases the mNrOfLifes
*/
public void kill()
{
mNrOfLifes--;
}
/**
* Return number of lifes left
* @return mNrOfLifes
*/
public int getNrOfLifes()
{
return mNrOfLifes;
}
/*
* Check if the param collides with any bullet
* @param obj Sprite to check collision with
* @return True if there is a collision, else return false
*/
public boolean collisionWithBullet(Sprite obj)
{
boolean collision = false;
for(int i = 0; i < mNrOfBullets; ++i)
{
if( mBullets[i].isAlive() && mBullets[i].collidesWith(obj, true))
{
collision = true;
mBullets[i].destroy();
mBullets[i].setPosition(-100, -100);
}
}
return collision;
}
/**
* Update
*/
public void update()
{
super.update();
for(int i = 0; i < mNrOfBullets; ++i)
{
if( mBullets[i].isAlive() )
{
mBullets[i].update();
if( ((int)mBullets[i].getY() + mBullets[i].getHeight()) > 230) //Off screen
mBullets[i].destroy();
}
}
}
public void update2()
{
int i = 0;
super.update();
if( mBullets[i].isAlive() )
{
mBullets[i].update2();
if( ((int)mBullets[i].getY() + mBullets[i].getHeight()) > 230) //Off screen
mBullets[i].destroy();
}
if( mBullets[i+4].isAlive() )
{
mBullets[i+4].update1();
if( ((int)mBullets[i+4].getY() + mBullets[i+4].getHeight()) > 230) //Off screen
mBullets[i+4].destroy();
}
if(mBullets[i+1].isAlive() && mBullets[i+2].isAlive() && mBullets[i+3].isAlive())
{
mBullets[i+1].update();
mBullets[i+2].update();
mBullets[i+3].update();
for(i=1;i<4;i++)
{
if( ((int)mBullets[i].getY() + mBullets[i].getHeight()) > 230) //Off screen
mBullets[i].destroy();
}
}
}
public void MoveEnemyShipUpdate(int x, int y)
{
super.update();
Random r = new Random();
int random = r.nextInt(2)+1;
if(random == 1)
{
moveShip(3, 0);
}
else
if(random == 2)
{
moveShip(-3, 0);
}
}
public void MoveEnemyShipUpdateHard(int x, int y)
{
super.update();
Random r = new Random();
int random = r.nextInt(4)+1;
if(random == 1)
{
moveShip(1, 0);
}
else
if(random == 2)
{
moveShip(-1, 0);
}
else
if(random == 3)
{
moveShip(0, 1);
}
else
if(random == 4)
{
moveShip(0, -1);
}
}
}