📄 lev.java
字号:
import java.awt.*;
class lev
{
// All the ents on the level - a lot
public ent[] curEnt;
// An array of the ents that move at any moment in a 'room'
public ent[] movInRm;
public int numInRm;
// Items class used to get IDs etc
private items item;
// Always point to correct page to draw on
private Graphics paper;
private Image gfx;
private Image gfx2;
public lev(String st, Graphics gr, Image im, Image im2)
{
int len=st.length()-1;
this.curEnt=new ent[len];
this.movInRm=new ent[100];
this.numInRm=0;
this.paper=gr;
this.gfx=im;
this.gfx2=im2;
this.item=new items();
int x=0;
int y=0;
int z=0;
int ms=0;
char s;
boolean mv;
boolean dirU;
boolean dirD;
boolean dirL;
boolean dirR;
// Create level from string passed in st, char by char
for (int a=0; a<len; a++)
{
mv=false;
dirU=false;
dirD=false;
dirL=false;
dirR=false;
ms=4;
// Get ID from items class
s=st.charAt(a);
z=item.getID(s);
switch (z)
{
// Moving things
// Ghost
case 96:
mv=true;
dirL=true;
break;
// Bat Left/Right
case 87:
mv=true;
dirL=true;
break;
// Bat Up/Down
case 88:
mv=true;
dirU=true;
break;
// Demon
case 94:
mv=true;
dirR=true;
break;
// Devil
case 92:
mv=true;
dirU=true;
break;
// Trader
case 90:
mv=true;
dirR=true;
break;
// Death!
case 85:
mv=true;
dirD=true;
break;
// Plant
case 100:
mv=true;
dirU=true;
ms=2;
break;
// Grunt
case 102:
mv=true;
dirD=true;
break;
// Spider
case 104:
mv=true;
dirR=true;
ms=5;
break;
// Nice Zombie
case 106:
mv=true;
dirL=true;
ms=2;
break;
// Evil Wizard
case 108:
mv=true;
dirL=true;
break;
default:
break;
}
int q=x+(y*200);
curEnt[q]=new ent(x*20,y*20,z);
curEnt[q].moves=mv;
curEnt[q].dirU=dirU;
curEnt[q].dirD=dirD;
curEnt[q].dirL=dirL;
curEnt[q].dirR=dirR;
curEnt[q].moveSpeed=ms;
x++;
if (x>199)
{
x=0;
y++;
}
}
}
// Takes an x block, yblock and returns int i which is the block nearest to us
// that contains nothing (for dropping an item for example)
// return 0 if nothing available
public int getNearestSpace(int x, int y)
{
int y2;
y2=y;
for (int x2=x-1; x2<x+2; x2++)
{
if (curEnt[x2+(y2*200)].z==0)
{
if (!(y2==y && x2==x))
{
return x2+(y2*200);
}
}
}
y2=y-1;
for (int x2=x-1; x2<x+2; x2++)
{
if (curEnt[x2+(y2*200)].z==0)
{
if (!(y2==y && x2==x))
{
return x2+(y2*200);
}
}
}
y2=y+1;
for (int x2=x-1; x2<x+2; x2++)
{
if (curEnt[x2+(y2*200)].z==0)
{
if (!(y2==y && x2==x))
{
return x2+(y2*200);
}
}
}
return 0;
}
// Figure out how to draw a window on the world...
// Also, set up movInRm array... containing ents in this room that move
public void setUpWindow(int xp, int yp)
{
int xabs=0;
int yabs=0;
numInRm=0;
// A neat window from our origin
for (int enty=yp-5; enty <= yp+6; enty++)
{
for (int entx=xp-5; entx <= xp+6; entx++)
{
int a=entx+(enty*200);
if (a<0 || a>27399) a=0;
int xoff=curEnt[a].z;
doDraw(xabs, yabs, xoff, true);
xabs+=20;
// An ent that moves is in the room, add to the movInRm list
if (curEnt[a].moves)
{
addToMvArray(a);
}
}
xabs=0;
yabs+=20;
}
}
// Redistribute item when used randomly, means that we always
// have enough food, health and plants to complete the game
public void reDist(int a)
{
int c=200*135;
int b=(int)(c*Math.random())+1;
while (curEnt[b].z>0)
{
b=(int)(c*Math.random())+1;
}
curEnt[b].z=a;
}
// Update window (re-draw), leaving monsters as they are
public void updateWindow(int xp, int yp)
{
int xabs=0;
int yabs=0;
for (int enty=yp-5; enty <= yp+6; enty++)
{
for (int entx=xp-5; entx <= xp+6; entx++)
{
int a=entx+(enty*200);
if (a<0 || a>27399) a=0;
int xoff=curEnt[a].z;
doDraw(xabs, yabs, xoff, true);
xabs+=20;
}
xabs=0;
yabs+=20;
}
}
// Add ent 'a' to movInRm array list
public void addToMvArray(int a)
{
movInRm[numInRm]=new ent(curEnt[a].x, curEnt[a].y, curEnt[a].z);
movInRm[numInRm].moves=curEnt[a].moves;
movInRm[numInRm].dirU=curEnt[a].dirU;
movInRm[numInRm].dirD=curEnt[a].dirD;
movInRm[numInRm].dirL=curEnt[a].dirL;
movInRm[numInRm].moveSpeed=curEnt[a].moveSpeed;
movInRm[numInRm++].dirR=curEnt[a].dirR;
curEnt[a].z=0;
curEnt[a].moves=false;
}
// Remove ent 'a' to movInRm array list
// Method: Copy last member into position to delete and subtract 1 from number of members
public void removeFromRm(int a)
{
numInRm--;
if (numInRm<0) numInRm=0;
if (numInRm>0 && a!=numInRm)
{
movInRm[a].moves=movInRm[numInRm].moves;
movInRm[a].dirU=movInRm[numInRm].dirU;
movInRm[a].dirD=movInRm[numInRm].dirD;
movInRm[a].dirL=movInRm[numInRm].dirL;
movInRm[a].dirR=movInRm[numInRm].dirR;
movInRm[a].x=movInRm[numInRm].x;
movInRm[a].y=movInRm[numInRm].y;
movInRm[a].z=movInRm[numInRm].z;
movInRm[a].i=movInRm[numInRm].i;
movInRm[a].moveSpeed=movInRm[numInRm].moveSpeed;
}
movInRm[numInRm].z=0;
}
// ** Routines for moving left, right, up, down. There is a better way of doing
// this, I know.
// We're moving downwards, copy screen to top and draw in bottom
public void moveDown()
{
paper.copyArea(0,20,240,220, 0, -20);
}
public void drawBottom(int xp, int yp)
{
int xabs=0;
int yabs=220;
int enty=yp+6;
for (int entx=xp-5; entx <= xp+6; entx++)
{
int a=entx+(enty*200);
if (a<0 || a>27399) a=0;
int xoff=curEnt[a].z;
doDraw(xabs, yabs, xoff, true);
xabs+=20;
// An ent that moves is in the room, add to the movInRm list
if (curEnt[a].moves)
{
addToMvArray(a);
}
}
}
// We're moving upwards, copy screen to bottom and draw in top
public void moveUp()
{
paper.copyArea(0,0,240,220, 0, 20);
}
public void drawTop(int xp, int yp)
{
int xabs=0;
int yabs=0;
int enty=yp-5;
for (int entx=xp-5; entx <= xp+6; entx++)
{
int a=entx+(enty*200);
if (a<0 || a>27399) a=0;
int xoff=curEnt[a].z;
doDraw(xabs, yabs, xoff, true);
xabs+=20;
// An ent that moves is in the room, add to the movInRm list
if (curEnt[a].moves)
{
addToMvArray(a);
}
}
}
// We're moving left, copy screen to right and draw in left
public void moveLeft()
{
paper.copyArea(0,0,220,240, 20, 0);
}
public void drawLeft(int xp, int yp)
{
int xabs=0;
int yabs=0;
int entx=xp-5;
for (int enty=yp-5; enty <= yp+6; enty++)
{
int a=entx+(enty*200);
if (a<0 || a>27399) a=0;
int xoff=curEnt[a].z;
doDraw(xabs, yabs, xoff, true);
yabs+=20;
// An ent that moves is in the room, add to the movInRm list
if (curEnt[a].moves)
{
addToMvArray(a);
}
}
}
// We're moving right, copy screen to left and draw in right
public void moveRight()
{
paper.copyArea(20,0,220,240, -20, 0);
}
public void drawRight(int xp, int yp)
{
int xabs=220;
int yabs=0;
int entx=xp+6;
for (int enty=yp-5; enty <= yp+6; enty++)
{
int a=entx+(enty*200);
if (a<0 || a>27399) a=0;
int xoff=curEnt[a].z;
doDraw(xabs, yabs, xoff, true);
yabs+=20;
// An ent that moves is in the room, add to the movInRm list
if (curEnt[a].moves)
{
addToMvArray(a);
}
}
}
public void doDraw(int xabs, int yabs, int xoff, boolean b)
{
int yoff=0;
if (xoff>0)
{
yoff=((int)xoff/10)*20;
xoff%=10;
xoff*=20;
if (b)
{
paper.drawImage(gfx,
xabs, yabs, xabs+20, yabs+20,
xoff, yoff, xoff+20, yoff+20,
null);
}
else
{
paper.drawImage(gfx2,
xabs, yabs, xabs+20, yabs+20,
xoff, yoff, xoff+20, yoff+20,
null);
}
}
else
{
paper.setColor(Color.black);
paper.fillRect(xabs, yabs, 20,20);
}
}
// ent Collision ? Does ent e collide with ent f
// If so, return value of ent f's z
public int collide(ent e, ent f)
{
int x1,y1,x2,y2;
if (f.z==0) return 0;
x1=e.x;
y1=e.y;
x2=f.x;
if (x2>x1-20)
{
if (x2<x1+20)
{
y2=f.y;
if (y2>y1-20)
{
if (y2<y1+20) return f.z;
}
}
}
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -