Log In  
[ :: Read More :: ]

Cart #44917 | 2017-10-04 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

This is a tiny (47 token!) Entity Component System library.

Entity Component Systems are design patterns where entities are generic collections of components and logic is written for specific component types. It's popular for game design as we often have many 'things' that don't always fall within clear cut types.

There's different approaches to ECS, here we'll consider a table to be an entity, and the key/value pairs to be component names and values.

This library is basically a single function, system, which creates a function that selects every entity with the specified components and calls the user provided function on each one.

function _has(e, ks)
  for n in all(ks) do
    if not e[n] then
      return false
  return true

function system(ks, f)
  return function(es)
    for e in all(es) do
      if _has(e, ks) then

To use it we'll need a collection of entities:

world = {}

add(world, {pos={x=32, y=64}, color=12})
add(world, {pos={x=64, y=64}, sprite=0})
add(world, {pos={x=96, y=64}, color=8, sprite=1})

Now we define a couple system functions, one for things with position and color components, and one for position and sprite.

circles = system({"pos", "color"}, 
    circfill(e.pos.x, e.pos.y, 8, e.color)

sprites = system({"pos", "sprite"}, 
    spr(e.sprite, e.pos.x-4, e.pos.y-4)

Finally, we call our systems on our entity collection:

function _draw()
P#44926 2017-10-05 09:17 ( Edited 2018-02-04 16:16)

[ :: Read More :: ]

This is a small (259 token) library for finding entities close to a point.

A bucket hash (also called a spatial or grid hash) breaks up space by a grid size, and maintains a sparse table of "buckets" representing a grid location. When entity membership in these buckets is updated, you can quickly
retrieve entities near a given point.

-- usage

-- create a data 'store' {size=30,prop="pos"}
-- store.prop should match your entities' position property name, 
-- which should be a 'point' value like {x=0,y=0}
-- store.size should be tuned to the max neighbor distance you'll be finding

-- periodically call bstore(store, entity) to update their bucket membership

-- bget(store, point) returns stored entities from a 3x3 square of buckets around 
-- the given point, filter these by a distance function if you need more precision

-- bdel(store, entity) to remove an entity

-- remember you can maintain multiple stores based on the needs of your game!
P#41493 2017-06-09 15:26 ( Edited 2021-08-08 03:34)

[ :: Read More :: ]

Cart #41463 | 2017-06-09 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

This is a small (257 token) tweening library. It allows you to tween any value over time, and can be extended
by supplying your own easing and lerping functions. It also allows callbacks, functions that are called when the tween has finished, letting you sequence animations and code.

quick start:

tween(player.position, "x", 64, 100, {e=pow(2)})

-- tween(object, prop_name, target_value, duration, options_table)
-- options are 
-- "e" dual in+out easing fn
-- "ei","eo" in,out easing fn
-- "f" tween callback (takes 1 argument - the object)
-- "l" lerping function, default lerp works with number values

Please check the cart code for further examples.

Advanced tips:

If you need to stop a tween, keep a reference to it (the return value of "tween") and set it's "o" prop to nil.


v 1.0 - shaved off 7 tokens, moved bbs post to "code snippets"

P#41337 2017-06-06 15:02 ( Edited 2017-06-09 15:07)