Refactor of health int -> float and fixed armor being useless when

computed damage reduction is less than 1
This commit is contained in:
Dylan K. Taylor
2017-08-30 11:03:07 +01:00
parent ea5bd0348a
commit fd52022065
8 changed files with 61 additions and 71 deletions

View File

@ -55,21 +55,20 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
const CAUSE_CUSTOM = 14;
const CAUSE_STARVATION = 15;
/** @var int */
private $cause;
/** @var array */
/** @var float[] */
private $modifiers;
/** @var float[] */
private $originals;
/**
* @param Entity $entity
* @param int $cause
* @param int|int[] $damage
*
* @throws \Exception
* @param Entity $entity
* @param int $cause
* @param float|float[] $damage
*/
public function __construct(Entity $entity, $cause, $damage){
public function __construct(Entity $entity, int $cause, $damage){
$this->entity = $entity;
$this->cause = $cause;
if(is_array($damage)){
@ -90,43 +89,41 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
/**
* @return int
*/
public function getCause(){
public function getCause() : int{
return $this->cause;
}
/**
* @param int $type
*
* @return int
* @return float
*/
public function getOriginalDamage($type = self::MODIFIER_BASE){
public function getOriginalDamage(int $type = self::MODIFIER_BASE) : float{
if(isset($this->originals[$type])){
return $this->originals[$type];
}
return 0;
return 0.0;
}
/**
* @param int $type
*
* @return int
* @return float
*/
public function getDamage($type = self::MODIFIER_BASE){
public function getDamage(int $type = self::MODIFIER_BASE) : float{
if(isset($this->modifiers[$type])){
return $this->modifiers[$type];
}
return 0;
return 0.0;
}
/**
* @param float $damage
* @param int $type
*
* @throws \UnexpectedValueException
*/
public function setDamage($damage, $type = self::MODIFIER_BASE){
public function setDamage(float $damage, int $type = self::MODIFIER_BASE){
$this->modifiers[$type] = $damage;
}
@ -135,20 +132,15 @@ class EntityDamageEvent extends EntityEvent implements Cancellable{
*
* @return bool
*/
public function isApplicable($type){
public function isApplicable(int $type){
return isset($this->modifiers[$type]);
}
/**
* @return int
* @return float
*/
public function getFinalDamage(){
$damage = 0;
foreach($this->modifiers as $type => $d){
$damage += $d;
}
return $damage;
public function getFinalDamage() : float{
return array_sum($this->modifiers);
}
}