Skip to content

Commit

Permalink
Implement crawling
Browse files Browse the repository at this point in the history
  • Loading branch information
DavyCraft648 committed Mar 2, 2024
1 parent e8bf3aa commit 18654c3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/entity/Living.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ abstract class Living extends Entity{
protected bool $sneaking = false;
protected bool $gliding = false;
protected bool $swimming = false;
protected bool $crawling = false;

protected function getInitialDragMultiplier() : float{ return 0.02; }

Expand Down Expand Up @@ -268,11 +269,21 @@ public function setSwimming(bool $value = true) : void{
$this->recalculateSize();
}

public function isCrawling() : bool{
return $this->crawling;
}

public function setCrawling(bool $value = true) : void{
$this->crawling = $value;
$this->networkPropertiesDirty = true;
$this->recalculateSize();
}

private function recalculateSize() : void{
$size = $this->getInitialSizeInfo();
if($this->isSwimming() || $this->isGliding()){
if($this->isSwimming() || $this->isGliding() || $this->isCrawling()){
$width = $size->getWidth();
$this->setSize((new EntitySizeInfo($width, $width, $width * 0.9))->scale($this->getScale()));
$this->setSize((new EntitySizeInfo(5 / 8, $width, 5 / 8 * 0.9))->scale($this->getScale()));
}elseif($this->isSneaking()){
$this->setSize((new EntitySizeInfo(3 / 4 * $size->getHeight(), $size->getWidth(), 3 / 4 * $size->getEyeHeight()))->scale($this->getScale()));
}else{
Expand Down Expand Up @@ -894,6 +905,7 @@ protected function syncNetworkData(EntityMetadataCollection $properties) : void{
$properties->setGenericFlag(EntityMetadataFlags::SPRINTING, $this->sprinting);
$properties->setGenericFlag(EntityMetadataFlags::GLIDING, $this->gliding);
$properties->setGenericFlag(EntityMetadataFlags::SWIMMING, $this->swimming);
$properties->setGenericFlag(EntityMetadataFlags::CRAWLING, $this->crawling);
}

protected function onDispose() : void{
Expand Down
40 changes: 40 additions & 0 deletions src/event/player/PlayerToggleCrawlEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/

declare(strict_types=1);

namespace pocketmine\event\player;

use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
use pocketmine\player\Player;

class PlayerToggleCrawlEvent extends PlayerEvent implements Cancellable{
use CancellableTrait;

public function __construct(Player $player, protected bool $isCrawling){
$this->player = $player;
}

public function isCrawling() : bool{
return $this->isCrawling;
}
}
18 changes: 14 additions & 4 deletions src/player/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
use pocketmine\event\player\PlayerPostChunkSendEvent;
use pocketmine\event\player\PlayerQuitEvent;
use pocketmine\event\player\PlayerRespawnEvent;
use pocketmine\event\player\PlayerToggleCrawlEvent;
use pocketmine\event\player\PlayerToggleFlightEvent;
use pocketmine\event\player\PlayerToggleGlideEvent;
use pocketmine\event\player\PlayerToggleSneakEvent;
Expand Down Expand Up @@ -1994,10 +1995,6 @@ public function toggleFlight(bool $fly) : bool{
return true;
}

public function toggleCrawl(bool $crawl) : bool{
return false;
}

public function toggleGlide(bool $glide) : bool{
if($glide === $this->gliding){
return true;
Expand All @@ -2024,6 +2021,19 @@ public function toggleSwim(bool $swim) : bool{
return true;
}

public function toggleCrawl(bool $crawl) : bool{
if($crawl === $this->crawling){
return true;
}
$ev = new PlayerToggleCrawlEvent($this, $crawl);
$ev->call();
if($ev->isCancelled()){
return false;
}
$this->setCrawling($crawl);
return true;
}

public function emote(string $emoteId) : void{
$currentTick = $this->server->getTick();
if($currentTick - $this->lastEmoteTick > 5){
Expand Down

0 comments on commit 18654c3

Please sign in to comment.