Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace minicore\entities;
- use minicore\handler\Anti\GrapnelHandler;
- use minicore\CustomPlayer;
- use minicore\items\tool\Grapnel;
- use pocketmine\entity\EntitySizeInfo;
- use pocketmine\entity\Location;
- use pocketmine\network\mcpe\protocol\types\entity\EntityIds;
- use pocketmine\player\Player;
- use pocketmine\entity\Entity;
- use pocketmine\entity\projectile\Projectile;
- use pocketmine\math\RayTraceResult;
- use pocketmine\nbt\tag\CompoundTag;
- use pocketmine\Server;
- use pocketmine\utils\Random;
- class FishingHook extends Projectile{ //In part from from GrapplingHook / korado531m7 TODO: Fix this
- public const NETWORK_ID = EntityIds::FISHING_HOOK; //Restant de PM3
- public float $height = 0.25;
- public float $width = 0.25;
- protected $gravity = 0.1;
- public static float $coef1 = 2;//coef1/dist+coef2
- public static float $coef2 = 1;
- public function __construct(Location $location, ?Entity $owner, ?CompoundTag $nbt = null)
- {
- $this->setHasGravity(true);
- if($owner instanceof CustomPlayer){
- parent::__construct($location, $owner, $nbt);
- $this->setPosition($location->add(0, $owner->getEyeHeight() - 0.1, 0));
- $this->setMotion($owner->getDirectionVector());
- GrapnelHandler::setFishingHook($this, $owner->getName());
- $this->handleHookCasting($this->motion->x, $this->motion->y, $this->motion->z, 1.5, 1.0);
- }
- }
- public function onHitEntity(Entity $entityHit, RayTraceResult $hitResult) : void{}//Do nothing
- public function handleHookCasting(float $x, float $y, float $z, float $f1, float $f2)
- {
- $rand = new Random();
- $magic = 0.007499999832361937;
- $f = sqrt($x * $x + $y * $y + $z * $z); //Racine carré dégueu
- $x = $x / $f;
- $y = $y / $f;
- $z = $z / $f;
- $x += $rand->nextSignedFloat() * $magic * $f2;
- $y += $rand->nextSignedFloat() * $magic * $f2;
- $z += $rand->nextSignedFloat() * $magic * $f2;
- $x *= $f1;
- $y *= $f1;
- $z *= $f1;
- $this->motion->x += $x;
- $this->motion->y += $y;
- $this->motion->z += $z;
- }
- public function entityBaseTick(int $tickDiff = 1) : bool
- {
- var_dump("base tick");
- if($this->isClosed()) return false;
- var_dump("base tick 2");
- $hasUpdate = parent::entityBaseTick($tickDiff);
- $owner = $this->getOwningEntity();
- if($owner instanceof Player){
- if(!$owner->getInventory()->getItemInHand() instanceof Grapnel || !$owner->isAlive() || $owner->isClosed()) {
- var_dump("close 1");
- GrapnelHandler::setFishingHook(null, $owner->getName());
- $this->flagForDespawn();
- return false;
- }
- } else {
- var_dump("close 2");
- $this->flagForDespawn();
- return false;
- }
- return $hasUpdate;
- }
- public function onDispose() : void
- {
- var_dump("dispose");
- $owner = $this->getOwningEntity();
- if($owner instanceof Player) GrapnelHandler::setFishingHook(null, $owner->getName());
- }
- public function handleHookRetraction() : void
- {
- var_dump("hook retraction");
- $owner = $this->getOwningEntity();
- if(!$owner instanceof Player) return;
- if(!isset($this)) {
- Server::getInstance()->getLogger()->warning("Unset hook entity for grapnel from ".$owner->getNameTag());
- return;
- }
- $posHook = $this->getPosition();
- if(!isset($posHook)) {
- Server::getInstance()->getLogger()->warning("Unset pos hook entity for grapnel from ".$owner->getNameTag());
- return;
- }
- $ownerPos = $owner->getPosition();
- if(!isset($ownerPos)) {
- Server::getInstance()->getLogger()->warning("Unset owner pos for grapnel from ".$owner->getNameTag());
- return;
- }
- $dist = sqrt((($posHook->x - $ownerPos->x) ** 2) + ((($posHook->y - $ownerPos->y) ** 2) * 0.5) + (($posHook->z - $ownerPos->z) ** 2));
- //Calcul de l'hypoténuse entre les 2 vecteurs en prenant moins en compte le Y
- //code initial de $vector->distanceSquared()
- $owner->setMotion($posHook->subtractVector($ownerPos)->multiply($this->getGrapplingSpeed($dist)));
- var_dump("close 3");
- $this->flagForDespawn();
- GrapnelHandler::setFishingHook(null, $owner->getName());
- }
- private function getGrapplingSpeed(float $dist) : float
- {
- $motion = self::$coef1 / ($dist + self::$coef2);
- return (float)$motion;
- }
- protected function getInitialSizeInfo(): EntitySizeInfo
- {
- return new EntitySizeInfo(0.25, 0.25);
- }
- public static function getNetworkTypeId(): string
- {
- return self::NETWORK_ID;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment