Class: Termfront::Weapon::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/termfront/weapon/base.rb

Direct Known Subclasses

AssaultRifle, Pistol, ShockPistol, ShockRifle

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ammo: nil) ⇒ Base

Returns a new instance of Base.



8
9
10
# File 'lib/termfront/weapon/base.rb', line 8

def initialize(ammo: nil)
  @ammo = ammo.nil? ? max_ammo : ammo
end

Instance Attribute Details

#ammoObject

Returns the value of attribute ammo.



6
7
8
# File 'lib/termfront/weapon/base.rb', line 6

def ammo
  @ammo
end

Class Method Details

.build(type, ammo = nil) ⇒ Object



37
38
39
40
# File 'lib/termfront/weapon/base.rb', line 37

def build(type, ammo = nil)
  klass = registry[type] || raise(ArgumentError, "Unknown weapon type: #{type}")
  ammo ? klass.new(ammo: ammo) : klass.new
end

.register(type, klass) ⇒ Object



33
34
35
# File 'lib/termfront/weapon/base.rb', line 33

def register(type, klass)
  registry[type] = klass
end

.registryObject



29
30
31
# File 'lib/termfront/weapon/base.rb', line 29

def registry
  @registry ||= {}
end

Instance Method Details

#can_fire?(last_fire, now) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/termfront/weapon/base.rb', line 20

def can_fire?(last_fire, now)
  (now - last_fire) > cooldown
end

#consume_ammo!Object



24
25
26
# File 'lib/termfront/weapon/base.rb', line 24

def consume_ammo!
  @ammo -= 1 if @ammo
end

#cooldownObject

Raises:

  • (NotImplementedError)


14
# File 'lib/termfront/weapon/base.rb', line 14

def cooldown    = raise(NotImplementedError)

#hit_widthObject

Raises:

  • (NotImplementedError)


15
# File 'lib/termfront/weapon/base.rb', line 15

def hit_width   = raise(NotImplementedError)

#infinite_ammo?Boolean

Returns:

  • (Boolean)


18
# File 'lib/termfront/weapon/base.rb', line 18

def infinite_ammo? = max_ammo.nil?

#max_ammoObject

Raises:

  • (NotImplementedError)


13
# File 'lib/termfront/weapon/base.rb', line 13

def max_ammo    = raise(NotImplementedError)

#nameObject

Raises:

  • (NotImplementedError)


12
# File 'lib/termfront/weapon/base.rb', line 12

def name        = raise(NotImplementedError)

#type_idObject

Raises:

  • (NotImplementedError)


16
# File 'lib/termfront/weapon/base.rb', line 16

def type_id     = raise(NotImplementedError)