Class: Hearthstone::Models::Entity

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

Overview

represent an object in game

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, card: nil) ⇒ Entity

Returns a new instance of Entity.



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

def initialize(id: nil, card: nil)
  @id = id
  @card = card
  @damaged = 0
  @attachments = []
end

Instance Attribute Details

#attachmentsObject (readonly)

Returns the value of attribute attachments.



6
7
8
# File 'lib/hearthstone/models/entity.rb', line 6

def attachments
  @attachments
end

#cardObject

Returns the value of attribute card.



5
6
7
# File 'lib/hearthstone/models/entity.rb', line 5

def card
  @card
end

#damagedObject

Returns the value of attribute damaged.



5
6
7
# File 'lib/hearthstone/models/entity.rb', line 5

def damaged
  @damaged
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/hearthstone/models/entity.rb', line 5

def id
  @id
end

Instance Method Details

#attach(card) ⇒ Object



23
24
25
# File 'lib/hearthstone/models/entity.rb', line 23

def attach(card)
  attachments << card
end

#attackObject



39
40
41
# File 'lib/hearthstone/models/entity.rb', line 39

def attack
  self.card.attack
end

#current_healthObject



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

def current_health
  if self.card
    [0, self.card.health - damaged].max
  else
    0
  end
end

#detach(card) ⇒ Object



27
28
29
# File 'lib/hearthstone/models/entity.rb', line 27

def detach(card)
  attachments.delete(card)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/hearthstone/models/entity.rb', line 15

def eql?(other)
  other.equal?(id) || id == other.id
end

#hashObject



19
20
21
# File 'lib/hearthstone/models/entity.rb', line 19

def hash
  id.hash
end

#nameObject



43
44
45
# File 'lib/hearthstone/models/entity.rb', line 43

def name
  card.name
end

#to_sObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hearthstone/models/entity.rb', line 47

def to_s
  attachment_names = attachments.collect(&:name)
  attachment_str = ""
  if attachment_names.count > 0
    attachment_str = attachment_names.join(", ")
  end

  if card
    case card.type
    when "Minion"
      "##{id} #{card.name} (#{attack}/#{current_health}) #{attachment_str}"
    else
      "##{id} #{card.name}"
    end
  else
    "##{id} Hidden"
  end
end