mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-24 05:29:44 +00:00
added entity collision, new entity sorting system
This commit is contained in:
parent
db8b221c02
commit
79831c457d
2 changed files with 81 additions and 0 deletions
|
|
@ -34,12 +34,38 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
|
||||||
unset($this->ai->entity);
|
unset($this->ai->entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function collideHandler(){
|
||||||
|
$this->level->applyCallbackToNearbyEntities($this, [$this, "applyCollision"], 2); //TODO radiuses
|
||||||
|
}
|
||||||
|
|
||||||
|
public function applyCollision(Entity $collided){
|
||||||
|
if($collided->boundingBox->intersectsWith($this->boundingBox) && !($this->isPlayer() && $collided->isPlayer()) && $this->eid != $collided->eid){
|
||||||
|
$diffX = $collided->x - $this->x;
|
||||||
|
$diffZ = $collided->z - $this->z;
|
||||||
|
$maxDiff = max(abs($diffX), abs($diffZ));
|
||||||
|
if($maxDiff > 0.01){
|
||||||
|
$sqrtMax = sqrt($maxDiff);
|
||||||
|
$diffX /= $sqrtMax;
|
||||||
|
$diffZ /= $sqrtMax;
|
||||||
|
|
||||||
|
$col = (($v = 1 / $sqrtMax) > 1 ? 1 : $v);
|
||||||
|
$diffX *= $col;
|
||||||
|
$diffZ *= $col;
|
||||||
|
$diffX *= 0.05;
|
||||||
|
$diffZ *= 0.05;
|
||||||
|
$this->addVelocity(-$diffX, 0, -$diffZ);
|
||||||
|
$collided->addVelocity($diffX, 0, $diffZ);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function update(){
|
public function update(){
|
||||||
if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) {
|
if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) {
|
||||||
$this->ai->updateTasks();
|
$this->ai->updateTasks();
|
||||||
}
|
}
|
||||||
$this->ai->mobController->rotateTick();
|
$this->ai->mobController->rotateTick();
|
||||||
$this->ai->mobController->movementTick();
|
$this->ai->mobController->movementTick();
|
||||||
|
$this->collideHandler();
|
||||||
if($this->onGround){
|
if($this->onGround){
|
||||||
//if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){
|
//if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){
|
||||||
// $this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10);
|
// $this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10);
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,12 @@ class Level{
|
||||||
* @var Entity[]
|
* @var Entity[]
|
||||||
*/
|
*/
|
||||||
public $entityList;
|
public $entityList;
|
||||||
|
/**
|
||||||
|
* Entity list based on their chunk positions
|
||||||
|
* @var array()
|
||||||
|
*/
|
||||||
|
public $entityChunked;
|
||||||
|
|
||||||
public $tiles, $blockUpdates, $nextSave, $players = [], $level;
|
public $tiles, $blockUpdates, $nextSave, $players = [], $level;
|
||||||
private $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime;
|
private $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime;
|
||||||
|
|
||||||
|
|
@ -274,14 +280,63 @@ class Level{
|
||||||
unset($this->entityList[$k]);
|
unset($this->entityList[$k]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
$prevIndex = (((int)$e->x) >> 4).":".(((int)$e->y) >> 4).":".(((int)$e->z) >> 4);
|
||||||
|
if(!(nullsafe($this->entityChunked[$prevIndex][$k], false) instanceof Entity)){
|
||||||
|
$this->entityChunked[$prevIndex][$k] = &$this->entityList[$k];
|
||||||
|
}
|
||||||
if($e->needsUpdate){
|
if($e->needsUpdate){
|
||||||
$e->update();
|
$e->update();
|
||||||
}
|
}
|
||||||
|
$newIndex = (((int)$e->x) >> 4).":".(((int)$e->y) >> 4).":".(((int)$e->z) >> 4);
|
||||||
|
if($newIndex != $prevIndex){
|
||||||
|
unset($this->entityChunked[$prevIndex][$k]);
|
||||||
|
$this->entityChunked[$newIndex][$k] = &$this->entityList[$k];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(Entity::$updateOnTick && $server->ticks % 40 === 0){ //40 ticks delay
|
if(Entity::$updateOnTick && $server->ticks % 40 === 0){ //40 ticks delay
|
||||||
$this->mobSpawner->handle();
|
$this->mobSpawner->handle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public function applyCallbackToNearbyEntities(Entity $e, callable $c, $radius = 5){
|
||||||
|
$minChunkX = ((int)($e->x - $radius)) >> 4;
|
||||||
|
$minChunkY = ((int)($e->y - $radius)) >> 4;
|
||||||
|
$minChunkZ = ((int)($e->z - $radius)) >> 4;
|
||||||
|
$maxChunkX = ((int)($e->x + $radius)) >> 4;
|
||||||
|
$maxChunkY = ((int)($e->y + $radius)) >> 4;
|
||||||
|
$maxChunkZ = ((int)($e->z + $radius)) >> 4;
|
||||||
|
$ents = [];
|
||||||
|
for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){
|
||||||
|
for($chunkY = $minChunkY; $chunkY <= $maxChunkY; ++$chunkY){
|
||||||
|
for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){
|
||||||
|
$ind = (((int)$e->x) >> 4).":".(((int)$e->y) >> 4).":".(((int)$e->z) >> 4);
|
||||||
|
foreach(nullsafe($this->entityChunked[$ind], []) as $e){
|
||||||
|
$c($e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//unset($ents[$e->eid]);
|
||||||
|
}
|
||||||
|
public function getEntitiesNearby(Entity $e, $radius = 5){
|
||||||
|
$minChunkX = ((int)($e->x - $radius)) >> 4;
|
||||||
|
$minChunkY = ((int)($e->y - $radius)) >> 4;
|
||||||
|
$minChunkZ = ((int)($e->z - $radius)) >> 4;
|
||||||
|
$maxChunkX = ((int)($e->x + $radius)) >> 4;
|
||||||
|
$maxChunkY = ((int)($e->y + $radius)) >> 4;
|
||||||
|
$maxChunkZ = ((int)($e->z + $radius)) >> 4;
|
||||||
|
$ents = [];
|
||||||
|
for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){
|
||||||
|
for($chunkY = $minChunkY; $chunkY <= $maxChunkY; ++$chunkY){
|
||||||
|
for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){
|
||||||
|
$ind = (((int)$e->x) >> 4).":".(((int)$e->y) >> 4).":".(((int)$e->z) >> 4);
|
||||||
|
$ents += $this->entityChunked[$ind];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
unset($ents[$e->eid]);
|
||||||
|
//if(count($ents) > 0) console(count($ents));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){
|
public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){
|
||||||
if(!isset($this->level) or (($pos instanceof Position) and $pos->level !== $this) or $pos->x < 0 or $pos->y < 0 or $pos->z < 0){
|
if(!isset($this->level) or (($pos instanceof Position) and $pos->level !== $this) or $pos->x < 0 or $pos->y < 0 or $pos->z < 0){
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue