baku 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 28ac783aa88b25f6c40da785a9455b8ceffc57ce
4
+ data.tar.gz: e1352417ee1b095d41da14d033fa84bdadb57097
5
+ SHA512:
6
+ metadata.gz: 5c68d176ad7eda25d66ec1674c3cf5a739c7eea941a3edcbe0133c8aa14242af7fd7277e95e20198e8fc9fd6b06bbead8e04ff5210ac270bb40a0a7472e3255f
7
+ data.tar.gz: 0fb615ce7c1b69ed0aad6147c96e9f02b5da16e62e8daf6c77dcd37194c0316ac5c75aef5c08135c3a06279f4249596500bcff3bca0b93d291af26d68249a234
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.4
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 John Tuttle
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # Baku
2
+
3
+ Baku provides a simple Entity Component System framework for use with Ruby game
4
+ engines. It has been tested with [Gosu](https://www.libgosu.org/ruby.html), but
5
+ should be flexible enough to work with any Ruby project that has a game loop.
6
+
7
+ Baku is still very much a work in progress. There are undoubtedly bugs. I will
8
+ be continually iterating and improving on it as I use it for my personal game
9
+ development projects. Enjoy!
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'baku'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install baku
26
+
27
+ ## Usage
28
+
29
+ Create components by overriding `Baku::Component` and calling `super()`. The
30
+ example component below simple stores the x and y coordinates of an entity in
31
+ a 2D space.
32
+
33
+ ```
34
+ class TransformComponent < Baku::Component
35
+ attr_accessor :x, :y
36
+
37
+ def initialize(x, y)
38
+ super()
39
+
40
+ @x = x
41
+ @y = y
42
+ end
43
+ end
44
+ ```
45
+
46
+ Create systems by overriding `Baku::System` and calling `super()` to specify the
47
+ components that an entity must possess in order for it to be processed by the
48
+ system. The example system below will only operate on entities that possess both
49
+ a `TransformComponent` and a `VelocityComponent`. You will also need to specify
50
+ whether the system will be run during the `:update` or `:draw` loop.
51
+
52
+ ```
53
+ class MovementSystem < Baku::System
54
+ def initialize
55
+ super([TransformComponent, VelocityComponent], :update)
56
+ end
57
+
58
+ def process_entity(entity, transform, velocity)
59
+ transform.x += velocity.x
60
+ transform.y += velocity.y
61
+ transform.z += velocity.z
62
+ end
63
+ end
64
+ ```
65
+
66
+ In your game initialization logic, create a `Baku::World` instance and register
67
+ any systems you want to use. In the below example, we register our example
68
+ system from above and create an entity that will be processed by the system:
69
+
70
+ ```
71
+ def game_initialization
72
+ @world = Baku::World.new
73
+ @world.add_system(MovementSystem.new)
74
+
75
+ entity = @world.create_entity
76
+ entity.add_component(TransformComponent)
77
+ entity.add_component(VelocityComponent)
78
+ end
79
+ ```
80
+
81
+ In your game `update` and `draw` loops, call the `Baku::World` instance's
82
+ `update` and `draw` methods. You'll want to keep track of the millseconds
83
+ between frames and pass that to the `update` method if you're planning to
84
+ use the entity component system for anything that needs it.
85
+
86
+ ```
87
+ def game_update_loop
88
+ @world.update(delta_ms)
89
+ end
90
+
91
+ def game_draw_loop
92
+ @world.draw
93
+ end
94
+ ```
95
+
96
+ ## Development
97
+
98
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
99
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
100
+ prompt that will allow you to experiment.
101
+
102
+ To install this gem onto your local machine, run `bundle exec rake install`. To
103
+ release a new version, update the version number in `version.rb`, and then run
104
+ `bundle exec rake release`, which will create a git tag for the version, push git
105
+ commits and tags, and push the `.gem` file to
106
+ [rubygems.org](https://rubygems.org).
107
+
108
+ ## Contributing
109
+
110
+ Bug reports and pull requests are welcome on GitHub at
111
+ https://github.com/jtuttle/baku.
112
+
113
+ ## License
114
+
115
+ The gem is available as open source under the terms of the
116
+ [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/baku.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "baku/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "baku"
8
+ spec.version = Baku::VERSION
9
+ spec.authors = ["John Tuttle"]
10
+ spec.email = ["jtuttle.develops@gmail.com"]
11
+
12
+ spec.summary = "An Entity Component System framework for Ruby"
13
+ spec.description = "An Entity Component System framework for Ruby"
14
+ spec.homepage = "https://github.com/jtuttle/baku"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.15"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "baku"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,9 @@
1
+ module Baku
2
+ class Component
3
+ attr_reader :id
4
+
5
+ def initialize
6
+ @id = SecureRandom.uuid
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,39 @@
1
+ module Baku
2
+ class Entity
3
+ attr_reader :id, :components, :tags
4
+
5
+ def initialize(world, tags = [])
6
+ @world = world
7
+ @tags = tags
8
+
9
+ @id = SecureRandom.uuid
10
+ @components = {}
11
+ end
12
+
13
+ def add_component(component)
14
+ if @components.has_key?(component.class)
15
+ raise StandardError.
16
+ new("Entity already has component: #{component.class}")
17
+ end
18
+
19
+ # TODO: this is some pretty ugly coupling, figure out if there's a cleaner
20
+ # way to do this. Callbacks or something.
21
+ @world.entity_manager.entity_add_component(self, component)
22
+ end
23
+
24
+ def remove_component(component_class)
25
+ if !@components.has_key?(component_class)
26
+ raise StandardError.
27
+ new("Entity does not have component: #{component_class}")
28
+ end
29
+
30
+ # TODO: this is some pretty ugly coupling, figure out if there's a cleaner
31
+ # way to do this. Callbacks or something.
32
+ @world.entity_manager.entity_remove_component(self, get_component(component_class))
33
+ end
34
+
35
+ def get_component(component_class)
36
+ @components[component_class]
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,83 @@
1
+ module Baku
2
+ class EntityManager
3
+ def initialize
4
+ @entities_by_system_mask = {}
5
+ @entities_by_tag = {}
6
+
7
+ @component_set = Set.new
8
+ @system_mask_cache = {}
9
+ end
10
+
11
+ def register_entity(entity)
12
+ entity.tags.each do |tag|
13
+ @entities_by_tag[tag] ||= []
14
+ @entities_by_tag[tag] << entity
15
+ end
16
+ end
17
+
18
+ def register_system(system)
19
+ system.components.each do |component|
20
+ @component_set << component
21
+ end
22
+
23
+ system_component_mask =
24
+ get_component_mask(system.components)
25
+
26
+ @system_mask_cache[system] = system_component_mask
27
+
28
+ @entities_by_system_mask[system_component_mask] = []
29
+ end
30
+
31
+ # TODO: there may be a more efficent way to do this, like by comparing
32
+ # the diff of the mask before and after, but this works for now.
33
+ def entity_add_component(entity, component)
34
+ old_mask = get_component_mask(entity.components)
35
+ entity.components[component.class] = component
36
+ new_mask = get_component_mask(entity.components)
37
+
38
+ update_system_membership(entity, old_mask, new_mask)
39
+ end
40
+
41
+ def entity_remove_component(entity, component)
42
+ old_mask = get_component_mask(entity.components)
43
+ entity.components.delete(component.class)
44
+ new_mask = get_component_mask(entity.components)
45
+
46
+ update_system_membership(entity, old_mask, new_mask)
47
+ end
48
+
49
+ def get_entities_for_system(system)
50
+ system_mask = @system_mask_cache[system]
51
+ @entities_by_system_mask[system_mask]
52
+ end
53
+
54
+ def get_entities_by_tag(tag)
55
+ @entities_by_tag[tag]
56
+ end
57
+
58
+ private
59
+
60
+ def get_component_mask(components)
61
+ mask = 0
62
+
63
+ @component_set.each_with_index do |c, i|
64
+ mask |= (1 << i) if components.include?(c)
65
+ end
66
+
67
+ mask
68
+ end
69
+
70
+ def update_system_membership(entity, old_mask, new_mask)
71
+ @entities_by_system_mask.each do |system_mask, entities|
72
+ old_match = (system_mask & old_mask == system_mask)
73
+ new_match = (system_mask & new_mask == system_mask)
74
+
75
+ if old_match && !new_match
76
+ entities.delete(entity)
77
+ elsif !old_match && new_match
78
+ entities << entity
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,25 @@
1
+ module Baku
2
+ class System
3
+ attr_reader :components, :game_loop_step
4
+ attr_accessor :world
5
+
6
+ def initialize(components, game_loop_step)
7
+ @components = components
8
+ @game_loop_step = game_loop_step
9
+ end
10
+
11
+ def execute
12
+ entities =
13
+ @world.entity_manager.get_entities_for_system(self)
14
+
15
+ entities.each do |entity|
16
+ entity_components = @components.map { |c| entity.get_component(c) }
17
+ process_entity(entity, *entity_components)
18
+ end
19
+ end
20
+
21
+ def process_entity(entity)
22
+ raise NotImplementedError
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module Baku
2
+ VERSION = "0.1.0"
3
+ end
data/lib/baku/world.rb ADDED
@@ -0,0 +1,53 @@
1
+ module Baku
2
+ class World
3
+ attr_reader :entity_manager, :blackboard, :delta_ms
4
+
5
+ def initialize
6
+ # TODO: there's currently no way to interleave update and draw systems.
7
+ # Is this something we'll eventually need?
8
+ @update_systems = []
9
+ @draw_systems = []
10
+
11
+ @entity_manager = EntityManager.new
12
+
13
+ @blackboard = {}
14
+ end
15
+
16
+ def add_system(system)
17
+ system_list =
18
+ if system.game_loop_step == :update
19
+ @update_systems
20
+ elsif system.game_loop_step == :draw
21
+ @draw_systems
22
+ end
23
+
24
+ if system_list.map(&:class).include?(system.class)
25
+ raise StandardError.new("Already added #{system.class} system to world.")
26
+ end
27
+
28
+ system_list << system
29
+ system.world = self
30
+
31
+ @entity_manager.register_system(system)
32
+ end
33
+
34
+ def create_entity(tags = [])
35
+ entity = Entity.new(self, tags)
36
+ @entity_manager.register_entity(entity)
37
+ entity
38
+ end
39
+
40
+ def get_entities_by_tag(tag)
41
+ @entity_manager.get_entities_by_tag(tag)
42
+ end
43
+
44
+ def update(delta_ms)
45
+ @delta_ms = delta_ms
46
+ @update_systems.each(&:execute)
47
+ end
48
+
49
+ def draw
50
+ @draw_systems.each(&:execute)
51
+ end
52
+ end
53
+ end
data/lib/baku.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "baku/version"
2
+
3
+ module Baku
4
+ require 'securerandom'
5
+ require 'set'
6
+
7
+ Gem.find_files("baku/**/*.rb").each { |path| require path }
8
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: baku
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - John Tuttle
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.15'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.15'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: An Entity Component System framework for Ruby
56
+ email:
57
+ - jtuttle.develops@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - baku.gemspec
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/baku.rb
73
+ - lib/baku/component.rb
74
+ - lib/baku/entity.rb
75
+ - lib/baku/entity_manager.rb
76
+ - lib/baku/system.rb
77
+ - lib/baku/version.rb
78
+ - lib/baku/world.rb
79
+ homepage: https://github.com/jtuttle/baku
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.6.12
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: An Entity Component System framework for Ruby
103
+ test_files: []