Blocks Generator 1

--[[ 

* Blocks generator *

* Spread max blocks throughout
* Make player movement "attach" to block/trigger (each trigger having it's own coordinate data)
* Grid block map will have single digit for each block "type" (example: 12345678844050609943343, read from each Char)
* Get block it's own reference (array) number
* 128 m x 128 m (blocks) == 16,384 to equal roughly the size of a Super WalMart
* Run in any direction and a floor block stays underneath you.. (Point that stays on character to tell where floor block to be)
* Virtualized block area. Load in all possible blocks from start, then assign their positions via array (map)



* Activate "fly" mode with keypress

--]]


-- SPREAD MAX BLOCKS (to start)
local _blockTypes = require( script:GetCustomProperty("ListBlocks") )
local _blocksMap = require( script:GetCustomProperty("BlocksMap") )
local defAnimStance = nil
local _blocksFolder = World.FindObjectByName("BlocksFolder")
--print(#_blocksMap)
local _footer = World.FindObjectByName("footer")

_G["blocks"] = {} -- the actual world blocks (global array)

local _p2f = nil -- player to follow

local function hex (hex, alpha)-- convert hex to rgba (for Core)
    local redColor,greenColor,blueColor=hex:match('(..)(..)(..)')
    redColor, greenColor, blueColor = tonumber(redColor, 16)/255, tonumber(greenColor, 16)/255, tonumber(blueColor, 16)/255
    redColor, greenColor, blueColor = math.floor(redColor*100)/255, math.floor(greenColor*100)/255, math.floor(blueColor*100)/255
    if alpha == nil then
        return redColor, greenColor, blueColor
    elseif alpha > 1 then
        alpha = alpha / 100
    end
    return redColor, greenColor, blueColor, alpha
end

function ToggleFlyMode(player) -- Player presses "F1" to toggle fly mode, called from Client to Server
    if (player.isFlying) then
        player:ActivateWalking()
    elseif (player.isWalking or player.isJumping) then
        if (player.isWalking) then
            player.AddImpulse(player, Vector3.New(0,0,100000)) -- Launches player off ground to fly
        end
        Task.Wait(0.1)
        player:ActivateFlying()
    end    
end
ReturnToLobby = function(player) -- PLAYER PRESSES "P" TO GO BACK TO LOBBY
    Task.Wait(1)
    player:SetWorldPosition(Vector3.New(0, 0, 450))
    player.animationStance = defAnimStance
end

Events.ConnectForPlayer("ReturnToLobby", ReturnToLobby)
Events.ConnectForPlayer("ToggleFlyMode", ToggleFlyMode)


Game.playerJoinedEvent:Connect(function(player)
    player.stepHeight = 200
    --player.diedEvent:Connect(OnPlayerDied)
    --player.spawnedEvent:Connect(OnPlayerRespawn)
    defAnimStance = player.animationStance
end)

local _blockList = _blocksFolder:GetChildren()
-- GLOBAL function to grant each block it's id for _blocks array call

local _cid = #_blocksFolder:GetChildren()+1 -- folder read is reversed when calling upon init..
print("Total blocks: "..tostring(_cid-1))
_G["assignID"] = function()
    _cid = _cid - 1
    return _cid
end

local _step = 200 -- how much to "step" (block size)

local _stretchXY = 20 -- how many blocks to extend (x,y only), limit z to maybe a 3rd of that (40)
local _stretchZ = 5 -- extend height (z)

local _curX = 1
local _curY = 1 -- how many blocks are in the stretch, currently?
local _curZ = 1

local _stepX = 0 -- origins + next step
local _stepY = 0
local _stepZ = 0

function _init()
    --METHOD FROM FOLDER
    local a = 1 -- number of blocks..
    local _r = 1
    local MeshSlot = nil    
    local _line = nil
    local _nb = 1 -- each block to use    
    for i=1,_stretchZ do -- read each map line: (20x by 20y = 400) x 5z chunk
        _line = _blocksMap[i][1] -- get line (x of 10)        
        for b=1, #_line do -- read 100 chars: "block type"
            --print( _line:sub(b,b) )
            _blockList[_nb]:SetWorldPosition( Vector3.New(_stepX,_stepY,_stepZ) )
            
            -- apply material and color
            --_r = math.random(1,#_blockTypes)
            _r = tonumber(_line:sub(b,b))
            MeshSlot = _blockList[_nb]:GetMaterialSlots()[1]
            _blockList[_nb]:SetMaterialForSlot(_blockTypes[_r][2], MeshSlot.slotName)
            if(_r ~= 1)then
                _blockList[_nb]:SetColor( Color.New(hex(_blockTypes[_r][3])) )
            end
            
            _nb = _nb + 1 -- reference a new block
            _stepX = _stepX + _step
            _curX = _curX + 1
            if(_curX > _stretchXY)then -- step over 1 Y and reset X
                _stepY = _stepY + _step
                _stepX = 0
                _curX = 1                
            end
        end        
        _stepZ = _stepZ + _step -- move to next z layer
        _curZ = _curZ + 1
        _stepY = 0 -- reset Y
        _curY = 1
    end
    
    -- finished    
    _startFollowTask()
end


Game.playerJoinedEvent:Connect(function(player)
    _p2f = player
    _p2f.gravityScale = .25
    --print(player:GetWorldPosition())
    _p2f.isVisible = false
end)


local FollowTask = nil
local _fLoc = nil -- update loc of footer
local _fpX = nil
local _fpY = nil
local _fpZ = nil
function _followPlayer()
    _p2f:AddImpulse(Vector3.UP *1.1)
    _fpX = _p2f:GetWorldPosition().x
    _fpY = _p2f:GetWorldPosition().y
    _fpZ = -105 --_p2f:GetWorldPosition().z
    _fLoc = Vector3.New(_fpX,_fpY,_fpZ)
    _footer:SetWorldPosition(_fLoc)
end

function _startFollowTask()
    --_person = Game.GetPlayers()[1]
    FollowTask = Task.Spawn(function() _followPlayer() end)
    FollowTask.repeatCount = -1 -- forever
    FollowTask.repeatInterval = 2.5 -- every x seconds
end


--FollowTask:Cancel()




Task.Wait(1) -- wait for server start

_init() -- start the show

Latest Posts

Emulation ProcessEmulation Process

  Record Locs for Emulation in Core Grab data. Here, I am recording x,yz, and name data to emulate. Next Convert to usable array for Core template data. Snippet>> Pattern  Core template replacement pattern: Path:[C:\Users\soandso\Documents\My Games\CORE\Saved\Maps\mycoregame\Data\Tree\Emulator_