Log In  

Hello again!

I had a peek into Celeste's code and i believe it is made using ECS or Entity Component systems. It gripped me immediately as i can sort-of understand the way it works, i think. I believe that each entity in a game contains its own update draw and init functions, and in the main update draw and init functions you just have a foreach loop that cycles through them and updates/draws them individually. My first question would be is this right? My second and main question is can anyone expand and put it into (preferably extremely simple) code. I would love to use this new architecture.

Thanks!

P#77255 2020-05-27 07:22

Something like that?

Cart #jerafunubu-0 | 2020-05-27 | Code ▽ | Embed ▽ | No License

That's a (very) simple way to do some sort of ECS, I think^^. It works like this: There's a global Variable called "actors", and a Function called "make_actors" creates an List with predefined Variables (X- and Y-Pos, Default Draw- and Update-Function, Sprite). Now you can create other Functions that'll call "make_actors" and modify these Values (for example, "make_player" and overwrite their Update-Function with "update_player"). On this Example, it just "beeps" after pressing X or Z, but you could do all other stuff you like - for example, check Inputs and move the Player...

This Method overall is still somewhat limited (like, Interactions between Objects are a bit difficult), but I think it's a good point to start. It's my preferably Method^^.

P#77257 2020-05-27 08:00
2

The previous example misses the Systems part of ECS.

For something closer, see this post and mini-framework by @selfsame.

Simply put, your entities are kv tables of components. A component is a kv table of properties. A systems targets specific components and acts upon them.

Sample usage:

A factory for cannon entities, each containing components type, status, pos, size, offset, input, move, and more:

function mk_cannon(mx,my)
    return {
        type="cannon",
        status={missiles=6,hits=0,missile_spr=2},
        pos=mk_pair(mx*8,my*8),
        size=mk_pair(6,5),
        offset=mk_pair(1,3),
        input_2=true,
        move=mk_movement(0,0, 2,0, 0,0, 1,0, 1),
        mcoll={flags={1}},
        shoot={order=false,cooldown=30,t=0},
        spr={frame=1,fx=false,fy=false},
    }
end

Yet another:

function mk_station(mx,my)
    return {
        type="station",
        pos=mk_pair(mx,my),
        size=mk_pair(8,10),
        offset=mk_pair(0,-2),
        repair_rate=30,
        spr={n=11,fx=false,fy=false},
    }
end

A system targeting entities with a lifespan component:

aging = system({"lifespan"},
    function(e)
        e.lifespan-=1
        if e.lifespan<=0 then
            tag(e,"die",true)
        end
    end)

A system targeting entities that have just died:

dying = system({"die"},
    function(e)
        del(world,e)
        if (e.player) reset()
    end)

A system to render entities with sprite and position components:

sprite = system({"pos", "spr"},
    function(e)
        spr(e.spr.n,e.pos.x,e.pos.y,1,1,e.spr.fx,e.spr.fy)
    end)

Game init and system invokations:

-- entity container
world = {}

function _init()
    add(world, mk_cannon(8,12))
    add(world, mk_station(8,4))
end

function _update()
    aging(world)
    dying(world)
end

function _draw()
    cls()
    sprite(world)
end

This but scratches the surface of what can be done with that (fascinating, to me) framework.

P#77258 2020-05-27 08:35 ( Edited 2020-05-27 09:26)

These are all extremely detailed responses but are all extremely past my level and i also feel like ECS isnt what im looking for. I looked in Celeste's code and i thought that it was ECS but it doesnt seem to be now i look back. Could anyone explain how Celeste was coded?

P#77261 2020-05-27 09:43 ( Edited 2020-05-27 09:44)
2

@RaspberryPye: I mean this in the nicest possible way, but I honestly think you have to move past the fixation with how Celeste was coded. Start asking yourself how you would code it, and avoid using other carts that were at the token limit as learning examples. Their code isn't going to be easily understood because it was intentionally minified, and no explanation other people give you is going to help with that.

There are many examples of platformer starter kits here that are heavily commented for newbies. This one, or this one, or this one. Start with one of those, and if something doesn't make sense, then do some experiments! Delete chunks of code and see what changes, change numbers, mess with it until you understand what's happening. In my experience, that's the best way to learn how to code. :)

P#77274 2020-05-27 14:48

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-21 17:11:16 | 0.033s | Q:24