Class: EntityStore

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

Instance Method Summary collapse

Constructor Details

#initializeEntityStore



7
8
9
10
11
12
13
14
# File 'lib/entity.rb', line 7

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

Instance Method Details

#entity_countObject



54
55
56
57
58
59
60
61
62
# File 'lib/entity.rb', line 54

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



97
98
99
100
101
# File 'lib/entity.rb', line 97

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

#get(entityId) ⇒ Object

Returns an entity



41
42
43
# File 'lib/entity.rb', line 41

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

#has_flag(entityId, flagId) ⇒ bool



67
68
69
70
71
72
73
# File 'lib/entity.rb', line 67

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



50
51
52
# File 'lib/entity.rb', line 50

def kill(entityId)
  @deadEntities.add entityId
end

#new_idObject

Returns a new entity id (int)



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/entity.rb', line 17

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



85
86
87
88
89
90
91
# File 'lib/entity.rb', line 85

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

  @flags[entityId].remove(flagId)
end

#reset_flags(entityId) ⇒ Object



93
94
95
# File 'lib/entity.rb', line 93

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

#set_flag(entityId, flagId) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/entity.rb', line 75

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



30
31
32
33
34
35
36
37
# File 'lib/entity.rb', line 30

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