Class: MapDungeon::Dungeon

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "", description = "") ⇒ Dungeon

Returns a new instance of Dungeon.


8
9
10
11
# File 'lib/map/dungeon.rb', line 8

def initialize(name = "", description = "")
  @name, @description = name, description
  @entities = Hash.new()
end

Instance Attribute Details

#descriptionObject

Attributes access


14
15
16
# File 'lib/map/dungeon.rb', line 14

def description
  @description
end

#nameObject

Attributes access


14
15
16
# File 'lib/map/dungeon.rb', line 14

def name
  @name
end

Instance Method Details

#add_entity(entity, type) ⇒ Object

Add entities to the hash


17
18
19
20
21
22
23
# File 'lib/map/dungeon.rb', line 17

def add_entity(entity, type)
  if(@entities[type] == nil)
    @entities[type] = [entity]
  else
    @entities[type] << entity
  end
end

#each_entityObject

Entity iterator


36
37
38
39
40
# File 'lib/map/dungeon.rb', line 36

def each_entity()
  @entities.each { |key, value|
    value.each { |entity| yield entity }
  }
end

#each_type_entity(type) ⇒ Object

Concrete entity iterator


43
44
45
# File 'lib/map/dungeon.rb', line 43

def each_type_entity(type)
  @entities[type].each { |entity| yield entity }
end

#has_entity?(entity, type) ⇒ Boolean

Returns true if the entity exists

Returns:

  • (Boolean)

31
32
33
# File 'lib/map/dungeon.rb', line 31

def has_entity?(entity, type)
  @entities[type].find { |e| entity.eql?(e) }  
end

#remove_entity(entity, type) ⇒ Object

Remove the entity from the array


26
27
28
# File 'lib/map/dungeon.rb', line 26

def remove_entity(entity, type)
  @entities[type].delete(entity)
end

#to_sObject

To string


48
49
50
# File 'lib/map/dungeon.rb', line 48

def to_s()
  "#{@name}: #{@description}"
end