ported painting placement from 0.8(didnt add painting collisoion checks yet)

This commit is contained in:
GameHerobrine 2024-11-26 17:49:10 +03:00
parent 61ee4ab335
commit 96f4cb6382
3 changed files with 175 additions and 65 deletions

View file

@ -3,15 +3,109 @@
class Painting extends Entity{ class Painting extends Entity{
const CLASS_TYPE = ENTITY_OBJECT; const CLASS_TYPE = ENTITY_OBJECT;
const TYPE = OBJECT_PAINTING; const TYPE = OBJECT_PAINTING;
public $motive;
public $direction;
public $xPos, $yPos, $zPos;
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data); parent::__construct($level, $eid, $class, $type, $data);
$this->x = $this->data["TileX"] ?? $this->x; $this->x = $this->data["TileX"] ?? $this->x;
$this->y = $this->data["TileY"] ?? $this->y; $this->y = $this->data["TileY"] ?? $this->y;
$this->z = $this->data["TileZ"] ?? $this->z; $this->z = $this->data["TileZ"] ?? $this->z;
$this->xPos = $this->data["xPos"] ?? (int)$this->x;
$this->zPos = $this->data["zPos"] ?? (int)$this->z;
$this->yPos = $this->data["yPos"] ?? (int)$this->y;
$this->setHealth(1, "generic"); $this->setHealth(1, "generic");
$this->canBeAttacked = true; $this->canBeAttacked = true;
$this->width = 1; $this->width = 1;
$this->isStatic = true; $this->isStatic = true;
$mot = $this->data["Motive"] ?? false;
$dir = $this->data["Direction"] ?? false;
if($dir === false){
$dir = floor($this->yaw / 90);
}
if($mot === false){
$this->setRandomMotive($dir);
}else{
$this->motive = $mot;
$this->direction = $dir;
//TODO:
}
}
public function setRandomMotive($dir){
$tochoose = [];
foreach(PaintingItem::$motives as $name => $motive){
$this->motive = $name;
$this->setDirection($dir);
if($this->survives()){
$tochoose[] = $name;
}
}
if(count($tochoose) > 0){
$ind = mt_rand(0, count($tochoose)-1);
}else{
$ind = 0;
}
$this->motive = $tochoose[$ind];
$this->setDirection($dir);
}
public static function offs($n){
if($n == 32 || $n == 64) return 0.5;
return 0;
}
const DIRECTION_OPPOSITE = [2, 3, 0, 1];
public function setDirection($dir){
$this->direction = $dir;
$this->lastYaw = $this->yaw = $dir * 90;
$size = PaintingItem::$motives[$this->motive] ?? array(1, 1);
$width = $size[0]*16;
$height = $size[1]*16;
if($dir == 2 || $dir == 0){
$v10 = 2;
$v9 = $width;
$this->yaw = $this->lastYaw = 90 * self::DIRECTION_OPPOSITE[$dir];
}else{
$v9 = 2;
$v10 = $width;
}
$v12 = $v9 * 0.03125;
$v13 = $height * 0.03125;
$v14 = $v10 * 0.03125;
$v15 = $this->xPos + 0.5;
$v16 = $this->yPos + 0.5;
$v17 = $this->zPos + 0.5; //XXX check
if($dir == 2){
$v17 = $v17 - 0.5625;
$v15 = $v15 - self::offs($width);
}else if($dir == 1){
$v15 = $v15 - 0.5625;
$v17 = $v17 + self::offs($width);
}else if($dir == 3){
$v15 = $v15 + 0.5625;
$v17 = $v17 - self::offs($width);
}else{
$v17 = $v17 + 0.5625;
$v15 = $v15 + self::offs($width);
}
$v23 = $v16 + self::offs($height);
$this->setPos($v15, $v23, $v17);
$this->boundingBox->minX = ($v15 - $v12) + 0.03125;
$this->boundingBox->minY = ($v23 - $v13) + 0.03125;
$this->boundingBox->minZ = ($v17 - $v14) + 0.03125;
$this->boundingBox->maxX = ($v15 + $v12) - 0.03125;
$this->boundingBox->maxY = ($v23 + $v13) - 0.03125;
$this->boundingBox->maxZ = ($v17 + $v14) - 0.03125;
} }
public function getDrops(){ public function getDrops(){
@ -25,25 +119,62 @@ class Painting extends Entity{
} }
public function survives(){ public function survives(){
$cubes = $this->level->getCubes($this, $this->boundingBox);
if(count($cubes) == 0){
$size = PaintingItem::$motives[$this->motive] ?? array(1, 1);
$width = $size[0];
$width2 = $width*16/32;
$height = $size[1];
$height2 = $height*16/32;
if($width < 1) $width = 1;
if($height < 1) $height = 1;
$positionX = $this->xPos;
$positionZ = $this->zPos;
if($this->direction == 2 || $this->direction == 0) $positionX = floor($this->x - $width2);
if($this->direction == 1 || $this->direction == 3) $positionZ = floor($this->z - $width2);
$minY = floor($this->y - $height2);
for($off = 0; $off < $width; ++$off){
for($yoff = 0; $yoff < $height; ++$yoff){
$y = $minY + $yoff;
if($this->direction == 1 || $this->direction == 3){
$id = $this->level->level->getBlockId($this->xPos, $y, $positionZ + $off);
}else{
$id = $this->level->level->getBlockId($positionX + $off, $y, $this->zPos);
}
if(!StaticBlock::getIsSolid($id)){
return false;
}
}
}
return true;
}
return false;
} }
public function createSaveData(){ public function createSaveData(){
$data = parent::createSaveData(); $data = parent::createSaveData();
$data["Motive"] = $this->data["Motive"]; $data["Motive"] = $this->motive;
$data["Direction"] = $this->direction;
$data["xPos"] = $this->xPos;
$data["yPos"] = $this->yPos;
$data["zPos"] = $this->zPos;
return $data; return $data;
} }
public function spawn($player){ public function spawn($player){
$pk = new AddPaintingPacket; $pk = new AddPaintingPacket;
$pk->eid = $this->eid; $pk->eid = $this->eid;
$pk->x = (int) $this->x; $pk->x = (int) $this->xPos;
$pk->y = (int) $this->y; $pk->y = (int) $this->yPos;
$pk->z = (int) $this->z; $pk->z = (int) $this->zPos;
$pk->direction = $this->getDirection(); $pk->direction = $this->direction;
$pk->title = $this->data["Motive"]; $pk->title = $this->motive;
$player->dataPacket($pk); $player->dataPacket($pk);
} }
} }

