PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
Squid.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\entity;
25
33use function atan2;
34use function mt_rand;
35use function sqrt;
36use const M_PI;
37
38class Squid extends WaterAnimal{
39
40 public static function getNetworkTypeId() : string{ return EntityIds::SQUID; }
41
42 public ?Vector3 $swimDirection = null;
43 public float $swimSpeed = 0.1;
44
45 private int $switchDirectionTicker = 0;
46
47 protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.95, 0.95); }
48
49 public function initEntity(CompoundTag $nbt) : void{
50 $this->setMaxHealth(10);
51 parent::initEntity($nbt);
52 }
53
54 public function getName() : string{
55 return "Squid";
56 }
57
58 public function attack(EntityDamageEvent $source) : void{
59 parent::attack($source);
60 if($source->isCancelled()){
61 return;
62 }
63
64 if($source instanceof EntityDamageByEntityEvent){
65 $this->swimSpeed = mt_rand(150, 350) / 2000;
66 $e = $source->getDamager();
67 if($e !== null){
68 $this->swimDirection = $this->location->subtractVector($e->location)->normalize();
69 }
70
72 }
73 }
74
75 private function generateRandomDirection() : Vector3{
76 return new Vector3(mt_rand(-1000, 1000) / 1000, mt_rand(-500, 500) / 1000, mt_rand(-1000, 1000) / 1000);
77 }
78
79 protected function entityBaseTick(int $tickDiff = 1) : bool{
80 if($this->closed){
81 return false;
82 }
83
84 if(++$this->switchDirectionTicker === 100){
85 $this->switchDirectionTicker = 0;
86 if(mt_rand(0, 100) < 50){
87 $this->swimDirection = null;
88 }
89 }
90
91 $hasUpdate = parent::entityBaseTick($tickDiff);
92
93 if($this->isAlive()){
94
95 if($this->location->y > 62 && $this->swimDirection !== null){
96 $this->swimDirection = $this->swimDirection->withComponents(null, -0.5, null);
97 }
98
99 $inWater = $this->isUnderwater();
100 $this->setHasGravity(!$inWater);
101 if(!$inWater){
102 $this->swimDirection = null;
103 }elseif($this->swimDirection !== null){
104 if($this->motion->lengthSquared() <= $this->swimDirection->lengthSquared()){
105 $this->motion = $this->swimDirection->multiply($this->swimSpeed);
106 }
107 }else{
108 $this->swimDirection = $this->generateRandomDirection();
109 $this->swimSpeed = mt_rand(50, 100) / 2000;
110 }
111
112 $f = sqrt(($this->motion->x ** 2) + ($this->motion->z ** 2));
113 $this->setRotation(
114 -atan2($this->motion->x, $this->motion->z) * 180 / M_PI,
115 -atan2($f, $this->motion->y) * 180 / M_PI
116 );
117 }
118
119 return $hasUpdate;
120 }
121
122 public function getDrops() : array{
123 return [
124 VanillaItems::INK_SAC()->setCount(mt_rand(1, 3))
125 ];
126 }
127}
broadcastAnimation(Animation $animation, ?array $targets=null)
Definition: Entity.php:1687