Skip to content

Instantly share code, notes, and snippets.

@kingsleyh
Created July 11, 2020 19:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kingsleyh/7532be746d6527600273820adc16cd06 to your computer and use it in GitHub Desktop.
Save kingsleyh/7532be746d6527600273820adc16cd06 to your computer and use it in GitHub Desktop.
Entitas
require "crsfml"
require "entitas"
# ---- 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 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
# ---- 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
@systems = create_systems(Contexts.shared_instance)
logger.info { "App init" }
end
def create_systems(contexts)
Entitas::Feature.new("Systems")
.add(Systems::Board.new(contexts))
end
def run
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