Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Some Craft quality of life improvements

JohnJohn Admin Mod
edited January 8 in Codea Craft Posts: 708

Hey guys, based on feedback from @UberGoober I've experimented with a few additions to Craft to make things a little easier to work with. These improvements are mostly meant for Codea 4 but I've back ported some of them where it made sense.

The Main Scene

Use craft.scene.main to update and draw a scene automatically

-- This is all you need to do to draw and update your scene
function setup()
    scene = craft.scene()
    craft.scene.main = scene
end

Entity Callbacks

A set of new callback events in entities let you attach functions that will be called automatically:

  • entity.update
  • entity.fixedUpdate
  • entity.destroyed
function setup()
    -- Create a new craft scene
    scene = craft.scene()
    scene.camera.z = 10
    scene.camera.eulerAngles = vec3(0, 180, 0)
    scene.sun.eulerAngles = vec3(45, 45, 0)

    -- Create a new entity
    e = scene:entity()
    e.model = craft.model(asset.builtin.Primitives.Monkey)

    e.update = function(entity, dt)
        entity.y = math.sin(ElapsedTime)
    end

    e.destroyed = function(entity)
        sound(SOUND_POWERUP)
    end

    craft.scene.main = scene
end

function touched(touch)
    if touch.state == BEGAN then 
        e:destroy()
    end
end

The plan was is to forward touch events automatically, but this is a little bit complicated and may come later down the line

Comments

  • dave1707dave1707 Mod
    Posts: 9,808

    Remove the “local” in the statement local e = scene:entity() or you’ll get an error when you touch the screen to destroy e.

  • Posts: 35
    The calling of the functions automatically is going to be huge!
  • Posts: 1,303

    Very nice!

  • Posts: 1,629
    Oh wow that’s great. Thanks so much.
  • JohnJohn Admin Mod
    Posts: 708

    @dave1707 Removed the extraneous "local"

  • Posts: 190

    I gave it a try, it’s a nice extension, and it looks like you’ll be able to use multiple scenarios with ease

  • Posts: 190

    I’m having a problem with how to apply the craft.scene.main to the block ?

    Here is the test code:

    viewer.mode = FULLSCREEN
    
    function setup()
        scene = craft.scene()
    
        player = scene.camera:add(OrbitViewer, vec3( 40, 20, -10), 80, 1, 900)
    
        -- 设置 voxel 地形参数:大小规模和坐标
        scene.voxels:resize(vec3(5,1,5))      
        scene.voxels.coordinates = vec3(0,0,0)    
    
        -- 创建自己的 block type
        scene.voxels.blocks:addAssetPack("Blocks")  
        grass = scene.voxels.blocks:new("myGrass")
        grass.setTexture(ALL, "Blocks:Dirt")
        grass.setColor(ALL, color(239, 222, 5))
    
        -- 使用 craft.voxels 默认提供的基础 block type:SOLID
        scene.voxels:fill("myGrass")
        -- 用默认的方块 SOLID 创建地面,球体,线条,单独的方块
        scene.voxels:box(0,0,0, 16*8,1,16*8)
    
        blinky()
        scene.voxels:fill("Blinky")
        scene.voxels:sphere(40,50,90,16)
    
        scene.voxels:fill("Blinky")
        scene.voxels:line(10,20,0,10,50,30)
        scene.voxels:block(vec3(30,20,35))
    
        -- 用新建的 block type 绘制
        scene.voxels:fill("myGrass")
        scene.voxels:block(vec3(40,20,35))
    
        -- How to write the block update here???
    
        craft.scene.main = scene
    end
    
    
    -- Create a blinky block type
    function blinky()
        local Blinky = scene.voxels.blocks:new("Blinky")
        Blinky.setTexture(ALL, "Blocks:Stone")
        Blinky.geometry = TRANSLUCENT
        Blinky.renderPass = TRANSLUCENT
        Blinky.tinted = false
        Blinky.dynamic = true
        Blinky.scriped = true
    
        function Blinky:created()
            -- Schedule an update in one second
            self:schedule(60)
        end
    
        function Blinky:blockUpdate(ticks)
            local randomColor = color(math.random(128,255), math.random(128,255), math.random(128,255))
            self:set(COLOR, randomColor)
            self:schedule(10)
        end  
    end
    
  • JohnJohn Admin Mod
    Posts: 708

    @binaryblues It looks like DeltaTime was not being passed properly to scene:update() causing it to stall voxel updates. I've pushed a fix which will be in the next beta and app store versions

  • Posts: 190

    @John Thanks so much! A very good news! I’ll be looking forward to the new version.

Sign In or Register to comment.