Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function generateGround()
- -- some constants here
- local minX = 0
- local maxX = 100
- local minY = 0
- local maxY = 100
- local defaultGroundTexture = "textures/tiles/ground"
- local defaultAirTexture = "textures/tiles/stone"
- -- function start
- local defaultElevationValue = (maxY + minY) / 2
- local getNearestHigherPo2 = function(n) local val = 2; while val < n do val = val * 2; end; return val; end
- local adaptedGroundWidth = getNearestHigherPo2(1 + maxX - minX)
- -- this table will contain, for each integral x coord, the height of the floor
- local requestedFloorLevels = {}
- for x = 0, adaptedGroundWidth, 1 do requestedFloorLevels[x] = defaultElevationValue; end -- filling the table with default elevation
- -- loop that fills requestedFloorLevels
- local step = adaptedGroundWidth / 2
- while step >= 1 do
- local stepLog = math.log(step)
- for x = step, adaptedGroundWidth - 1, step * 2 do
- local averageValue = (requestedFloorLevels[x - step] + requestedFloorLevels[x + step]) / 2
- local elevationDelta = (math.random() - 0.5) * stepLog * 2
- requestedFloorLevels[x] = averageValue + elevationDelta
- end
- step = step / 2
- end
- -- loop that will create all entities
- for x = 0, maxX - minX, 1 do
- local realX = x + minX
- local groundElevation = math.floor(requestedFloorLevels[realX] + 0.5)
- local leftGroundElevation = defaultElevationValue
- if x ~= 0 then leftGroundElevation = math.floor(requestedFloorLevels[realX - 1] + 0.5); end
- local rightGroundElevation = defaultElevationValue
- if realX ~= maxX then rightGroundElevation = math.floor(requestedFloorLevels[realX + 1] + 0.5); end
- for y = minY, maxY, 1 do
- local entity = Entities:new()
- entity:add("position")
- entity.position.x, entity.position.y = realX, y
- entity:add("tileDisplay")
- if y < groundElevation then
- entity.tileDisplay.textureBottom = defaultGroundTexture
- entity.tileDisplay.textureUp = entity.tileDisplay.textureBottom
- elseif y == groundElevation then
- entity.tileDisplay.textureBottom = defaultGroundTexture
- if leftGroundElevation > groundElevation then
- entity.tileDisplay.textureUp = defaultAirTexture
- entity.tileDisplay.leftGoesUp = true
- elseif rightGroundElevation > groundElevation then
- entity.tileDisplay.textureUp = defaultAirTexture
- entity.tileDisplay.leftGoesUp = false
- else
- entity.tileDisplay.textureUp = entity.tileDisplay.textureBottom
- end
- else
- entity.tileDisplay.textureBottom = defaultAirTexture
- entity.tileDisplay.textureUp = defaultAirTexture
- end
- end
- end
- end
- generateGround()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement