PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
Loading...
Searching...
No Matches
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
34use function atan2;
35use function mt_rand;
36use function sqrt;
37use const M_PI;
38
39class Squid extends WaterAnimal{
40
41 public function getNetworkTypeId() : string{ return EntityIds::SQUID; }
42
43 public ?Vector3 $swimDirection = null;
44 public float $swimSpeed = 0.1;
45
46 private int $switchDirectionTicker = 0;
47
48 protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.95, 0.95); }
49
50 public function initEntity(CompoundTag $nbt) : void{
51 $this->setMaxHealth(10);
52 parent::initEntity($nbt);
53 }
54
55 public function getName() : string{
56 return "Squid";
57 }
58
59 public function attack(EntityDamageEvent $source) : void{
60 parent::attack($source);
61 if($source->isCancelled()){
62 return;
63 }
64
65 if($source instanceof EntityDamageByEntityEvent){
66 $this->swimSpeed = mt_rand(150, 350) / 2000;
67 $e = $source->getDamager();
68 if($e !== null){
69 $this->swimDirection = $this->location->subtractVector($e->location)->normalize();
70 }
71
73 }
74 }
75
76 private function generateRandomDirection() : Vector3{
77 return new Vector3(mt_rand(-1000, 1000) / 1000, mt_rand(-500, 500) / 1000, mt_rand(-1000, 1000) / 1000);
78 }
79
80 protected function entityBaseTick(int $tickDiff = 1) : bool{
81 if($this->closed){
82 return false;
83 }
84
85 if(++$this->switchDirectionTicker === 100){
86 $this->switchDirectionTicker = 0;
87 if(mt_rand(0, 100) < 50){
88 $this->swimDirection = null;
89 }
90 }
91
92 $hasUpdate = parent::entityBaseTick($tickDiff);
93
94 if($this->isAlive()){
95
96 if($this->location->y > 62 && $this->swimDirection !== null){
97 $this->swimDirection = $this->swimDirection->withComponents(null, -0.5, null);
98 }
99
100 $inWater = $this->isUnderwater();
101 $this->setHasGravity(!$inWater);
102 if(!$inWater){
103 $this->swimDirection = null;
104 }elseif($this->swimDirection !== null){
105 if($this->motion->lengthSquared() <= $this->swimDirection->lengthSquared()){
106 $this->motion = $this->swimDirection->multiply($this->swimSpeed);
107 }
108 }else{
109 $this->swimDirection = $this->generateRandomDirection();
110 $this->swimSpeed = mt_rand(50, 100) / 2000;
111 }
112
113 $f = sqrt(($this->motion->x ** 2) + ($this->motion->z ** 2));
114 $this->setRotation(
115 -atan2($this->motion->x, $this->motion->z) * 180 / M_PI,
116 -atan2($f, $this->motion->y) * 180 / M_PI
117 );
118 }
119
120 return $hasUpdate;
121 }
122
123 public function getDrops() : array{
124 return [
125 VanillaItems::INK_SAC()->setCount(mt_rand(1, 3))
126 ];
127 }
128
129 public function getPickedItem() : ?Item{
130 return VanillaItems::SQUID_SPAWN_EGG();
131 }
132}
broadcastAnimation(Animation $animation, ?array $targets=null)
Definition Entity.php:1695