Skip to content

Quick Start Guide

John Tuttle edited this page Jan 2, 2021 · 17 revisions

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.

Components

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

Systems

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

World

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

Next Steps

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.