Skip to content

Instantly share code, notes, and snippets.

@shawn42
Last active November 17, 2018 19:33
Show Gist options
  • Save shawn42/dd7fea1dc590460988098b853fb360ab to your computer and use it in GitHub Desktop.
Save shawn42/dd7fea1dc590460988098b853fb360ab to your computer and use it in GitHub Desktop.
module GameEcs
class EcsSystem
def self.system_for_query(query, &block)
new(query, &block)
end
def initialize(query=Query.none, &block)
@query = query
@system_block = block
end
def update(entity_store, *args)
before_update(entity_store, *args)
entity_store.query.each do |ent|
each(ent, *args)
end
after_update(entity_store, *args)
end
def before_update(entity_store, *args)
end
def after_update(entity_store, *args)
end
def update_each(entity_store, ent, *args)
# handle a single update of an ent
@system_block.call(entity_store, ent, *args)
end
end
end
include GameEcs
MovementSystem = EcsSystem.system_for_query( Q.musts(Position, Velocity) ) do |store, ent, dt, input|
pos,vel = ent.components
pos.x += vel.x * dt
pos.y += vel.y * dt
end
# long hand via subclassing
class MovementSystem < EcsSystem
def after_update(store, *args)
# whatever
end
def update_each(store, ent, dt, input)
Q.each_entity(Position, Velocity) do
pos,vel = ent.components
pos.x += vel.x * dt
pos.y += vel.y * dt
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment