This commit is contained in:
Victor 2018-11-15 00:00:02 +02:00
commit d05ddef0cf
4 changed files with 334 additions and 0 deletions

BIN
src/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 105 B

56
src/Keyboard.java Normal file
View File

@ -0,0 +1,56 @@
import javax.microedition.lcdui.*;
public class Keyboard {
private final int[] keys = {
Canvas.UP, Canvas.DOWN, Canvas.LEFT, Canvas.RIGHT
};
private boolean[] states;
public Keyboard() {
states = new boolean[keys.length];
for (int i = 0; i < states.length; i++) {
states[i] = false;
}
}
private int getKeyNum(int key) {
if (key == Canvas.KEY_NUM0) Main.midlet.destroyApp(true);
else if(key == Canvas.KEY_STAR) Main.midlet.dsp.setCurrent(null);
else {
for (int i = 0; i < keys.length; i++) {
if (key == keys[i]) return i;
}
}
return -1;
}
public void onPressed(int key, int act) {
int iKey = getKeyNum(key);
if (iKey != -1) {
states[iKey] = true;
}
iKey = getKeyNum(act);
if (iKey != -1) {
states[iKey] = true;
}
}
public void onReleased(int key, int act) {
int iKey = getKeyNum(key);
if (iKey != -1) {
states[iKey] = false;
}
iKey = getKeyNum(act);
if (iKey != -1) {
states[iKey] = false;
}
}
public boolean getKeyState(int key) {
int iKey = getKeyNum(key);
if (iKey != -1) return states[iKey];
return false;
}
}

28
src/Main.java Normal file
View File

