PocketMine-MP 5.23.3 git-976fc63567edab7a6fb6aeae739f43cf9fe57de4
Loading...
Searching...
No Matches
Projectile.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\projectile;
25
47use function atan2;
48use function ceil;
49use function count;
50use function sqrt;
51use const M_PI;
52use const PHP_INT_MAX;
53
54abstract class Projectile extends Entity{
55 private const TAG_STUCK_ON_BLOCK_POS = "StuckToBlockPos";
56 private const TAG_DAMAGE = "damage"; //TAG_Double
57 private const TAG_TILE_X = "tileX"; //TAG_Int
58 private const TAG_TILE_Y = "tileY"; //TAG_Int
59 private const TAG_TILE_Z = "tileZ"; //TAG_Int
60
61 protected float $damage = 0.0;
62 protected ?Vector3 $blockHit = null;
63
64 public function __construct(Location $location, ?Entity $shootingEntity, ?CompoundTag $nbt = null){
65 parent::__construct($location, $nbt);
66 if($shootingEntity !== null){
67 $this->setOwningEntity($shootingEntity);
68 }
69 }
70
71 public function attack(EntityDamageEvent $source) : void{
72 if($source->getCause() === EntityDamageEvent::CAUSE_VOID){
73 parent::attack($source);
74 }
75 }
76
77 protected function initEntity(CompoundTag $nbt) : void{
78 parent::initEntity($nbt);
79
80 $this->setMaxHealth(1);
81 $this->setHealth(1);
82 $this->damage = $nbt->getDouble(self::TAG_DAMAGE, $this->damage);
83
84 if(($stuckOnBlockPosTag = $nbt->getListTag(self::TAG_STUCK_ON_BLOCK_POS)) !== null){
85 if($stuckOnBlockPosTag->getTagType() !== NBT::TAG_Int || count($stuckOnBlockPosTag) !== 3){
86 throw new SavedDataLoadingException(self::TAG_STUCK_ON_BLOCK_POS . " tag should be a list of 3 TAG_Int");
87 }
88
90 $values = $stuckOnBlockPosTag->getValue();
91
92 $this->blockHit = new Vector3($values[0]->getValue(), $values[1]->getValue(), $values[2]->getValue());
93 }elseif(($tileXTag = $nbt->getTag(self::TAG_TILE_X)) instanceof IntTag && ($tileYTag = $nbt->getTag(self::TAG_TILE_Y)) instanceof IntTag && ($tileZTag = $nbt->getTag(self::TAG_TILE_Z)) instanceof IntTag){
94 $this->blockHit = new Vector3($tileXTag->getValue(), $tileYTag->getValue(), $tileZTag->getValue());
95 }
96 }
97
98 public function canCollideWith(Entity $entity) : bool{
99 return ($entity instanceof Living || $entity instanceof EndCrystal) && !$this->onGround;
100 }
101
102 public function canBeCollidedWith() : bool{
103 return false;
104 }
105
110 public function getBaseDamage() : float{
111 return $this->damage;
112 }
113
117 public function setBaseDamage(float $damage) : void{
118 $this->damage = $damage;
119 }
120
124 public function getResultDamage() : int{
125 return (int) ceil($this->damage);
126 }
127
128 public function saveNBT() : CompoundTag{
129 $nbt = parent::saveNBT();
130
131 $nbt->setDouble(self::TAG_DAMAGE, $this->damage);
132
133 if($this->blockHit !== null){
134 $nbt->setTag(self::TAG_STUCK_ON_BLOCK_POS, new ListTag([
135 new IntTag($this->blockHit->getFloorX()),
136 new IntTag($this->blockHit->getFloorY()),
137 new IntTag($this->blockHit->getFloorZ())
138 ]));
139 }
140
141 return $nbt;
142 }
143
144 protected function applyDragBeforeGravity() : bool{
145 return true;
146 }
147
148 public function onNearbyBlockChange() : void{
149 if($this->blockHit !== null && $this->getWorld()->isInLoadedTerrain($this->blockHit)){
150 $blockHit = $this->getWorld()->getBlock($this->blockHit);
151 if(!$blockHit->collidesWithBB($this->getBoundingBox()->expandedCopy(0.001, 0.001, 0.001))){
152 $this->blockHit = null;
153 }
154 }
155
156 parent::onNearbyBlockChange();
157 }
158
159 public function hasMovementUpdate() : bool{
160 return $this->blockHit === null && parent::hasMovementUpdate();
161 }
162
163 protected function move(float $dx, float $dy, float $dz) : void{
164 $this->blocksAround = null;
165
166 Timings::$projectileMove->startTiming();
167 Timings::$projectileMoveRayTrace->startTiming();
168
169 $start = $this->location->asVector3();
170 $end = $start->add($dx, $dy, $dz);
171
172 $hitResult = null;
173
174 $world = $this->getWorld();
175 foreach(VoxelRayTrace::betweenPoints($start, $end) as $vector3){
176 $block = $world->getBlockAt($vector3->x, $vector3->y, $vector3->z);
177
178 $blockHitResult = $this->calculateInterceptWithBlock($block, $start, $end);
179 if($blockHitResult !== null){
180 $end = $blockHitResult->hitVector;
181 $hitResult = [$block, $blockHitResult];
182 break;
183 }
184 }
185
186 $entityDistance = PHP_INT_MAX;
187
188 $newDiff = $end->subtractVector($start);
189 foreach($world->getCollidingEntities($this->boundingBox->addCoord($newDiff->x, $newDiff->y, $newDiff->z)->expand(1, 1, 1), $this) as $entity){
190 if($entity->getId() === $this->getOwningEntityId() && $this->ticksLived < 5){
191 continue;
192 }
193
194 $entityBB = $entity->boundingBox->expandedCopy(0.3, 0.3, 0.3);
195 $entityHitResult = $entityBB->calculateIntercept($start, $end);
196
197 if($entityHitResult === null){
198 continue;
199 }
200
201 $distance = $this->location->distanceSquared($entityHitResult->hitVector);
202
203 if($distance < $entityDistance){
204 $entityDistance = $distance;
205 $hitResult = [$entity, $entityHitResult];
206 $end = $entityHitResult->hitVector;
207 }
208 }
209
210 Timings::$projectileMoveRayTrace->stopTiming();
211
212 $this->location = Location::fromObject(
213 $end,
214 $this->location->world,
215 $this->location->yaw,
216 $this->location->pitch
217 );
218 $this->recalculateBoundingBox();
219
220 if($hitResult !== null){
221 [$objectHit, $rayTraceResult] = $hitResult;
222 if($objectHit instanceof Entity){
223 $ev = new ProjectileHitEntityEvent($this, $rayTraceResult, $objectHit);
224 $specificHitFunc = fn() => $this->onHitEntity($objectHit, $rayTraceResult);
225 }else{
226 $ev = new ProjectileHitBlockEvent($this, $rayTraceResult, $objectHit);
227 $specificHitFunc = fn() => $this->onHitBlock($objectHit, $rayTraceResult);
228 }
229
230 $ev->call();
231 $this->onHit($ev);
232 $specificHitFunc();
233
234 $this->isCollided = $this->onGround = true;
235 $this->motion = Vector3::zero();
236 }else{
237 $this->isCollided = $this->onGround = false;
238 $this->blockHit = null;
239
240 //recompute angles...
241 $f = sqrt(($this->motion->x ** 2) + ($this->motion->z ** 2));
242 $this->setRotation(
243 atan2($this->motion->x, $this->motion->z) * 180 / M_PI,
244 atan2($this->motion->y, $f) * 180 / M_PI
245 );
246 }
247
248 $world->onEntityMoved($this);
249 $this->checkBlockIntersections();
250
251 Timings::$projectileMove->stopTiming();
252 }
253
261 protected function calculateInterceptWithBlock(Block $block, Vector3 $start, Vector3 $end) : ?RayTraceResult{
262 return $block->calculateIntercept($start, $end);
263 }
264
269 protected function onHit(ProjectileHitEvent $event) : void{
270
271 }
272
276 protected function onHitEntity(Entity $entityHit, RayTraceResult $hitResult) : void{
277 $damage = $this->getResultDamage();
278
279 if($damage >= 0){
280 $owner = $this->getOwningEntity();
281 if($owner === null){
282 $ev = new EntityDamageByEntityEvent($this, $entityHit, EntityDamageEvent::CAUSE_PROJECTILE, $damage);
283 }else{
284 $ev = new EntityDamageByChildEntityEvent($owner, $this, $entityHit, EntityDamageEvent::CAUSE_PROJECTILE, $damage);
285 }
286
287 $entityHit->attack($ev);
288
289 if($this->isOnFire()){
290 $ev = new EntityCombustByEntityEvent($this, $entityHit, 5);
291 $ev->call();
292 if(!$ev->isCancelled()){
293 $entityHit->setOnFire($ev->getDuration());
294 }
295 }
296 }
297
298 $this->flagForDespawn();
299 }
300
304 protected function onHitBlock(Block $blockHit, RayTraceResult $hitResult) : void{
305 $this->blockHit = $blockHit->getPosition()->asVector3();
306 $blockHit->onProjectileHit($this, $hitResult);
307 }
308}
onProjectileHit(Projectile $projectile, RayTraceResult $hitResult)
Definition Block.php:900
setOwningEntity(?Entity $owner)
Definition Entity.php:418
setHealth(float $amount)
Definition Entity.php:594
onHitBlock(Block $blockHit, RayTraceResult $hitResult)
calculateInterceptWithBlock(Block $block, Vector3 $start, Vector3 $end)
onHit(ProjectileHitEvent $event)
setTag(string $name, Tag $tag)
setDouble(string $name, float $value)