TehPencilmaster
  • Home
  • News
  • About me
  • Projects
    • Old Scratch Games >
      • SpaceFighter2000
      • 10 Secels
      • Decido
      • Your Artistic Level
    • LOVE2D >
      • Deep Down
      • Simulator Generator
      • TehMapEditor
    • Art
  • More Stuff
    • TehTextEditor
    • Team Lemon
function love.load()
require "tserial"
require "vars"

loadvars()
loadchunk()
generateterrain()

if not love.filesystem.exists(worldname) then
love.filesystem.createDirectory(worldname)
end
end


function love.update(dt)
local x = love.mouse.getX()
local y = love.mouse.getY()

pointer.x = math.floor(x/32)
pointer.y = math.floor(y/32)

player.x = math.floor(player.actx/32)
player.y = math.floor(player.acty/32)

pointer.dotx = math.floor(x)
pointer.doty = math.floor(y)

if player.mining == true then 
mineseconds = mineseconds+dt
else
mineseconds = 0
end

if mineseconds >= 1 then
if pointer.x >= 2 and pointer.x <= 13 and pointer.y >= 2 and pointer.y <=12 then
if map[pointer.y][pointer.x] == 2 then
map[pointer.y][pointer.x] = 1
elseif map[pointer.y][pointer.x] == 1 then
map[pointer.y][pointer.x] = 3
elseif map[pointer.y][pointer.x] == 3 then
map[pointer.y][pointer.x] = 4
elseif map[pointer.y][pointer.x] == 5 then
map[pointer.y][pointer.x] = 3
end
mineseconds = 0
minedblocks = minedblocks+1
savechunk()
end
end

loadchunk()

if love.mouse.isDown("l") then
if pointer.dotx >= player.actx-32 and pointer.dotx <= player.actx+64 and pointer.doty >= player.acty-32 and pointer.doty <= player.acty+64 then
player.mining = true
else
player.mining = false
end
else
player.mining = false
end

if love.mouse.isDown("r") then
if pointer.dotx >= player.actx-32 and pointer.dotx <= player.actx+64 and pointer.doty >= player.acty-32 and pointer.doty <= player.acty+64 then
player.building = true
else
player.building = false
end
else
player.building = false
end

if player.building == true then
if pointer.x >= 2 and pointer.x <= 13 and pointer.y >= 2 and pointer.y <=12 then
map[pointer.y][pointer.x] = 5
savechunk()
end
end
end

function love.draw()
for y=1, #map do
        for x=1, #map[y] do
            if map[y][x] == 1 then
                love.graphics.draw(IMGGRASS, x * 32, y * 32, 0, 2)
end
if map[y][x] == 2 then
love.graphics.draw(IMGGRASS, x * 32, y * 32, 0, 2)
end
if map[y][x] == 3 then
love.graphics.draw(IMGDIRT, x * 32, y * 32, 0, 2)
end
if map[y][x] == 4 then
love.graphics.draw(IMGSTONE, x * 32, y * 32, 0, 2)
end
if map[y][x] == 5 then
love.graphics.draw(IMGPLANKS, x * 32, y * 32, 0, 2)
end
end
end

love.graphics.setColor(255, 255, 255)
    love.graphics.rectangle("fill", player.actx+2, player.acty+2, 28, 28)

love.graphics.setColor(0, 0, 0)
    love.graphics.rectangle("line", pointer.x*32, pointer.y*32, 32, 32)

--Borders
love.graphics.setColor(0, 0, 0)
love.graphics.rectangle("fill", 32, 32, 500, 32)
love.graphics.rectangle("fill", 32, 416, 500, 32)
love.graphics.rectangle("fill", 32, 32, 32, 500)
love.graphics.rectangle("fill", 448, 32, 32, 500)

for y=1, #map do
for x=1, #map[y] do
if map[y][x] == 2 then
love.graphics.setColor(255, 255, 255)
love.graphics.draw(IMGTREE1, x * 32 - 16, y * 32 - 96, 0, 2)
end
end
end

love.graphics.setColor(255, 255, 255)
love.graphics.print(player.actx .. " " .. player.acty)

if minedblocks == 46 then
love.graphics.print("Do you feel it now, Mr. Krabs?", 0, 16)
else
--love.graphics.print(chunk.x .. " " .. chunk.y, 0, 16)
end
end

