-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextra-lesson-4-1.php
92 lines (68 loc) · 1.89 KB
/
extra-lesson-4-1.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
function show($message) {
echo "<p>{$message}</p>";
}
abstract class Unit {
protected $hp = 40;
protected $alive = true;
protected $name;
public function getName() {
return $this->name;
}
private function setHp($points) {
$this->hp = $points;
show("{$this->name} ahora tiene {$this->hp} puntos de vida");
}
public function getHp() {
return $this->hp;
}
public function __construct($name) {
$this->name = $name;
}
public function move($direction) {
show("{$this->name} camina hacia $direction");
}
abstract public function attack(Unit $oponent);
public function takeDamage($damage) {
$this->setHp($this->hp - $damage);
if ($this->hp <= 0) {
$this->dier();
}
}
public function dier() {
show("{$this->name} muere");
}
}
class Soldier extends Unit {
protected $damage = 40;
public function attack(Unit $oponent) {
show("{$this->name} corta a {$oponent->getName()} en dos");
$oponent->takeDamage($this->damage);
}
public function takeDamage($damage) {
return parent::takeDamage($damage / 2);
}
}
class Archer extends Unit {
protected $damage = 20;
public function attack(Unit $oponent) {
show("{$this->name} dispara una flecha a {$oponent->getName()}");
$oponent->takeDamage($this->damage);
}
public function takeDamage($damage) {
if (rand(0, 1) == 1) {
return parent::takeDamage($damage);
}
}
}
$sar = new Soldier('Bestia');
$edwin = new Archer('Sar');
//$edwin->move('Norte');
$edwin->attack($sar);
$edwin->attack($sar);
$sar->attack($edwin);