Last active
September 19, 2024 13:34
-
-
Save xenobrain/93bc37ea8555ea075d2926eb13ccafb6 to your computer and use it in GitHub Desktop.
Dynamic Entities
This file contains hidden or 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
{ | |
"name": "Camera", | |
"components": [ | |
{ | |
"name": "Position", | |
"attributes": [ | |
{ "x": 100 } | |
] | |
} | |
] | |
} |
This file contains hidden or 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
module Component | |
end | |
module System | |
module World | |
class << self | |
def load args | |
args.state.entities = [] | |
fn.each $gtk.list_files('data/components') do |component_def| | |
new_component_module $gtk.parse_json_file "data/components/#{component_def}" | |
end | |
fn.each $gtk.list_files('data/entities') do |entity_def| | |
new_entity_class $gtk.parse_json_file "data/entities/#{entity_def}" | |
end | |
end | |
def new_component_module component_def | |
Component.const_set(component_def['name'], Module.new do | |
if component_def['attributes'] | |
fn.each component_def['attributes'] do |attribute| | |
send :attr_accessor, attribute.keys[0].to_sym | |
send :instance_variable_set, :"@#{attribute.keys[0]}", attribute.values[0] | |
end | |
end | |
if component_def['behaviors'] | |
fn.each component_def['behaviors'] do |behavior| | |
send :include, Component.const_get(behavior) | |
end | |
end | |
def self.included klass | |
fn.each instance_variables do |var| | |
klass.instance_variable_set var, (instance_variable_get var) | |
end | |
end | |
end) | |
end | |
def new_entity_class entity_def | |
Object.const_set(entity_def['name'], Class.new do | |
if entity_def['components'] | |
fn.each entity_def['components'] do |component| | |
send :include, Component.const_get(component['name']) | |
if component['attributes'] | |
fn.each component['attributes'] do |attribute| | |
send :instance_variable_set, :"@#{attribute.keys[0]}", attribute.values[0] | |
end | |
end | |
end | |
end | |
def self.new(...) | |
object = allocate | |
object.send(:initialize, ...) | |
fn.each instance_variables do |var| | |
object.instance_variable_set var, (instance_variable_get var) | |
end | |
object | |
end | |
end) | |
end | |
end | |
end | |
end |
This file contains hidden or 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
{ | |
"name": "Position", | |
"attributes": [ | |
{ "x": 0 }, | |
{ "y": 0 }, | |
{ "z" : 0 } | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment