draco 0.2.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -0
- data/lib/draco.rb +68 -4
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f135d47efafef38fd731f870c963e9764f113317f1156ca3c93b10a99a87cdb3
|
4
|
+
data.tar.gz: c8ef07e0313011c1333886410fe43f68804da261b74b1d197742824f07c2e907
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8858f4d209549cb36b23aa300f18bde9548dfcd56cd1519a61d8a04c4f32c309e5453805358733773a9f63d0cbc8e63c0e9c21bc909acd34d3d272f549b6679
|
7
|
+
data.tar.gz: 175629d4ac05169ed4bfe393eff7507d0dd0e7afa62c112a464dd30d3c97876233fa6e93b51af1ecc1ccd2cb22b5b708ffc96cb1e1517232164d61b38532fa46
|
data/README.md
CHANGED
@@ -129,6 +129,27 @@ world.systems << RenderSpriteSystem
|
|
129
129
|
world.tick(args)
|
130
130
|
```
|
131
131
|
|
132
|
+
Just like with entities, we can define a subclassed template for our world.
|
133
|
+
|
134
|
+
```ruby
|
135
|
+
class Overworld < Draco::World
|
136
|
+
entity Goblin
|
137
|
+
entity Player, position: { x: 50, y: 50 }, as: :player
|
138
|
+
systems RenderSpriteSystem, InputSystem
|
139
|
+
end
|
140
|
+
|
141
|
+
world = Overworld.new
|
142
|
+
```
|
143
|
+
|
144
|
+
### Named Entities
|
145
|
+
|
146
|
+
If there are entities that are frequently accessed in our systems, we can give these a name. In the above example, our
|
147
|
+
player entity has been given the name `player`. We can now access this directly from our world:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
world.player
|
151
|
+
```
|
152
|
+
|
132
153
|
## Learn More
|
133
154
|
|
134
155
|
Here are some good resources to learn about Entity Component Systems
|
data/lib/draco.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
# An Entity Component System is an architectural pattern used in game development to decouple behavior from objects.
|
6
6
|
module Draco
|
7
7
|
# Public: The version of the library. Draco uses semver to version releases.
|
8
|
-
VERSION = "0.
|
8
|
+
VERSION = "0.3.1"
|
9
9
|
|
10
10
|
# Public: A general purpose game object that consists of a unique id and a collection of Components.
|
11
11
|
class Entity
|
@@ -39,8 +39,8 @@ module Draco
|
|
39
39
|
@default_components[component] = defaults
|
40
40
|
end
|
41
41
|
|
42
|
-
# Internal: Returns the default components for the class.
|
43
42
|
class << self
|
43
|
+
# Internal: Returns the default components for the class.
|
44
44
|
attr_reader :default_components
|
45
45
|
end
|
46
46
|
|
@@ -373,6 +373,62 @@ module Draco
|
|
373
373
|
|
374
374
|
# Public: The container for current Entities and Systems.
|
375
375
|
class World
|
376
|
+
@default_entities = {}
|
377
|
+
@default_systems = []
|
378
|
+
|
379
|
+
# Internal: Resets the default components for each class that inherites Entity.
|
380
|
+
#
|
381
|
+
# sub - The class that is inheriting Entity.
|
382
|
+
#
|
383
|
+
# Returns nothing.
|
384
|
+
def self.inherited(sub)
|
385
|
+
super
|
386
|
+
sub.instance_variable_set(:@default_entities, {})
|
387
|
+
sub.instance_variable_set(:@default_systems, [])
|
388
|
+
end
|
389
|
+
|
390
|
+
# Public: Adds a default Entity to the World.
|
391
|
+
#
|
392
|
+
# entity - The class of the Entity to add by default.
|
393
|
+
# defaults - The Hash of default values for the Entity. (default: {})
|
394
|
+
#
|
395
|
+
# Examples
|
396
|
+
#
|
397
|
+
# entity(Player)
|
398
|
+
#
|
399
|
+
# entity(Player, position: { x: 0, y: 0 })
|
400
|
+
#
|
401
|
+
# Returns nothing.
|
402
|
+
def self.entity(entity, defaults = {})
|
403
|
+
name = defaults[:as]
|
404
|
+
@default_entities[entity] = defaults
|
405
|
+
|
406
|
+
attr_reader(name.to_sym) if name
|
407
|
+
end
|
408
|
+
|
409
|
+
# Public: Adds default Systems to the World.
|
410
|
+
#
|
411
|
+
# systems - The System or Array list of System classes to add to the World.
|
412
|
+
#
|
413
|
+
# Examples
|
414
|
+
#
|
415
|
+
# systems(RenderSprites)
|
416
|
+
#
|
417
|
+
# systems(RenderSprites, RenderLabels)
|
418
|
+
#
|
419
|
+
# Returns nothing.
|
420
|
+
def self.systems(*systems)
|
421
|
+
@default_systems += Array(systems).flatten
|
422
|
+
end
|
423
|
+
|
424
|
+
class << self
|
425
|
+
# Internal: Returns the default Entities for the class.
|
426
|
+
attr_reader :default_entities
|
427
|
+
|
428
|
+
# Internal: Returns the default Systems for the class.
|
429
|
+
attr_reader :default_systems
|
430
|
+
end
|
431
|
+
|
376
432
|
# Public: Returns the Array of Systems.
|
377
433
|
attr_reader :systems
|
378
434
|
|
@@ -384,8 +440,16 @@ module Draco
|
|
384
440
|
# entities - The Array of Entities for the World (default: []).
|
385
441
|
# systems - The Array of System Classes for the World (default: []).
|
386
442
|
def initialize(entities: [], systems: [])
|
387
|
-
|
388
|
-
|
443
|
+
default_entities = self.class.default_entities.map do |klass, attributes|
|
444
|
+
name = attributes[:as]
|
445
|
+
entity = klass.new(attributes)
|
446
|
+
instance_variable_set("@#{name}", entity) if name
|
447
|
+
|
448
|
+
entity
|
449
|
+
end
|
450
|
+
|
451
|
+
@entities = EntityStore.new(default_entities + entities)
|
452
|
+
@systems = self.class.default_systems + systems
|
389
453
|
end
|
390
454
|
|
391
455
|
# Public: Runs all of the Systems every tick.
|