Skip to content

Commit 45c59ac

Browse files
committed
lesson 10
1 parent af1f9fc commit 45c59ac

26 files changed

+870
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Este curso explica programación orientada a objetos desde cero, para verlo sól
3030

3131
9. [Factory y value objects](https://styde.net/patron-factory-y-value-objects-oop/) - 20:09
3232

33-
10. Reducción de condicionales y código estructurado
33+
10. [Reducción de condicionales y código estructurado](https://styde.net/reduccion-de-uso-de-condicionales-if-y-sentencias-switch/) - 8:20
3434

3535
11. Métodos y propiedades estáticos
3636

lesson10/composer.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "styde/oop",
3+
"description": "Proyecto demo para el curso de programacion orientada a objetos",
4+
"type": "project",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Duilio Palacios",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"require": {},
13+
"autoload": {
14+
"files": ["src/helpers.php"],
15+
"psr-4": {
16+
"Styde\\": "src/"
17+
}
18+
}
19+
}

lesson10/composer.phar

1.63 MB
Binary file not shown.

lesson10/public/index.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Styde;
4+
5+
require '../vendor/autoload.php';
6+
7+
$ramm = new Unit('Ramm', new Weapons\BasicSword);
8+
9+
//$ramm->setArmor(new Armors\SilverArmor());
10+
11+
$silence = new Unit('Silence', new Weapons\FireBow);
12+
13+
$silence->attack($ramm);
14+
15+
$silence->attack($ramm);
16+
17+
$ramm->attack($silence);

lesson10/src/Armor.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Styde;
4+
5+
abstract class Armor
6+
{
7+
public function absorbDamage(Attack $attack)
8+
{
9+
if ($attack->isMagical()) {
10+
return $this->absorbMagicDamage($attack);
11+
}
12+
13+
return $this->absorbPhysicalDamage($attack);
14+
}
15+
16+
public function absorbPhysicalDamage(Attack $attack)
17+
{
18+
return $attack->getDamage();
19+
}
20+
21+
public function absorbMagicDamage(Attack $attack)
22+
{
23+
return $attack->getDamage();
24+
}
25+
}

lesson10/src/Armors/BronzeArmor.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Styde\Armors;
4+
5+
use Styde\Armor;
6+
use Styde\Attack;
7+
8+
class BronzeArmor extends Armor
9+
{
10+
public function absorbDamage(Attack $attack)
11+
{
12+
return $attack->getDamage() / 2;
13+
}
14+
}

lesson10/src/Armors/CursedArmor.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Styde\Armors;
4+
5+
use Styde\Armor;
6+
use Styde\Attack;
7+
8+
class CursedArmor extends Armor
9+
{
10+
public function absorbDamage(Attack $Attack)
11+
{
12+
return $attack->getDamage() * 2;
13+
}
14+
}

lesson10/src/Armors/MissingArmor.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Styde\Armors;
4+
5+
use Styde\Armor;
6+
use Styde\Attack;
7+
8+
class MissingArmor extends Armor
9+
{
10+
public function absorbDamage(Attack $attack)
11+
{
12+
return $attack->getDamage();
13+
}
14+
}

lesson10/src/Armors/SilverArmor.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Styde\Armors;
4+
5+
use Styde\Armor;
6+
use Styde\Attack;
7+
8+
class SilverArmor extends Armor
9+
{
10+
public function absorbPhysicalDamage(Attack $attack)
11+
{
12+
return $attack->getDamage() / 3;
13+
}
14+
}

lesson10/src/Attack.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Styde;
4+
5+
class Attack
6+
{
7+
protected $damage;
8+
protected $magical;
9+
protected $description;
10+
11+
public function __construct($damage, $magical, $description)
12+
{
13+
$this->damage = $damage;
14+
$this->magical = $magical;
15+
$this->description = $description;
16+
}
17+
18+
public function getDescription(Unit $attacker, Unit $opponent)
19+
{
20+
return str_replace(
21+
[':unit', ':opponent'],
22+
[$attacker->getName(), $opponent->getName()],
23+
$this->description
24+
);
25+
}
26+
27+
public function getDamage()
28+
{
29+
return $this->damage;
30+
}
31+
32+
public function isMagical()
33+
{
34+
return $this->magical;
35+
}
36+
37+
public function isPhysical()
38+
{
39+
return ! $this->isMagical();
40+
}
41+
}

lesson10/src/Unit.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Styde;
4+
5+
class Unit
6+
{
7+
protected $hp = 40;
8+
protected $name;
9+
protected $weapon;
10+
protected $armor;
11+
12+
public function __construct($name, Weapon $weapon)
13+
{
14+
$this->name = $name;
15+
$this->weapon = $weapon;
16+
$this->armor = new Armors\MissingArmor();
17+
}
18+
19+
public function setWeapon(Weapon $weapon)
20+
{
21+
$this->weapon = $weapon;
22+
}
23+
24+
public function setArmor(Armor $armor = null)
25+
{
26+
$this->armor = $armor;
27+
}
28+
29+
public function getName()
30+
{
31+
return $this->name;
32+
}
33+
34+
public function getHp()
35+
{
36+
return $this->hp;
37+
}
38+
39+
public function move($direction)
40+
{
41+
show("{$this->name} camina hacia $direction");
42+
}
43+
44+
public function attack(Unit $opponent)
45+
{
46+
$attack = $this->weapon->createAttack();
47+
48+
show($attack->getDescription($this, $opponent));
49+
50+
$opponent->takeDamage($attack);
51+
}
52+
53+
public function takeDamage(Attack $attack)
54+
{
55+
$this->hp = $this->hp - $this->armor->absorbDamage($attack);
56+
57+
show("{$this->name} ahora tiene {$this->hp} puntos de vida");
58+
59+
if ($this->hp <= 0) {
60+
$this->die();
61+
}
62+
}
63+
64+
public function die()
65+
{
66+
show("{$this->name} muere");
67+
68+
exit();
69+
}
70+
}

lesson10/src/Weapon.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Styde;
4+
5+
abstract class Weapon
6+
{
7+
protected $damage = 0;
8+
protected $magical = false;
9+
protected $description = ':unit ataca a :opponent';
10+
11+
public function createAttack()
12+
{
13+
return new Attack($this->damage, $this->magical, $this->description);
14+
}
15+
}

lesson10/src/Weapons/BasicBow.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Styde\Weapons;
4+
5+
use Styde\Weapon;
6+
use Styde\Unit;
7+
8+
class BasicBow extends Weapon
9+
{
10+
protected $damage = 20;
11+
protected $description = ':unit dispara una flecha :opponent';
12+
}

lesson10/src/Weapons/BasicSword.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Styde\Weapons;
4+
5+
use Styde\Weapon;
6+
use Styde\Unit;
7+
8+
class BasicSword extends Weapon
9+
{
10+
protected $damage = 40;
11+
protected $description = ":unit ataca con la espada a :opponent";
12+
}

lesson10/src/Weapons/CrossBow.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Styde\Weapons;
4+
5+
use Styde\Weapon;
6+
use Styde\Unit;
7+
8+
class CrossBow extends Weapon
9+
{
10+
protected $damage = 40;
11+
protected $description = ':unit dispara una flecha a :opponent';
12+
}

lesson10/src/Weapons/FireBow.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Styde\Weapons;
4+
5+
use Styde\Weapon;
6+
use Styde\Unit;
7+
8+
class FireBow extends Weapon
9+
{
10+
protected $damage = 30;
11+
protected $magical = true;
12+
protected $description = ':unit dispara una flecha de fuego a :opponent';
13+
}

lesson10/src/helpers.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
if (! function_exists('show')) {
4+
function show($message)
5+
{
6+
echo "<p>$message</p>";
7+
}
8+
}

lesson10/vendor/autoload.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
// autoload.php @generated by Composer
4+
5+
require_once __DIR__ . '/composer' . '/autoload_real.php';
6+
7+
return ComposerAutoloaderInitfba152e24576c07c9de0b11390affa40::getLoader();

0 commit comments

Comments
 (0)