import java.applet.Applet;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.*;
import java.awt.geom.*;
import java.awt.Event;
import java.awt.image.*;
public class Kyle extends Applet implements Runnable {
Graphics gBG,gMain,mapGraphics;
Image grphImg,mapImg,terrainAndTextures[]=new Image[1000] ;
int i,j,k,count100,count1,lp,speedMUL=1,speed=30,loopCount,playerPos;
int count2,dir,staleFrame=1,gpX,gpY;
int motion,staleMotion,inFront,sideIX[]={0,-16,16,-1,1,-17,-15,15,17};
int whichFrame,xAdder,yAdder;
int indexSaver;
int mapData,height=1,width=1;
double countSlow=1,countSuperSlow=1,di,dj;
double playerX=33,playerY=33,movUD=1, movLR=1,oldMovUD=1, oldMovLR=1;
boolean u,d,l,r,mD,pause,noDir,skip;
Image ih=null;
int mapBG[]=new int[256];
Thread game;
int centerX, centerY;
Image pers[],persBig,backBuffer,main,curs[],cursCol,map;
MediaTracker tracker;
ImageFilter filter;
int terrain[]=new int[257];
Rectangle2D terrainBox[]= new Rectangle2D[256], playerBox;
ImageProducer person,curser;
String data[]=new String[500];
readmapfile rd=new readmapfile();
Maps maps=new Maps();
Graphics rg;
@Override
public void init() {
maps.declareAndReadMaps();
resize(512,512);
mapImg=createImage(512,512);
mapGraphics=mapImg.getGraphics();
tracker=new MediaTracker(this);
pers=new Image[15];
curs=new Image[15];
backBuffer= createImage(512,512);
main= createImage(512,512);
gMain=main.getGraphics();
gBG = backBuffer.getGraphics();
persBig=getImage(getCodeBase(),"resources/char.png");
cursCol=getImage(getCodeBase(),"resources/cursor.png");
person=persBig.getSource();
curser=cursCol.getSource();
tracker.addImage(cursCol, 0);
buildMap();
tracker.addImage(main, 0);
tracker.addImage(backBuffer, 0);
tracker.addImage(persBig, 0);
waitToLoad(0);
k=1;j=0;i=0;
while (k<13)
{
filter=new CropImageFilter(j*32,i*32,32,32);
pers[k]=createImage(new FilteredImageSource(person,filter));
tracker.addImage(pers[k], 1);
k++;
j++;
if (j==3)
{
j=0;
i++;
}
}
k=1;
j=0;
i=0;
while (k<13)
{
filter=new CropImageFilter(j*32,i*32,32,32);
curs[k]=createImage(new FilteredImageSource(curser,filter));
tracker.addImage(curs[k], 1);
k++;
j++;
if (j==3)
{
j=0;
i++;
}
}
setBackground(Color.WHITE);
getImage();
waitToLoad(1);
//mapDataDraw();
}
private void getImage() {
int pk,pj,pi;
Image img,img1;
ImageProducer collectionProducer,collectionProducer1;
img=getImage(getCodeBase(),"resources/TileB.png");
img1=getImage(getCodeBase(),"resources/TileA2.png");
collectionProducer=img.getSource();
collectionProducer1=img1.getSource();
pk=0;pi=0;pj=0;
while (pk<256)
{
filter=new CropImageFilter(pj*32,pi*32,32,32);
terrainAndTextures[pk]=createImage(new FilteredImageSource(
collectionProducer,filter));
tracker.addImage(terrainAndTextures[pk],1);
pk++;
pj++;
if (pj==16)
{
pj=0;
pi++;
}
}
pj=0;pi=0;
while (pk<500)
{
filter=new CropImageFilter(pj*32,pi*32,32,32);
terrainAndTextures[pk]=createImage(new FilteredImageSource(
collectionProducer1,filter));
tracker.addImage(terrainAndTextures[pk],1);
pk++;
pj++;
if (pj==15)
{
pj=0;
pi++;
}
}
int xAdd=0,yAdd=0;
k=0;
for (i=1;i<33;i++)
{
filter=new CropImageFilter(16+64*xAdd,48+96*yAdd,32,32);
terrainAndTextures[500+k]=createImage(new FilteredImageSource(
collectionProducer1,filter));
tracker.addImage(terrainAndTextures[500+k],1);
filter=new CropImageFilter(16+64*xAdd,32+96*yAdd,32,32);
terrainAndTextures[501+k]=createImage(new FilteredImageSource(
collectionProducer1,filter));
tracker.addImage(terrainAndTextures[501+k],1);
filter=new CropImageFilter(16+64*xAdd,64+96*yAdd,32,32);
terrainAndTextures[502+k]=createImage(new FilteredImageSource(
collectionProducer1,filter));
tracker.addImage(terrainAndTextures[502+k],1);
filter=new CropImageFilter(0+64*xAdd,48+96*yAdd,32,32);
terrainAndTextures[503+k]=createImage(new FilteredImageSource(
collectionProducer1,filter));
tracker.addImage(terrainAndTextures[503+k],1);
filter=new CropImageFilter(32+64*xAdd,48+96*yAdd,32,32);
terrainAndTextures[504+k]=createImage(new FilteredImageSource(
collectionProducer1,filter));
tracker.addImage(terrainAndTextures[504+k],1);
filter=new CropImageFilter(0+64*xAdd,32+96*yAdd,32,32);
terrainAndTextures[505+k]=createImage(new FilteredImageSource(
collectionProducer1,filter));
tracker.addImage(terrainAndTextures[505+k],1);
filter=new CropImageFilter(32+64*xAdd,32+96*yAdd,32,32);
terrainAndTextures[506+k]=createImage(new FilteredImageSource(
collectionProducer1,filter));
tracker.addImage(terrainAndTextures[506+k],1);
filter=new CropImageFilter(0+64*xAdd,64+96*yAdd,32,32);
terrainAndTextures[507+k]=createImage(new FilteredImageSource(
collectionProducer1,filter));
tracker.addImage(terrainAndTextures[507+k],1);
filter=new CropImageFilter(32+64*xAdd,64+96*yAdd,32,32);
terrainAndTextures[508+k]=createImage(new FilteredImageSource(
collectionProducer1,filter));
tracker.addImage(terrainAndTextures[508+k],1);
filter=new CropImageFilter(0+64*xAdd,0+96*yAdd,32,32);
terrainAndTextures[509+k]=createImage(new FilteredImageSource(
collectionProducer1,filter));
tracker.addImage(terrainAndTextures[509+k],1);
if(xAdd==7)
{
xAdd=0;
yAdd++;
}
else
xAdd++;
k+=10;
}
}
@Override
public void run()
{
while (game !=null)
{
while (pause==false)
{
try
{
game.sleep(30);
} catch (InterruptedException e) {}
count2++;
if (count2>2)
count2=1;
countSlow+=.2;
if (countSlow>2)
countSlow=1;
countSuperSlow+=.01;
if (countSuperSlow>2)
countSuperSlow=1;
cursorSet(1);
move();
math();
draw();
mapCheck();
resetVar();
}
}
}
@Override
public void start()
{
if (game==null)
{
game=new Thread(this);
game.start();
}
}
@Override
public void stop()
{
if ((game!=null)&&(game.isAlive()))
{
game.stop();
}
game=null;
}
public void resetVar()
{
motion=0;
movUD=0;
movLR=0;
speedMUL=speed;
loopCount=0;
}
@Override
public boolean mouseDown(java.awt.Event e,int x, int y)
{
/*int lp=512;
boolean ans=false;
int xDiff=x-(int)playerX;
int yDiff=y-(int)playerY;
mD=true;
while (ans==false)
{
System.out.println(lp);
if (xDiff%lp<=1&&yDiff%lp<=1)
{
ans=true;
xAdder=xDiff/lp;
yAdder=yDiff/lp;
System.out.print("the lowest common denominator is: "+lp+".");
}
else
lp--;
}*/
return true;
}
public void setMap(Map map)
{
j=0;k=0;
for(i=0;i<256;i++)
{
mapGraphics.drawImage(terrainAndTextures[map.getBGValueBySquare(i)], j*32, k*32, this);
mapGraphics.drawImage(terrainAndTextures[map.getFGValueBySquare(i)], j*32, k*32, this);
j++;k++;
if (j==16)
{
j=0;k++;
}
}
}
@Override
public boolean keyDown(java.awt.Event e,int key)
{
switch (key)
{
case 119://w
u=true;
System.out.println("kyle:"+maps.tutorial.getBGValueBySquare(1));
setMap(maps.forrestOutOfTown);
break;
case 97://a
l=true;
setMap(maps.tutorial);
break;
case 115://s
d=true;
break;
case 100://d
r=true;
break;
default:
break;
}
return true;
}
@Override
public boolean keyUp(Event e, int key)
{
switch (key)
{
case 119://w
u=false;
break;
case 97://a
l=false;
break;
case 115://s
d=false;
break;
case 100://d
r=false;
break;
default:
break;
}
return true;
}
public boolean isCharMoving()
{
if (u==true||l==true||u==true||r==true||d==true||l==true||d==true||r==true)
return true;
else
return false;
}
public void waitToLoad(int id)
{
try
{
tracker.waitForAll(id);
}
catch(InterruptedException e) {}
}
public void cursorSet(int k)
{
Point p=new Point(9,9);
setCursor(getToolkit().createCustomCursor(curs[k],p, "kyle"));
}
public void move()
{
if(u&&l||u&&r||d&&l||d&&r)
{
}
else
{
if (u==true)
{
motion=1;
dir=10;
movUD-=speedMUL;
}
if (d==true)
{
motion=2;
dir=1;
movUD+=speedMUL;
}
if (l==true)
{
motion=3;
dir=4;
movLR-=speedMUL;
}
if (r==true)
{
motion=4;
dir=7;
movLR+=speedMUL;
}
}
staleMotion=motion;
movUD/=10;
movLR/=10;
defineBoxes();
mapCheck();
switch(motion)
{
case 0:
break;
case 1:
playerX+=movLR;
playerY+=movUD;
break;
case 2:
playerX+=movLR;
playerY+=movUD;
break;
case 3:
playerX+=movLR;
playerY+=movUD;
break;
case 4:
playerX+=movLR;
playerY+=movUD;
break;
case 5:
playerX+=movLR;
playerY+=movUD;
break;
case 6:
playerX+=movLR;
playerY+=movUD;
break;
case 7:
playerX+=movLR;
playerY+=movUD;
break;
case 8:
playerX+=movLR;
playerY+=movUD;
break;
default:
break;
}
}
public void buildMap()
{
Graphics g;
map=createImage(512,512);
g=map.getGraphics();
g.setColor(Color.BLACK);
g.setClip(300, 300, 100, 100);
g.fillRect(300,300,100,100);
}
public void defineBoxes()
{
double tempY=(playerY+16-((playerY+16)%32))/32;
playerPos=(int)Math.round((tempY*16+playerX/32));
playerBox=new Rectangle2D.Double(playerX-1,playerY-1,34,34);
loopCount=0;
indexSaver=0;
if (playerY<=32&&u)
indexSaver=2;
if(playerPos+sideIX[staleMotion]<0)
{
indexSaver+=20;
}
else
{
indexSaver=0;
}
if(playerPos+sideIX[staleMotion]>256)
{
indexSaver=-20;
}
for (dj=0;dj<16;dj++)
{
for (di=0;di<16;di++)
{
terrainBox[loopCount]=new Rectangle2D.Double(di*32,dj*32,32,32);
terrain[loopCount]=0;
loopCount+=1;
}
}
inFront = terrain[playerPos+sideIX[staleMotion]+indexSaver];
}
public void math()
{
gpX=(int)Math.round(playerX);
gpY=(int)Math.round(playerY);
whichFrame=(int)Math.round(countSlow)+dir;
if (isCharMoving())
staleFrame=whichFrame;
//HEIGHT & WIDTH CONFIGURATION
height=this.getHeight();
width=this.getWidth();
if (width!=height)
{
if (width>height)
{
int diff=width-height;
centerX=diff/2;
width=height;
}
else
{
int diff=height-width;
centerY=diff/2;
height=width;
}
}
int h,w;
if ((h=height%32)!=0||(w=width%32)!=0)
{
height=height-height%32;
width=width-width%32;
}
}
public void wait(int howLong)
{
try
{
game.sleep(howLong);
} catch (InterruptedException e) {}
}
public void draw()
{
gBG.setColor(Color.red);
gBG.drawImage(mapImg, 32, 32, this);
gBG.drawString(""+playerPos,100,100);
gBG.drawImage(pers[staleFrame], gpX, gpY, this);
gBG.drawString(""+maps.tutorial.getBGValueBySquare(0), 300, 300);
repaint();
}
public void mapCheck()
{
if (inFront==1&&playerBox.intersects(terrainBox[playerPos+sideIX[staleMotion]]))
{
motion=0;
gBG.setColor(Color.red);
gBG.drawString("ya",100,300);
}
if (playerY==0&&u||playerY==480&&d)
{
motion=0;
}
if (playerX==0&&l||playerX==480&&r)
{
motion=0;
}
}
public void paint(Graphics g)
{
g.drawImage(ih, centerX, centerY, this);
ih=createImage(width,height);
Graphics rg=ih.getGraphics();
rg.drawImage(backBuffer, 0, 0,width,height, this);
}
}