Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

adding dolphins, polar bears and rabbits #292

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/pocketmine/entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@
use pocketmine\entity\passive\Cat;
use pocketmine\entity\passive\Chicken;
use pocketmine\entity\passive\Cow;
use pocketmine\entity\passive\Dolphin;
use pocketmine\entity\passive\Horse;
use pocketmine\entity\passive\Mooshroom;
use pocketmine\entity\passive\Pig;
use pocketmine\entity\passive\PolarBear;
use pocketmine\entity\passive\Rabbit;
use pocketmine\entity\passive\Sheep;
use pocketmine\entity\passive\Squid;
use pocketmine\entity\passive\Villager;
Expand Down Expand Up @@ -398,6 +401,9 @@ public static function init() : void{
Entity::registerEntity(Snowball::class, false, ['Snowball', 'minecraft:snowball']);
Entity::registerEntity(SplashPotion::class, false, ['ThrownPotion', 'minecraft:potion', 'thrownpotion']);
Entity::registerEntity(Squid::class, false, ['Squid', 'minecraft:squid']);
Entity::registerEntity(Dolphin::class, false, ['Dolphin', 'minecraft:dolphin']);
Entity::registerEntity(Rabbit::class, false, ['Rabbit', 'minecraft:rabbit']);
Entity::registerEntity(PolarBear::class, false, ['PolarBear', 'minecraft:polar_bear']);
Entity::registerEntity(Villager::class, false, ['Villager', 'minecraft:villager']);
Entity::registerEntity(Wolf::class, false, ['Wolf', 'minecraft:wolf']);
Entity::registerEntity(Zombie::class, false, ['Zombie', 'minecraft:zombie']);
Expand Down
133 changes: 133 additions & 0 deletions src/pocketmine/entity/passive/Dolphin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?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\entity\passive;

use pocketmine\entity\WaterAnimal;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\math\Vector3;
use pocketmine\network\mcpe\protocol\ActorEventPacket;
use function atan2;
use function mt_rand;
use function sqrt;
use const M_PI;

class Dolphin extends WaterAnimal{
public const NETWORK_ID = self::DOLPHIN;

public $width = 0.95;
public $height = 0.95;

/** @var Vector3|null */
public $swimDirection = null;
/** @var float */
public $swimSpeed = 1.0;

/** @var int */
private $switchDirectionTicker = 0;

public function initEntity() : void{
$this->setMaxHealth(10);
parent::initEntity();
}

public function getName() : string{
return "Dolphin";
}

public function attack(EntityDamageEvent $source) : void{
parent::attack($source);
if($source->isCancelled()){
return;
}

if($source instanceof EntityDamageByEntityEvent){
$this->swimSpeed = mt_rand(150, 350) / 2000;
$e = $source->getDamager();
if($e !== null){
$this->swimDirection = (new Vector3($this->x - $e->x, $this->y - $e->y, $this->z - $e->z))->normalize();
}
}
}

private function generateRandomDirection() : Vector3{
return new Vector3(mt_rand(-1000, 1000) / 1000, mt_rand(-500, 500) / 1000, mt_rand(-1000, 1000) / 1000);
}

public function entityBaseTick(int $tickDiff = 1) : bool{
if($this->closed){
return false;
}

if(++$this->switchDirectionTicker === 100){
$this->switchDirectionTicker = 0;
if(mt_rand(0, 100) < 50){
$this->swimDirection = null;
}
}

$hasUpdate = parent::entityBaseTick($tickDiff);

if($this->isAlive()){

if($this->y > 62 and $this->swimDirection !== null){
$this->swimDirection->y = -0.5;
}

$inWater = $this->isUnderwater();
if(!$inWater){
$this->swimDirection = null;
}elseif($this->swimDirection !== null){
if($this->motion->lengthSquared() <= $this->swimDirection->lengthSquared()){
$this->motion = $this->swimDirection->multiply($this->swimSpeed);
}
}else{
$this->swimDirection = $this->generateRandomDirection();
$this->swimSpeed = mt_rand(50, 100) / 2000;
}

$f = sqrt(($this->motion->x ** 2) + ($this->motion->z ** 2));
$this->yaw = (-atan2($this->motion->x, $this->motion->z) * 180 / M_PI);
$this->pitch = (-atan2($f, $this->motion->y) * 180 / M_PI);
}

return $hasUpdate;
}

public function getDrops() : array{
return [];
}

public function canSpawnHere() : bool{
return parent::canSpawnHere() and $this->y > 45 and $this->y < 63;
}

protected function applyGravity() : void{
if(!$this->isUnderwater()){
parent::applyGravity();
}
}
}
96 changes: 96 additions & 0 deletions src/pocketmine/entity/passive/PolarBear.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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 TuranicTeam
* @link https://github.com/TuranicTeam/Altay
*
*/

declare(strict_types=1);

namespace pocketmine\entity\passive;

use pocketmine\entity\Animal;
use pocketmine\entity\behavior\FloatBehavior;
use pocketmine\entity\behavior\FollowParentBehavior;
use pocketmine\entity\behavior\LookAtPlayerBehavior;
use pocketmine\entity\behavior\MateBehavior;
use pocketmine\entity\behavior\PanicBehavior;
use pocketmine\entity\behavior\RandomLookAroundBehavior;
use pocketmine\entity\behavior\TemptBehavior;
use pocketmine\entity\behavior\RandomStrollBehavior;
use pocketmine\network\mcpe\protocol\ActorEventPacket;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\math\Vector3;
use pocketmine\Player;
use function boolval;
use function intval;
use function rand;

class PolarBear extends Animal{

public const NETWORK_ID = self::POLAR_BEAR;

public $width = 0.9;
public $height = 0.9;

protected function addBehaviors() : void{
$this->behaviorPool->setBehavior(0, new FloatBehavior($this));
$this->behaviorPool->setBehavior(1, new PanicBehavior($this, 1.25));
$this->behaviorPool->setBehavior(2, new MateBehavior($this, 1.0));
$this->behaviorPool->setBehavior(3, new TemptBehavior($this, [Item::CARROT], 1.2));
$this->behaviorPool->setBehavior(4, new FollowParentBehavior($this, 1.1));
$this->behaviorPool->setBehavior(5, new RandomStrollBehavior($this, 1.0));
$this->behaviorPool->setBehavior(6, new LookAtPlayerBehavior($this, 6.0));
$this->behaviorPool->setBehavior(7, new RandomLookAroundBehavior($this));
}

protected function initEntity() : void{
$this->setMaxHealth(10);
$this->setMovementSpeed(0.25);
$this->setFollowRange(10);

parent::initEntity();
}

public function getName() : string{
return "PolarBear";
}

public function onInteract(Player $player, Item $item, Vector3 $clickPos) : bool{
if(parent::onInteract($player, $item, $clickPos)){
return true;
}
return false;
}

public function getXpDropAmount() : int{
return rand(1, ($this->isInLove() ? 7 : 3));
}

public function getDrops() : array{
$drops = [];
array_push($drops, Item::get(Item::RAW_SALMON, 0, mt_rand(0, 1)));
array_push($drops, Item::get(Item::RAW_FISH, 0, mt_rand(0, 1)));
return $drops;
}

public function entityBaseTick(int $diff = 1) : bool{
return parent::entityBaseTick($diff);
}
}
106 changes: 106 additions & 0 deletions src/pocketmine/entity/passive/Rabbit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?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 TuranicTeam
* @link https://github.com/TuranicTeam/Altay
*
*/

declare(strict_types=1);

namespace pocketmine\entity\passive;

use pocketmine\entity\Animal;
use pocketmine\entity\behavior\FloatBehavior;
use pocketmine\entity\behavior\FollowParentBehavior;
use pocketmine\entity\behavior\LookAtPlayerBehavior;
use pocketmine\entity\behavior\MateBehavior;
use pocketmine\entity\behavior\PanicBehavior;
use pocketmine\entity\behavior\RandomLookAroundBehavior;
use pocketmine\entity\behavior\TemptBehavior;
use pocketmine\entity\behavior\RandomStrollBehavior;
use pocketmine\network\mcpe\protocol\ActorEventPacket;
use pocketmine\item\Item;
use pocketmine\item\ItemFactory;
use pocketmine\math\Vector3;
use pocketmine\Player;
use function boolval;
use function intval;
use function rand;

class Rabbit extends Animal{

public const NETWORK_ID = self::RABBIT;

public $width = 0.9;
public $height = 0.9;

protected function addBehaviors() : void{
$this->behaviorPool->setBehavior(0, new FloatBehavior($this));
$this->behaviorPool->setBehavior(1, new PanicBehavior($this, 1.25));
$this->behaviorPool->setBehavior(2, new MateBehavior($this, 1.0));
$this->behaviorPool->setBehavior(3, new TemptBehavior($this, [Item::CARROT], 1.2));
$this->behaviorPool->setBehavior(4, new FollowParentBehavior($this, 1.1));
$this->behaviorPool->setBehavior(5, new RandomStrollBehavior($this, 1.0));
$this->behaviorPool->setBehavior(6, new LookAtPlayerBehavior($this, 6.0));
$this->behaviorPool->setBehavior(7, new RandomLookAroundBehavior($this));
}

protected function initEntity() : void{
$this->setMaxHealth(10);
$this->setMovementSpeed(0.25);
$this->setFollowRange(10);

parent::initEntity();
}

public function getName() : string{
return "Rabbit";
}

public function onInteract(Player $player, Item $item, Vector3 $clickPos) : bool{
if(parent::onInteract($player, $item, $clickPos)){
return true;
}
return false;
}

public function getXpDropAmount() : int{
return rand(1, ($this->isInLove() ? 7 : 3));
}

public function getDrops() : array{
$drops = [];
array_push($drops, Item::get(Item::RABBIT_HIDE, 0, mt_rand(0, 1)));
if($this->isOnFire()){
array_push($drops, Item::get(Item::COOKED_RABBIT, 0, mt_rand(0, 1)));
}else{
array_push($drops, Item::get(Item::RAW_RABBIT, 0, mt_rand(0, 1)));
}
if(mt_rand(0, 100) <= 10){ // 10 percent chance of dropping rabbits foot
array_push($drops, Item::get(Item::RABBIT_FOOT, 0, 1));
}
return $drops;
}

public function entityBaseTick(int $diff = 1) : bool{
if($this->motion->x != 0 or $this->motion->y != 0 or $this->motion->z != 0)
$this->jump();

return parent::entityBaseTick($diff);
}
}
9 changes: 7 additions & 2 deletions src/pocketmine/level/biome/Biome.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
use pocketmine\entity\passive\Cat;
use pocketmine\entity\passive\Chicken;
use pocketmine\entity\passive\Cow;
use pocketmine\entity\passive\Dolphin;
use pocketmine\entity\passive\PolarBear;
use pocketmine\entity\passive\Pig;
use pocketmine\entity\passive\Rabbit;
use pocketmine\entity\passive\Sheep;
use pocketmine\entity\passive\Squid;
use pocketmine\entity\WaterAnimal;
Expand Down Expand Up @@ -104,7 +107,8 @@ abstract class Biome{

public function __construct(){
$this->spawnableCreatureList[] = new SpawnListEntry(Sheep::class, 12, 4, 4);
//$this->spawnableCreatureList[] = new SpawnListEntry(Rabbit::class, 10, 3, 3);
$this->spawnableCreatureList[] = new SpawnListEntry(Rabbit::class, 10, 4, 4);
$this->spawnableCreatureList[] = new SpawnListEntry(PolarBear::class, 10, 3, 3);
$this->spawnableCreatureList[] = new SpawnListEntry(Pig::class, 10, 4, 4);
$this->spawnableCreatureList[] = new SpawnListEntry(Chicken::class, 10, 4, 4);
$this->spawnableCreatureList[] = new SpawnListEntry(Cow::class, 8, 4, 4);
Expand All @@ -117,6 +121,7 @@ public function __construct(){
//$this->spawnableMonsterList[] = new SpawnListEntry(Enderman::class, 10, 1, 4);
//$this->spawnableMonsterList[] = new SpawnListEntry(Witch::class, 5, 1, 1);
$this->spawnableWaterCreatureList[] = new SpawnListEntry(Squid::class, 10, 4, 4);
$this->spawnableWaterCreatureList[] = new SpawnListEntry(Dolphin::class, 10, 4, 4);
//$this->spawnableCaveCreatureList[] = new SpawnListEntry(Bat::class, 10, 8, 8);
}

Expand Down Expand Up @@ -271,4 +276,4 @@ public function getSpawnableList(CreatureType $creatureType) : array{
public function getSpawningChance() : float{
return 0.1;
}
}
}