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

Commit

Permalink
Implemented Explosion and PrimedTNT, closes #2139
Browse files Browse the repository at this point in the history
  • Loading branch information
shoghicp committed Oct 7, 2014
1 parent 5fb2054 commit 582c165
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/pocketmine/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,7 @@ public function __construct(\ClassLoader $autoloader, \ThreadedLogger $logger, $
Generator::addGenerator(Normal::class, "default");

//Temporal workaround, pthreads static property nullification test
if(PluginManager::$pluginParentTimer === null){
if(PluginManager::$pluginParentTimer === null or Timings::$serverTickTimer === null){
$this->getLogger()->emergency("You are using an invalid pthreads version. Please update your binaries.");
kill(getmypid());
return;
Expand Down
5 changes: 4 additions & 1 deletion src/pocketmine/block/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,10 @@ public static function get($id, $meta = 0, Position $pos = null){
}

if($pos instanceof Position){
$block->position($pos);
$block->x = $pos->x;
$block->y = $pos->y;
$block->z = $pos->z;
$block->level = $pos->level;
}

return $block;
Expand Down
1 change: 0 additions & 1 deletion src/pocketmine/block/Fallable.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public function onUpdate($type){
new Double("", $this->y + 0.5),
new Double("", $this->z + 0.5)
]),
//TODO: add random motion with physics
"Motion" => new Enum("Motion", [
new Double("", 0),
new Double("", 0),
Expand Down
37 changes: 31 additions & 6 deletions src/pocketmine/block/TNT.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,16 @@

namespace pocketmine\block;

use pocketmine\entity\PrimedTNT;
use pocketmine\item\Item;
use pocketmine\level\Explosion;
use pocketmine\nbt\tag\Byte;
use pocketmine\nbt\tag\Compound;
use pocketmine\nbt\tag\Double;
use pocketmine\nbt\tag\Enum;
use pocketmine\nbt\tag\Float;
use pocketmine\Player;
use pocketmine\utils\Random;

class TNT extends Solid{
public function __construct(){
Expand All @@ -33,9 +41,8 @@ public function __construct(){

public function onActivate(Item $item, Player $player = null){
if($item->getID() === Item::FLINT_STEEL){
if(($player->gamemode & 0x01) === 0){
$item->useOn($this);
}
$item->useOn($this);

$data = [
"x" => $this->x + 0.5,
"y" => $this->y + 0.5,
Expand All @@ -44,9 +51,27 @@ public function onActivate(Item $item, Player $player = null){
"fuse" => 20 * 4, //4 seconds
];
$this->getLevel()->setBlock($this, new Air(), false, false, true);
//TODO
//$e = Server::getInstance()->api->entity->add($this->level, ENTITY_OBJECT, OBJECT_PRIMEDTNT, $data);
//$e->spawnToAll();

$mot = (new Random())->nextSignedFloat() * M_PI * 2;
$tnt = new PrimedTNT($this->getLevel()->getChunk($this->x >> 4, $this->z >> 4), new Compound("", [
"Pos" => new Enum("Pos", [
new Double("", $this->x + 0.5),
new Double("", $this->y + 0.5),
new Double("", $this->z + 0.5)
]),
"Motion" => new Enum("Motion", [
new Double("", -sin($mot) * 0.02),
new Double("", 0.2),
new Double("", -cos($mot) * 0.02)
]),
"Rotation" => new Enum("Rotation", [
new Float("", 0),
new Float("", 0)
]),
"Fuse" => new Byte("Fuse", 80)
]));

$tnt->spawnToAll();

return true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/pocketmine/entity/DroppedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ public function onUpdate(){
$this->motionY *= 1 - $this->drag;
$this->motionZ *= $friction;

$this->updateMovement();

if($this->onGround){
$this->motionY *= -0.5;
}
Expand All @@ -105,7 +107,6 @@ public function onUpdate(){
$this->kill();
}

$this->updateMovement();
}

$this->timings->stopTiming();
Expand Down
8 changes: 4 additions & 4 deletions src/pocketmine/entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@ public function sendMetadata($player){
foreach($player as $p){
if($p === $this){
/** @var Player $p */
$pk = new SetEntityDataPacket();
$pk->eid = 0;
$pk->metadata = $this->getData();
$p->dataPacket($pk);
$pk2 = new SetEntityDataPacket();
$pk2->eid = 0;
$pk2->metadata = $this->getData();
$p->dataPacket($pk2);
}else{
$p->dataPacket($pk);
}
Expand Down
1 change: 1 addition & 0 deletions src/pocketmine/entity/Explosive.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@

interface Explosive{

public function explode();
}
114 changes: 114 additions & 0 deletions src/pocketmine/entity/PrimedTNT.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,124 @@
namespace pocketmine\entity;


use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\level\Explosion;
use pocketmine\nbt\tag\Byte;
use pocketmine\nbt\tag\String;
use pocketmine\network\protocol\AddEntityPacket;
use pocketmine\network\protocol\SetEntityMotionPacket;
use pocketmine\Player;

class PrimedTNT extends Entity implements Explosive{
const NETWORK_ID = 65;

public $width = 0.98;
public $length = 0.98;
public $height = 0.98;

protected $gravity = 0.04;
protected $drag = 0.02;

protected $fuse;

public $canCollide = false;

protected function initEntity(){
$this->namedtag->id = new String("id", "PrimedTNT");
if(isset($this->namedtag->Fuse)){
$this->fuse = $this->namedtag["Fuse"];
}else{
$this->fuse = 80;
}
}


public function canCollideWith(Entity $entity){
return false;
}

public function getData(){
return [
16 => ["type" => 0, "value" => $this->fuse],
];
}

public function saveNBT(){
parent::saveNBT();
$this->namedtag->Fuse = new Byte("Fuse", $this->fuse);
}

public function onUpdate(){

if($this->closed){
return false;
}

$this->timings->startTiming();

$this->entityBaseTick();

if(!$this->dead){

$this->motionY -= $this->gravity;

$this->move($this->motionX, $this->motionY, $this->motionZ);

$friction = 1 - $this->drag;

$this->motionX *= $friction;
$this->motionY *= $friction;
$this->motionZ *= $friction;

$this->updateMovement();

if($this->onGround){
$this->motionY *= -0.5;
$this->motionX *= 0.7;
$this->motionZ *= 0.7;
}

if($this->fuse-- <= 0){
$this->kill();
$this->explode();
}else{
$this->sendMetadata($this->getViewers());
}

}


return !$this->onGround or ($this->motionX == 0 and $this->motionY == 0 and $this->motionZ == 0);
}

public function attack($damage, $source = EntityDamageEvent::CAUSE_MAGIC){

}

public function heal($amount){

}

public function explode(){
(new Explosion($this, 4, $this))->explode();
}

public function spawnTo(Player $player){
$pk = new AddEntityPacket();
$pk->type = PrimedTNT::NETWORK_ID;
$pk->eid = $this->getID();
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->did = 0;
$player->dataPacket($pk);

$pk = new SetEntityMotionPacket();
$pk->entities = [
[$this->getID(), $this->motionX, $this->motionY, $this->motionZ]
];
$player->dataPacket($pk);

parent::spawnTo($player);
}
}
Loading

6 comments on commit 582c165

@iksaku
Copy link
Contributor

@iksaku iksaku commented on 582c165 Oct 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍 👍 👍 👍 👍 👍 👍 @Darunia18 :'D Finally :3

@jose-vm
Copy link

@jose-vm jose-vm commented on 582c165 Oct 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just about time! Just arrows remaining! :D

@shoghicp
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jose-vm It seems you have never tested PocketMine. Such a shame

@PEMapModder
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arrows can be shot a long time ago...

@jose-vm
Copy link

@jose-vm jose-vm commented on 582c165 Oct 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shoghicp @PEMapModder But they don't make any damage, or they do?

@Darunia18
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woo Explosions! :D

Please sign in to comment.