diff --git a/Applications/RayWalk.app/Main.lua b/Applications/RayWalk.app/Main.lua index 419f64f3..26317c95 100755 --- a/Applications/RayWalk.app/Main.lua +++ b/Applications/RayWalk.app/Main.lua @@ -174,6 +174,7 @@ local controls = { rayEngine.loadEngineProperties(applicationResourcesDirectory .. "RayEngine.cfg") rayEngine.loadWeapons(applicationResourcesDirectory .. "Weapons/") +rayEngine.loadSprites(applicationResourcesDirectory .. "Sprites/") rayEngine.loadWorld(worldsPath .. "ExampleWorld") rayEngine.changeResolution(rayEngine.properties.screenResolution.width, rayEngine.properties.screenResolution.height) -- rayEngine.intro() diff --git a/Applications/RayWalk.app/RayEngine.cfg b/Applications/RayWalk.app/RayEngine.cfg index 0d11d9c0..72356dd6 100755 --- a/Applications/RayWalk.app/RayEngine.cfg +++ b/Applications/RayWalk.app/RayEngine.cfg @@ -1,28 +1,28 @@ { + tileWidth = 32, screenResolution = { - height = 50, - width = 160 + width = 110, + height = 45 }, shadingTransparencyMap = { - [12] = 0.50196078431373, - [6] = 1, - [13] = 0.4, [1] = 0.2, + [2] = 0.4, [3] = 0.6, + [4] = 0.8, + [5] = 1, + [6] = 1, [7] = 1, [8] = 0.8, - [4] = 0.8, [9] = 0.8, - [14] = 0.30196078431373, - [2] = 0.4, - [5] = 1, + [10] = 0.70196078431373, [11] = 0.6, - [10] = 0.70196078431373 + [12] = 0.50196078431373, + [13] = 0.4, + [14] = 0.30196078431373 }, - shadingDistance = 500, - shadingCascades = 8, + raycastQuality = 4.8953488372093, useSimpleRenderer = false, - tileWidth = 32, - raycastQuality = 6.4, - drawDistance = 1000 -} \ No newline at end of file + shadingCascades = 8, + drawDistance = 1000, + shadingDistance = 500 +} diff --git a/Applications/RayWalk.app/RayEngine.lua b/Applications/RayWalk.app/RayEngine.lua index 345f6f1d..faa52f2c 100755 --- a/Applications/RayWalk.app/RayEngine.lua +++ b/Applications/RayWalk.app/RayEngine.lua @@ -20,7 +20,6 @@ local endRenderClock = os.clock() local frameCount = 0 local fps = 0 local lastFPSCheck = os.clock() - ---------------------------------------------------- Константы ------------------------------------------------------------------ local rayEngine = {} @@ -32,7 +31,7 @@ rayEngine.watchEnabled = false rayEngine.drawFieldOfViewOnMinimap = false rayEngine.chatShowTime = 4 rayEngine.chatHistory = {} - +rayEngine.sprites = {} ---------------------------------------------- Расчетные функции ------------------------------------------------------------------ -- Позиция горизонта, относительно которой рисуется мир @@ -78,11 +77,13 @@ end ---------------------------------------------- Вспомогательные функции ------------------------------------------------------------------ local function constrainAngle(value) - if ( value < 0 ) then - value = value + 360 - elseif ( value > 360 ) then - value = value - 360 - end + value = value % 360 + return value +end + + +local function constrain180(value) + value = (value + 180) % 360 - 180 return value end @@ -149,9 +150,34 @@ function rayEngine.loadWorld(pathToWorldFolder) rayEngine.map = filesystem.readTable(pathToWorldFolder .. "/Map.cfg") rayEngine.player = filesystem.readTable(pathToWorldFolder .. "/Player.cfg") rayEngine.blocks = filesystem.readTable(pathToWorldFolder .. "/Blocks.cfg") + rayEngine.entities = filesystem.readTable(pathToWorldFolder .. "/Entities.cfg") -- Дополняем карту ее размерами rayEngine.map.width = #rayEngine.map[1] rayEngine.map.height = #rayEngine.map + -- Создаём карту блоков для коллизий + rayEngine.hitBlocks = {} + for i = 1,rayEngine.map.width do + line={} + rayEngine.hitBlocks[i] = {} + for j =1,rayEngine.map.height do + rayEngine.hitBlocks[i][j] = {} + end + end + + for i =1,#rayEngine.entities do + for dx = -1,1 do + for dy = -1,1 do + mapX = math.floor((rayEngine.entities[i].x + rayEngine.entities[i].w * dx) / rayEngine.properties.tileWidth) + mapY = math.floor((rayEngine.entities[i].y+rayEngine.entities[i].w * dy) / rayEngine.properties.tileWidth) + + if rayEngine.hitBlocks[mapX] and rayEngine.hitBlocks[mapX][mapY] then + cellCount = #rayEngine.hitBlocks[mapX][mapY] + + rayEngine.hitBlocks[mapX][mapY][cellCount + 1] = i + end + end + end + end -- Ебашим правильную позицию игрока, основанную на этой ХУЙНЕ, которую ГЛЕБ так ЛЮБИТ rayEngine.player.position.x = rayEngine.properties.tileWidth * rayEngine.player.position.x - rayEngine.properties.tileWidth / 2 rayEngine.player.position.y = rayEngine.properties.tileWidth * rayEngine.player.position.y - rayEngine.properties.tileWidth / 2 @@ -188,6 +214,14 @@ function rayEngine.changeWeapon(weaponID) end end +function rayEngine.loadSprites(pathToSprites) + fileList = filesystem.readTable(pathToSprites .. "Sprites.cfg") + for i = 1,#fileList do + rayEngine.sprites[i] = image.load(pathToSprites .. fileList[i]) + + end +end + function rayEngine.move(distanceForward, distanceRight) local forwardRotation = math.rad(rayEngine.player.rotation) local rightRotation = math.rad(rayEngine.player.rotation + 90) @@ -311,6 +345,15 @@ function rayEngine.drawMap(x, y, width, height, transparency) if rayEngine.map[i] and rayEngine.map[i][j] then screen.semiPixelSet(xPos, yPos, rayEngine.blocks[rayEngine.map[i][j]].color) end + + if rayEngine.hitBlocks[i + 1] and rayEngine.hitBlocks[i+1][j+1] then + entities = rayEngine.hitBlocks[i + 1][j + 1] + for entityID= 1,#entities do + entity = rayEngine.entities[entityID] + scale = rayEngine.properties.tileWidth + screen.semiPixelSet(entity.x//scale - xMap + x, entity.y*2//scale - yMap + y, 0x00FFFF) + end + end xPos = xPos + 1 end xPos = x; yPos = yPos + 1 @@ -510,12 +553,38 @@ end ---------------------------------------------------- Функции отрисовки мира ------------------------------------------------------------------ -local function raycast(angle) +rayEngine.caughtEntities = {} + +--Свистнул функцию с StackOverflow. Заменяет оператор in из питона +local function has_value (tab, val) + for index, value in ipairs(tab) do + if value == val then + return true + end + end + + return false +end + + +function rayEngine.getEntitiesAt(x,y) + mapX = math.floor(x / rayEngine.properties.tileWidth) + mapY = math.floor(y / rayEngine.properties.tileWidth) + if rayEngine.hitBlocks[mapX] and rayEngine.hitBlocks[mapX][mapY] then + return rayEngine.hitBlocks[mapX][mapY] + else + return {} + end +end + +local function raycast(angle,maxRange) + rayEngine.caughtEntities = {} angle = math.rad(angle) + local raySteps = 0 local angleSinDistance, angleCosDistance, currentDistance, xWorld, yWorld, xMap, yMap, tile = math.sin(angle) * rayEngine.properties.raycastQuality, math.cos(angle) * rayEngine.properties.raycastQuality, 0, rayEngine.player.position.x, rayEngine.player.position.y while true do - if currentDistance <= rayEngine.properties.drawDistance then + if currentDistance <= maxRange then xMap, yMap = math.floor(xWorld / rayEngine.properties.tileWidth), math.floor(yWorld / rayEngine.properties.tileWidth) if rayEngine.map[yMap] and rayEngine.map[yMap][xMap] then return currentDistance, rayEngine.map[yMap][xMap] @@ -523,12 +592,71 @@ local function raycast(angle) xWorld, yWorld = xWorld + angleSinDistance, yWorld + angleCosDistance currentDistance = currentDistance + rayEngine.properties.raycastQuality + raySteps = raySteps + 1 + if raySteps % 1 == 0 then + local entityList = rayEngine.getEntitiesAt(xWorld,yWorld) + + if entityList then + for i = 1,#entityList do + if not has_value(rayEngine.caughtEntities,entityList[i]) then + table.insert(rayEngine.caughtEntities, entityList[i]) + end + end + end + end else return nil end end end +function calcDistance(x1,y1,x2,y2) + relativeX = (x2-x1) + relativeY = (y2-y1) + distance = math.sqrt(relativeX*relativeX + relativeY*relativeY) + return distance +end + +function rayEngine.drawSpriteColumn(x,y,rayAngle,scrX,sprName) + + sprImage = rayEngine.sprites[sprName] + relativeX = (rayEngine.player.position.x - x) + relativeY = (rayEngine.player.position.y - y) + distance = math.sqrt(relativeX*relativeX + relativeY*relativeY) + + + iwidth,iheight = image.getSize(sprImage) + + angleTheta = math.deg(math.atan(-relativeX, -relativeY)) + angleDelta = angleTheta - rayEngine.player.rotation + + angleDelta = constrain180(angleDelta) + + centerX = angleDelta / rayEngine.raycastStep + screen.getWidth() / 2 + wingShift = iwidth / distance * rayEngine.distanceToProjectionPlane + startX = centerX - wingShift / 2 + endX = centerX + wingShift / 2 + + + height = iheight / distance * rayEngine.distanceToProjectionPlane + startY = rayEngine.horizonPosition - height / 2 + 1 + + picX = math.floor((scrX - endX) / wingShift * iwidth) + if scrX >= startX and scrX <= endX then + for iterY = 0, height do + coordY = math.floor(startY + iterY) + picY = math.floor(iterY/height * iheight)+1 + b,f,a,s = image.get(sprImage,picX,picY) + if b and f and (a == 0) then + --print(centerX,wingShift,startX,endX,picX,endX,f, b) --раскомментируй на случай дебага + --print(rayEngine.player.rotation,angleTheta,angleDelta," ", startX) + screen.drawRectangle(scrX,coordY,1,1,b,f,s) + end + end + end + +end + function rayEngine.drawWorld() --Земля screen.clear(rayEngine.world.colors.groundByTime) @@ -537,7 +665,10 @@ function rayEngine.drawWorld() --Сцена local startAngle, endAngle, startX, distanceToTile, tileID, height, startY, tileColor = rayEngine.player.rotation - rayEngine.player.fieldOfView / 2, rayEngine.player.rotation + rayEngine.player.fieldOfView / 2, 1 for angle = startAngle, endAngle, rayEngine.raycastStep do - distanceToTile, tileID = raycast(angle) + distanceToTile, tileID = raycast(angle,rayEngine.properties.drawDistance) + + + if distanceToTile then -- Получаем цвет стенки tileColor = getTileColor(rayEngine.blocks[tileID].color, distanceToTile) @@ -560,6 +691,17 @@ function rayEngine.drawWorld() -- column = image.transform(column, 1, height) -- screen.drawImage(math.floor(startX), math.floor(startY), column) -- end + + --Поверх рисуем спрайты + if rayEngine.caughtEntities then + for i = #rayEngine.caughtEntities,1,-1 do + local entityID = rayEngine.caughtEntities[i] + local entity = rayEngine.entities[entityID] + rayEngine.drawSpriteColumn(entity.x,entity.y,angle,startX,entity.sprite.image) + end + end + + end startX = startX + 1 end @@ -567,6 +709,8 @@ end function rayEngine.update() + + local frameRenderClock = os.clock() rayEngine.rotate(rayEngine.player.rotationSpeed * inputYaw) rayEngine.move(rayEngine.player.moveSpeed * inputY, rayEngine.player.moveSpeed * inputX) @@ -583,12 +727,18 @@ function rayEngine.update() if rayEngine.chatEnabled then rayEngine.chat() end doDayNightCycle() + raycast(rayEngine.player.rotation,rayEngine.properties.drawDistance) + if rayEngine.debugInformationEnabled then rayEngine.drawDebugInformation(3, 2 + (rayEngine.minimapEnabled and 12 or 0), 24, 0.6, "renderTime: " .. string.format("%.2f", ((os.clock() - frameRenderClock) + (endRenderClock-startRenderClock)) * 1000) .. " ms", "FPS: " .. string.format("%.2f",fps), "freeRAM: " .. string.format("%.2f", computer.freeMemory() / 1024) .. " KB", - "pos: " .. string.format("%.2f", rayEngine.player.position.x) .. " x " .. string.format("%.2f", rayEngine.player.position.y) + "pos: " .. string.format("%.2f", rayEngine.player.position.x) .. " x " .. string.format("%.2f", rayEngine.player.position.y), + "collision check blocks: " .. string.format("%i", #rayEngine.hitBlocks) .. "x" .. string.format("%i", #rayEngine.hitBlocks[1]), + "entities: " .. string.format("%i", #rayEngine.entities), + "entities at: " .. string.format("%i", #rayEngine.caughtEntities), + "sprites: " .. string.format("%i", #rayEngine.sprites) ) end startRenderClock = os.clock() @@ -639,3 +789,4 @@ end ---------------------------------------------------------------------------------------------------------------------------------- return rayEngine + diff --git a/Applications/RayWalk.app/Sprites/Banana.pic b/Applications/RayWalk.app/Sprites/Banana.pic new file mode 100644 index 00000000..bed15360 Binary files /dev/null and b/Applications/RayWalk.app/Sprites/Banana.pic differ diff --git a/Applications/RayWalk.app/Sprites/Plasma.pic b/Applications/RayWalk.app/Sprites/Plasma.pic new file mode 100644 index 00000000..ad855c72 Binary files /dev/null and b/Applications/RayWalk.app/Sprites/Plasma.pic differ diff --git a/Applications/RayWalk.app/Sprites/Sprites.cfg b/Applications/RayWalk.app/Sprites/Sprites.cfg new file mode 100644 index 00000000..209767af --- /dev/null +++ b/Applications/RayWalk.app/Sprites/Sprites.cfg @@ -0,0 +1,4 @@ +{ + "Banana.pic", + "Plasma.pic", +} diff --git a/Applications/RayWalk.app/Worlds/ExampleWorld/Entities.cfg b/Applications/RayWalk.app/Worlds/ExampleWorld/Entities.cfg new file mode 100644 index 00000000..1ee4ded1 --- /dev/null +++ b/Applications/RayWalk.app/Worlds/ExampleWorld/Entities.cfg @@ -0,0 +1,27 @@ +{ + { + type="sprite", + x = 180, + y = 500, + w = 60, + sprite = {image = 1}, + params = {image = "Banana.pic"}, + }, + { + type="sprite", + x = 180, + y = 700, + w = 60, + sprite = {image = 2}, + params = {image = "Plasma.pic"}, + }, + + { + type="sprite", + x = 385, + y = 758, + w = 60, + sprite = {image = 1}, + params = {image = "Banana.pic"}, + }, +} diff --git a/Applications/RayWalk.app/Worlds/SundownBeams/Entities.cfg b/Applications/RayWalk.app/Worlds/SundownBeams/Entities.cfg new file mode 100644 index 00000000..1ee4ded1 --- /dev/null +++ b/Applications/RayWalk.app/Worlds/SundownBeams/Entities.cfg @@ -0,0 +1,27 @@ +{ + { + type="sprite", + x = 180, + y = 500, + w = 60, + sprite = {image = 1}, + params = {image = "Banana.pic"}, + }, + { + type="sprite", + x = 180, + y = 700, + w = 60, + sprite = {image = 2}, + params = {image = "Plasma.pic"}, + }, + + { + type="sprite", + x = 385, + y = 758, + w = 60, + sprite = {image = 1}, + params = {image = "Banana.pic"}, + }, +}