Battlefield 0.0.2 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/battlefield.rb +35 -13
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ea35d68f2814c70585aa9f1520c7eb501cf6365d
|
4
|
+
data.tar.gz: 7e1e69a87a0f70caaae3f96f68341d7d4d32044a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d53ac2d164dc1bc2680f7a286c13e65f93d993c2fed8d708dc7d3e5e082ad0f45cc94ffc1fe62bf5eed8687adc7c685a070a1587baef37244e9a7513e6340b56
|
7
|
+
data.tar.gz: b938407b8c1622d7e47a28a961a7f57e33255418ffbeefe15e38e06836b8d99f19554fd8143407b1cb46dde17681df000819b6fea018f3a5ab94e3bc77a93f67
|
data/lib/battlefield.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
=begin
|
2
|
-
If you want to make a new enemy, you have two ways to do it
|
3
|
-
|
2
|
+
If you want to make a new enemy, you have two ways to do it:
|
4
3
|
1. Directly initialize it
|
5
4
|
Creature.new (insert health here)
|
6
5
|
|
@@ -8,34 +7,56 @@ Creature.new (insert health here)
|
|
8
7
|
class (Insert enemy name here) < Creature
|
9
8
|
def initialize; @health = _insert health here_; end
|
10
9
|
end
|
10
|
+
|
11
|
+
|
12
|
+
Common ancestor of any battle-able objects
|
11
13
|
=end
|
12
|
-
class Creature
|
14
|
+
class Creature
|
13
15
|
attr_accessor :health
|
14
|
-
|
16
|
+
# Really should specify health and now you can battle
|
17
|
+
def initialize(health = 0)
|
15
18
|
@health = health
|
16
19
|
end
|
17
|
-
|
20
|
+
# Damage function
|
21
|
+
def damage(amt)
|
18
22
|
@health -= amt
|
19
23
|
end
|
20
|
-
|
24
|
+
# rolling function - higher # = more power
|
25
|
+
def roll
|
21
26
|
(1..6).to_a.sample; end
|
22
|
-
|
27
|
+
# roll twice
|
28
|
+
def droll
|
23
29
|
self.roll + self.roll
|
24
30
|
end
|
25
|
-
|
31
|
+
# The 1st argument is your attack power. The other is the enemy's. You heal c1 - c2 health
|
32
|
+
def heal(c1, c2)
|
26
33
|
@health += c1 - c2
|
27
34
|
end
|
28
35
|
end
|
29
|
-
|
30
|
-
|
36
|
+
# To use, create an instance first
|
37
|
+
class Hero < Creature
|
38
|
+
# The hero starts with 100 health
|
39
|
+
def initialize
|
31
40
|
@health = 100
|
32
41
|
end
|
33
42
|
end
|
34
|
-
|
43
|
+
# Initializes an ArmoredCreature. It takes a reduced amount of damage.
|
44
|
+
class ArmoredCreature < Creature
|
45
|
+
attr_reader = :armour
|
46
|
+
# specify damage reduction as a float in parameter armour
|
47
|
+
def initialize(health = 0, armour = 0.5); @health, @armour = health, armor; end
|
48
|
+
# Reduces damage by multiplying it by armour
|
49
|
+
def damage(amt)
|
50
|
+
@health -= (amt*@armour)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
# specify your combatants. If you want your hero to heal, set heal to 'hero'. If you want your enemy to heal, set heal to 'enemy'. If you want them both to heal, set heal to 'both'. Otherwise leave it blank.
|
54
|
+
def battle(hero, enemy, heal = false)
|
35
55
|
until hero.health <= 0 or enemy.health <= 0
|
36
56
|
roll1 = hero.droll
|
37
57
|
roll2 = enemy.droll
|
38
|
-
|
58
|
+
# You hit enemy and it takes your roll amount
|
59
|
+
if roll1 > roll2
|
39
60
|
if heal == "hero" || "both"
|
40
61
|
hero.heal roll1, roll2
|
41
62
|
puts "You gained #{roll1 - roll2} health"
|
@@ -44,7 +65,8 @@ def battle(hero, enemy, heal = false) # specify your combatants. If you want you
|
|
44
65
|
puts "The enemy took #{roll1} damage"
|
45
66
|
enemy.damage roll1
|
46
67
|
sleep 1
|
47
|
-
|
68
|
+
# Enemy hits you and you take its roll amount
|
69
|
+
elsif roll2 > roll1
|
48
70
|
if heal == "enemy" || "both"
|
49
71
|
enemy.heal roll2, roll1
|
50
72
|
puts "The enemy gained #{roll2 - roll1} health"
|