draco 0.4.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +11 -1
  4. data/lib/draco.rb +57 -10
  5. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1057daf5d6bb88b4877d001fe21d123557cb720df446d9ff2c0111a58baceb0f
4
- data.tar.gz: a96296838c903442df84482a8eb57346fc8225a34a6842f352d8d3ec1cc955cb
3
+ metadata.gz: 363ccf9c3649a752219bca392bdc13b88defbd864a63afb5615969986e59c070
4
+ data.tar.gz: 81dcbc8225646cec6efc357ecaacfd89334e9a183caad035cf4bd2ece9a01470
5
5
  SHA512:
6
- metadata.gz: 502d00bc7c2d3e099548c8c2211182ae95bbb0d35de8dba9e20f232046803740bfa3331a8bf94a414b4c5ecaad7c86a570fdcb53fec8325e07191eec4342a3ec
7
- data.tar.gz: f19a25eca8d942dea85a8835f8e239d3a81613a9667c788dba4eadb021876b90938e533bb049bf50155f316a16fe43065dd730b9578a927ea3691a2775b8538f
6
+ metadata.gz: aa089a7a6abe9b615d07e8def023fad13fedcbca605e78bc1acf181431b1659961811074a9238f79702b7e1e9ddd76782a173fd079ed5f7ae04a2ee83905c660
7
+ data.tar.gz: e6a70346719b8cf5e5269924158bcbad7ed4ea66440686c57931b7dc6232d467adf6abeac5740970e6d16a573d61a66af27c5626599ed115a7a87624d9ac9bdf
@@ -0,0 +1,5 @@
1
+ ## 0.5.0 (December 31, 2020)
2
+
3
+ Features:
4
+
5
+ - Add `Draco::Tag` to easily define tag components [#6](https://github.com/guitsaru/draco/pull/6)
data/README.md CHANGED
@@ -47,7 +47,7 @@ These can be shared across many different types of game objects.
47
47
  class Visible < Draco::Component; end
48
48
  ```
49
49
 
50
- `Visible` is an example of a label component. An entity either has it, or it doesn't. We can also associate data with our
50
+ `Visible` is an example of a tag component. An entity either has it, or it doesn't. We can also associate data with our
51
51
  components.
52
52
 
53
53
  ```ruby
@@ -68,6 +68,16 @@ component.x
68
68
  # => 110
69
69
  ```
70
70
 
71
+ #### Tag Components
72
+
73
+ The `Visible` class above is an example of a tag component. These are common enough that we don't necessarily want to
74
+ define a bunch of empty component classes. Draco provides a way to generate these classes at runtime.
75
+
76
+ ```ruby
77
+ Draco::Tag(:visible)
78
+ # => Visible
79
+ ```
80
+
71
81
  ### Entities
72
82
 
73
83
  Entities are independant game objects. They consist of a unique id and a list of components.
@@ -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.4.0"
8
+ VERSION = "0.5.0"
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
@@ -300,6 +300,23 @@ module Draco
300
300
  end
301
301
  end
302
302
 
303
+ # Internal: Empty module to enable Tag() method.
304
+ module Tag; end
305
+
306
+ # Public: Creates a new empty component at runtime. If the given Class already exists, it reuses the existing Class.
307
+ #
308
+ # name - The symbol or string name of the component. It can be either camelcase or underscored.
309
+ #
310
+ # Returns a Class with superclass of Draco::Component.
311
+ def self.Tag(name) # rubocop:disable Naming/MethodName
312
+ klass_name = camelize(name)
313
+
314
+ return Object.const_get(klass_name) if Object.const_defined?(klass_name)
315
+
316
+ klass = Class.new(Component)
317
+ Object.const_set(klass_name, klass)
318
+ end
319
+
303
320
  # Public: Systems contain the logic of the game.
304
321
  # The System runs on each tick and manipulates the Entities in the World.
305
322
  class System
@@ -518,16 +535,23 @@ module Draco
518
535
  def [](*components_or_ids)
519
536
  components_or_ids
520
537
  .flatten
521
- .map do |component_or_id|
522
- if component_or_id.is_a?(Numeric)
523
- Array(@entity_ids[component_or_id])
524
- else
525
- @component_to_entities[component_or_id]
526
- end
527
- end
538
+ .map { |component_or_id| select_entities(component_or_id) }
528
539
  .reduce { |acc, i| i & acc }
529
540
  end
530
541
 
542
+ # Internal: Gets entities by component or id.
543
+ #
544
+ # component_or_id - The Component Class or entity id to select.
545
+ #
546
+ # Returns an Array of Entities.
547
+ def select_entities(component_or_id)
548
+ if component_or_id.is_a?(Numeric)
549
+ Array(@entity_ids[component_or_id])
550
+ else
551
+ @component_to_entities[component_or_id]
552
+ end
553
+ end
554
+
531
555
  # Internal: Adds Entities to the EntityStore
532
556
  #
533
557
  # entities - The Entity or Array list of Entities to add to the EntityStore.
@@ -717,13 +741,36 @@ module Draco
717
741
  #
718
742
  # Returns a String.
719
743
  def self.underscore(string)
720
- string.split("::").last.bytes.map.with_index do |byte, i|
744
+ string.to_s.split("::").last.bytes.map.with_index do |byte, i|
721
745
  if byte > 64 && byte < 97
722
- downcased = byte + 32 # gemspec
746
+ downcased = byte + 32
723
747
  i.zero? ? downcased.chr : "_#{downcased.chr}"
724
748
  else
725
749
  byte.chr
726
750
  end
727
751
  end.join
728
752
  end
753
+
754
+ # Internal: Converts an underscored string into a camel case string.
755
+ #
756
+ # Examples
757
+ #
758
+ # camlize("camel_case")
759
+ # # => "CamelCase"
760
+ #
761
+ # Returns a string.
762
+ def self.camelize(string) # rubocop:disable Metrics/MethodLength
763
+ modifier = -32
764
+
765
+ string.to_s.bytes.map do |byte|
766
+ if byte == 95
767
+ modifier = -32
768
+ nil
769
+ else
770
+ char = (byte + modifier).chr
771
+ modifier = 0
772
+ char
773
+ end
774
+ end.compact.join
775
+ end
729
776
  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.0
4
+ version: 0.5.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: 2020-12-28 00:00:00.000000000 Z
11
+ date: 2020-12-31 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