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 public function onActivate(Item $item, Player $player){ //uwu
/*$oldtime = microtime(1); //$oldtime = microtime(true);
if(mt_rand(0, 1) === 0){ //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); // Structures::$SMALLFARM_VILLAGE->rotate(Structure::DEG_240, $this->level)->build($this->level, $this->x, $this->y, $this->z);
}else{ //}else{
$time = Structures::$SMALLFARM_VILLAGE->build($this->level, $this->x, $this->y, $this->z); // Structures::$SMALLFARM_VILLAGE->build($this->level, $this->x, $this->y, $this->z);
} //}
console("builded in ".($time - $oldtime));*/ //$time = microtime(true);
//console("builded in ".($time - $oldtime));
//(new WoodHutStructure())->build($this->level, $this->getX(), $this->getY(), $this->getZ()); //(new WoodHutStructure())->build($this->level, $this->getX(), $this->getY(), $this->getZ());
} }

View file

@ -7,6 +7,10 @@
*/ */
abstract class Structure{ abstract class Structure{
const MAP_NO_KEY = -127; const MAP_NO_KEY = -127;
const DEG_90 = 0;
const DEG_180 = 1;
const DEG_240 = 2;
protected $structure; protected $structure;
protected $map; protected $map;
public $width, $length; public $width, $length;
@ -15,8 +19,8 @@ abstract class Structure{
public function __construct(){ public function __construct(){
foreach($this->structure as &$s){ foreach($this->structure as &$s){
foreach($s as &$e){ foreach($s as &$e){
if(strlen($e) < $this->length){ if(strlen($e) < $this->width){
$e .= str_repeat(" ", $this->length - strlen($e)); $e .= str_repeat(" ", $this->width - strlen($e));
} }
} }
} }
@ -39,26 +43,66 @@ abstract class Structure{
return $this->structure; return $this->structure;
} }
public function rotate90deg(Level $level, $x, $y, $z){ public function rotate($rotationLevel = self::DEG_90, $level = null, $x = 0, $y = 0, $z = 0){
$str = $this->getFinalStructure($level, $x, $y, $z); $str = $level instanceof Level ? $this->getFinalStructure($level, $x, $y, $z) : $this->structure;
foreach($str as &$arr){ var_dump($str);
foreach($arr as &$e){ switch($rotationLevel){
$e = str_split($e); case self::DEG_90:
} $l = $this->width;
$new = []; $this->width = $this->length;
for($i = 0; $i < $this->length; ++$i){ $this->length = $l;
$new[] = implode("", array_reverse(array_column($arr, $i)));
} foreach($str as &$arr){
$arr = $new; 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){ private function setStructure($struct, $width, $length){
$this->structure = $struct; $this->structure = $struct;
$this->width = $width; $this->width = $width;
$this->length = $length; $this->length = $length;
//console("Modifed"); ConsoleAPI::debug("Modifed");
return $this; return $this;
} }
@ -79,7 +123,6 @@ abstract class Structure{
} }
$offsetZ = 0; $offsetZ = 0;
} }
return microtime(1);
} }
} }