Class: Kiwi::Internal::EntityStore

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

Instance Method Summary collapse

Constructor Details

#initializeEntityStore

Returns a new instance of EntityStore.



10
11
12
13
14
15
16
17
# File 'lib/entity.rb', line 10

def initialize
  # [Entity]
  @entities = []
  # [Bitmap]
  @flags = []
  @nextId = 0
  @deadEntities = Set.new
end

Instance Method Details

#entity_countObject



57
58
59
60
61
62
63
64
65
# File 'lib/entity.rb', line 57

def entity_count
  return ((0...@entities.count).reduce(0) do |count, id|
    if @deadEntities.include? id
      return count
    else
      return count + 1
    end
  end)
end

#entity_idsObject



100
101
102
103
104
# File 'lib/entity.rb', line 100

def entity_ids
  return (0...@nextId).filter do |id|
    !@deadEntities.include?(id)
  end
end

#get(entityId) ⇒ Object

Returns an entity

Parameters:

  • entityId (Integer)


44
45
46
# File 'lib/entity.rb', line 44

def get(entityId)
  return @entities[entityId]
end

#has_flag(entityId, flagId) ⇒ bool

Parameters:

  • entityId (Integer, #read)
  • flagId (Integer, #read)

Returns:

  • (bool)


70
71
72
73
74
75
76
# File 'lib/entity.rb', line 70

def has_flag(entityId, flagId)
  if @flags.size <= entityId
    return false
  else
    return @flags[entityId].contains(flagId)
  end
end

#kill(entityId) ⇒ Object

Mark an entity as dead



53
54
55
# File 'lib/entity.rb', line 53

def kill(entityId)
  @deadEntities.add entityId
end

#new_idObject

Returns a new entity id (int)



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/entity.rb', line 20

def new_id
  id = @deadEntities.first
  if id != nil
    @deadEntities.delete id
    return id
  else
    id = @nextId
    @nextId += 1
    return id
  end
end

#remove_flag(entityId, flagId) ⇒ Object



88
89
90
91
92
93
94
# File 'lib/entity.rb', line 88

def remove_flag(entityId, flagId)
  if @flags.size <= entityId
    return
  end

  @flags[entityId].remove(flagId)
end

#reset_flags(entityId) ⇒ Object



96
97
98
# File 'lib/entity.rb', line 96

def reset_flags(entityId)
  @flags[entityId].clear()
end

#set_flag(entityId, flagId) ⇒ Object



78
79
80
81
82
83
84
85
86
# File 'lib/entity.rb', line 78

def set_flag(entityId, flagId)
  if @flags.size <= entityId
    (@flags.size..entityId).each do |_|
      @flags.push(Bitmap.new)
    end
  end

  @flags[entityId].set(flagId)
end

#spawn(entityId, archId, archRowId) ⇒ Object

Spawn a new entity with the given ids



33
34
35
36
37
38
39
40
# File 'lib/entity.rb', line 33

def spawn(entityId, archId, archRowId)
  if @entities.count <= entityId
    @entities.push(Entity.new(archId, archRowId))
  else
    @entities[entityId] = Entity.new(archId, archRowId)
    self.reset_flags(entityId)
  end
end