Changed Effect colours to use Color objects instead of arrays (#1814)

This commit is contained in:
Dylan K. Taylor 2017-12-14 10:21:07 +00:00 committed by GitHub
parent 06f605879a
commit e75fbd7fb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 37 deletions

View File

@ -28,6 +28,7 @@ use pocketmine\event\entity\EntityRegainHealthEvent;
use pocketmine\event\player\PlayerExhaustEvent; use pocketmine\event\player\PlayerExhaustEvent;
use pocketmine\network\mcpe\protocol\MobEffectPacket; use pocketmine\network\mcpe\protocol\MobEffectPacket;
use pocketmine\Player; use pocketmine\Player;
use pocketmine\utils\Color;
use pocketmine\utils\Config; use pocketmine\utils\Config;
class Effect{ class Effect{
@ -64,16 +65,16 @@ class Effect{
$config = new Config(\pocketmine\RESOURCE_PATH . "effects.json", Config::JSON, []); $config = new Config(\pocketmine\RESOURCE_PATH . "effects.json", Config::JSON, []);
foreach($config->getAll() as $name => $data){ foreach($config->getAll() as $name => $data){
$color = hexdec(substr($data["color"], 3)); $color = hexdec(substr($data["color"], 1));
$a = ($color >> 24) & 0xff;
$r = ($color >> 16) & 0xff; $r = ($color >> 16) & 0xff;
$g = ($color >> 8) & 0xff; $g = ($color >> 8) & 0xff;
$b = $color & 0xff; $b = $color & 0xff;
self::registerEffect($name, new Effect( self::registerEffect($name, new Effect(
$data["id"], $data["id"],
"%potion." . $data["name"], "%potion." . $data["name"],
$r, new Color($r, $g, $b, $a),
$g,
$b,
$data["isBad"] ?? false, $data["isBad"] ?? false,
$data["default_duration"] ?? 300 * 20, $data["default_duration"] ?? 300 * 20,
$data["has_bubbles"] ?? true $data["has_bubbles"] ?? true
@ -122,7 +123,7 @@ class Effect{
protected $duration; protected $duration;
/** @var int */ /** @var int */
protected $amplifier = 0; protected $amplifier = 0;
/** @var int[] */ /** @var Color */
protected $color; protected $color;
/** @var bool */ /** @var bool */
protected $visible = true; protected $visible = true;
@ -136,20 +137,18 @@ class Effect{
protected $hasBubbles = true; protected $hasBubbles = true;
/** /**
* @param int $id Effect ID as per Minecraft PE * @param int $id Effect ID as per Minecraft PE
* @param string $name Translation key used for effect name * @param string $name Translation key used for effect name
* @param int $r 0-255, red balance of potion particle colour * @param Color $color
* @param int $g 0-255, green balance of potion particle colour * @param bool $isBad Whether the effect is harmful
* @param int $b 0-255, blue balance of potion particle colour
* @param bool $isBad Whether the effect is harmful
* @param int $defaultDuration Duration in ticks the effect will last for by default if applied without a duration. * @param int $defaultDuration Duration in ticks the effect will last for by default if applied without a duration.
* @param bool $hasBubbles Whether the effect has potion bubbles. Some do not (e.g. Instant Damage has its own particles instead of bubbles) * @param bool $hasBubbles Whether the effect has potion bubbles. Some do not (e.g. Instant Damage has its own particles instead of bubbles)
*/ */
public function __construct(int $id, string $name, int $r, int $g, int $b, bool $isBad = false, int $defaultDuration = 300 * 20, bool $hasBubbles = true){ public function __construct(int $id, string $name, Color $color, bool $isBad = false, int $defaultDuration = 300 * 20, bool $hasBubbles = true){
$this->id = $id; $this->id = $id;
$this->name = $name; $this->name = $name;
$this->bad = $isBad; $this->bad = $isBad;
$this->setColor($r, $g, $b); $this->color = $color;
$this->defaultDuration = $defaultDuration; $this->defaultDuration = $defaultDuration;
$this->duration = $defaultDuration; $this->duration = $defaultDuration;
$this->hasBubbles = $hasBubbles; $this->hasBubbles = $hasBubbles;
@ -380,22 +379,20 @@ class Effect{
} }
/** /**
* Returns an RGB color array of this effect's color. * Returns a Color object representing this effect's particle colour.
* @return int[] * @return Color
*/ */
public function getColor() : array{ public function getColor() : Color{
return [$this->color >> 16, ($this->color >> 8) & 0xff, $this->color & 0xff]; return clone $this->color;
} }
/** /**
* Sets the color of this effect. * Sets the color of this effect.
* *
* @param int $r * @param Color $color
* @param int $g
* @param int $b
*/ */
public function setColor(int $r, int $g, int $b){ public function setColor(Color $color){
$this->color = (($r & 0xff) << 16) + (($g & 0xff) << 8) + ($b & 0xff); $this->color = clone $color;
} }
/** /**
@ -490,4 +487,8 @@ class Effect{
break; break;
} }
} }
public function __clone(){
$this->color = clone $this->color;
}
} }

View File

@ -44,6 +44,7 @@ use pocketmine\network\mcpe\protocol\MobEffectPacket;
use pocketmine\Player; use pocketmine\Player;
use pocketmine\utils\Binary; use pocketmine\utils\Binary;
use pocketmine\utils\BlockIterator; use pocketmine\utils\BlockIterator;
use pocketmine\utils\Color;
abstract class Living extends Entity implements Damageable{ abstract class Living extends Entity implements Damageable{
@ -277,29 +278,25 @@ abstract class Living extends Entity implements Damageable{
* Recalculates the mob's potion bubbles colour based on the active effects. * Recalculates the mob's potion bubbles colour based on the active effects.
*/ */
protected function recalculateEffectColor(){ protected function recalculateEffectColor(){
//TODO: add transparency values /** @var Color[] $colors */
$color = [0, 0, 0]; //RGB $colors = [];
$count = 0;
$ambient = true; $ambient = true;
foreach($this->effects as $effect){ foreach($this->effects as $effect){
if($effect->isVisible() and $effect->hasBubbles()){ if($effect->isVisible() and $effect->hasBubbles()){
$c = $effect->getColor(); $level = $effect->getEffectLevel();
$color[0] += $c[0] * $effect->getEffectLevel(); $color = $effect->getColor();
$color[1] += $c[1] * $effect->getEffectLevel(); for($i = 0; $i < $level; ++$i){
$color[2] += $c[2] * $effect->getEffectLevel(); $colors[] = $color;
$count += $effect->getEffectLevel(); }
if(!$effect->isAmbient()){ if(!$effect->isAmbient()){
$ambient = false; $ambient = false;
} }
} }
} }
if($count > 0){ if(!empty($colors)){
$r = ($color[0] / $count) & 0xff; $this->setDataProperty(Entity::DATA_POTION_COLOR, Entity::DATA_TYPE_INT, Color::mix(...$colors)->toARGB());
$g = ($color[1] / $count) & 0xff;
$b = ($color[2] / $count) & 0xff;
$this->setDataProperty(Entity::DATA_POTION_COLOR, Entity::DATA_TYPE_INT, 0xff000000 | ($r << 16) | ($g << 8) | $b);
$this->setDataProperty(Entity::DATA_POTION_AMBIENT, Entity::DATA_TYPE_BYTE, $ambient ? 1 : 0); $this->setDataProperty(Entity::DATA_POTION_AMBIENT, Entity::DATA_TYPE_BYTE, $ambient ? 1 : 0);
}else{ }else{
$this->setDataProperty(Entity::DATA_POTION_COLOR, Entity::DATA_TYPE_INT, 0); $this->setDataProperty(Entity::DATA_POTION_COLOR, Entity::DATA_TYPE_INT, 0);