darkholme 0.9.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: a622c249ab3e57ac8e924f96a3e8016eb703c1df
4
+ data.tar.gz: 4299d324cf08736524dfdaabc1248de738afe29b
5
+ SHA512:
6
+ metadata.gz: b8eaed977e5057199acb6a6f2ce459a49d043b389aced53d02434943ee0d2325db46b08a4ac0175d0fd53ab30661cfbc5d679e15ffd3a2effcf9740110c00849
7
+ data.tar.gz: 3bb5b0208608cbb869b409a458d99ad7584191bca187dd6bb05519028876b914dc5ebec2be7cc511dfc85a1367ef364b09eaa0ed749710411372fed251117c27
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem "pry", "~> 0.9.12.6"
5
+ gem "rspec", "~> 3.0.0.beta1"
6
+ gem "yard", "~> 0.7"
7
+ gem "rdoc", "~> 3.12"
8
+ gem "bundler", "~> 1.0"
9
+ gem "jeweler", "~> 2.0.1"
10
+ gem "simplecov", ">= 0"
11
+
12
+ gem "guard-rspec", "~> 4.2.6", require: false
13
+ gem "inch", "~> 0.2.3"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,117 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ addressable (2.3.5)
5
+ builder (3.2.2)
6
+ celluloid (0.15.2)
7
+ timers (~> 1.1.0)
8
+ coderay (1.1.0)
9
+ descendants_tracker (0.0.3)
10
+ diff-lcs (1.2.5)
11
+ docile (1.1.3)
12
+ faraday (0.9.0)
13
+ multipart-post (>= 1.2, < 3)
14
+ ffi (1.9.3)
15
+ formatador (0.2.4)
16
+ git (1.2.6)
17
+ github_api (0.11.2)
18
+ addressable (~> 2.3)
19
+ descendants_tracker (~> 0.0.1)
20
+ faraday (~> 0.8, < 0.10)
21
+ hashie (>= 1.2)
22
+ multi_json (>= 1.7.5, < 2.0)
23
+ nokogiri (~> 1.6.0)
24
+ oauth2
25
+ guard (2.4.0)
26
+ formatador (>= 0.2.4)
27
+ listen (~> 2.1)
28
+ lumberjack (~> 1.0)
29
+ pry (>= 0.9.12)
30
+ thor (>= 0.18.1)
31
+ guard-rspec (4.2.6)
32
+ guard (~> 2.1)
33
+ rspec (>= 2.14, < 4.0)
34
+ hashie (2.0.5)
35
+ highline (1.6.20)
36
+ inch (0.2.3)
37
+ sparkr (>= 0.2.0)
38
+ term-ansicolor
39
+ yard (~> 0.8.7)
40
+ jeweler (2.0.1)
41
+ builder
42
+ bundler (>= 1.0)
43
+ git (>= 1.2.5)
44
+ github_api
45
+ highline (>= 1.6.15)
46
+ nokogiri (>= 1.5.10)
47
+ rake
48
+ rdoc
49
+ json (1.8.1)
50
+ jwt (0.1.11)
51
+ multi_json (>= 1.5)
52
+ listen (2.4.1)
53
+ celluloid (>= 0.15.2)
54
+ rb-fsevent (>= 0.9.3)
55
+ rb-inotify (>= 0.9)
56
+ lumberjack (1.0.4)
57
+ method_source (0.8.2)
58
+ mini_portile (0.5.2)
59
+ multi_json (1.8.4)
60
+ multi_xml (0.5.5)
61
+ multipart-post (2.0.0)
62
+ nokogiri (1.6.1)
63
+ mini_portile (~> 0.5.0)
64
+ oauth2 (0.9.3)
65
+ faraday (>= 0.8, < 0.10)
66
+ jwt (~> 0.1.8)
67
+ multi_json (~> 1.3)
68
+ multi_xml (~> 0.5)
69
+ rack (~> 1.2)
70
+ pry (0.9.12.6)
71
+ coderay (~> 1.0)
72
+ method_source (~> 0.8)
73
+ slop (~> 3.4)
74
+ rack (1.5.2)
75
+ rake (10.1.1)
76
+ rb-fsevent (0.9.4)
77
+ rb-inotify (0.9.3)
78
+ ffi (>= 0.5.0)
79
+ rdoc (3.12.2)
80
+ json (~> 1.4)
81
+ rspec (3.0.0.beta1)
82
+ rspec-core (= 3.0.0.beta1)
83
+ rspec-expectations (= 3.0.0.beta1)
84
+ rspec-mocks (= 3.0.0.beta1)
85
+ rspec-core (3.0.0.beta1)
86
+ rspec-expectations (3.0.0.beta1)
87
+ diff-lcs (>= 1.1.3, < 2.0)
88
+ rspec-support (= 3.0.0.beta1)
89
+ rspec-mocks (3.0.0.beta1)
90
+ rspec-support (3.0.0.beta1)
91
+ simplecov (0.8.2)
92
+ docile (~> 1.1.0)
93
+ multi_json
94
+ simplecov-html (~> 0.8.0)
95
+ simplecov-html (0.8.0)
96
+ slop (3.4.7)
97
+ sparkr (0.4.1)
98
+ term-ansicolor (1.3.0)
99
+ tins (~> 1.0)
100
+ thor (0.18.1)
101
+ timers (1.1.0)
102
+ tins (1.0.0)
103
+ yard (0.8.7.3)
104
+
105
+ PLATFORMS
106
+ ruby
107
+
108
+ DEPENDENCIES
109
+ bundler (~> 1.0)
110
+ guard-rspec (~> 4.2.6)
111
+ inch (~> 0.2.3)
112
+ jeweler (~> 2.0.1)
113
+ pry (~> 0.9.12.6)
114
+ rdoc (~> 3.12)
115
+ rspec (~> 3.0.0.beta1)
116
+ simplecov
117
+ yard (~> 0.7)
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Massive Danger
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Darkholme
2
+
3
+ [![Build Status](https://travis-ci.org/massivedanger/darkholme.png?branch=master)](https://travis-ci.org/massivedanger/darkholme)
4
+
5
+ > Who am I? That, my dear, is an excellent question. Though not one easily answered.
6
+
7
+ Darkholme is an [entity-component system](http://en.wikipedia.org/wiki/Entity_component_system)
8
+ written in Ruby. It's still early days for it, but I think it's ready for most basic use cases.
9
+
10
+ ## Usage
11
+
12
+ _COMING SOON!_
13
+
14
+ ## Key concepts
15
+
16
+ ### Engine
17
+
18
+ Generally, your game will have one Engine. It contains all the entities, systems,
19
+ and components. Every frame of your game, you'll need to call `#update` on it. This will,
20
+ in turn, update all its relevant systems.
21
+
22
+ ### Entity
23
+
24
+ This is the basic component-holding class. It also has some callbacks, but it generally just
25
+ keeps a list of what it does. These get added to Engines.
26
+
27
+ ### Component
28
+
29
+ These define data the Systems end up working with. Also, they get added to Entities.
30
+
31
+ ### System
32
+
33
+ These get called once per frame and they update all the entities with components they're
34
+ interested in.
35
+
36
+ ### Family
37
+
38
+ Systems have a Family, which defines exactly what components they're interested in. This
39
+ means that systems won't loop through every single entity on update. They only loop through
40
+ relevant entities. This is _awesome_. Maybe.
41
+
42
+ ## Contributing to darkholme
43
+
44
+ - Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
45
+ - Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
46
+ - Fork the project.
47
+ - Start a feature/bugfix branch.
48
+ - Commit and push until you are happy with your contribution.
49
+ - Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
50
+ - Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
51
+
52
+ ## Copyright
53
+
54
+ Copyright (c) 2014 Massive Danger. See LICENSE.txt for further details.
55
+
data/Rakefile ADDED
@@ -0,0 +1,54 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+
13
+ require 'rake'
14
+ require 'yard'
15
+ require 'pry'
16
+ require 'jeweler'
17
+ require 'rspec/core'
18
+ require 'rspec/core/rake_task'
19
+
20
+ Jeweler::Tasks.new do |gem|
21
+ # gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
22
+ gem.name = "darkholme"
23
+ gem.homepage = "http://github.com/evanwalsh/darkholme"
24
+ gem.license = "MIT"
25
+ gem.summary = %Q{An entity-component system in Ruby for libgdx}
26
+ gem.description = %Q{An entity-component system in Ruby for libgdx}
27
+ gem.email = "evan@massivedanger.com"
28
+ gem.authors = ["Massive Danger"]
29
+ # dependencies defined in Gemfile
30
+ end
31
+ Jeweler::RubygemsDotOrgTasks.new
32
+
33
+ RSpec::Core::RakeTask.new(:spec) do |spec|
34
+ spec.pattern = FileList['spec/**/*_spec.rb']
35
+ end
36
+
37
+ desc "Code coverage detail"
38
+ task :simplecov do
39
+ ENV['COVERAGE'] = "true"
40
+ Rake::Task['spec'].execute
41
+ end
42
+
43
+ YARD::Rake::YardocTask.new
44
+
45
+ desc "Start a Pry console with the gem required"
46
+ task :pry do
47
+ $LOAD_PATH.unshift("./lib")
48
+ Dir["./lib/*.rb"].each do |file|
49
+ Pry.config.requires << file
50
+ end
51
+ Pry.start
52
+ end
53
+
54
+ task :default => :spec
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.9.0
data/darkholme.gemspec ADDED
@@ -0,0 +1,91 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: darkholme 0.9.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "darkholme"
9
+ s.version = "0.9.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.authors = ["Massive Danger"]
13
+ s.date = "2014-02-22"
14
+ s.description = "An entity-component system in Ruby for libgdx"
15
+ s.email = "evan@massivedanger.com"
16
+ s.extra_rdoc_files = [
17
+ "LICENSE.txt",
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ ".rspec",
23
+ ".travis.yml",
24
+ "Gemfile",
25
+ "Gemfile.lock",
26
+ "Guardfile",
27
+ "LICENSE.txt",
28
+ "README.md",
29
+ "Rakefile",
30
+ "VERSION",
31
+ "darkholme.gemspec",
32
+ "lib/darkholme.rb",
33
+ "lib/darkholme/bitset.rb",
34
+ "lib/darkholme/component.rb",
35
+ "lib/darkholme/engine.rb",
36
+ "lib/darkholme/entity.rb",
37
+ "lib/darkholme/family.rb",
38
+ "lib/darkholme/system.rb",
39
+ "spec/lib/darkholme/bitset_spec.rb",
40
+ "spec/lib/darkholme/component_spec.rb",
41
+ "spec/lib/darkholme/engine_spec.rb",
42
+ "spec/lib/darkholme/entity_spec.rb",
43
+ "spec/lib/darkholme/family_spec.rb",
44
+ "spec/lib/darkholme/system_spec.rb",
45
+ "spec/lib/darkholme_spec.rb",
46
+ "spec/spec_helper.rb",
47
+ "spec/support/mock_classes.rb"
48
+ ]
49
+ s.homepage = "http://github.com/evanwalsh/darkholme"
50
+ s.licenses = ["MIT"]
51
+ s.require_paths = ["lib"]
52
+ s.rubygems_version = "2.1.11"
53
+ s.summary = "An entity-component system in Ruby for libgdx"
54
+
55
+ if s.respond_to? :specification_version then
56
+ s.specification_version = 4
57
+
58
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
59
+ s.add_development_dependency(%q<pry>, ["~> 0.9.12.6"])
60
+ s.add_development_dependency(%q<rspec>, ["~> 3.0.0.beta1"])
61
+ s.add_development_dependency(%q<yard>, ["~> 0.7"])
62
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
63
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
64
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
65
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
66
+ s.add_development_dependency(%q<guard-rspec>, ["~> 4.2.6"])
67
+ s.add_development_dependency(%q<inch>, ["~> 0.2.3"])
68
+ else
69
+ s.add_dependency(%q<pry>, ["~> 0.9.12.6"])
70
+ s.add_dependency(%q<rspec>, ["~> 3.0.0.beta1"])
71
+ s.add_dependency(%q<yard>, ["~> 0.7"])
72
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
73
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
74
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
75
+ s.add_dependency(%q<simplecov>, [">= 0"])
76
+ s.add_dependency(%q<guard-rspec>, ["~> 4.2.6"])
77
+ s.add_dependency(%q<inch>, ["~> 0.2.3"])
78
+ end
79
+ else
80
+ s.add_dependency(%q<pry>, ["~> 0.9.12.6"])
81
+ s.add_dependency(%q<rspec>, ["~> 3.0.0.beta1"])
82
+ s.add_dependency(%q<yard>, ["~> 0.7"])
83
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
84
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
85
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
86
+ s.add_dependency(%q<simplecov>, [">= 0"])
87
+ s.add_dependency(%q<guard-rspec>, ["~> 4.2.6"])
88
+ s.add_dependency(%q<inch>, ["~> 0.2.3"])
89
+ end
90
+ end
91
+
@@ -0,0 +1,102 @@
1
+ module Darkholme
2
+ class Bitset
3
+ attr_reader :bits, :set_indexes
4
+
5
+ def initialize(*initial_bits)
6
+ @bits = initial_bits.map {|bit| convert_bit(bit) }.inject(:+) || 0
7
+
8
+ @set_indexes = []
9
+ @set_indexes += initial_bits.uniq if initial_bits
10
+ end
11
+
12
+ def set(bit, value = true)
13
+ if value
14
+ @bits = union convert_bit(bit)
15
+ @set_indexes << bit unless @set_indexes.include?(bit)
16
+ elsif set?(bit)
17
+ @bits = xor convert_bit(bit)
18
+ @set_indexes.delete(bit)
19
+ end
20
+
21
+ @bits
22
+ end
23
+
24
+ def clear(bit)
25
+ set(bit, false)
26
+ end
27
+
28
+ def set?(bit)
29
+ bit = convert_bit(bit)
30
+ intersect(bit) == bit
31
+ end
32
+
33
+ def include?(other)
34
+ set_indexes & other.set_indexes == other.set_indexes
35
+ end
36
+
37
+ def intersect(other)
38
+ @bits & bits_from_object(other)
39
+ end
40
+ alias_method :&, :intersect
41
+
42
+ def intersect?(other)
43
+ if other_bits = bits_from_object(other) > 0
44
+ intersect(other_bits) == other_bits
45
+ else
46
+ false
47
+ end
48
+ end
49
+
50
+ def not
51
+ ~@bits
52
+ end
53
+ alias_method :~, :not
54
+
55
+ def xor(other)
56
+ @bits ^ bits_from_object(other)
57
+ end
58
+ alias_method :^, :xor
59
+
60
+ def union(other)
61
+ @bits | bits_from_object(other)
62
+ end
63
+ alias_method :|, :union
64
+
65
+ def to_s
66
+ @bits.to_s(2)
67
+ end
68
+
69
+ def to_i
70
+ @bits.to_i
71
+ end
72
+
73
+ def to_f
74
+ @bits.to_f
75
+ end
76
+
77
+ def [](index)
78
+ set?(index)
79
+ end
80
+
81
+ def []=(index, value)
82
+ set(index, value)
83
+ end
84
+
85
+ def ==(other)
86
+ @bits == other.bits
87
+ end
88
+
89
+ private
90
+
91
+ def flip(bit)
92
+ end
93
+
94
+ def convert_bit(bit)
95
+ 2 ** bit
96
+ end
97
+
98
+ def bits_from_object(object)
99
+ object.respond_to?(:bits) ? object.bits : object.to_i
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,14 @@
1
+ module Darkholme
2
+ class Component
3
+ @next_bit = 0
4
+ @bits = {}
5
+
6
+ def self.bit_for(klass)
7
+ @bits[klass] ||= @next_bit += 1
8
+ end
9
+
10
+ def bit
11
+ Component.bit_for(self.class)
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,50 @@
1
+ module Darkholme
2
+ class Engine
3
+ attr_reader :systems, :entities
4
+
5
+ def initialize
6
+ @systems = {}
7
+ @families = {}
8
+ @entities = Set.new
9
+ end
10
+
11
+ def add_entity(entity)
12
+ @entities << entity
13
+ entity.added_to_engine self
14
+ end
15
+
16
+ def remove_entity(entity)
17
+ @entities.delete entity
18
+ entity.removed_from_engine self
19
+ end
20
+
21
+ def add_system(system)
22
+ @systems[system.class] = system
23
+ system.added_to_engine self
24
+ end
25
+
26
+ def remove_system(systemClass)
27
+ system = @systems.delete systemClass
28
+ system.removed_from_engine(self) if system
29
+ end
30
+
31
+ def system(systemClass)
32
+ @systems[systemClass]
33
+ end
34
+
35
+ def entities_for_family(family)
36
+ @families[family.index] ||= begin
37
+ Set.new.tap do |entities|
38
+ @entities.each do |entity|
39
+ entities << entity if family.member?(entity)
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ def update(delta)
46
+ @systems.each {|klass, system| system.update(delta) }
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,43 @@
1
+ module Darkholme
2
+ class Entity
3
+ attr_accessor :engine, :components, :component_bits
4
+
5
+ def initialize
6
+ self.components = {}
7
+ self.component_bits = Bitset.new
8
+ self.engine = nil
9
+ end
10
+
11
+ def added_to_engine(engine)
12
+ self.engine = engine
13
+ end
14
+
15
+ def removed_from_engine(engine)
16
+ self.engine = nil
17
+ end
18
+
19
+ def add_component(component)
20
+ self.components[component.class] = component
21
+ self.component_bits.set(component.bit)
22
+
23
+ component
24
+ end
25
+
26
+ def remove_component(component_class)
27
+ if removed_component = component_for(component_class)
28
+ self.component_bits.clear(removed_component.bit)
29
+ self.components.delete(component_class)
30
+ end
31
+
32
+ removed_component
33
+ end
34
+
35
+ def has_component?(component_class)
36
+ self.components.include? component_class
37
+ end
38
+
39
+ def component_for(component_class)
40
+ self.components[component_class]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,36 @@
1
+ module Darkholme
2
+ class Family
3
+ @next_index = 0
4
+ @families = {}
5
+
6
+ class << self
7
+ attr_accessor :next_index
8
+ end
9
+
10
+ attr_reader :index, :bits
11
+
12
+ def self.for(*component_classes)
13
+ bits = Bitset.new
14
+ component_classes.each do |klass|
15
+ bits.set Component.bit_for(klass)
16
+ end
17
+
18
+ hash = bits.to_i
19
+ @families[hash] ||= begin
20
+ new bits
21
+ end
22
+ end
23
+
24
+ def member?(entity)
25
+ entity.component_bits.include?(bits)
26
+ end
27
+
28
+ private
29
+
30
+ def initialize(bits)
31
+ @index = self.class.next_index += 1
32
+ @bits = bits
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,33 @@
1
+ module Darkholme
2
+ class System
3
+ class << self
4
+ attr_reader :family
5
+
6
+ def has_family(*component_classes)
7
+ @family = Family.for(*component_classes)
8
+ end
9
+ end
10
+
11
+ attr_accessor :engine
12
+
13
+ def update(delta)
14
+ raise NotImplementedError.new("You must override #update(delta)")
15
+ end
16
+
17
+ def added_to_engine(engine)
18
+ self.engine = engine
19
+ end
20
+
21
+ def removed_from_engine(engine)
22
+ self.engine = nil if self.engine == engine
23
+ end
24
+
25
+ def entities
26
+ engine.entities_for_family(family)
27
+ end
28
+
29
+ def family
30
+ self.class.family
31
+ end
32
+ end
33
+ end
data/lib/darkholme.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'darkholme/bitset'
2
+ require 'darkholme/engine'
3
+ require 'darkholme/entity'
4
+ require 'darkholme/system'
5
+ require 'darkholme/component'
6
+ require 'darkholme/family'
7
+
8
+ module Darkholme
9
+
10
+ end
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ module Darkholme
4
+ describe Bitset do
5
+ subject { Bitset.new }
6
+ let(:bit) { 3 }
7
+
8
+ it "can set bits" do
9
+ subject.set(bit)
10
+ expect(subject.set?(bit)).to eq(true)
11
+ end
12
+
13
+ it "can set bits to false" do
14
+ subject.set(bit)
15
+ expect(subject.set?(bit)).to eq(true)
16
+
17
+ subject.set(bit, false)
18
+ expect(subject.set?(bit)).to eq(false)
19
+ end
20
+
21
+ it "has a list of set indexes" do
22
+ subject.set(3)
23
+ subject.set(4)
24
+ subject.set(5)
25
+
26
+ expect(subject.set_indexes).to match_array([3, 4, 5])
27
+ end
28
+
29
+ it "can clear bits" do
30
+ subject.set(bit)
31
+ expect(subject.set?(bit)).to eq(true)
32
+
33
+ expect {
34
+ subject.clear(bit)
35
+ }.to change { subject.bits }
36
+
37
+ expect(subject.set?(bit)).to eq(false)
38
+ end
39
+
40
+ it "removes the set index during a clear" do
41
+ subject.set(bit)
42
+ expect(subject.set_indexes).to include(bit)
43
+ subject.clear(bit)
44
+
45
+ expect(subject.set_indexes).to be_empty
46
+ end
47
+
48
+ it "can check for bit set status via array operators" do
49
+ subject.set(bit)
50
+
51
+ expect(subject[bit]).to eq(true)
52
+ end
53
+
54
+ it "can set bits via array operator" do
55
+ subject[bit] = true
56
+
57
+ expect(subject.set?(bit)).to eq(true)
58
+ end
59
+
60
+ it "can be compared to other bitsets" do
61
+ other = Bitset.new(bit)
62
+ subject.set(bit)
63
+
64
+ expect(subject).to eq(other)
65
+ end
66
+
67
+ it "can check for complete inclusion of other bitsets" do
68
+ subject.set(3)
69
+ subject.set(4)
70
+ subject.set(5)
71
+
72
+ other = Bitset.new
73
+ other.set(4)
74
+ other.set(5)
75
+
76
+ expect(subject.include? other).to eq(true)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ module Darkholme
4
+ describe Component do
5
+ subject { MockComponent.new }
6
+
7
+ it "returns a unique bit for Components" do
8
+ first_bit = Component.bit_for(MockComponent)
9
+ second_bit = Component.bit_for(Component)
10
+
11
+ expect(first_bit).not_to eq(second_bit)
12
+ end
13
+
14
+ it "gets a bit from an instance" do
15
+ expect(Component).to receive(:bit_for).with(MockComponent)
16
+ .and_return(:bit)
17
+ expect(subject.bit).to eq(:bit)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,58 @@
1
+ require 'spec_helper'
2
+
3
+ module Darkholme
4
+ describe Engine do
5
+ subject { Engine.new }
6
+ let(:entity) { MockEntity.new }
7
+ let(:system) { MockSystem.new }
8
+
9
+ describe "with entities" do
10
+ it "can add them" do
11
+ expect {
12
+ subject.add_entity(entity)
13
+ }.to change { subject.entities.count }.from(0).to(1)
14
+ end
15
+
16
+ it "can remove them" do
17
+ subject.add_entity(entity)
18
+
19
+ expect {
20
+ subject.remove_entity(entity)
21
+ }.to change { subject.entities.count }.from(1).to(0)
22
+ end
23
+
24
+ it "can find them by family" do
25
+ entity.add_component(MockComponent.new)
26
+ subject.add_entity(entity)
27
+ subject.add_system(system)
28
+
29
+ expect(subject.entities_for_family(system.family)).to include(entity)
30
+ end
31
+ end
32
+
33
+ describe "with systems" do
34
+ it "can add a system" do
35
+ expect {
36
+ subject.add_system(system)
37
+ }.to change { subject.systems.count }.from(0).to(1)
38
+ end
39
+
40
+ it "can remove a system" do
41
+ subject.add_system(system)
42
+
43
+ expect {
44
+ subject.remove_system(system.class)
45
+ }.to change { subject.systems.count }.from(1).to(0)
46
+ end
47
+
48
+ it "updates them" do
49
+ subject.add_system(system)
50
+ expect(system).to receive(:update).with(:delta)
51
+ .and_return(:updated)
52
+
53
+ subject.update(:delta)
54
+ end
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,73 @@
1
+ require 'spec_helper'
2
+
3
+ module Darkholme
4
+ describe Entity do
5
+ subject { MockEntity.new }
6
+ let(:engine) { Engine.new }
7
+ let(:system) { MockSystem.new }
8
+ let(:component) { MockComponent.new }
9
+
10
+ describe "with callbacks" do
11
+ it "sets self.engine when added to an engine" do
12
+ subject.added_to_engine(engine)
13
+
14
+ expect(subject.engine).to eq(engine)
15
+ end
16
+
17
+ it "clears self.engine when removed from an engine" do
18
+ subject.engine = engine
19
+ expect(subject.engine).to eq(engine)
20
+ subject.removed_from_engine(engine)
21
+
22
+ expect(subject.engine).to be_nil
23
+ end
24
+ end
25
+
26
+ describe "with components" do
27
+ describe "adding one" do
28
+ it "makes has_component? return true" do
29
+ subject.add_component(component)
30
+ expect(subject.has_component?(component.class)).to eq(true)
31
+ end
32
+
33
+ it "returns the component" do
34
+ added = subject.add_component(component)
35
+ expect(added).to eq(component)
36
+ end
37
+
38
+ it "sets the associated component bit" do
39
+ bits = subject.component_bits
40
+ expect(bits).to receive(:set).with(component.bit)
41
+
42
+ subject.add_component(component)
43
+ end
44
+ end
45
+
46
+ describe "removing one" do
47
+ let!(:added) { subject.add_component(component) }
48
+
49
+ it "makes has_component? return false" do
50
+ subject.remove_component(component.class)
51
+ expect(subject.has_component?(component.class)).to eq(false)
52
+ end
53
+
54
+ it "returns the component" do
55
+ removed = subject.remove_component(component.class)
56
+ expect(removed).to eq(component)
57
+ end
58
+
59
+ it "clears the associated component bit" do
60
+ bits = subject.component_bits
61
+ expect(bits).to receive(:clear).with(component.bit)
62
+
63
+ subject.remove_component(component.class)
64
+ end
65
+ end
66
+
67
+ it "returns a component instance when asked for one" do
68
+ subject.add_component(component)
69
+ expect(subject.component_for(component.class)).to be_a MockComponent
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ module Darkholme
4
+ describe Family do
5
+ subject { Family.for(MockComponent) }
6
+
7
+ describe ".for" do
8
+ it "creates a new family" do
9
+ expect(subject).to be_a Family
10
+ end
11
+
12
+ it "returns the same family if it exists" do
13
+ second_family = Family.for(MockComponent)
14
+ expect(second_family).to eq(subject)
15
+ end
16
+ end
17
+
18
+ describe "#member?" do
19
+ let(:entity) { MockEntity.new }
20
+ it "returns true if the component bits match" do
21
+ entity.add_component(MockComponent.new)
22
+ expect(subject.member?(entity)).to eq(true)
23
+ end
24
+
25
+ it "returns false if the bits don't match" do
26
+ expect(subject.member?(entity)).to eq(false)
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ module Darkholme
4
+ describe System do
5
+ subject { MockSystem.new }
6
+
7
+ describe ".has_family" do
8
+ it "sets the class-wide family" do
9
+ expect {
10
+ EmptySystem.has_family MockComponent
11
+ }.to change { EmptySystem.family }
12
+ .from(nil).to(an_instance_of Family)
13
+ end
14
+ end
15
+
16
+ it "raises on update" do
17
+ expect { subject.update(:delta) }.to raise_error NotImplementedError
18
+ end
19
+
20
+ describe "with callbacks" do
21
+ it "sets engine when added" do
22
+ subject.added_to_engine(:engine)
23
+ expect(subject.engine).to eq(:engine)
24
+ end
25
+
26
+ it "clears engine when removed if they match" do
27
+ subject.engine = :engine
28
+ subject.removed_from_engine(:engine)
29
+ expect(subject.engine).to be_nil
30
+ end
31
+
32
+ it "leaves engine alone when removed if they don't match" do
33
+ subject.engine = :no
34
+ subject.removed_from_engine(:engine)
35
+ expect(subject.engine).to eq(:no)
36
+ end
37
+ end
38
+
39
+ it "has entities" do
40
+ subject.engine = Engine.new
41
+ expect(subject.engine).to receive(:entities_for_family)
42
+ .with(subject.family)
43
+ .and_return([])
44
+
45
+ expect(subject.entities).to eq([])
46
+ end
47
+
48
+ it "has a family" do
49
+ expect(subject.family).to eq(subject.class.family)
50
+ end
51
+
52
+ private
53
+
54
+ class EmptySystem < System; end
55
+ end
56
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+
3
+ describe Darkholme do
4
+
5
+ end
@@ -0,0 +1,29 @@
1
+ require 'simplecov'
2
+
3
+ module SimpleCov::Configuration
4
+ def clean_filters
5
+ @filters = []
6
+ end
7
+ end
8
+
9
+ SimpleCov.configure do
10
+ clean_filters
11
+ load_profile 'test_frameworks'
12
+ end
13
+
14
+ ENV["COVERAGE"] && SimpleCov.start do
15
+ add_filter "/.rvm/"
16
+ end
17
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
18
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
19
+
20
+ require 'rspec'
21
+ require 'darkholme'
22
+
23
+ # Requires supporting files with custom matchers and macros, etc,
24
+ # in ./support/ and its subdirectories.
25
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f }
26
+
27
+ RSpec.configure do |config|
28
+
29
+ end
@@ -0,0 +1,9 @@
1
+ class MockEntity < Darkholme::Entity
2
+ end
3
+
4
+ class MockComponent < Darkholme::Component
5
+ end
6
+
7
+ class MockSystem < Darkholme::System
8
+ has_family MockComponent
9
+ end
metadata ADDED
@@ -0,0 +1,198 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: darkholme
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Massive Danger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.12.6
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.12.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 3.0.0.beta1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 3.0.0.beta1
41
+ - !ruby/object:Gem::Dependency
42
+ name: yard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '0.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '0.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rdoc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '3.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '3.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '1.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: jeweler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: 2.0.1
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: 2.0.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: guard-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 4.2.6
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: 4.2.6
125
+ - !ruby/object:Gem::Dependency
126
+ name: inch
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ~>
130
+ - !ruby/object:Gem::Version
131
+ version: 0.2.3
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ~>
137
+ - !ruby/object:Gem::Version
138
+ version: 0.2.3
139
+ description: An entity-component system in Ruby for libgdx
140
+ email: evan@massivedanger.com
141
+ executables: []
142
+ extensions: []
143
+ extra_rdoc_files:
144
+ - LICENSE.txt
145
+ - README.md
146
+ files:
147
+ - .document
148
+ - .rspec
149
+ - .travis.yml
150
+ - Gemfile
151
+ - Gemfile.lock
152
+ - Guardfile
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - VERSION
157
+ - darkholme.gemspec
158
+ - lib/darkholme.rb
159
+ - lib/darkholme/bitset.rb
160
+ - lib/darkholme/component.rb
161
+ - lib/darkholme/engine.rb
162
+ - lib/darkholme/entity.rb
163
+ - lib/darkholme/family.rb
164
+ - lib/darkholme/system.rb
165
+ - spec/lib/darkholme/bitset_spec.rb
166
+ - spec/lib/darkholme/component_spec.rb
167
+ - spec/lib/darkholme/engine_spec.rb
168
+ - spec/lib/darkholme/entity_spec.rb
169
+ - spec/lib/darkholme/family_spec.rb
170
+ - spec/lib/darkholme/system_spec.rb
171
+ - spec/lib/darkholme_spec.rb
172
+ - spec/spec_helper.rb
173
+ - spec/support/mock_classes.rb
174
+ homepage: http://github.com/evanwalsh/darkholme
175
+ licenses:
176
+ - MIT
177
+ metadata: {}
178
+ post_install_message:
179
+ rdoc_options: []
180
+ require_paths:
181
+ - lib
182
+ required_ruby_version: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - '>='
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ required_rubygems_version: !ruby/object:Gem::Requirement
188
+ requirements:
189
+ - - '>='
190
+ - !ruby/object:Gem::Version
191
+ version: '0'
192
+ requirements: []
193
+ rubyforge_project:
194
+ rubygems_version: 2.1.11
195
+ signing_key:
196
+ specification_version: 4
197
+ summary: An entity-component system in Ruby for libgdx
198
+ test_files: []