public class MyWorld extends World
{
private final int START = 0;
private final int PLAYING1 = 1;
private final int PLAYING2 = 2;
private final int SUSPENDED = 3;
private final int WIN = 4;
private final int LOSE = 5;
private int SPAWN_TIMER = 0;
private int SPAWN_TIMER2 = 0;
private int SPAWN_TIME_LIMIT = 65;
private int SPAWN_TIME_LIMIT2 = 80;
private int ENEMY_COUNTER = 0;
private int ENEMY_COUNTER2 = 0;
private int ENEMY_Y = 300;
private Counter theCounter;
private Counter theCounter2;
private int curState;
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 900x600 cells with a cell size of 1x1 pixels.
super(900, 600, 1);
theCounter = new Counter();
curState = START;
this.setBackground("start.jpg");
}
public void act() {
// This starts the game whenever the player is ready
if (curState == START && Greenfoot.isKeyDown("y") || Greenfoot.isKeyDown("Y"))
{
curState = PLAYING1;
this.setBackground("PLAYING1.png");
initializeLevel1();
}
// This spawns enemies at a certain rate
if (curState == PLAYING1 && ENEMY_COUNTER < 15) {
if (SPAWN_TIMER == SPAWN_TIME_LIMIT){
spawnEnemy1();
SPAWN_TIMER = 0;
ENEMY_COUNTER ++;
}
else {
SPAWN_TIMER ++;
}
}
// This sets the game to a suspended level when the player
// finishes level 1
if(curState == PLAYING1 && theCounter.getValue() == 15)
{
curState = SUSPENDED;
removeLevel1Objs();
this.setBackground("splash2.png");
}
if (curState == SUSPENDED && (Greenfoot.isKeyDown("y") || Greenfoot.isKeyDown("Y")))
{
initializeLevel2();
this.setBackground("level2.png");
curState = PLAYING2;
}
if (curState == PLAYING2 && ENEMY_COUNTER2 < 4)
{
if (SPAWN_TIMER == SPAWN_TIME_LIMIT2){
spawnEnemy2();
SPAWN_TIMER = 0;
ENEMY_COUNTER2 ++;
}
else {
SPAWN_TIMER ++;
}
}
if(curState == PLAYING2 && theCounter.getValue() == 19)
{
curState = WIN;
removeLevel2Objs();
this.setBackground("win.png");
}
}
// utility methods
private void initializeLevel1()
{
this.addObject(new Player(),100,300);
this.addObject(new Computer(), 700,300);
}
private void initializeLevel2()
{
this.addObject(new Player(), 100, 300);
this.addObject(new Rock(), 450, 100);
this.addObject(new Rock(), 450, 500);
}
private void removeLevel1Objs()
{
List<Computer> myCList = this.getObjects(Computer.class);
List<Player> myPList = this.getObjects(Player.class);
for(Computer curComp : myCList) {
this.removeObject(curComp);
}
for(Player curComp : myPList) {
this.removeObject(curComp);
}
}
private void removeLevel2Objs()
{
List<Computer2> myC2List = this.getObjects(Computer2.class);
List<Player> myPList = this.getObjects(Player.class);
List<Rock> myRList = this.getObjects(Rock.class);
for(Computer2 curComp : myC2List) {
this.removeObject(curComp);
}
for(Player curComp : myPList) {
this.removeObject(curComp);
}
for(Rock curComp : myRList) {
this.removeObject(curComp);
}
}
// Method used to spawn enemies in the world
private void spawnEnemy1()
{
this.addObject(new Computer(),700,Greenfoot.getRandomNumber(ENEMY_Y));
}
public void spawnEnemy2()
{
this.addObject(new Computer2(), 700, Greenfoot.getRandomNumber(ENEMY_Y));
}
public Counter getCounter()
{
return theCounter;
}
}

