Class: MISSIONGAME::Enemy

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = nil) ⇒ Enemy

Returns a new instance of Enemy.



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

def initialize(args = nil)
  # Pick a random enemy
  selected_enemy = ENEMY_CATALOG.sample[0]
  @name = selected_enemy[:name]
  @lives = selected_enemy[:lives] + rand(0..3)
  @bonus = selected_enemy[:bonus] + rand(0..3)
  @str = selected_enemy[:str]
  @points = selected_enemy[:points]
  @int = rand(2..6)
end

Instance Attribute Details

#bonusObject

Returns the value of attribute bonus.



24
25
26
# File 'lib/enemy.rb', line 24

def bonus
  @bonus
end

#intObject

Returns the value of attribute int.



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

def int
  @int
end

#livesObject

Returns the value of attribute lives.



23
24
25
# File 'lib/enemy.rb', line 23

def lives
  @lives
end

#nameObject

Returns the value of attribute name.



22
23
24
# File 'lib/enemy.rb', line 22

def name
  @name
end

#pointsObject

Returns the value of attribute points.



27
28
29
# File 'lib/enemy.rb', line 27

def points
  @points
end

#strObject

Returns the value of attribute str.



25
26
27
# File 'lib/enemy.rb', line 25

def str
  @str
end

Instance Method Details

#attack(args) ⇒ Object

Enemy attacks player



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/enemy.rb', line 41

def attack(args)
  enemy = self
  player = args[:player]

  # Does the enemy even hit the player?
  str_diff = (enemy.str - player.str) * 2
  hit_chance = rand(1...100) + str_diff

  if hit_chance > 30
    # Determine value of the attack
    attack_value = rand(1...player.str)
    print enemy.name.light_red + " hits you for " +
      attack_value.to_s.light_yellow + " damage(s)!\n"
    if attack_value > player.lives
      return PLAYER_DEAD
    else
      return attack_value
    end
  else
    print enemy.name.light_red + " sees you as an easy prey!\n"
  end
  true
end