function love.keypressed(key)
    if key == "w" then
        if testMap(0, -1) then
            player.acty = player.acty - 32
        end
    elseif key == "s" then
        if testMap(0, 1) then
            player.acty = player.acty + 32
        end
    elseif key == "a" then
        if testMap(-1, 0) then
            player.actx = player.actx - 32
        end
    elseif key == "d" then
        if testMap(1, 0) then
            player.actx = player.actx + 32
        end
    end

if key == "f5" then
savechunk()
end
end

function testMap(x, y)
    if map[(player.acty / 32) + y][(player.actx / 32) + x] == 1 then
        return true
    end
if map[(player.acty / 32) + y][(player.actx / 32) + x] == 3 then
        return true
    end
if map[(player.acty / 32) + y][(player.actx / 32) + x] == 4 then
        return true
    end
    return false
end

function generateterrain()
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, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, math.random(1, 2), 1, 1, 1, 1, 1, 1, 1, math.random(1, 2), 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, math.random(1, 2), 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, 1, 1, 1, 1, 1, math.random(1, 2), 1, 1, 1 },
{ 1, 1, 1, 1, math.random(1, 2), 1, 1, 1, 1, math.random(1, 2), 1, 1, 1, 1 },
{ 1, math.random(1, 2), 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, math.random(1, 2), 1, 1, 1, math.random(1, 2), 1, 1, 1 },
{ 1, math.random(1, 2), 1, 1, 1, 1, 1, math.random(1, 2), 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
}
end

function loadchunk()
if love.filesystem.exists(worldname .. "/chunkx" .. chunk.x .. "y" .. chunk.y .. ".map") then
lchunk = love.filesystem.newFile(worldname .. "/chunkx" .. chunk.x .. "y" .. chunk.y .. ".map" )
lchunk:open("r")
map = TSerial.unpack( lchunk:read() )
lchunk:close()
end

if player.acty == 32 then
player.acty = 384
chunk.y = chunk.y+1

if love.filesystem.exists(worldname .. "/chunkx" .. chunk.x .. "y" .. chunk.y .. ".map") then
lchunk = love.filesystem.newFile(worldname .. "/chunkx" .. chunk.x .. "y" .. chunk.y .. ".map" )
lchunk:open("r")
map = TSerial.unpack( lchunk:read() )
lchunk:close()
else
generateterrain()
savechunk()
end
elseif player.acty == 416 then
player.acty = 64
chunk.y = chunk.y-1

if love.filesystem.exists(worldname .. "/chunkx" .. chunk.x .. "y" .. chunk.y .. ".map") then
lchunk = love.filesystem.newFile(worldname .. "/chunkx" .. chunk.x .. "y" .. chunk.y .. ".map" )
lchunk:open("r")
map = TSerial.unpack( lchunk:read() )
lchunk:close()
else
generateterrain()
savechunk()
end
end

if player.actx == 32 then
player.actx = 416
chunk.x = chunk.x-1

if love.filesystem.exists(worldname .. "/chunkx" .. chunk.x .. "y" .. chunk.y .. ".map") then
lchunk = love.filesystem.newFile(worldname .. "/chunkx" .. chunk.x .. "y" .. chunk.y .. ".map" )
lchunk:open("r")
map = TSerial.unpack( lchunk:read() )
lchunk:close()
else
generateterrain()
savechunk()
end
elseif player.actx == 448 then
player.actx = 64
chunk.x = chunk.x+1

if love.filesystem.exists(worldname .. "/chunkx" .. chunk.x .. "y" .. chunk.y .. ".map") then
lchunk = love.filesystem.newFile(worldname .. "/chunkx" .. chunk.x .. "y" .. chunk.y .. ".map" )
lchunk:open("r")
map = TSerial.unpack( lchunk:read() )
lchunk:close()
else
generateterrain()
savechunk()
end
end
end

function savechunk()
schunk = love.filesystem.newFile(worldname .. "/chunkx" .. chunk.x .. "y" .. chunk.y .. ".map" )
schunk:open("w")
schunk:write( TSerial.pack( map ) )
schunk:close()
end

function love.mousepressed( x, y, button )
if button == "l" then
if pointer.dotx >= player.actx-32 and pointer.dotx <= player.actx+64 and pointer.doty >= player.acty-32 and pointer.doty <= player.acty+64 then
mining = true
else
mining = false
end
elseif button == "r" then
map[pointer.y][pointer.x] = player.selected
end
end
Powered by Create your own unique website with customizable templates.