📄 world.java
字号:
import net.jscience.math.kvm.MathFP;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
public class World
{
public static final int TILE_WIDTH=32;
public static final int TILE_HEIGHT=32;
public static final int TILE_WIDTH_FP = MathFP.toFP(TILE_WIDTH);
public static final int TILE_HEIGHT_FP = MathFP.toFP(TILE_HEIGHT);
public static final int TILE_HALF_HEIGHT = TILE_HEIGHT / 2;
public static final int TILE_HALF_WIDTH = TILE_WIDTH / 2;
private int tilesWide = 20;
private int tilesHigh = 20;
private byte[][] tileMap;
private int viewWidth;
private int viewHeight;
// tile types
public static final byte NO_TILE = 0;
private long lastCycleTime;
// raytracer
private int columnsPerDegree;
static int[] lookupDistortionFP;
private int projWallHeight;
static int WALL_HEIGHT = TILE_WIDTH*2; // makes walls twice as high as
// they are wide
static int PROJ_PLANE_HEIGHT = 128;
static int PROJ_PLANE_HEIGHT_CENTER = PROJ_PLANE_HEIGHT/2;
static int EDGE_IGNORE_FP = MathFP.toFP(5);
static int HORIZON_DISTANCE_FP = MathFP.toFP(TILE_WIDTH * 10 * 2);
private int focalDistance;
private int fovColumns;
private int halfFovColumns; // precalced cause we use it a bit
private Image background;
private Actor player;
// lookup table for trig values (0 to 359)
public static int[] lookupCosFP = new int[360];
public static int[] lookupSinFP = new int[360];
public static int[] lookupTanFP = new int[360];
// static init
{
for (int i = 0; i < 360; i++)
{
// build an array of precalculated trig values (much faster to just
// look them up here) note we add 0.01 to avoid all divide by zero
// cases.
lookupCosFP[i] = MathFP.cos(MathFP.add(MathFP.toFP("0.01"), Actor.getRadiansFPFromAngle(i)));
lookupSinFP[i] = MathFP.sin(MathFP.add(MathFP.toFP("0.01"), Actor.getRadiansFPFromAngle(i)));
lookupTanFP[i] = MathFP.tan(MathFP.add(MathFP.toFP("0.01"), Actor.getRadiansFPFromAngle(i)));
}
}
public World(int viewWidthArg, int viewHeightArg, Actor p)
{
player = p;
viewWidth = viewWidthArg;
viewHeight = viewHeightArg;
// calculate field of view (60 degrees is ideal, but we need to base
// it on how many columns we can display in terms of pixels
columnsPerDegree = viewWidth / 60;
fovColumns = viewWidth / columnsPerDegree;
halfFovColumns = fovColumns / 2;
// calculate the focal distance
int distFP = MathFP.div( MathFP.toFP(viewWidth/2), lookupTanFP[halfFovColumns] );
focalDistance = MathFP.toInt(distFP);
projWallHeight = MathFP.toFP(WALL_HEIGHT * focalDistance);
//#ifdef debug
System.out.println("Focal distance: " + focalDistance);
System.out.println("FOV columns: " + fovColumns);
System.out.println("Degrees per column: " + columnsPerDegree);
//#endif
// precalculate the distortion values
lookupDistortionFP = new int[fovColumns+1];
for (int i = -halfFovColumns; i <= halfFovColumns; i++)
lookupDistortionFP[i+halfFovColumns] =
MathFP.div(MathFP.toFP(1),
MathFP.cos(Actor.getRadiansFPFromAngle(i)));
// some pretty clouds
background = ImageSet.loadClippedImage("/background.png", 0, 0);
// setup the map
tileMap = new byte[20][20];
// side walls
for (int tx=0; tx < 20; tx++)
{
tileMap[0][tx] = 1;
tileMap[19][tx] = 1;
}
// top and bottom walls
for (int ty=0; ty < 20; ty++)
{
tileMap[ty][0] = 1;
tileMap[ty][19] = 1;
}
// pillars every four tiles
for (int ty=0; ty < 20; ty+=4)
for (int tx=0; tx < 20; tx+=4)
tileMap[ty][tx] = 1;
}
protected final void cycle()
{
if (lastCycleTime > 0)
{
long msSinceLastCycle = System.currentTimeMillis() - lastCycleTime;
player.cycle(msSinceLastCycle);
}
lastCycleTime = System.currentTimeMillis();
}
public final int getTileXCenter(int x)
{
return (getTileX(x) * TILE_WIDTH) + (TILE_WIDTH / 2);
}
public final int getTileYCenter(int y)
{
return (getTileY(y) * TILE_HEIGHT) + (TILE_HEIGHT / 2);
}
public final int getTileX(int x) { return x / TILE_WIDTH; }
public final int getTileY(int y) { return y / TILE_HEIGHT; }
public final byte getTile(int x, int y)
{
int tx = getTileX(x);
int ty = getTileY(y);
if (tx < 0 || tx >= tilesWide || ty < 0 || ty >= tilesHigh) return -1;
return tileMap[ ty ][ tx ];
}
private boolean isWall(int px, int py)
{
if ( getTile( px, py ) == 1)
return true;
return false;
}
private boolean isOnMap(int px, int py)
{
if (px < 0 || py < 0) return false;
if (px > TILE_WIDTH * tilesWide) return false;
if (py > TILE_HEIGHT * tilesHigh) return false;
return true;
}
private int angleChange(int a, int change)
{
int angle = a + change;
if (angle > 359) angle -= 360;
if (angle < 0) angle = 360 - Math.abs(angle);
return angle;
}
private int getDistanceFP(int dxFP, int dyFP, int angle)
{
int distanceFP = 0;
if ( MathFP.abs(dxFP) > MathFP.abs(dyFP) )
distanceFP = MathFP.div(MathFP.abs(dxFP), lookupCosFP[angle]);
else
distanceFP = MathFP.div(MathFP.abs(dyFP), lookupSinFP[angle]);
return MathFP.abs(distanceFP);
}
private static boolean lastHitWasVert=false;
public final void render(Graphics g)
{
// draw the background
g.drawImage(background, 0, 0, Graphics.TOP|Graphics.LEFT);
/////////////////////////////////////////////////////////////////////////
// RAYCASTING
/////////////////////////////////////////////////////////////////////////
int startingXFP = MathFP.toFP(player.getX());
int startingYFP = MathFP.toFP(player.getY());
int startingX = player.getX();
int startingY = player.getY();
int rayAngle=angleChange(player.getDirection(), halfFovColumns);
int pixelColumn = 0;
int destinationXFP=0,destinationYFP=0;
int originXFP=0,originYFP=0;
for (int castColumn=0; castColumn < fovColumns; castColumn++)
{
rayAngle = angleChange(rayAngle, -1);
// --------------- HORIZONTAL RAY ------------------
// try casting a horizontal line until we get a hit
boolean gotHorizHit = false;
int horizDistanceFP = 0;
// if the ray can possibly hit a horizontal line
if (rayAngle != 0 && rayAngle != 180)
{
// cast the first ray to the intersection point
// figure out the coord of the vert tile line we're after
// (based on whether we're looking left or right). Note we offset
// by one pixel so we know we're *inside* a tile cell
if (rayAngle > 180)
originYFP = MathFP.toFP( (((startingY / TILE_HEIGHT) +1) * TILE_HEIGHT) + 1 );
else
originYFP = MathFP.toFP(((startingY / TILE_HEIGHT) * TILE_HEIGHT) - 1);
destinationYFP = MathFP.sub(startingYFP, originYFP);
destinationXFP = MathFP.div(destinationYFP, lookupTanFP[rayAngle]);
originXFP = MathFP.add(startingXFP, destinationXFP);
// get the distance to the first grid cell
horizDistanceFP = getDistanceFP(destinationXFP, destinationYFP, rayAngle);
while (!gotHorizHit)
{
// abort if we're past the horizon
if (horizDistanceFP > HORIZON_DISTANCE_FP) break;
// did we hit a wall?
if (isWall(MathFP.toInt(originXFP), MathFP.toInt(originYFP)))
{
gotHorizHit = true;
break;
}
// are we still on the map?
if (!isOnMap(MathFP.toInt(originXFP), MathFP.toInt(originYFP)))
break;
int lastXFP = originXFP;
int lastYFP = originYFP;
// project to the next point along
if (rayAngle > 180) // if ray going down
originYFP = MathFP.add(lastYFP, TILE_HEIGHT_FP);
else // if ray going up
originYFP = MathFP.sub(lastYFP, TILE_HEIGHT_FP);
destinationYFP = MathFP.sub(lastYFP, originYFP);
destinationXFP = MathFP.div(destinationYFP, lookupTanFP[rayAngle]);
// move out to the next tile position
originXFP = MathFP.add(lastXFP, destinationXFP);
// add the distance to our running total
horizDistanceFP = MathFP.add(horizDistanceFP,
getDistanceFP(destinationXFP, destinationYFP, rayAngle));
}
}
if (gotHorizHit)
{
// got a hit, lets work out what position along the cell wall the
// ray struck (disabled because it's not being used)
//cellPos = MathFP.toInt(ixFP) % TILE_HEIGHT;
}
else
// make sure we dont think this is the closest (otherwise it would
// still be 0)
horizDistanceFP = MathFP.toFP(99999);
// --------------- VERTICAL RAY ------------------
boolean gotVertHit = false;
int vertDistanceFP = 0;
// if the ray can possibly hit a vertical line
if (rayAngle != 90 && rayAngle != 270)
{
// cast the first ray to the intersection point
// figure out the coord of the vert tile line we're after
// (based on whether we're looking left or right). Note we offset
// by one pixel so we know we're *inside* a tile cell
if (rayAngle < 90 || rayAngle > 270) // if ray going right
originXFP = MathFP.toFP((((startingX / TILE_WIDTH) +1) *
TILE_WIDTH) + 1);
else // if ray going left
originXFP = MathFP.toFP(((startingX / TILE_WIDTH) *
TILE_WIDTH) - 1);
destinationXFP = MathFP.sub(originXFP, startingXFP);
destinationYFP = MathFP.div(destinationXFP,
lookupTanFP[angleChange(rayAngle,-90)]);
originYFP = MathFP.add(startingYFP, destinationYFP);
// get the distance to the first grid cell
vertDistanceFP = getDistanceFP(destinationXFP, destinationYFP,
rayAngle);
while (!gotVertHit)
{
if (vertDistanceFP > HORIZON_DISTANCE_FP) break;
// did we hit a wall?
if (isWall(MathFP.toInt(originXFP), MathFP.toInt(originYFP)))
{
gotVertHit = true;
break;
}
if (!isOnMap(MathFP.toInt(originXFP), MathFP.toInt(originYFP)))
break;
int lastXFP = originXFP;
int lastYFP = originYFP;
// project to the next point along
if (rayAngle < 90 || rayAngle > 270) // if ray going right
originXFP = MathFP.add(lastXFP, TILE_WIDTH_FP);
else
originXFP = MathFP.sub(lastXFP, TILE_WIDTH_FP);
destinationXFP = MathFP.sub(originXFP, lastXFP);
destinationYFP = MathFP.div(destinationXFP,
lookupTanFP[angleChange(rayAngle,-90)]);
originYFP = MathFP.add(lastYFP, destinationYFP);
// extend out the distance (so we know when we're casting
// beyond the world edge)
vertDistanceFP = MathFP.add(vertDistanceFP, getDistanceFP(
destinationXFP, destinationYFP, rayAngle));
}
}
if (gotVertHit)
{
// got a hit, lets work out what position along the cell wall the
// ray struck
//cellPos = MathFP.toInt(iyFP) % TILE_WIDTH;
}
else // make sure we dont think this is the closest
vertDistanceFP = MathFP.toFP(99999);
// -------------- DRAW WALL SLICE ------------------
// since it's possible (especially with nearby walls) to get a ray
// hit on both vertical and horizontal lines we have to figure out
// which one to use (the closest)
if (gotVertHit || gotHorizHit)
{
int distanceFP = MathFP.min(horizDistanceFP, vertDistanceFP);
int diffFP = MathFP.abs(MathFP.sub(horizDistanceFP, vertDistanceFP));
boolean wasVerticalHit = true;
if (horizDistanceFP > vertDistanceFP)
wasVerticalHit = false;
if (diffFP <= EDGE_IGNORE_FP)
// if distance is the same then we hit a corner, we assume the
// previous dir hit so as to give us a smooth line along walls
wasVerticalHit = lastHitWasVert;
if (wasVerticalHit)
{
// VERTICAL EDGE
if (diffFP > EDGE_IGNORE_FP) lastHitWasVert = true;
g.setColor(0x777777);
}
else
{
// HORIZONTAL EDGE
if (diffFP > EDGE_IGNORE_FP) lastHitWasVert = false;
g.setColor(0x333333);
}
if (lookupDistortionFP[castColumn] > 0 && distanceFP > 0)
// adjust for distortion
distanceFP = MathFP.div(distanceFP, lookupDistortionFP[castColumn]);
if (distanceFP > 0)
{
int wallHeight = MathFP.toInt(MathFP.div(projWallHeight, distanceFP));
int bottomOfWall = PROJ_PLANE_HEIGHT_CENTER + (wallHeight/2);
int topOfWall = PROJ_PLANE_HEIGHT - bottomOfWall;
if (bottomOfWall >= PROJ_PLANE_HEIGHT)
bottomOfWall = PROJ_PLANE_HEIGHT-1;
// draw the wall (pixel column)
g.fillRect(pixelColumn, topOfWall, columnsPerDegree, wallHeight);
}
// and move across to the next column position
pixelColumn += columnsPerDegree;
}
}
//System.out.println("done ----------------------");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -