/*
* Created on Oct 27, 2007
*/
package gr.skill;
import gr.bluevibe.fire.displayables.FireScreen;
import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
/**
* @author padeler
*
*/
public class Cave extends GameCanvas
{
private Random rnd = new Random(System.currentTimeMillis());
private LayerManager layerManager;
private Sprite ship;
// private Image worldPanel=null;
// private Image frontLayer=null;
private Hero hero;
private int score=0;
private int clockStep=3;
private Skill parent;
private Thread clock =null;
/**
*
*/
public Cave(Skill parent)
{
super(false);
this.parent=parent;
validate();
}
public void validate()
{
hero = new Hero();
ship = new Sprite(hero.sprite[0]);
hero.x=(getWidth())/3;
hero.y=getHeight()/2;
ship.setPosition(hero.x,hero.y);
TiledLayer frontLayer=null;
try
{
frontLayer = generateCave(50);
} catch (Exception e1)
{
e1.printStackTrace();
}
layerManager = new LayerManager();
layerManager.setViewWindow(0,0,getWidth(),getHeight());
layerManager.append(ship);
layerManager.append(frontLayer);
clock = new Thread(){
public void run() {
while(true)
{
try{Thread.sleep(100);}catch(InterruptedException e){break;}
if(clock()){
repaint();
}
}
}
};
clock.start();
}
private TiledLayer generateCave(int lenght) throws Exception
{
Image tile = Image.createImage(this.getClass().getResourceAsStream("/front.jpg"));
int tw = 20;
int th = 20;
int xmax = getWidth()/tw;
int ymax = getHeight()/th;
TiledLayer frontLayer = new TiledLayer(lenght,ymax,tile,tw,th);
int caveSize=(getHeight() * 1)/3;
int caveGeneratorPos = getHeight()/2;
int caveGeneratorSpeed=2;
int caveGeneratorRandomSpeedReverse=10;
int caveGeneratorRandomSpeedChange=35;
// generate new terain to the empty part on the right
int h = getHeight();
int w = getWidth();
for(int i=0;i<lenght;++i)
{
if(rnd.nextLong()%(100)<caveGeneratorRandomSpeedReverse) caveGeneratorSpeed=-caveGeneratorSpeed;
if(rnd.nextLong()%(100)<caveGeneratorRandomSpeedChange)
{
caveGeneratorSpeed+= (rnd.nextLong()%(2)==0?-1:1);
if(caveGeneratorSpeed>=3 || caveGeneratorSpeed<=-3) caveGeneratorSpeed=0;
}
if(caveGeneratorPos>=h || caveGeneratorPos<0) caveGeneratorSpeed=-caveGeneratorSpeed;
caveGeneratorPos += caveGeneratorSpeed;
for(int j=0 ;j<(caveGeneratorPos-(caveSize/2))/th;++j) frontLayer.setCell(i,j,1);
for(int j=(caveGeneratorPos+(caveSize/2))/th ;j<ymax;++j) frontLayer.setCell(i,j,1);
}
return frontLayer;
}
protected void keyPressed(int k)
{
keyEvent(FIRE);
}
protected void keyRepeated(int k)
{
keyPressed(k);
}
public void paint(Graphics g)
{
g.setColor(0xFFFFFFFF);
g.fillRect(0,0,getWidth(),getHeight());
layerManager.paint(g,0,0);
g.setColor(0x000000FF);
g.drawString(""+score,0,0,Graphics.TOP|Graphics.LEFT);
}
private boolean checkIfCrashed()
{
// // check position of the hero.
// if(hero.y<=2 || hero.y>=getHeight()-2)
// {
// System.out.println("Crash !!!!!!!!!!!");
// return true;
// }
// int[] rgb=new int[16];
// worldPanel.getRGB(rgb,0,4,hero.x-2,hero.y-2,4,4);
// int res = 0xFFFFFFFF;
// for(int i=0;i<rgb.length;++i) res &= rgb[i];
// if(res!=0xFFFFFFFF)
// { // game over.
// System.out.println("Crash !!!!!!!!!!!");
// return true;
//
// }
return false;
}
public boolean clock()
{
// layerManager.setViewWindow((lmX+=clockStep),lmY,getWidth(),getHeight());
ship.setPosition(hero.x+=clockStep,hero.y);
boolean crashed = checkIfCrashed();
if(crashed)
{
parent.gameOver(score);
return false; // game over. no repaint needed.
}
// sent event to hero.
hero.clock();
// // scroll the worldPanel to the left
// Graphics g = worldPanel.getGraphics();
//
// g.copyArea(clockStep,0,worldPanel.getWidth()-clockStep,worldPanel.getHeight(),0,0,Graphics.TOP|Graphics.LEFT);
// g.setColor(0x00202020);
// g.drawRect(hero.x-10,hero.y-1,2,2);
score++;
updateDifficulty();
return true;
}
private void updateDifficulty()
{
if(score%100==0) ++clockStep;
}
public boolean keyEvent(int key)
{
if(key == Canvas.FIRE) hero.fire();
return true;
}
public boolean isAnimated()
{
return true;
}
class Hero
{
static final long timeStep = FireScreen.CLOCK_STEP;
static final int maxSpeed=10000;
static final int heroRadious=1;
static final int heroEngineStep=-1500;
static final int gravityStep=2400;
private Image sprite[]=new Image[3];
int x;// hero's x position
int y;// hero's y position
long vy=0; // velocity on Y axis.
public Hero()
{
try{
sprite[0] = Image.createImage(this.getClass().getResourceAsStream("/ship.png"));
sprite[1] = Image.createImage(this.getClass().getResourceAsStream("/ship-up.png"));
sprite[2] = Image.createImage(this.getClass().getResourceAsStream("/ship-down.png"));
}catch(IOException e)
{
System.out.println("Failed to load ship sprite.");
}
}
/**
* adjust horizontal speed (vy)
*
*/
void fire()
{
vy +=heroEngineStep;
}
/**
* time related events like position changes and gravity induced speed change.
* Reduce the vertical speed (vx) only if vy>=0
*
*
*/
void clock()
{
vy += (gravityStep*timeStep)/1000;
if(vy>maxSpeed) vy=maxSpeed;
if(vy<-maxSpeed) vy=-maxSpeed;
y+= (vy*timeStep)/100000;
}
public Image getSprite()
{
if(vy<heroEngineStep) return sprite[1];
if(vy>gravityStep) return sprite[2];
return sprite[0];
}
}
}