draco 0.2.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +21 -0
  3. data/lib/draco.rb +68 -4
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fabf6d184411f5d8dc5782499dfd2e7d07174333f6927566fb3fe51d59ae0df3
4
- data.tar.gz: 5a647308729162793c8d4841b86f954298640d319dbfa836f8cb072f491c04cb
3
+ metadata.gz: f135d47efafef38fd731f870c963e9764f113317f1156ca3c93b10a99a87cdb3
4
+ data.tar.gz: c8ef07e0313011c1333886410fe43f68804da261b74b1d197742824f07c2e907
5
5
  SHA512:
6
- metadata.gz: 6e754598051acec8e35c62d40d52c255bf2187fa68b6e2e18112de90661af7faa53b9bb7fc402b8425df99bac3fb6d9d6a3cf15dabb0f956004dd43a63d04ae7
7
- data.tar.gz: a558139ee8f227e9307c406453b18a5280ceac36ee0386fbd3998eebfe975cb0fc6e21effff52292aab4b594fbef56498a69623aa75586d6229a0b34740613a2
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
@@ -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.2.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
- @entities = EntityStore.new(entities)
388
- @systems = systems
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.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: draco
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Pruitt