90, 180, 240 deg rot + structure fixes

This commit is contained in:
GameHerobrine 2023-03-04 23:42:32 +03:00
parent a07a3ed486
commit aa9080d91f
2 changed files with 68 additions and 24 deletions

View file

@ -15,13 +15,14 @@ class MyceliumBlock extends SolidBlock{
}
public function onActivate(Item $item, Player $player){ //uwu
/*$oldtime = microtime(1);
if(mt_rand(0, 1) === 0){
$time = Structures::$SMALLFARM_VILLAGE->rotate90deg($this->level, $this->x, $this->y, $this->z)->build($this->level, $this->x, $this->y, $this->z);
}else{
$time = Structures::$SMALLFARM_VILLAGE->build($this->level, $this->x, $this->y, $this->z);
}
console("builded in ".($time - $oldtime));*/
//$oldtime = microtime(true);
//if(mt_rand(0, 1) === 0){
// Structures::$SMALLFARM_VILLAGE->rotate(Structure::DEG_240, $this->level)->build($this->level, $this->x, $this->y, $this->z);
//}else{
// Structures::$SMALLFARM_VILLAGE->build($this->level, $this->x, $this->y, $this->z);
//}
//$time = microtime(true);
//console("builded in ".($time - $oldtime));
//(new WoodHutStructure())->build($this->level, $this->getX(), $this->getY(), $this->getZ());
}

View file

@ -7,6 +7,10 @@
*/
abstract class Structure{
const MAP_NO_KEY = -127;
const DEG_90 = 0;
const DEG_180 = 1;
const DEG_240 = 2;
protected $structure;
protected $map;
public $width, $length;
@ -15,8 +19,8 @@ abstract class Structure{
public function __construct(){
foreach($this->structure as &$s){
foreach($s as &$e){
if(strlen($e) < $this->length){
$e .= str_repeat(" ", $this->length - strlen($e));
if(strlen($e) < $this->width){
$e .= str_repeat(" ", $this->width - strlen($e));
}
}
}
@ -38,27 +42,67 @@ abstract class Structure{
protected function getFinalStructure(Level $level, $x, $y, $z){
return $this->structure;
}
public function rotate90deg(Level $level, $x, $y, $z){
$str = $this->getFinalStructure($level, $x, $y, $z);
foreach($str as &$arr){
foreach($arr as &$e){
$e = str_split($e);
}
$new = [];
for($i = 0; $i < $this->length; ++$i){
$new[] = implode("", array_reverse(array_column($arr, $i)));
}
$arr = $new;
public function rotate($rotationLevel = self::DEG_90, $level = null, $x = 0, $y = 0, $z = 0){
$str = $level instanceof Level ? $this->getFinalStructure($level, $x, $y, $z) : $this->structure;
var_dump($str);
switch($rotationLevel){
case self::DEG_90:
$l = $this->width;
$this->width = $this->length;
$this->length = $l;
foreach($str as &$arr){
foreach($arr as &$e){
$e = str_split($e);
}
$new = [];
for($i = 0; $i < $this->length; ++$i){
$new[] = implode("", array_reverse(array_column($arr, $i)));
}
$arr = $new;
}
break;
case self::DEG_180:
foreach($str as &$lay){
$keys = array_keys($lay);
$ak = count($keys)-1;
$lay = array_reverse($lay, true);
foreach($lay as $k => $v){
unset($lay[$k]);
$lay[$keys[$ak--]] = strrev($v);
}
}
break;
case self::DEG_240:
$l = $this->width;
$this->width = $this->length;
$this->length = $l;
foreach($str as &$arr){
foreach($arr as &$e){
$e = str_split(strrev($e));
}
$new = [];
for($i = 0; $i < $this->length; ++$i){
$new[] = implode("", array_column($arr, $i));
}
$arr = $new;
}
break; //TODO fix
default:
ConsoleAPI::warn(self::class." tried to rotate to invalid state($rotationLevel)");
return $this;
}
return (clone $this)->setStructure($str, $this->length, $this->width);
var_dump($str);
return (clone $this)->setStructure($str, $this->width, $this->length);
}
private function setStructure($struct, $width, $length){
$this->structure = $struct;
$this->width = $width;
$this->length = $length;
//console("Modifed");
ConsoleAPI::debug("Modifed");
return $this;
}
@ -79,7 +123,6 @@ abstract class Structure{
}
$offsetZ = 0;
}
return microtime(1);
}
}