/src/pocketmine/entity/passive/Wolf.php

https://github.com/TuranicTeam/Altay · PHP · 152 lines · 108 code · 24 blank · 20 comment · 12 complexity · 63b50779e9db0bcaf50280a6f0a6a74d MD5 · raw file

  1. <?php
  2. /*
  3. * _ _
  4. * /\ | | |
  5. * / \ | | |_ __ _ _ _
  6. * / /\ \ | | __/ _` | | | |
  7. * / ____ \| | || (_| | |_| |
  8. * /_/ \_|_|\__\__,_|\__, |
  9. * __/ |
  10. * |___/
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Lesser General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * @author TuranicTeam
  18. * @link https://github.com/TuranicTeam/Altay
  19. *
  20. */
  21. declare(strict_types=1);
  22. namespace pocketmine\entity\passive;
  23. use pocketmine\entity\behavior\FloatBehavior;
  24. use pocketmine\entity\behavior\FollowOwnerBehavior;
  25. use pocketmine\entity\behavior\HurtByTargetBehavior;
  26. use pocketmine\entity\behavior\LeapAtTargetBehavior;
  27. use pocketmine\entity\behavior\LookAtPlayerBehavior;
  28. use pocketmine\entity\behavior\MeleeAttackBehavior;
  29. use pocketmine\entity\behavior\NearestAttackableTargetBehavior;
  30. use pocketmine\entity\behavior\OwnerHurtByTargetBehavior;
  31. use pocketmine\entity\behavior\OwnerHurtTargetBehavior;
  32. use pocketmine\entity\behavior\RandomLookAroundBehavior;
  33. use pocketmine\entity\behavior\StayWhileSittingBehavior;
  34. use pocketmine\entity\behavior\RandomStrollBehavior;
  35. use pocketmine\entity\Entity;
  36. use pocketmine\entity\hostile\Skeleton;
  37. use pocketmine\entity\Tamable;
  38. use pocketmine\item\Item;
  39. use pocketmine\math\Vector3;
  40. use pocketmine\network\mcpe\protocol\ActorEventPacket;
  41. use pocketmine\Player;
  42. use function mt_rand;
  43. class Wolf extends Tamable{
  44. public const NETWORK_ID = self::WOLF;
  45. public $width = 0.6;
  46. public $height = 0.8;
  47. /** @var StayWhileSittingBehavior */
  48. protected $behaviorSitting;
  49. protected function addBehaviors() : void{
  50. $this->behaviorPool->setBehavior(0, new FloatBehavior($this));
  51. $this->behaviorPool->setBehavior(1, $this->behaviorSitting = new StayWhileSittingBehavior($this));
  52. $this->behaviorPool->setBehavior(2, new LeapAtTargetBehavior($this, 0.4));
  53. $this->behaviorPool->setBehavior(3, new MeleeAttackBehavior($this, 1.0));
  54. $this->behaviorPool->setBehavior(4, new FollowOwnerBehavior($this, 1, 10 ,2));
  55. $this->behaviorPool->setBehavior(5, new RandomStrollBehavior($this, 1.0));
  56. $this->behaviorPool->setBehavior(6, new LookAtPlayerBehavior($this, 8.0));
  57. $this->behaviorPool->setBehavior(7, new RandomLookAroundBehavior($this));
  58. $this->targetBehaviorPool->setBehavior(0, new HurtByTargetBehavior($this, true));
  59. $this->targetBehaviorPool->setBehavior(1, new OwnerHurtByTargetBehavior($this));
  60. $this->targetBehaviorPool->setBehavior(2, new OwnerHurtTargetBehavior($this));
  61. $this->targetBehaviorPool->setBehavior(3, new NearestAttackableTargetBehavior($this, Skeleton::class, false));
  62. }
  63. protected function initEntity() : void{
  64. $this->setMaxHealth(8);
  65. $this->setMovementSpeed(0.3);
  66. $this->setAttackDamage(3);
  67. $this->setFollowRange(16);
  68. $this->propertyManager->setInt(self::DATA_COLOR, 14); // collar color
  69. $this->setTamed(false);
  70. parent::initEntity();
  71. }
  72. public function getName() : string{
  73. return "Wolf";
  74. }
  75. public function onInteract(Player $player, Item $item, Vector3 $clickPos) : bool{
  76. if(!$this->isImmobile()){
  77. if($this->isTamed()){
  78. if($this->getOwningEntityId() == $player->id){
  79. $this->setSittingFromBehavior(!$this->isSitting());
  80. $this->setTargetEntity(null);
  81. }
  82. }else{
  83. if($item->getId() == Item::BONE){
  84. if($player->isSurvival()){
  85. $item->pop();
  86. }
  87. if(mt_rand(0, 2) == 0){
  88. $this->setOwningEntity($player);
  89. $this->setTamed();
  90. $this->setSittingFromBehavior(true);
  91. $this->setAngry(false);
  92. $this->broadcastEntityEvent(ActorEventPacket::TAME_SUCCESS);
  93. }else{
  94. $this->broadcastEntityEvent(ActorEventPacket::TAME_FAIL);
  95. }
  96. return true;
  97. }
  98. }
  99. }
  100. return parent::onInteract($player, $item, $clickPos);
  101. }
  102. public function setSittingFromBehavior(bool $value) : void{
  103. $this->behaviorSitting->setSitting($value);
  104. }
  105. public function setTargetEntity(?Entity $target) : void{
  106. parent::setTargetEntity($target);
  107. if($target == null){
  108. $this->setAngry(false);
  109. }elseif(!$this->isTamed()){
  110. $this->setAngry();
  111. }
  112. }
  113. public function isAngry() : bool{
  114. return $this->getGenericFlag(self::DATA_FLAG_ANGRY);
  115. }
  116. public function setAngry(bool $angry = true) : void{
  117. $this->setGenericFlag(self::DATA_FLAG_ANGRY, $angry);
  118. }
  119. public function setTamed(bool $tamed = true) : void{
  120. parent::setTamed($tamed);
  121. if($tamed){
  122. $this->setMaxHealth(20);
  123. }else{
  124. $this->setMaxHealth(8);
  125. }
  126. $this->setAttackDamage(4);
  127. }
  128. }