dadodasyra

Untitled

Mar 25th, 2022
1,327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.90 KB | None | 0 0
  1. <?php
  2.  
  3. namespace minicore\entities;
  4.  
  5. use minicore\handler\Anti\GrapnelHandler;
  6. use minicore\CustomPlayer;
  7. use minicore\items\tool\Grapnel;
  8. use pocketmine\entity\EntitySizeInfo;
  9. use pocketmine\entity\Location;
  10. use pocketmine\network\mcpe\protocol\types\entity\EntityIds;
  11. use pocketmine\player\Player;
  12. use pocketmine\entity\Entity;
  13. use pocketmine\entity\projectile\Projectile;
  14. use pocketmine\math\RayTraceResult;
  15. use pocketmine\nbt\tag\CompoundTag;
  16. use pocketmine\Server;
  17. use pocketmine\utils\Random;
  18.  
  19. class FishingHook extends Projectile{ //In part from from GrapplingHook / korado531m7 TODO: Fix this
  20.  
  21.     public const NETWORK_ID = EntityIds::FISHING_HOOK; //Restant de PM3
  22.  
  23.     public float $height = 0.25;
  24.     public float $width = 0.25;
  25.     protected $gravity = 0.1;
  26.  
  27.     public static float $coef1 = 2;//coef1/dist+coef2
  28.     public static float $coef2 = 1;
  29.  
  30.     public function __construct(Location $location, ?Entity $owner, ?CompoundTag $nbt = null)
  31.     {
  32.         $this->setHasGravity(true);
  33.         if($owner instanceof CustomPlayer){
  34.             parent::__construct($location, $owner, $nbt);
  35.             $this->setPosition($location->add(0, $owner->getEyeHeight() - 0.1, 0));
  36.             $this->setMotion($owner->getDirectionVector());
  37.             GrapnelHandler::setFishingHook($this, $owner->getName());
  38.             $this->handleHookCasting($this->motion->x, $this->motion->y, $this->motion->z, 1.5, 1.0);
  39.         }
  40.     }
  41.  
  42.     public function onHitEntity(Entity $entityHit, RayTraceResult $hitResult) : void{}//Do nothing
  43.  
  44.     public function handleHookCasting(float $x, float $y, float $z, float $f1, float $f2)
  45.     {
  46.         $rand = new Random();
  47.         $magic = 0.007499999832361937;
  48.         $f = sqrt($x * $x + $y * $y + $z * $z); //Racine carré dégueu
  49.         $x = $x / $f;
  50.         $y = $y / $f;
  51.         $z = $z / $f;
  52.         $x += $rand->nextSignedFloat() * $magic * $f2;
  53.         $y += $rand->nextSignedFloat() * $magic * $f2;
  54.         $z += $rand->nextSignedFloat() * $magic * $f2;
  55.         $x *= $f1;
  56.         $y *= $f1;
  57.         $z *= $f1;
  58.         $this->motion->x += $x;
  59.         $this->motion->y += $y;
  60.         $this->motion->z += $z;
  61.     }
  62.  
  63.     public function entityBaseTick(int $tickDiff = 1) : bool
  64.     {
  65.         var_dump("base tick");
  66.         if($this->isClosed()) return false;
  67.         var_dump("base tick 2");
  68.         $hasUpdate = parent::entityBaseTick($tickDiff);
  69.         $owner = $this->getOwningEntity();
  70.         if($owner instanceof Player){
  71.             if(!$owner->getInventory()->getItemInHand() instanceof Grapnel || !$owner->isAlive() || $owner->isClosed()) {
  72.                 var_dump("close 1");
  73.                 GrapnelHandler::setFishingHook(null, $owner->getName());
  74.                 $this->flagForDespawn();
  75.                 return false;
  76.             }
  77.         } else {
  78.             var_dump("close 2");
  79.             $this->flagForDespawn();
  80.             return false;
  81.         }
  82.  
  83.         return $hasUpdate;
  84.     }
  85.  
  86.     public function onDispose() : void
  87.     {
  88.         var_dump("dispose");
  89.         $owner = $this->getOwningEntity();
  90.         if($owner instanceof Player) GrapnelHandler::setFishingHook(null, $owner->getName());
  91.     }
  92.  
  93.     public function handleHookRetraction() : void
  94.     {
  95.         var_dump("hook retraction");
  96.         $owner = $this->getOwningEntity();
  97.         if(!$owner instanceof Player) return;
  98.  
  99.         if(!isset($this)) {
  100.             Server::getInstance()->getLogger()->warning("Unset hook entity for grapnel from ".$owner->getNameTag());
  101.             return;
  102.         }
  103.  
  104.         $posHook = $this->getPosition();
  105.         if(!isset($posHook)) {
  106.             Server::getInstance()->getLogger()->warning("Unset pos hook entity for grapnel from ".$owner->getNameTag());
  107.             return;
  108.         }
  109.  
  110.         $ownerPos = $owner->getPosition();
  111.         if(!isset($ownerPos)) {
  112.             Server::getInstance()->getLogger()->warning("Unset owner pos for grapnel from ".$owner->getNameTag());
  113.             return;
  114.         }
  115.         $dist = sqrt((($posHook->x - $ownerPos->x) ** 2) + ((($posHook->y - $ownerPos->y) ** 2) * 0.5) + (($posHook->z - $ownerPos->z) ** 2));
  116.         //Calcul de l'hypoténuse entre les 2 vecteurs en prenant moins en compte le Y
  117.         //code initial de $vector->distanceSquared()
  118.         $owner->setMotion($posHook->subtractVector($ownerPos)->multiply($this->getGrapplingSpeed($dist)));
  119.         var_dump("close 3");
  120.         $this->flagForDespawn();
  121.         GrapnelHandler::setFishingHook(null, $owner->getName());
  122.     }
  123.    
  124.     private function getGrapplingSpeed(float $dist) : float
  125.     {
  126.         $motion = self::$coef1 / ($dist + self::$coef2);
  127.         return (float)$motion;
  128.     }
  129.  
  130.     protected function getInitialSizeInfo(): EntitySizeInfo
  131.     {
  132.         return new EntitySizeInfo(0.25, 0.25);
  133.     }
  134.  
  135.     public static function getNetworkTypeId(): string
  136.     {
  137.         return self::NETWORK_ID;
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment