-
-
Save kalinon/1ab9139fe29e1715b402d84ff2c6650f to your computer and use it in GitHub Desktop.
Entitas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "crsfml" | |
require "entitas" | |
::Log.builder.clear | |
spoved_logger(bind: true) | |
# ---- domain ---- | |
module Domain | |
struct Dimension | |
property rows : Int32 | |
property cols : Int32 | |
def initialize(@rows, @cols); end | |
end | |
struct Position | |
property row : Int32 | |
property col : Int32 | |
def initialize(@row, @col); end | |
end | |
end | |
# ---- components ---- | |
@[Component::Unique] | |
@[Context(Game)] | |
class Board < Entitas::Component | |
prop :dimension, Domain::Dimension, default: Domain::Dimension.new(8, 8) | |
end | |
@[Context(Game)] | |
class Gem < Entitas::Component | |
end | |
@[Context(Game)] | |
class Hidden < Entitas::Component | |
end | |
@[Context(Game)] | |
class Position < Entitas::Component | |
prop :position, Domain::Position, default: Domain::Position.new(0, 0) | |
end | |
# ---- systems ---- | |
module Systems | |
class Board < Entitas::ReactiveSystem | |
spoved_logger | |
include Entitas::Systems::InitializeSystem | |
protected property contexts : Contexts | |
protected property gems : Entitas::Group(GameEntity) | |
def initialize(@contexts) | |
@gems = @contexts.game.get_group(GameMatcher.all_of(GameMatcher.gem, GameMatcher.position)) | |
@collector = get_trigger(@contexts.game) | |
end | |
def get_trigger(context : Entitas::Context) : Entitas::ICollector | |
context.create_collector(GameMatcher.board) | |
end | |
def init | |
logger.info { "INIT" } | |
end | |
def execute(entities : Array(Entitas::IEntity)) | |
logger.info { "EXEC" } | |
puts "EXEC" | |
end | |
end | |
end | |
module Systems | |
class Renderer < Entitas::ReactiveSystem | |
protected property contexts : Contexts | |
protected property gems : Entitas::Group(GameEntity) | |
protected property render_service : RenderService | |
def initialize(@contexts, @render_service) | |
@gems = @contexts.game.get_group(GameMatcher.all_of(GameMatcher.gem, GameMatcher.position).none_of(GameMatcher.hidden)) | |
@collector = get_trigger(@contexts.game) | |
end | |
def get_trigger(context : Entitas::Context) : Entitas::ICollector | |
context.create_collector(GameMatcher.board) | |
end | |
def execute(entities : Array(Entitas::IEntity)) | |
entities.each do |entity| | |
render_service.render(entity) | |
end | |
end | |
end | |
end | |
# ---- services ---- | |
class RenderService | |
def render(entity) | |
# render entity | |
end | |
end | |
# ---- game application ---- | |
class App | |
spoved_logger | |
getter systems : Entitas::Systems = Entitas::Systems.new | |
getter window : SF::RenderWindow | |
def initialize | |
mode = SF::VideoMode.new(2560, 1440) | |
@window = SF::RenderWindow.new(mode, "Match-3 game!", SF::Style::Titlebar | SF::Style::Close) | |
@window.vertical_sync_enabled = true | |
@window.mouse_cursor_visible = false | |
@render_service = RenderService.new | |
@systems = create_systems(Contexts.shared_instance, @render_service) | |
logger.info { "App init" } | |
@systems.execute | |
end | |
def create_systems(contexts, rs) | |
Entitas::Feature.new("Systems") | |
.add(Systems::Board.new(contexts)) | |
.add(Systems::Renderer.new(contexts, rs)) | |
end | |
def run | |
systems.init | |
while window.open? | |
while event = window.poll_event | |
if event.is_a? SF::Event::Closed | |
window.close | |
end | |
end | |
systems.execute | |
window.clear(SF::Color::Red) | |
# @window.draw(some_sprite_etc) | |
window.display | |
systems.cleanup | |
end | |
end | |
end | |
App.new.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment