SHOW:
|
|
- or go back to the newest paste.
| 1 | - | public function shot(PlayerInteractEvent $event){
|
| 1 | + | public function shot(PlayerInteractEvent $event) : void {
|
| 2 | - | $player = $event->getPlayer(); |
| 2 | + | if($player->getInventory()->getItemInHand()->getId() == 388){
|
| 3 | - | $item = $player->getInventory()->getItemInHand(); |
| 3 | + | $this->spawnParticles($event->getPlayer()); |
| 4 | - | |
| 4 | + | $this->calculateDamage($event->getPlayer()); |
| 5 | - | if($item->getId() == 388){
|
| 5 | + | } |
| 6 | - | $from = new Vector3($player->x, $player->y + $player->getEyeHeight(), $player->z); |
| 6 | + | } |
| 7 | - | for ($i = 1; $i < 10; $i++) {
|
| 7 | + | |
| 8 | - | $player->getLevel()->addParticle($components = new FlameParticle(new Vector3($from->x + $player->getDirectionVector()->x * $i, $from->y + $player->getDirectionVector()->y * $i, $from->z + $player->getDirectionVector()->z * $i))); |
| 8 | + | public function calculateDamage($player) : void {
|
| 9 | - | $boundingBox = new AxisAlignedBB($components->x - 1, $components->y - 1, $components->z - 1, $components->x + 1, $components->y + 1, $components->z + 1); |
| 9 | + | $delta = $player->getDirectionVector() * 20; |
| 10 | - | $list = $player->getLevel()->getCollidingEntities($boundingBox); |
| 10 | + | $from = new Vector3($player->x, $player->y + $player->getEyeHeight(), $player->z); |
| 11 | - | |
| 11 | + | $to = new Vector3($player->x + $delta, $player->y + $player->getEyeHeight() + $delta, $player->z + $delta); |
| 12 | - | foreach ($list as $entity) {
|
| 12 | + | |
| 13 | - | if($entity instanceof Player){
|
| 13 | + | foreach ($player->getLevel()->getPlayer() as $p) {
|
| 14 | - | if($entity->getName() != $player->getName()){
|
| 14 | + | // Если расстояние между игроками больше 60 блоков - не проверяем |
| 15 | - | $entity->sendMessage("в тебя попали");
|
| 15 | + | if ($player->distance($p) > 30) continue; |
| 16 | - | $player->sendMessage("ты попал");
|
| 16 | + | |
| 17 | - | } |
| 17 | + | // Прямая задана двумя точками, считаем расстояние от прямой до каждого игрока |
| 18 | - | } |
| 18 | + | // https://ru.wikipedia.org/wiki/Расстояние_от_точки_до_прямой_на_плоскости |
| 19 | - | } |
| 19 | + | // Чтобы не мучиться с каноническими уравнениями и решениями СЛАУ |
| 20 | - | } |
| 20 | + | // Подсчитаем расстояние от игрока до лазера в двух плоскостях Oxy, и Oxz |
| 21 | - | } |
| 21 | + | $Oxy = abs(($to->y - $from->y) * $p->x) - ($to->x - $from->x) * $p->y + $to->x * $from->y - $to->y * $from->x)) / |
| 22 | - | } |
| 22 | + | sqrt(pow($to->y - $from->y, 2) + pow($to->x - $from->x, 2)); |
| 23 | if ($Oxy < 2) continue; | |
| 24 | $Oxz = abs(($to->z - $from->z) * $p->x) - ($to->x - $from->x) * $p->z + $to->x * $from->z - $to->z * $from->x)) / | |
| 25 | sqrt(pow($to->z - $from->z, 2) + pow($to->x - $from->x, 2)); | |
| 26 | if ($Oxz < 2) continue; | |
| 27 | $p->sendMessage("По вам попали!!!");
| |
| 28 | } | |
| 29 | } | |
| 30 | ||
| 31 | public function spawnParticles(Player $player) : void {
| |
| 32 | // Create redstone particle | |
| 33 | if (class_exists('\pocketmine\network\mcpe\protocol\LevelEventPacket', false)) {
| |
| 34 | $pk = new \pocketmine\network\mcpe\protocol\LevelEventPacket; | |
| 35 | $pk->evid = \pocketmine\network\mcpe\protocol\LevelEventPacket::EVENT_ADD_PARTICLE_MASK | 7; | |
| 36 | }else {
| |
| 37 | $pk = new \pocketmine\network\protocol\LevelEventPacket; | |
| 38 | $pk->evid = \pocketmine\network\protocol\LevelEventPacket::EVENT_ADD_PARTICLE_MASK | 7; | |
| 39 | } | |
| 40 | $pk->data = 2; // lifetime | |
| 41 | ||
| 42 | $particles = array(); | |
| 43 | ||
| 44 | $addParticle = function ($x, $y, $z, $pk) {
| |
| 45 | $particle = clone $pk; | |
| 46 | $particle->x = $x; | |
| 47 | $particle->y = $y; | |
| 48 | $particle->z = $z; | |
| 49 | $particle->encode(); | |
| 50 | return $particle; | |
| 51 | }; | |
| 52 | ||
| 53 | $direction = $player->getDirectionVector(); | |
| 54 | $pos = new Vector3($player->x, $player->y + $player->getEyeHeight(), $player->z); | |
| 55 | for ($i = 0; $i < 20; $i++) {
| |
| 56 | $pos->add($direction->x, $direction->y, $direction->z); | |
| 57 | $particles[] = $addParticle($pos->x, $pos->y, $pos->z, $pk); | |
| 58 | } | |
| 59 | ||
| 60 | $player->getLevel()->addChunkPacket($player->x >> 4, $player->z >> 4, $this->batchPackets($particles )); | |
| 61 | } | |
| 62 | ||
| 63 | private function batchPackets($packets) {
| |
| 64 | if (class_exists('pocketmine\network\mcpe\protocol\BatchPacket', false))
| |
| 65 | $pk = new \pocketmine\network\mcpe\protocol\BatchPacket; | |
| 66 | else $pk = new \pocketmine\network\protocol\BatchPacket; | |
| 67 | ||
| 68 | foreach ($packets as $packet) | |
| 69 | $pk->payload .= \pocketmine\utils\Binary::writeUnsignedVarInt(strlen($packet->buffer)) . $packet->buffer; | |
| 70 | ||
| 71 | $pk->payload = zlib_encode($pk->payload, ZLIB_ENCODING_DEFLATE, 1); | |
| 72 | $pk->encode(); | |
| 73 | ||
| 74 | return $pk; | |
| 75 | } |