NostalgiaCore/src/entity/object/Minecart.php
2024-06-08 22:57:49 +03:00

142 lines
2.7 KiB
PHP

<?php
class Minecart extends Vehicle{
const TYPE = OBJECT_MINECART;
/**
* A minecart rotation matrix
* @var int[][][]
*/
private static $matrix = [
[
[0, 0, -1],
[0, 0, 1]
],
[
[-1, 0, 0],
[1, 0, 0]
],
[
[-1, -1, 0],
[1, 0, 0]
],
[
[-1, 0, 0],
[1, -1, 0]
],
[
[0, 0, -1],
[0, -1, 1]
],
[
[0, -1, -1],
[0, 0, 1]
],
[
[0, 0, 1],
[1, 0, 0]
],
[
[0, 0, 1],
[-1, 0, 0]
],
[
[0, 0, -1],
[-1, 0, 0]
],
[
[0, 0, -1],
[1, 0, 0]
]
];
private $hurtTime = 0; //syncentdata 17 int
private $damage = 0; //syncentdata 19 float
private $minecartX = 0, $minecartY = 0, $minecartZ = 0;
private $turnProgress = 0;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
$this->canBeAttacked = true;
$this->x = isset($this->data["TileX"]) ? $this->data["TileX"]:$this->x;
$this->y = isset($this->data["TileY"]) ? $this->data["TileY"]:$this->y;
$this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"]:$this->z;
$this->setHealth(1, "generic"); //orig: 3
$this->width = 0.98;
$this->height = 0.7;
$this->yOffset = $this->height / 2;
}
public function isPickable(){
return !$this->dead;
}
public function getDrops(){
return [
[MINECART, 0, 1]
];
}
public function update($now){
if($this->closed === true){
return false;
}
}
public function close()
{
parent::close();
if($this->linkedEntity != 0){
$ent = $this->level->entityList[$this->linkedEntity] ?? false;
if($ent instanceof Entity){
$ent->stopRiding();
}else{
ConsoleAPI::warn("$this is being ridden by invalid entity {$this->linkedEntity}");
}
}
}
public function isPushable(){
return false; //TODO replace with true
}
public function spawn($player){
$pk = new AddEntityPacket;
$pk->eid = $this->eid;
$pk->type = $this->type;
$pk->x = $this->x;
$pk->y = $this->y;
$pk->z = $this->z;
$pk->yaw = $this->yaw;
$pk->pitch = $this->pitch;
$player->dataPacket($pk);
$pk = new SetEntityMotionPacket;
$pk->eid = $this->eid;
$pk->speedX = $this->speedX;
$pk->speedY = $this->speedY;
$pk->speedZ = $this->speedZ;
$player->dataPacket($pk);
}
public function interactWith(Entity $e, $action){
console($action);
if($action === InteractPacket::ACTION_HOLD && $e->isPlayer() && $this->canRide($e)){
console("set ride");
$e->setRiding($this);
return true;
}
if($action === InteractPacket::ACTION_ATTACK && $e->eid == $this->linkedEntity){
return false; //TODO more vanilla way?
}
parent::interactWith($e, $action);
}
public function canRide($e)
{
return $this->linkedEntity == 0 && $e->linkedEntity == 0;
}
}