Class: GameEcs::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/game_ecs/entity_store.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQuery

Returns a new instance of Query.



422
423
424
425
# File 'lib/game_ecs/entity_store.rb', line 422

def initialize
  @components = []
  @musts = []
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



411
412
413
# File 'lib/game_ecs/entity_store.rb', line 411

def components
  @components
end

#maybesObject (readonly)

Returns the value of attribute maybes.



411
412
413
# File 'lib/game_ecs/entity_store.rb', line 411

def maybes
  @maybes
end

#mustsObject (readonly)

Returns the value of attribute musts.



411
412
413
# File 'lib/game_ecs/entity_store.rb', line 411

def musts
  @musts
end

Class Method Details

.maybe(*args) ⇒ Object



418
419
420
# File 'lib/game_ecs/entity_store.rb', line 418

def self.maybe(*args)
  Query.new.maybe(*args)
end

.must(*args) ⇒ Object



415
416
417
# File 'lib/game_ecs/entity_store.rb', line 415

def self.must(*args)
  Query.new.must(*args)
end

.noneObject



412
413
414
# File 'lib/game_ecs/entity_store.rb', line 412

def self.none
  Query.new
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



455
456
457
# File 'lib/game_ecs/entity_store.rb', line 455

def ==(other)
  self.musts == other.musts && self.maybes == other.maybes
end

#cacheable?Boolean

Returns:

  • (Boolean)


459
460
461
# File 'lib/game_ecs/entity_store.rb', line 459

def cacheable?
  @cacheable ||= @musts.all?{|m| m.attr_conditions.values.all?{|ac| !ac.respond_to?(:call) } }
end

#hashObject



464
465
466
# File 'lib/game_ecs/entity_store.rb', line 464

def hash
  @_hash ||= self.musts.hash ^ self.maybes.hash
end

#matches?(eid, comps) ⇒ Boolean

Returns:

  • (Boolean)


451
452
453
# File 'lib/game_ecs/entity_store.rb', line 451

def matches?(eid, comps)
  @musts.all?{|m| m.matches?(eid, comps)} # ignore maybes  ;)
end

#maybe(k) ⇒ Object



443
444
445
446
447
448
449
# File 'lib/game_ecs/entity_store.rb', line 443

def maybe(k)
  @maybes ||= []
  @last_condition = Maybe.new(k)
  @maybes << @last_condition
  @components << k
  self
end

#must(k) ⇒ Object



427
428
429
430
431
432
# File 'lib/game_ecs/entity_store.rb', line 427

def must(k)
  @last_condition = Must.new(k)
  @musts << @last_condition
  @components << k
  self
end

#required_componentsObject



434
435
436
# File 'lib/game_ecs/entity_store.rb', line 434

def required_components
  @musts.flat_map(&:components).uniq
end

#with(attr_map) ⇒ Object



438
439
440
441
# File 'lib/game_ecs/entity_store.rb', line 438

def with(attr_map)
  @last_condition.merge_conditions(attr_map)
  self
end