Skip to content

Instantly share code, notes, and snippets.

@gmisail
Created November 15, 2016 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gmisail/e484979a89daa204dcff31c36be7f54b to your computer and use it in GitHub Desktop.
Save gmisail/e484979a89daa204dcff31c36be7f54b to your computer and use it in GitHub Desktop.
package flux;
import openfl.display.*;
import openfl.events.*;
import openfl.*;
import openfl.ui.Keyboard;
import flux.util.*;
class Game extends Sprite{
public var data:BitmapData;
public var posX:Float;
public var posY:Float;
public var dirX:Float;
public var dirY:Float;
public var planeX:Float;
public var planeY:Float;
public var time:Float;
public var oldTime:Float;
public var w:Int;
public var h:Int;
public var color:UInt;
public var map:Array<Array<Int>>;
var lastTick:Float = 0;
public var speed:Float = 0;
public var rot:Float = 0;
public var textures:Array<BitmapData>;
public var textureWidth:Float = 32;
public var textureHeight:Float = 32;
private var keys:Array<Bool>;
public function new () {
super ();
w = Lib.current.stage.stageWidth;
h = Lib.current.stage.stageHeight;
data = new BitmapData(w, h, true, 0xFF000000);
var bitmap = new Bitmap(data);
addChild(bitmap);
posX = 22;
posY = 12;
dirX = -1;
dirY = 0;
planeX = 0;
planeY = 0.66;
time = 0;
oldTime = 0;
map = [
[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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,2,2,2,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1],
[1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,3,0,0,0,3,0,0,0,1],
[1,0,0,0,0,0,2,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,2,2,0,2,2,0,0,0,0,3,0,3,0,3,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,4,0,0,0,0,5,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,4,0,4,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,4,0,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1],
[1,4,4,4,4,4,4,4,4,0,0,0,0,0,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]
];
var wallData = Assets.getBitmapData ("assets/wall.png");
textures = [wallData, wallData, wallData, wallData, wallData, wallData];
keys = [];
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
Lib.current.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
addEventListener (Event.ENTER_FRAME, this_onEnterFrame);
}
private function onKeyDown(evt:KeyboardEvent):Void {
if(evt.keyCode == Keyboard.W){
if(map[Std.int(posX + dirX * speed)][Std.int(posY)] == 0) posX += dirX * speed;
if(map[Std.int(posX)][Std.int(posY + dirY * speed)] == 0) posY += dirY * speed;
trace("w");
}
if(evt.keyCode == Keyboard.S){
if(map[Std.int(posX - dirX * speed)][Std.int(posY)] == 0) posX -= dirX * speed;
if(map[Std.int(posX)][Std.int(posY - dirY * speed)] == 0) posY -= dirY * speed;
trace("s");
}
if(evt.keyCode == Keyboard.D){
var oldDirX:Float = dirX;
dirX = dirX * Math.cos(-rot) - dirY * Math.sin(-rot);
dirY = oldDirX * Math.sin(-rot) + dirY * Math.cos(-rot);
var oldPlaneX:Float = planeX;
planeX = planeX * Math.cos(-rot) - planeY * Math.sin(-rot);
planeY = oldPlaneX * Math.sin(-rot) + planeY * Math.cos(-rot);
trace("d");
}
if(evt.keyCode == Keyboard.A){
var oldDirX:Float = dirX;
dirX = dirX * Math.cos(rot) - dirY * Math.sin(rot);
dirY = oldDirX * Math.sin(rot) + dirY * Math.cos(rot);
var oldPlaneX:Float = planeX;
planeX = planeX * Math.cos(rot) - planeY * Math.sin(rot);
planeY = oldPlaneX * Math.sin(rot) + planeY * Math.cos(rot);
trace("a");
}
}
private function onKeyUp(evt:KeyboardEvent):Void {
keys[evt.keyCode] = false;
}
private function update(delta:Float){
speed = 5 * delta;
rot = 3 * delta;
}
public function render(delta:Float):Void{
data.unlock();
for(x in 0...w){
var cameraX = 2 * x / w - 1;
var rayPosX = posX;
var rayPosY = posY;
var rayDirX = dirX + planeX * cameraX;
var rayDirY = dirY + planeY * cameraX;
var mapX = Std.int(rayPosX);
var mapY = Std.int(rayPosY);
var sideDistX:Float;
var sideDistY:Float;
var deltaDistX = Math.sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX));
var deltaDistY = Math.sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));
var perpWallDist:Float;
var stepX:Int;
var stepY:Int;
var hit = 0;
var side = 0;
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;
}
while(hit == 0){
if(sideDistX < sideDistY){
sideDistX += deltaDistX;
mapX += stepX;
side = 0;
}else{
sideDistY += deltaDistY;
mapY += stepY;
side = 1;
}
if(map[mapX][mapY] > 0){
hit = 1;
}
}
if (side == 0)
perpWallDist = (mapX - rayPosX + (1 - stepX) / 2) / rayDirX;
else
perpWallDist = (mapY - rayPosY + (1 - stepY) / 2) / rayDirY;
var lineHeight = Std.int(h / perpWallDist);
var drawStart = -lineHeight / 2 + h / 2;
if(drawStart < 0) drawStart = 0;
var drawEnd = lineHeight / 2 + h / 2;
if(drawEnd >= h) drawEnd = h - 1;
var textureNum = map[mapX][mapY] - 1;
var wallX:Float;
if(side == 0) wallX = rayPosY + perpWallDist * rayDirY;
else wallX = rayPosX + perpWallDist * rayDirX;
wallX -= Math.floor(wallX);
var textureX = Std.int(wallX * textureWidth);
if(side == 0 && rayDirX > 0) textureX = Std.int(textureWidth - textureX - 1);
if(side == 1 && rayDirX < 0) textureX = Std.int(textureWidth - textureX - 1);
for(y in Std.int(drawStart)...Std.int(drawEnd)){
var d = y * 256 - h * 128 + lineHeight * 128;
var textureY = ((d * textureHeight) / lineHeight) / 256;
var texColor = textures[textureNum].getPixel(Std.int(textureX), Std.int(textureY));
if(side == 1) texColor = (texColor >> 1) & 8355711;
data.setPixel(x, y, texColor);
}
}
data.lock();
}
private function this_onEnterFrame (event:Event):Void {
var delta = Lib.getTimer() - lastTick;
trace(delta);
data.fillRect(new openfl.geom.Rectangle(0, 0, w, h), 0xFF000000);
update(delta);
render(delta);
lastTick = Lib.getTimer();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment