Class: GameEcs::Condition

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

Direct Known Subclasses

Maybe, Must

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(k) ⇒ Condition

Returns a new instance of Condition.



360
361
362
363
# File 'lib/game_ecs/entity_store.rb', line 360

def initialize(k)
  @attr_conditions = {}
  @k = k
end

Instance Attribute Details

#attr_conditionsObject (readonly)

Returns the value of attribute attr_conditions.



359
360
361
# File 'lib/game_ecs/entity_store.rb', line 359

def attr_conditions
  @attr_conditions
end

#kObject (readonly)

Returns the value of attribute k.



359
360
361
# File 'lib/game_ecs/entity_store.rb', line 359

def k
  @k
end

Instance Method Details

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



365
366
367
368
369
# File 'lib/game_ecs/entity_store.rb', line 365

def ==(other)
  @k == other.k &&
    @attr_conditions.size == other.attr_conditions.size &&
    @attr_conditions.all?{|ac,v| other.attr_conditions[ac] == v}
end

#attrs_match?(id, comps) ⇒ Boolean

Returns:

  • (Boolean)


379
380
381
382
383
384
385
386
387
388
389
# File 'lib/game_ecs/entity_store.rb', line 379

def attrs_match?(id, comps)
  comp = comps[@k]
  @attr_conditions.all? do |name, cond|
    val = comp.send(name) 
    if cond.respond_to? :call
      cond.call val
    else
      val == cond
    end
  end
end

#componentsObject



375
376
377
# File 'lib/game_ecs/entity_store.rb', line 375

def components
  @k
end

#hashObject



371
372
373
# File 'lib/game_ecs/entity_store.rb', line 371

def hash
  @_hash ||= @k.hash ^ @attr_conditions.hash
end

#merge_conditions(attrs) ⇒ Object



391
392
393
394
# File 'lib/game_ecs/entity_store.rb', line 391

def merge_conditions(attrs)
  @attr_conditions ||= {}
  @attr_conditions.merge! attrs
end