-
Notifications
You must be signed in to change notification settings - Fork 3
Quick Start Guide
If you have some experience with other Entity Component Systems like Artemis or Entitas, then this guide will show you how to get up quickly with Baku.
Create components by overriding the Baku::Component
constructor and calling super()
. The example component below simple stores the x and y coordinates of an entity in a 2D space.
class TransformComponent < Baku::Component
attr_accessor :x, :y
def initialize(x, y)
super()
@x = x
@y = y
end
end
Create systems that process entities by overriding Baku::ComponentSystem
and calling super()
to specify the components that an entity must possess in order for it to be processed by the system. The example system below will only operate on entities that possess both a TransformComponent
and a VelocityComponent
. You will also need to specify whether the system will be run during the :update
or :draw
loop.
class MovementSystem < Baku::ComponentSystem
def initialize
super([TransformComponent, VelocityComponent], :update)
end
def process_entity(entity, transform, velocity)
transform.x += velocity.x
transform.y += velocity.y
end
end
In your game initialization logic, create a Baku::World
instance and register any systems you want to use. In the below example, we register our example system from above and create an entity that will be processed by the system:
def intialize
@world = Baku::World.new
@world.add_system(MovementSystem.new)
entity = @world.create_entity
entity.add_component(TransformComponent)
entity.add_component(VelocityComponent)
end
In your game update
and draw
loops, call the Baku::World
instance's update
and draw
methods. You'll want to keep track of the millseconds between frames and pass that to the update
method if you're planning to use the entity component system for anything that needs it.
def update
@world.update(delta_ms)
end
def draw
@world.draw
end
That should be enough to get you started with the basic Baku flow. See the Tips and Tricks section in the sidebar for additional information on specific topics.