PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
PrimedTNT.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 
22 namespace pocketmine\entity;
23 
24 
32 
33 class PrimedTNT extends Entity implements Explosive{
34  const NETWORK_ID = 65;
35 
36  public $width = 0.98;
37  public $length = 0.98;
38  public $height = 0.98;
39 
40  protected $gravity = 0.04;
41  protected $drag = 0.02;
42 
43  protected $fuse;
44 
45  public $canCollide = false;
46 
47  protected function initEntity(){
48  if(isset($this->namedtag->Fuse)){
49  $this->fuse = $this->namedtag["Fuse"];
50  }else{
51  $this->fuse = 80;
52  }
53  }
54 
55 
56  public function canCollideWith(Entity $entity){
57  return false;
58  }
59 
60  public function getData(){
61  return [
62  16 => ["type" => 0, "value" => $this->fuse],
63  ];
64  }
65 
66  public function saveNBT(){
67  parent::saveNBT();
68  $this->namedtag->Fuse = new Byte("Fuse", $this->fuse);
69  }
70 
71  public function onUpdate($currentTick){
72 
73  if($this->closed){
74  return false;
75  }
76 
77  $this->timings->startTiming();
78 
79  $tickDiff = max(1, $currentTick - $this->lastUpdate);
80  $this->lastUpdate = $currentTick;
81 
82  $hasUpdate = $this->entityBaseTick($tickDiff);
83 
84  if(!$this->dead){
85 
86  $this->motionY -= $this->gravity;
87 
88  $this->move($this->motionX, $this->motionY, $this->motionZ);
89 
90  $friction = 1 - $this->drag;
91 
92  $this->motionX *= $friction;
93  $this->motionY *= $friction;
94  $this->motionZ *= $friction;
95 
96  $this->updateMovement();
97 
98  if($this->onGround){
99  $this->motionY *= -0.5;
100  $this->motionX *= 0.7;
101  $this->motionZ *= 0.7;
102  }
103 
104  $this->fuse -= $tickDiff;
105 
106  if($this->fuse <= 0){
107  $this->kill();
108  $this->explode();
109  }else{
110  $this->sendMetadata($this->getViewers());
111  }
112 
113  }
114 
115 
116  return $hasUpdate or $this->fuse >= 0 or $this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0;
117  }
118 
119  public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){
120 
121  }
122 
123  public function heal($amount, $source = EntityRegainHealthEvent::CAUSE_MAGIC){
124 
125  }
126 
127  public function explode(){
128  $this->server->getPluginManager()->callEvent($ev = new ExplosionPrimeEvent($this, 4));
129 
130  if(!$ev->isCancelled()){
131  $explosion = new Explosion($this, $ev->getForce(), $this);
132  if($ev->isBlockBreaking()){
133  $explosion->explodeA();
134  }
135  $explosion->explodeB();
136  }
137  }
138 
139  public function spawnTo(Player $player){
140  $pk = new AddEntityPacket();
141  $pk->type = PrimedTNT::NETWORK_ID;
142  $pk->eid = $this->getId();
143  $pk->x = $this->x;
144  $pk->y = $this->y;
145  $pk->z = $this->z;
146  $pk->did = 0;
147  $player->dataPacket($pk);
148 
149  $player->addEntityMotion($this->getId(), $this->motionX, $this->motionY, $this->motionZ);
150 
151  parent::spawnTo($player);
152  }
153 }
dataPacket(DataPacket $packet, $needACK=false)
Definition: Player.php:745