draco 0.3.2 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +1 -2
- data/.gitignore +2 -0
- data/.rubocop.yml +1 -0
- data/CHANGELOG.md +30 -0
- data/Gemfile.lock +18 -16
- data/README.md +55 -3
- data/draco.gemspec +1 -1
- data/lib/draco.rb +280 -42
- data/lib/draco/benchmark.rb +53 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c330a2195d1f7b17f0bb1be16914cef57ccfcf5094ed034cf50a64a3abae52a
|
4
|
+
data.tar.gz: efa72ad0c7facd4c3143f858a07adc5be8e6b225635d54b4b60fc983343f33be
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b5223959feeb20099bca35233f704b91270757ed33d1f59cd3091e6ea8793bb28a3cc4467670ac305931f85c5bb12fe036ac2ae2655aa8c597e5d02ff3d19993
|
7
|
+
data.tar.gz: cb182bfbcbe2b69fc5a84adf2aceabc1dddc7d75c2159cb4b13fcfb4f3f3ead4659dd1d6e114152c8ba61c8d8100672fa2672b65b267feb413ff0aece91caf42
|
data/.github/workflows/ruby.yml
CHANGED
@@ -22,8 +22,7 @@ jobs:
|
|
22
22
|
- name: Set up Ruby
|
23
23
|
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
24
24
|
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
25
|
-
|
26
|
-
uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
|
25
|
+
uses: ruby/setup-ruby@v1
|
27
26
|
with:
|
28
27
|
ruby-version: 2.6
|
29
28
|
- name: Install dependencies
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
## 0.6.0
|
2
|
+
|
3
|
+
Features:
|
4
|
+
|
5
|
+
- `#serialize` now includes the class name for Entities, Components, and Systems.
|
6
|
+
- Added callbacks to Entities, Components, Systems and Worlds to better enable plugins.
|
7
|
+
- Added a sample plugin to `lib/draco/benchmark.rb` to benchmark your systems.
|
8
|
+
|
9
|
+
How to use:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
class World < Draco::World
|
13
|
+
include Draco::Benchmark
|
14
|
+
end
|
15
|
+
|
16
|
+
world.system_timers
|
17
|
+
# => { :system => 0.001 }
|
18
|
+
```
|
19
|
+
|
20
|
+
## 0.5.1 (December 31, 2020)
|
21
|
+
|
22
|
+
Features:
|
23
|
+
|
24
|
+
- Add an alias to `Draco::Tag` in Entities and Systems.
|
25
|
+
|
26
|
+
## 0.5.0 (December 31, 2020)
|
27
|
+
|
28
|
+
Features:
|
29
|
+
|
30
|
+
- Add `Draco::Tag` to easily define tag components [#6](https://github.com/guitsaru/draco/pull/6)
|
data/Gemfile.lock
CHANGED
@@ -3,43 +3,45 @@ GEM
|
|
3
3
|
specs:
|
4
4
|
ast (2.4.1)
|
5
5
|
diff-lcs (1.4.4)
|
6
|
-
docile (1.3.
|
7
|
-
parallel (1.
|
8
|
-
parser (
|
6
|
+
docile (1.3.4)
|
7
|
+
parallel (1.20.1)
|
8
|
+
parser (3.0.0.0)
|
9
9
|
ast (~> 2.4.1)
|
10
10
|
rainbow (3.0.0)
|
11
11
|
rake (12.3.3)
|
12
|
-
regexp_parser (
|
12
|
+
regexp_parser (2.0.3)
|
13
13
|
rexml (3.2.4)
|
14
14
|
rspec (3.10.0)
|
15
15
|
rspec-core (~> 3.10.0)
|
16
16
|
rspec-expectations (~> 3.10.0)
|
17
17
|
rspec-mocks (~> 3.10.0)
|
18
|
-
rspec-core (3.10.
|
18
|
+
rspec-core (3.10.1)
|
19
19
|
rspec-support (~> 3.10.0)
|
20
|
-
rspec-expectations (3.10.
|
20
|
+
rspec-expectations (3.10.1)
|
21
21
|
diff-lcs (>= 1.2.0, < 2.0)
|
22
22
|
rspec-support (~> 3.10.0)
|
23
|
-
rspec-mocks (3.10.
|
23
|
+
rspec-mocks (3.10.1)
|
24
24
|
diff-lcs (>= 1.2.0, < 2.0)
|
25
25
|
rspec-support (~> 3.10.0)
|
26
|
-
rspec-support (3.10.
|
27
|
-
rubocop (1.
|
26
|
+
rspec-support (3.10.1)
|
27
|
+
rubocop (1.7.0)
|
28
28
|
parallel (~> 1.10)
|
29
29
|
parser (>= 2.7.1.5)
|
30
30
|
rainbow (>= 2.2.2, < 4.0)
|
31
|
-
regexp_parser (>= 1.8)
|
31
|
+
regexp_parser (>= 1.8, < 3.0)
|
32
32
|
rexml
|
33
|
-
rubocop-ast (>= 1.0.
|
33
|
+
rubocop-ast (>= 1.2.0, < 2.0)
|
34
34
|
ruby-progressbar (~> 1.7)
|
35
35
|
unicode-display_width (>= 1.4.0, < 2.0)
|
36
|
-
rubocop-ast (1.
|
36
|
+
rubocop-ast (1.4.0)
|
37
37
|
parser (>= 2.7.1.5)
|
38
|
-
ruby-progressbar (1.
|
39
|
-
simplecov (0.
|
38
|
+
ruby-progressbar (1.11.0)
|
39
|
+
simplecov (0.21.0)
|
40
40
|
docile (~> 1.1)
|
41
41
|
simplecov-html (~> 0.11)
|
42
|
-
|
42
|
+
simplecov_json_formatter (~> 0.1)
|
43
|
+
simplecov-html (0.12.3)
|
44
|
+
simplecov_json_formatter (0.1.2)
|
43
45
|
unicode-display_width (1.7.0)
|
44
46
|
|
45
47
|
PLATFORMS
|
@@ -52,4 +54,4 @@ DEPENDENCIES
|
|
52
54
|
simplecov
|
53
55
|
|
54
56
|
BUNDLED WITH
|
55
|
-
2.
|
57
|
+
2.2.3
|
data/README.md
CHANGED
@@ -6,12 +6,39 @@ An Entity Component System is an architectural framework that decouples game obj
|
|
6
6
|
build game objects through composition. This allows you to easily share small logic components between different game
|
7
7
|
objects.
|
8
8
|
|
9
|
+
## Sample Application
|
10
|
+
|
11
|
+
This repository includes sample applications in the `samples/` directory.
|
12
|
+
|
13
|
+
* teeny-tiny - A 20 second space shooter created during the DragonRuby TeenyTiny Game Jam
|
14
|
+
* gorillas-basic - A recreation of the gorillas_basic sample game that comes with DragonRuby
|
15
|
+
|
16
|
+
### Running the sample application
|
17
|
+
|
18
|
+
1. Download the [latest release](https://github.com/guitsaru/draco/archive/main.zip) of this repository.
|
19
|
+
2. Create a copy of DragonRuby GTK in a new folder.
|
20
|
+
3. Copy the teeny-tiny directory from draco into your new DragonRuby GTK folder. `cp -r draco/samples/ dragonruby/`.
|
21
|
+
4. Run them using `./dragonruby teeny-tiny` or `./dragonruby gorillas-basic`.
|
22
|
+
|
9
23
|
## Installation
|
10
24
|
|
11
25
|
1. Create a `lib` directory inside your game's `app` directory.
|
12
26
|
2. Copy `lib/draco.rb` into your new `lib` directory.
|
13
27
|
3. In your `main.rb` file, require `app/lib/draco.rb`.
|
14
28
|
|
29
|
+
## Support
|
30
|
+
|
31
|
+
- Find a bug? [Open an issue](https://github.com/guitsaru/draco/issues).
|
32
|
+
- Need help? [Start a Discussion](https://github.com/guitsaru/draco/discussions) or [Join us in Discord: Channel #oss-draco](https://discord.gg/vPUNtwfm).
|
33
|
+
|
34
|
+
## Versioning
|
35
|
+
|
36
|
+
Draco uses [https://semver.org/].
|
37
|
+
|
38
|
+
* Major (X.y.z) - Incremented for any backwards incompatible public API changes.
|
39
|
+
* Minor (x.Y.z) - Incremented for new, backwards compatible, public API enhancements/fixes.
|
40
|
+
* Patch (x.y.Z) - Incremented for small, backwards compatible, bug fixes.
|
41
|
+
|
15
42
|
## Usage
|
16
43
|
|
17
44
|
### Components
|
@@ -23,7 +50,7 @@ These can be shared across many different types of game objects.
|
|
23
50
|
class Visible < Draco::Component; end
|
24
51
|
```
|
25
52
|
|
26
|
-
`Visible` is an example of a
|
53
|
+
`Visible` is an example of a tag component. An entity either has it, or it doesn't. We can also associate data with our
|
27
54
|
components.
|
28
55
|
|
29
56
|
```ruby
|
@@ -44,6 +71,16 @@ component.x
|
|
44
71
|
# => 110
|
45
72
|
```
|
46
73
|
|
74
|
+
#### Tag Components
|
75
|
+
|
76
|
+
The `Visible` class above is an example of a tag component. These are common enough that we don't necessarily want to
|
77
|
+
define a bunch of empty component classes. Draco provides a way to generate these classes at runtime.
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
Draco::Tag(:visible)
|
81
|
+
# => Visible
|
82
|
+
```
|
83
|
+
|
47
84
|
### Entities
|
48
85
|
|
49
86
|
Entities are independant game objects. They consist of a unique id and a list of components.
|
@@ -58,7 +95,7 @@ Often we have types of entities that are reused throughout the game. We can defi
|
|
58
95
|
```ruby
|
59
96
|
class Goblin < Draco::Entity
|
60
97
|
component Position, x: 50, y: 50
|
61
|
-
component
|
98
|
+
component Tag(:visible)
|
62
99
|
end
|
63
100
|
|
64
101
|
goblin = Goblin.new
|
@@ -89,7 +126,7 @@ entities that include all of the given components.
|
|
89
126
|
|
90
127
|
```ruby
|
91
128
|
class RenderSpriteSystem < Draco::System
|
92
|
-
filter
|
129
|
+
filter Tag(:visible), Position, Sprite
|
93
130
|
|
94
131
|
def tick(args)
|
95
132
|
# You can also access the world that called the system.
|
@@ -150,6 +187,19 @@ player entity has been given the name `player`. We can now access this directly
|
|
150
187
|
world.player
|
151
188
|
```
|
152
189
|
|
190
|
+
### Fetching entities by id
|
191
|
+
|
192
|
+
In some cases you'll want to keep track of entities by their id, such as when you want to keep track of another entity in a component.
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
entity = Player.new
|
196
|
+
entity.id
|
197
|
+
# => 12
|
198
|
+
|
199
|
+
world.entities[12] == entity
|
200
|
+
# => true
|
201
|
+
```
|
202
|
+
|
153
203
|
## Learn More
|
154
204
|
|
155
205
|
Here are some good resources to learn about Entity Component Systems
|
@@ -161,6 +211,8 @@ Here are some good resources to learn about Entity Component Systems
|
|
161
211
|
|
162
212
|
Draco is licensed under AGPL. You can purchase the right to use Draco under the [commercial license](https://github.com/guitsaru/draco/blob/master/COMM-LICENSE).
|
163
213
|
|
214
|
+
Each purchase comes with free upgrades for the current major version of Draco.
|
215
|
+
|
164
216
|
<a class="gumroad-button" href="https://guitsaru.itch.io/draco" target="_blank">Purchase Commercial License</a>
|
165
217
|
|
166
218
|
## Development
|
data/draco.gemspec
CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
|
|
19
19
|
# Specify which files should be added to the gem when it is released.
|
20
20
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
21
21
|
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
22
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|samples)/}) }
|
23
23
|
end
|
24
24
|
spec.bindir = "exe"
|
25
25
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
data/lib/draco.rb
CHANGED
@@ -4,8 +4,10 @@
|
|
4
4
|
#
|
5
5
|
# An Entity Component System is an architectural pattern used in game development to decouple behavior from objects.
|
6
6
|
module Draco
|
7
|
+
class NotAComponentError < StandardError; end
|
8
|
+
|
7
9
|
# Public: The version of the library. Draco uses semver to version releases.
|
8
|
-
VERSION = "0.
|
10
|
+
VERSION = "0.6.0"
|
9
11
|
|
10
12
|
# Public: A general purpose game object that consists of a unique id and a collection of Components.
|
11
13
|
class Entity
|
@@ -39,6 +41,15 @@ module Draco
|
|
39
41
|
@default_components[component] = defaults
|
40
42
|
end
|
41
43
|
|
44
|
+
# Public: Creates a tag Component. If the tag already exists, return it.
|
45
|
+
#
|
46
|
+
# name - The string or symbol name of the component.
|
47
|
+
#
|
48
|
+
# Returns a class with subclass Draco::Component.
|
49
|
+
def self.Tag(name) # rubocop:disable Naming/MethodName
|
50
|
+
Draco::Tag(name)
|
51
|
+
end
|
52
|
+
|
42
53
|
class << self
|
43
54
|
# Internal: Returns the default components for the class.
|
44
55
|
attr_reader :default_components
|
@@ -64,15 +75,33 @@ module Draco
|
|
64
75
|
def initialize(args = {})
|
65
76
|
@id = args.fetch(:id, @@next_id)
|
66
77
|
@@next_id = [@id + 1, @@next_id].max
|
67
|
-
@components = ComponentStore.new(self)
|
68
78
|
@subscriptions = []
|
69
79
|
|
80
|
+
setup_components(args)
|
81
|
+
after_initialize
|
82
|
+
end
|
83
|
+
|
84
|
+
# Internal: Sets up the default components for the class.
|
85
|
+
#
|
86
|
+
# args - A hash of arguments to pass into the generated components.
|
87
|
+
#
|
88
|
+
# Returns nothing.
|
89
|
+
def setup_components(args)
|
90
|
+
@components = ComponentStore.new(self)
|
91
|
+
|
70
92
|
self.class.default_components.each do |component, default_args|
|
71
93
|
arguments = default_args.merge(args[Draco.underscore(component.name.to_s).to_sym] || {})
|
72
94
|
@components << component.new(arguments)
|
73
95
|
end
|
74
96
|
end
|
75
97
|
|
98
|
+
# Public: Callback run after the entity is initialized.
|
99
|
+
#
|
100
|
+
# This is empty by default but is present to allow plugins to tie into.
|
101
|
+
#
|
102
|
+
# Returns nothing.
|
103
|
+
def after_initialize; end
|
104
|
+
|
76
105
|
# Public: Subscribe to an Entity's Component updates.
|
77
106
|
#
|
78
107
|
# subscriber - The object to notify when Components change.
|
@@ -82,18 +111,49 @@ module Draco
|
|
82
111
|
@subscriptions << subscriber
|
83
112
|
end
|
84
113
|
|
85
|
-
#
|
114
|
+
# Public: Callback run before a component is added.
|
86
115
|
#
|
87
|
-
#
|
88
|
-
|
89
|
-
|
116
|
+
# component - The component that will be added.
|
117
|
+
#
|
118
|
+
# Returns the component to add.
|
119
|
+
def before_component_added(component)
|
120
|
+
component
|
121
|
+
end
|
122
|
+
|
123
|
+
# Public: Callback run after a component is added.
|
124
|
+
#
|
125
|
+
# component - The component that was added.
|
126
|
+
#
|
127
|
+
# Returns the added component.
|
128
|
+
def after_component_added(component)
|
129
|
+
@subscriptions.each { |sub| sub.component_added(self, component) }
|
130
|
+
component
|
131
|
+
end
|
132
|
+
|
133
|
+
# Public: Callback run before a component is deleted.
|
134
|
+
#
|
135
|
+
# component - The component that will be removed.
|
136
|
+
#
|
137
|
+
# Returns the component to remove.
|
138
|
+
def before_component_removed(component)
|
139
|
+
component
|
140
|
+
end
|
141
|
+
|
142
|
+
# Public: Callback run after a component is deleted.
|
143
|
+
#
|
144
|
+
# component - The component that was removed.
|
145
|
+
#
|
146
|
+
# Returns the removed component.
|
147
|
+
def after_component_removed(component)
|
148
|
+
@subscriptions.each { |sub| sub.component_removed(self, component) }
|
149
|
+
component
|
90
150
|
end
|
91
151
|
|
92
152
|
# Public: Serializes the Entity to save the current state.
|
93
153
|
#
|
94
154
|
# Returns a Hash representing the Entity.
|
95
155
|
def serialize
|
96
|
-
serialized = { id: id }
|
156
|
+
serialized = { class: self.class.name.to_s, id: id }
|
97
157
|
|
98
158
|
components.each do |component|
|
99
159
|
serialized[Draco.underscore(component.class.name.to_s).to_sym] = component.serialize
|
@@ -165,7 +225,6 @@ module Draco
|
|
165
225
|
def <<(*components)
|
166
226
|
components.flatten.each { |component| add(component) }
|
167
227
|
|
168
|
-
@parent.components_updated
|
169
228
|
self
|
170
229
|
end
|
171
230
|
|
@@ -186,10 +245,16 @@ module Draco
|
|
186
245
|
#
|
187
246
|
# Returns the ComponentStore.
|
188
247
|
def add(component)
|
248
|
+
unless component.is_a?(Draco::Component)
|
249
|
+
message = component.is_a?(Class) ? " You might need to initialize the component before you add it." : ""
|
250
|
+
raise Draco::NotAComponentError, "The given value is not a component.#{message}"
|
251
|
+
end
|
252
|
+
|
253
|
+
component = @parent.before_component_added(component)
|
189
254
|
name = Draco.underscore(component.class.name.to_s).to_sym
|
190
255
|
@components[name] = component
|
256
|
+
@parent.after_component_added(component)
|
191
257
|
|
192
|
-
@parent.components_updated
|
193
258
|
self
|
194
259
|
end
|
195
260
|
|
@@ -201,10 +266,11 @@ module Draco
|
|
201
266
|
#
|
202
267
|
# Returns the ComponentStore.
|
203
268
|
def delete(component)
|
269
|
+
component = @parent.before_component_removed(component)
|
204
270
|
name = Draco.underscore(component.class.name.to_s).to_sym
|
205
271
|
@components.delete(name)
|
272
|
+
@parent.after_component_removed(component)
|
206
273
|
|
207
|
-
@parent.components_updated
|
208
274
|
self
|
209
275
|
end
|
210
276
|
|
@@ -255,6 +321,15 @@ module Draco
|
|
255
321
|
attr_reader :attribute_options
|
256
322
|
end
|
257
323
|
|
324
|
+
# Public: Creates a tag Component. If the tag already exists, return it.
|
325
|
+
#
|
326
|
+
# name - The string or symbol name of the component.
|
327
|
+
#
|
328
|
+
# Returns a class with subclass Draco::Component.
|
329
|
+
def self.Tag(name) # rubocop:disable Naming/MethodName
|
330
|
+
Draco::Tag(name)
|
331
|
+
end
|
332
|
+
|
258
333
|
# Public: Initializes a new Component.
|
259
334
|
#
|
260
335
|
# values - The Hash of values to set for the Component instance (default: {}).
|
@@ -273,13 +348,21 @@ module Draco
|
|
273
348
|
value = values.fetch(name.to_sym, options[:default])
|
274
349
|
instance_variable_set("@#{name}", value)
|
275
350
|
end
|
351
|
+
after_initialize
|
276
352
|
end
|
277
353
|
|
354
|
+
# Public: Callback run after the component is initialized.
|
355
|
+
#
|
356
|
+
# This is empty by default but is present to allow plugins to tie into.
|
357
|
+
#
|
358
|
+
# Returns nothing.
|
359
|
+
def after_initialize; end
|
360
|
+
|
278
361
|
# Public: Serializes the Component to save the current state.
|
279
362
|
#
|
280
363
|
# Returns a Hash representing the Component.
|
281
364
|
def serialize
|
282
|
-
attrs = {}
|
365
|
+
attrs = { class: self.class.name.to_s }
|
283
366
|
|
284
367
|
instance_variables.each do |attr|
|
285
368
|
name = attr.to_s.gsub("@", "").to_sym
|
@@ -300,6 +383,20 @@ module Draco
|
|
300
383
|
end
|
301
384
|
end
|
302
385
|
|
386
|
+
# Public: Creates a new empty component at runtime. If the given Class already exists, it reuses the existing Class.
|
387
|
+
#
|
388
|
+
# name - The symbol or string name of the component. It can be either camelcase or underscored.
|
389
|
+
#
|
390
|
+
# Returns a Class with superclass of Draco::Component.
|
391
|
+
def self.Tag(name) # rubocop:disable Naming/MethodName
|
392
|
+
klass_name = camelize(name)
|
393
|
+
|
394
|
+
return Object.const_get(klass_name) if Object.const_defined?(klass_name)
|
395
|
+
|
396
|
+
klass = Class.new(Component)
|
397
|
+
Object.const_set(klass_name, klass)
|
398
|
+
end
|
399
|
+
|
303
400
|
# Public: Systems contain the logic of the game.
|
304
401
|
# The System runs on each tick and manipulates the Entities in the World.
|
305
402
|
class System
|
@@ -332,6 +429,15 @@ module Draco
|
|
332
429
|
sub.instance_variable_set(:@filter, [])
|
333
430
|
end
|
334
431
|
|
432
|
+
# Public: Creates a tag Component. If the tag already exists, return it.
|
433
|
+
#
|
434
|
+
# name - The string or symbol name of the component.
|
435
|
+
#
|
436
|
+
# Returns a class with subclass Draco::Component.
|
437
|
+
def self.Tag(name) # rubocop:disable Naming/MethodName
|
438
|
+
Draco::Tag(name)
|
439
|
+
end
|
440
|
+
|
335
441
|
# Public: Initializes a new System.
|
336
442
|
#
|
337
443
|
# entities - The Entities to operate on (default: []).
|
@@ -339,8 +445,37 @@ module Draco
|
|
339
445
|
def initialize(entities: [], world: nil)
|
340
446
|
@entities = entities
|
341
447
|
@world = world
|
448
|
+
after_initialize
|
449
|
+
end
|
450
|
+
|
451
|
+
# Public: Callback run after the system is initialized.
|
452
|
+
#
|
453
|
+
# This is empty by default but is present to allow plugins to tie into.
|
454
|
+
#
|
455
|
+
# Returns nothing.
|
456
|
+
def after_initialize; end
|
457
|
+
|
458
|
+
# Public: Runs the system tick function.
|
459
|
+
#
|
460
|
+
# context - The context object of the current tick from the game engine. In DragonRuby this is `args`.
|
461
|
+
#
|
462
|
+
# Returns nothing.
|
463
|
+
def call(context)
|
464
|
+
before_tick(context)
|
465
|
+
tick(context)
|
466
|
+
after_tick(context)
|
467
|
+
self
|
342
468
|
end
|
343
469
|
|
470
|
+
# Public: Callback run before #tick is called.
|
471
|
+
#
|
472
|
+
# This is empty by default but is present to allow plugins to tie into.
|
473
|
+
#
|
474
|
+
# context - The context object of the current tick from the game engine. In DragonRuby this is `args`.
|
475
|
+
#
|
476
|
+
# Returns nothing.
|
477
|
+
def before_tick(context); end
|
478
|
+
|
344
479
|
# Public: Runs the System logic for the current game engine tick.
|
345
480
|
#
|
346
481
|
# This is where the logic is implemented and it should be overriden for each System.
|
@@ -350,11 +485,19 @@ module Draco
|
|
350
485
|
# Returns nothing
|
351
486
|
def tick(context); end
|
352
487
|
|
488
|
+
# Public: Callback run after #tick is called.
|
489
|
+
#
|
490
|
+
# This is empty by default but is present to allow plugins to tie into.
|
491
|
+
#
|
492
|
+
# Returns nothing.
|
493
|
+
def after_tick(context); end
|
494
|
+
|
353
495
|
# Public: Serializes the System to save the current state.
|
354
496
|
#
|
355
497
|
# Returns a Hash representing the System.
|
356
498
|
def serialize
|
357
499
|
{
|
500
|
+
class: self.class.name.to_s,
|
358
501
|
entities: entities.map(&:serialize),
|
359
502
|
world: world ? world.serialize : nil
|
360
503
|
}
|
@@ -373,7 +516,7 @@ module Draco
|
|
373
516
|
|
374
517
|
# Public: The container for current Entities and Systems.
|
375
518
|
class World
|
376
|
-
@default_entities =
|
519
|
+
@default_entities = []
|
377
520
|
@default_systems = []
|
378
521
|
|
379
522
|
# Internal: Resets the default components for each class that inherites Entity.
|
@@ -383,7 +526,7 @@ module Draco
|
|
383
526
|
# Returns nothing.
|
384
527
|
def self.inherited(sub)
|
385
528
|
super
|
386
|
-
sub.instance_variable_set(:@default_entities,
|
529
|
+
sub.instance_variable_set(:@default_entities, [])
|
387
530
|
sub.instance_variable_set(:@default_systems, [])
|
388
531
|
end
|
389
532
|
|
@@ -401,7 +544,7 @@ module Draco
|
|
401
544
|
# Returns nothing.
|
402
545
|
def self.entity(entity, defaults = {})
|
403
546
|
name = defaults[:as]
|
404
|
-
@default_entities[entity
|
547
|
+
@default_entities.push([entity, defaults])
|
405
548
|
|
406
549
|
attr_reader(name.to_sym) if name
|
407
550
|
end
|
@@ -440,7 +583,8 @@ module Draco
|
|
440
583
|
# entities - The Array of Entities for the World (default: []).
|
441
584
|
# systems - The Array of System Classes for the World (default: []).
|
442
585
|
def initialize(entities: [], systems: [])
|
443
|
-
default_entities = self.class.default_entities.map do |
|
586
|
+
default_entities = self.class.default_entities.map do |default|
|
587
|
+
klass, attributes = default
|
444
588
|
name = attributes[:as]
|
445
589
|
entity = klass.new(attributes)
|
446
590
|
instance_variable_set("@#{name}", entity) if name
|
@@ -448,8 +592,29 @@ module Draco
|
|
448
592
|
entity
|
449
593
|
end
|
450
594
|
|
451
|
-
@entities = EntityStore.new(default_entities + entities)
|
595
|
+
@entities = EntityStore.new(self, default_entities + entities)
|
452
596
|
@systems = self.class.default_systems + systems
|
597
|
+
after_initialize
|
598
|
+
end
|
599
|
+
|
600
|
+
# Public: Callback run after the world is initialized.
|
601
|
+
#
|
602
|
+
# This is empty by default but is present to allow plugins to tie into.
|
603
|
+
#
|
604
|
+
# Returns nothing.
|
605
|
+
def after_initialize; end
|
606
|
+
|
607
|
+
# Public: Callback run before #tick is called.
|
608
|
+
#
|
609
|
+
# context - The context object of the current tick from the game engine. In DragonRuby this is `args`.
|
610
|
+
#
|
611
|
+
# Returns the systems to run during this tick.
|
612
|
+
def before_tick(_context)
|
613
|
+
systems.map do |system|
|
614
|
+
entities = filter(system.filter)
|
615
|
+
|
616
|
+
system.new(entities: entities, world: self)
|
617
|
+
end
|
453
618
|
end
|
454
619
|
|
455
620
|
# Public: Runs all of the Systems every tick.
|
@@ -458,13 +623,39 @@ module Draco
|
|
458
623
|
#
|
459
624
|
# Returns nothing
|
460
625
|
def tick(context)
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
system.new(entities: entities, world: self).tick(context)
|
626
|
+
results = before_tick(context).map do |system|
|
627
|
+
system.call(context)
|
465
628
|
end
|
629
|
+
|
630
|
+
after_tick(context, results)
|
466
631
|
end
|
467
632
|
|
633
|
+
# Public: Callback run after #tick is called.
|
634
|
+
#
|
635
|
+
# This is empty by default but is present to allow plugins to tie into.
|
636
|
+
#
|
637
|
+
# context - The context object of the current tick from the game engine. In DragonRuby this is `args`.
|
638
|
+
# results - The System instances that were run.
|
639
|
+
#
|
640
|
+
# Returns nothing.
|
641
|
+
def after_tick(context, results); end
|
642
|
+
|
643
|
+
# Public: Callback to run when a component is added to an existing Entity.
|
644
|
+
#
|
645
|
+
# entity - The Entity the Component was added to.
|
646
|
+
# component - The Component that was added to the Entity.
|
647
|
+
#
|
648
|
+
# Returns nothing.
|
649
|
+
def component_added(entity, component); end
|
650
|
+
|
651
|
+
# Public: Callback to run when a component is added to an existing Entity.
|
652
|
+
#
|
653
|
+
# entity - The Entity the Component was removed from.
|
654
|
+
# component - The Component that was removed from the Entity.
|
655
|
+
#
|
656
|
+
# Returns nothing.
|
657
|
+
def component_removed(entity, component); end
|
658
|
+
|
468
659
|
# Public: Finds all Entities that contain all of the given Components.
|
469
660
|
#
|
470
661
|
# components - An Array of Component classes to match.
|
@@ -479,6 +670,7 @@ module Draco
|
|
479
670
|
# Returns a Hash representing the World.
|
480
671
|
def serialize
|
481
672
|
{
|
673
|
+
class: self.class.name.to_s,
|
482
674
|
entities: @entities.map(&:serialize),
|
483
675
|
systems: @systems.map { |system| system.name.to_s }
|
484
676
|
}
|
@@ -498,28 +690,45 @@ module Draco
|
|
498
690
|
class EntityStore
|
499
691
|
include Enumerable
|
500
692
|
|
693
|
+
attr_reader :parent
|
694
|
+
|
501
695
|
# Internal: Initializes a new EntityStore
|
502
696
|
#
|
503
697
|
# entities - The Entities to add to the EntityStore
|
504
|
-
def initialize(*entities)
|
698
|
+
def initialize(parent, *entities)
|
699
|
+
@parent = parent
|
505
700
|
@entity_to_components = Hash.new { |hash, key| hash[key] = Set.new }
|
506
701
|
@component_to_entities = Hash.new { |hash, key| hash[key] = Set.new }
|
702
|
+
@entity_ids = {}
|
507
703
|
|
508
704
|
self << entities
|
509
705
|
end
|
510
706
|
|
511
|
-
# Internal: Gets all Entities that implement all of the given Components
|
707
|
+
# Internal: Gets all Entities that implement all of the given Components or that match the given entity ids.
|
512
708
|
#
|
513
|
-
#
|
709
|
+
# components_or_ids - The Component Classes to filter by
|
514
710
|
#
|
515
711
|
# Returns a Set list of Entities
|
516
|
-
def [](*
|
517
|
-
|
712
|
+
def [](*components_or_ids)
|
713
|
+
components_or_ids
|
518
714
|
.flatten
|
519
|
-
.map { |
|
715
|
+
.map { |component_or_id| select_entities(component_or_id) }
|
520
716
|
.reduce { |acc, i| i & acc }
|
521
717
|
end
|
522
718
|
|
719
|
+
# Internal: Gets entities by component or id.
|
720
|
+
#
|
721
|
+
# component_or_id - The Component Class or entity id to select.
|
722
|
+
#
|
723
|
+
# Returns an Array of Entities.
|
724
|
+
def select_entities(component_or_id)
|
725
|
+
if component_or_id.is_a?(Numeric)
|
726
|
+
Array(@entity_ids[component_or_id])
|
727
|
+
else
|
728
|
+
@component_to_entities[component_or_id]
|
729
|
+
end
|
730
|
+
end
|
731
|
+
|
523
732
|
# Internal: Adds Entities to the EntityStore
|
524
733
|
#
|
525
734
|
# entities - The Entity or Array list of Entities to add to the EntityStore.
|
@@ -538,12 +747,12 @@ module Draco
|
|
538
747
|
def add(entity)
|
539
748
|
entity.subscribe(self)
|
540
749
|
|
750
|
+
@entity_ids[entity.id] = entity
|
541
751
|
components = entity.components.map(&:class)
|
542
752
|
@entity_to_components[entity].merge(components)
|
543
753
|
|
544
|
-
components.each
|
545
|
-
|
546
|
-
end
|
754
|
+
components.each { |component| @component_to_entities[component].add(entity) }
|
755
|
+
entity.components.each { |component| @parent.component_added(entity, component) }
|
547
756
|
|
548
757
|
self
|
549
758
|
end
|
@@ -554,6 +763,7 @@ module Draco
|
|
554
763
|
#
|
555
764
|
# Returns the EntityStore
|
556
765
|
def delete(entity)
|
766
|
+
@entity_ids.delete(entity.id)
|
557
767
|
components = Array(@entity_to_components.delete(entity))
|
558
768
|
|
559
769
|
components.each do |component|
|
@@ -571,21 +781,26 @@ module Draco
|
|
571
781
|
@entity_to_components.keys.each(&block)
|
572
782
|
end
|
573
783
|
|
574
|
-
# Internal: Updates the EntityStore when an Entity's Components are
|
784
|
+
# Internal: Updates the EntityStore when an Entity's Components are added.
|
575
785
|
#
|
576
|
-
# entity - The Entity
|
786
|
+
# entity - The Entity the Component was added to.
|
787
|
+
# component - The Component that was added to the Entity.
|
577
788
|
#
|
578
789
|
# Returns nothing.
|
579
|
-
def
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
added = components - old
|
585
|
-
deleted = old - components
|
790
|
+
def component_added(entity, component)
|
791
|
+
@component_to_entities[component.class].add(entity)
|
792
|
+
@parent.component_added(entity, component)
|
793
|
+
end
|
586
794
|
|
587
|
-
|
588
|
-
|
795
|
+
# Internal: Updates the EntityStore when an Entity's Components are removed.
|
796
|
+
#
|
797
|
+
# entity - The Entity the Component was removed from.
|
798
|
+
# component - The Component that was removed from the Entity.
|
799
|
+
#
|
800
|
+
# Returns nothing.
|
801
|
+
def component_removed(entity, component)
|
802
|
+
@component_to_entities[component.class].delete(entity)
|
803
|
+
@parent.component_removed(entity, component)
|
589
804
|
end
|
590
805
|
end
|
591
806
|
end
|
@@ -706,13 +921,36 @@ module Draco
|
|
706
921
|
#
|
707
922
|
# Returns a String.
|
708
923
|
def self.underscore(string)
|
709
|
-
string.split("::").last.bytes.map.with_index do |byte, i|
|
924
|
+
string.to_s.split("::").last.bytes.map.with_index do |byte, i|
|
710
925
|
if byte > 64 && byte < 97
|
711
|
-
downcased = byte + 32
|
926
|
+
downcased = byte + 32
|
712
927
|
i.zero? ? downcased.chr : "_#{downcased.chr}"
|
713
928
|
else
|
714
929
|
byte.chr
|
715
930
|
end
|
716
931
|
end.join
|
717
932
|
end
|
933
|
+
|
934
|
+
# Internal: Converts an underscored string into a camel case string.
|
935
|
+
#
|
936
|
+
# Examples
|
937
|
+
#
|
938
|
+
# camlize("camel_case")
|
939
|
+
# # => "CamelCase"
|
940
|
+
#
|
941
|
+
# Returns a string.
|
942
|
+
def self.camelize(string) # rubocop:disable Metrics/MethodLength
|
943
|
+
modifier = -32
|
944
|
+
|
945
|
+
string.to_s.bytes.map do |byte|
|
946
|
+
if byte == 95
|
947
|
+
modifier = -32
|
948
|
+
nil
|
949
|
+
else
|
950
|
+
char = (byte + modifier).chr
|
951
|
+
modifier = 0
|
952
|
+
char
|
953
|
+
end
|
954
|
+
end.compact.join
|
955
|
+
end
|
718
956
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Draco
|
4
|
+
# Public: Calculates the average time of
|
5
|
+
module Benchmark
|
6
|
+
def self.included(mod)
|
7
|
+
mod.prepend(World)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Internal: Plugin implementation for World.
|
11
|
+
module World
|
12
|
+
def before_tick(context)
|
13
|
+
super
|
14
|
+
@system_timer_data = Hash.new { |h, k| h[k] = [] }
|
15
|
+
@systems.each { |system| system.prepend(System) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def after_tick(context, results)
|
19
|
+
super
|
20
|
+
results.each do |system|
|
21
|
+
name = Draco.underscore(system.class.name.to_s).to_sym
|
22
|
+
@system_timer_data[name] = (system.timer * 1000.0).round(5)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def system_timers
|
27
|
+
@system_timer_data.sort_by { |_k, v| -v }.to_h
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Internal: Plugin implementation for System.
|
32
|
+
module System
|
33
|
+
def after_initialize
|
34
|
+
super
|
35
|
+
@timer = 0
|
36
|
+
end
|
37
|
+
|
38
|
+
def before_tick(context)
|
39
|
+
super
|
40
|
+
@start_time = Time.now
|
41
|
+
end
|
42
|
+
|
43
|
+
def after_tick(context)
|
44
|
+
super
|
45
|
+
@timer = Time.now - @start_time
|
46
|
+
end
|
47
|
+
|
48
|
+
def timer
|
49
|
+
@timer
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: draco
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matt Pruitt
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-05 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A library for Entities, Components, and Systems in games.
|
14
14
|
email:
|
@@ -22,6 +22,7 @@ files:
|
|
22
22
|
- ".gitignore"
|
23
23
|
- ".rspec"
|
24
24
|
- ".rubocop.yml"
|
25
|
+
- CHANGELOG.md
|
25
26
|
- CODE_OF_CONDUCT.md
|
26
27
|
- COMM-LICENSE
|
27
28
|
- Gemfile
|
@@ -34,6 +35,7 @@ files:
|
|
34
35
|
- bin/setup
|
35
36
|
- draco.gemspec
|
36
37
|
- lib/draco.rb
|
38
|
+
- lib/draco/benchmark.rb
|
37
39
|
homepage: https://github.com/guitsaru/draco
|
38
40
|
licenses: []
|
39
41
|
metadata:
|
@@ -54,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
56
|
- !ruby/object:Gem::Version
|
55
57
|
version: '0'
|
56
58
|
requirements: []
|
57
|
-
rubygems_version: 3.
|
59
|
+
rubygems_version: 3.2.3
|
58
60
|
signing_key:
|
59
61
|
specification_version: 4
|
60
62
|
summary: An ECS library.
|