View file

@ -5,28 +5,34 @@ class PaintingItem extends Item{
parent::__construct(PAINTING, 0, $count, "Painting"); parent::__construct(PAINTING, 0, $count, "Painting");
$this->isActivable = true; $this->isActivable = true;
} }
private static $motives = array( public static $motives = array(
// Motive Width Height // Motive Width Height
array("Wasteland", 1, 1), "Kebab" => array(1, 1),
array("Wanderer", 1, 2), "Aztec" => array(1, 1),
array("Graham", 1, 2), "Alban" => array(1, 1),
array("Pool", 2, 1), "Aztec2" => array(1, 1),
array("Courbet", 2, 1), "Bomb" => array(1, 1),
array("Sunset", 2, 1), "Plant" => array(1, 1),
array("Sea", 2, 1), "Wasteland" => array(1, 1),
array("Creebet", 2, 1), "Wanderer" => array(1, 2),
array("Match", 2, 2), "Graham" => array(1, 2),
array("Bust", 2, 2), "Pool" => array(2, 1),
array("Stage", 2, 2), "Courbet" => array(2, 1),
array("Void", 2, 2), "Sunset" => array(2, 1),
array("SkullAndRoses", 2, 2), "Sea" => array(2, 1),
//array("Wither", 2, 2), "Creebet" => array(2, 1),
array("Fighters", 4, 2), "Match" => array(2, 2),
array("Skeleton", 4, 3), "Bust" => array(2, 2),
array("DonkeyKong", 4, 3), "Stage" => array(2, 2),
array("Pointer", 4, 4), "Void" => array(2, 2),
array("Pigscene", 4, 4), "SkullAndRoses" => array(2, 2),
array("Flaming Skull", 4, 4), //"Wither" => array(2, 2),
"Fighters" => array(4, 2),
"Skeleton" => array(4, 3),
"DonkeyKong" => array(4, 3),
"Pointer" => array(4, 4),
"Pigscene" => array(4, 4),
"BurningSkull" => array(4, 4),
); );
private static $direction = array(2, 0, 1, 3); private static $direction = array(2, 0, 1, 3);
private static $right = array(4, 5, 3, 2); private static $right = array(4, 5, 3, 2);
@ -34,47 +40,21 @@ class PaintingItem extends Item{
public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ public function onActivate(Level $level, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){
if($target->isTransparent === false and $face > 1 and $block->isSolid === false){ if($target->isTransparent === false and $face > 1 and $block->isSolid === false){
$server = ServerAPI::request(); $server = ServerAPI::request();
$faces = array(
2 => 1,
3 => 3,
4 => 0,
5 => 2,
); if($face < 2 || $face > 5) return;
$validMotives = array(
array("Kebab", 1, 1),
array("Aztec", 1, 1),
array("Alban", 1, 1),
array("Aztec2", 1, 1),
array("Bomb", 1, 1),
array("Plant", 1, 1),
);
foreach(PaintingItem::$motives as $motive){
$valid = true;
for($x = 0; $x <= $motive[1]; $x++){
for($y = 0; $y <= $motive[2]; $y++){
if ($target->getSide(PaintingItem::$right[$face - 2], $x)->isTransparent || $target->getSide(1, $y)->isTransparent
|| $block->getSide(PaintingItem::$right[$face - 2], $x)->isSolid || $block->getSide(1, $y)->isSolid) {
$valid = false;
break;
}
}
if(!$valid){
break;
}
}
if ($valid) {
$validMotives[] = $motive;
}
}
$motive = $validMotives[array_rand($validMotives)];
$data = array( $data = array(
"x" => $target->x, "x" => $target->x,
"y" => $target->y, "y" => $target->y,
"z" => $target->z, "z" => $target->z,
"yaw" => $faces[$face] * 90, "Direction" => match($face){
"Motive" => $motive[0], 2 => 2,
3 => 0,
4 => 1,
default => 3
},
"xPos" => $target->x,
"yPos" => $target->y,
"zPos" => $target->z,
); );
$e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_PAINTING, $data); $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_PAINTING, $data);
$server->api->entity->spawnToAll($e); $server->api->entity->spawnToAll($e);

View file

@ -236,7 +236,6 @@ class Level{
return $appliedVelocity; return $appliedVelocity;
} }
/** /**
* @param Entity $e * @param Entity $e
* @param AxisAlignedBB $aABB * @param AxisAlignedBB $aABB