@ -0,0 +1,28 @@
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class Main extends MIDlet {
public Display dsp;
public static Main midlet;
private RayCanvas canvas;
public Main() {
dsp = Display.getDisplay(Main.this);
midlet = Main.this;
}
public void startApp() {
canvas = new RayCanvas();
dsp.vibrate(1000);
dsp.setCurrent(canvas);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
}

250
src/RayCanvas.java Normal file
View File

@ -0,0 +1,250 @@
import java.io.IOException;
import javax.microedition.lcdui.*;
public class RayCanvas extends Canvas {// implements Runnable {
private Keyboard keyb;
//private final int mapWidth = 24;
//private final int mapHeight = 24;
private int gluk;
private Image pr;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0,0,0,0,0,0,0,0,0,0,0,0,3,0,3,0,3,0,3,0,0,0,0,0,0,0,1},
{1,2,0,2,0,2,2,1,1,1,1,0,0,0,0,0,0,0,0,0,3,3,3,0,3,0,3,0,3,0,0,0,0,0,0,0,1},
{1,0,0,3,0,0,0,0,0,0,3,0,2,2,2,2,2,2,0,0,3,0,0,0,3,0,3,0,3,0,0,0,0,0,0,0,1},
{1,4,4,4,4,4,4,4,4,0,3,0,2,0,0,2,2,2,2,0,2,0,3,0,0,4,1,0,1,0,0,0,0,0,0,0,1},
{1,4,0,4,0,0,0,0,4,0,3,3,2,2,0,0,0,0,0,0,0,0,3,4,4,4,1,0,1,0,0,0,0,0,0,0,1},
{1,4,0,0,0,0,5,0,4,0,0,0,0,4,0,4,0,4,4,4,4,4,4,4,0,0,1,0,1,0,0,0,0,0,0,0,1},
{1,4,0,4,0,0,0,0,4,0,2,3,0,4,0,4,0,4,0,4,4,0,4,4,0,0,2,0,2,0,0,0,0,0,3,3,1},
{1,4,0,4,4,4,4,4,4,0,3,3,0,0,0,4,0,4,0,0,0,0,0,4,0,0,3,0,3,0,0,0,0,0,3,0,1},
{1,4,0,0,0,0,0,0,0,0,3,4,4,4,4,4,0,4,0,4,4,4,4,4,1,2,4,0,4,3,2,1,1,2,3,0,1},
{1,4,4,4,4,4,4,4,4,3,3,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
private int[][] colors = new int[][] {
{0xcccc44, 0x444444, 0x333333, 0x222222, 0x111111},//gluk0
{0xcccc44, 0xaaaaaa, 0x888888, 0x555555, 0xcccccc},//gluk1
{0xcccc44, 0xcc4444, 0x44cc44, 0x4444cc, 0xcccccc},//gluk2
{0xcccc44, 0xaa54a4, 0x803488, 0x5a5055, 0xc2087c} //gluk3
};
private double posX, posY; //x and y start position
private double dirX, dirY; //initial direction vector
private double planeX, planeY; //the 2d raycaster version of camera plane
private int w,h;
private long lastTime;
private int iFps, iActFps, iTimeToCountFps;
public RayCanvas() {
setFullScreenMode(true);
w = getWidth();
h = getHeight();
posX = 22;
posY = 27.5;
dirX = -1;
dirY = planeX = 0;
iFps = iActFps = iTimeToCountFps = 0;
planeY = 0.66;
gluk = 1;
keyb = new Keyboard();
try {
pr = Image.createImage("/2.png");
} catch (IOException ex) {}
// new Thread(this).start();
}
// public void run() {
// while (true) {
// try {
// repaint();
// Thread.sleep(3);
// } catch (Exception e) {}
// }
// }
protected void keyPressed(int key) {
keyb.onPressed(key, getGameAction(key));
}
protected void keyReleased(int key) {
keyb.onReleased(key, getGameAction(key));
}
public void paint(Graphics g) {
long thisTime = System.currentTimeMillis(); // òåêóùåå âðåìÿ
int dTime = (int) (thisTime - lastTime); // âðåìÿ, ïðîøåäøåå ñ ïðîøëîãî êàäðà
lastTime = thisTime;
iTimeToCountFps += dTime;
iFps++;
if (iTimeToCountFps >= 1000) {// åñëè íàáðàëîñü 1 ñåêóíäà - îáíîâëÿåì ñ÷åò÷èê
iActFps = iFps;
iTimeToCountFps = iFps = 0;
}
g.setColor(0);
g.fillRect(0, 0, w, h);
for (int x = 0; x < w; x++) {
//calculate ray position and direction
double cameraX = 2 * x / (double) w - 1; //x-coordinate in camera space
double rayPosX = posX;
double rayPosY = posY;
double rayDirX = dirX + planeX * cameraX;
double rayDirY = dirY + planeY * cameraX;
//which box of the map we're in
int mapX = (int) rayPosX;
int mapY = (int) rayPosY;
//length of ray from current position to next x or y-side
double sideDistX;
double sideDistY;
//length of ray from one x or y-side to next x or y-side
double deltaDistX = Math.sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX));
double deltaDistY = Math.sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));
double perpWallDist;
//what direction to step in x or y-direction (either +1 or -1)
int stepX;
int stepY;
int hit = 0; //was there a wall hit?
int side = 0; //was a NS or a EW wall hit?
//calculate step and initial sideDist
if (rayDirX < 0) {
stepX = -1;
sideDistX = (rayPosX - mapX) * deltaDistX;
} else {
stepX = 1;
sideDistX = (mapX + 1.0 - rayPosX) * deltaDistX;
}
if (rayDirY < 0) {
stepY = -1;
sideDistY = (rayPosY - mapY) * deltaDistY;
} else {
stepY = 1;
sideDistY = (mapY + 1.0 - rayPosY) * deltaDistY;
}
//perform DDA
while (hit == 0) {
//jump to next map square, OR in x-direction, OR in y-direction
if (sideDistX < sideDistY) {
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
} else {
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
//Check if ray has hit a wall
if (worldMap[mapX][mapY] > 0)
hit = 1;
}
//Calculate distance projected on camera direction (oblique distance will give fisheye effect!)
if (side == 0) {
perpWallDist = Math.abs((mapX - rayPosX + (1 - stepX) / 2) / rayDirX);
} else {
perpWallDist = Math.abs((mapY - rayPosY + (1 - stepY) / 2) / rayDirY);
}
//Calculate height of line to draw on screen
int lineHeight = Math.abs((int) (h / perpWallDist));
//calculate lowest and highest pixel to fill in current stripe
int drawStart = -lineHeight / 2 + h / 2;
if (drawStart < 0) drawStart = 0;
int drawEnd = lineHeight / 2 + h / 2;
if (drawEnd >= h) drawEnd = h - 1;
g.drawImage(pr, w / 2, h / 2, 3);
//choose wall color
if (posX < 8) gluk = 1;
else if(posX > 8 && posX < 16) gluk = 2;
else if (posX > 16 && posX < 24) gluk = 3;
else if ((posX > 13) && posY < 11) gluk = 0;
else if (posY < 9 && posX < 20 && posY > 4 && posX > 17) {
g.setColor(0xff0000);
g.drawString("Âû âûèãðàëè‘", w/2, h/2, 33);
}
int color = 0;
try {
color = colors[gluk][worldMap[mapX][mapY]];
//give x and y sides different brightness
if (side == 1) color -= 0x333333;
} catch (Exception e) {
color = 0xcccc44;
}
//draw the pixels of the stripe as a vertical line
//verLine(x, drawStart, drawEnd, color);
g.setColor(color);
g.drawLine(x, drawStart, x, drawEnd);
}
//speed modifiers
double moveSpeed = (((double) dTime) / 1000) * 5.0; //the constant value is in squares/second
double rotSpeed = (((double) dTime) / 1000) * 3.0; //the constant value is in radians/second
//move forward if no wall in front of you
if (keyb.getKeyState(UP)) {
if (worldMap[(int) (posX + dirX * moveSpeed)][(int) posY] == 0) {
posX += dirX * moveSpeed;
}
if (worldMap[(int) posX][(int) (posY + dirY * moveSpeed)] == 0) {
posY += dirY * moveSpeed;
}
}
//move backwards if no wall behind you
if (keyb.getKeyState(DOWN)) {
if (worldMap[(int) (posX - dirX * moveSpeed)][(int) posY] == 0) {
posX -= dirX * moveSpeed;
}
if (worldMap[(int) posX][(int) (posY - dirY * moveSpeed)] == 0) {
posY -= dirY * moveSpeed;
}
}
//rotate to the right
if (keyb.getKeyState(RIGHT)) {
//both camera direction and camera plane must be rotated
double oldDirX = dirX;
dirX = dirX * Math.cos(-rotSpeed) - dirY * Math.sin(-rotSpeed);
dirY = oldDirX * Math.sin(-rotSpeed) + dirY * Math.cos(-rotSpeed);
double oldPlaneX = planeX;
planeX = planeX * Math.cos(-rotSpeed) - planeY * Math.sin(-rotSpeed);
planeY = oldPlaneX * Math.sin(-rotSpeed) + planeY * Math.cos(-rotSpeed);
}
//rotate to the left
if (keyb.getKeyState(LEFT)) {
//both camera direction and camera plane must be rotated
double oldDirX = dirX;
dirX = dirX * Math.cos(rotSpeed) - dirY * Math.sin(rotSpeed);
dirY = oldDirX * Math.sin(rotSpeed) + dirY * Math.cos(rotSpeed);
double oldPlaneX = planeX;
planeX = planeX * Math.cos(rotSpeed) - planeY * Math.sin(rotSpeed);
planeY = oldPlaneX * Math.sin(rotSpeed) + planeY * Math.cos(rotSpeed);
}
// fps
g.setColor(0xff);
g.drawString("" + iActFps, 1, 1, 20);
repaint();
}
}