-
Notifications
You must be signed in to change notification settings - Fork 3
Quick Start Guide
First, follow the instructions to install Gosu, then set up an empty Gosu game window in quick_start.rb
like so:
require 'gosu'
class QuickStart < Gosu::Window
def initialize
super 640, 480
self.caption = "Quick Start"
end
def update
end
def draw
end
end
QuickStart.new.show
Try running it from the command line to make sure nothing is wrong:
$ ruby quick_start.rb
Next, install Baku by following the installation instructions on the repository's README. Add require 'baku'
to the top of quick_start.rb
and you should be good to go!
Components represent all the data that make up an Entity. Create a Component by overriding Baku::Component
and calling super()
in the constructor. In a pure ECS approach, Components should contain only data and possibly helper methods to shape the data where necessary. The TransformComponent
and VelocityComponent
examples below store the x and y position and velocity of an Entity in a 2D space. See the Components section for more details.
class TransformComponent < Baku::Component
attr_accessor :x, :y
def initialize(x, y)
super()
@x = x
@y = y
end
end
class VelocityComponent < Baku::Component
attr_accessor :x, :y
def initialize(x, y)
super()
@x = x
@y = y
end
end
Systems contain your game logic. Each system operates on all Entities that have the specified set of Components. Create a System by overriding Baku::System
and calling super()
to specify the Components that an entity must possess in order for it to be processed by the System. The example MovementSystem
below will only process entities that possess both a TransformComponent
and a VelocityComponent
. You will also need to specify whether the System should be run during the :update
or :draw
loop. See the Systems section for more details.
class MovementSystem < Baku::System
def initialize
super([TransformComponent, VelocityComponent], :update)
end
def process_entity(entity, transform, velocity)
transform.x += velocity.x
transform.y += velocity.y
end
end
The World is the glue that ties the ECS ecosystem together. It contains all of your Entities and the Systems that process them. 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 MovementSystem
from above and create an Entity with both a TransformComponent
and a VelocityComponent
so that it will be processed by the MovementSystem
. See the World section for more details.
def game_initialization
@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's update and draw loops, call the Baku::World
instance's #update
and #draw
methods. If possible, you'll likely want to keep track of the milliseconds between frames and pass that to the #update
method if you're planning to use the ECS for anything that needs it, such as smoothing out frame progression on sprite animations.
def game_update_loop
current_ms = Gosu::milliseconds()
delta_ms = current_ms - @prev_ms
@prev_ms = current_ms
@world.update(delta_ms)
end
def game_draw_loop
@world.draw
end
That should be enough to get you started with an entity that has a position and velocity in your game! Now hook it up to an InputSystem
to make it move or add a SpriteRenderSystem
to draw it on the screen. Check out the Asteroids example in the Baku example repository for a more complete example of what Baku can do.