Fix drop item fall to floor when player died mid air

This commit is contained in:
Daniel Maixner 2022-12-29 12:37:47 +01:00
parent f64f839799
commit 12dd784298
4 changed files with 30 additions and 18 deletions

View File

@ -109,11 +109,9 @@ final class Player
if ($dropCount > 0) {
$angle = 0;
$angleOffset = 360 / $dropCount;
$distance = (int)ceil($this->getBoundingRadius() / 2);
$pp = $this->getPositionClone();
foreach ($dropItems as $item) {
[$x, $z] = Util::rotatePointY($angle, 0, $distance);
$this->world->addDropItem($item, new Point($pp->x + $x, $pp->y, $pp->z + $z));
$this->sight->lookAt($angle, -78);
$this->world->dropItem($this, $item);
$angle += $angleOffset;
}
}

View File

@ -227,15 +227,6 @@ class World
}
}
public function addDropItem(Item $item, Point $position): void
{
$dropItem = new DropItem($item);
$dropItem->setPosition($position);
$this->dropItems[] = $dropItem;
$sound = new SoundEvent($position, SoundType::ITEM_DROP);
$this->makeSound($sound->setItem($item));
}
public function playerUse(Player $player): void
{
// Bomb defusing

View File

@ -86,10 +86,11 @@ class RoundTest extends BaseTestCase
$this->assertInstanceOf(Bomb::class, $drop2->getItem());
$pp = $game->getPlayer(1)->getPositionClone();
$pr = $game->getPlayer(1)->getBoundingRadius();
$this->assertSame($pp->y, $drop1->position->y);
$this->assertGreaterThan(0, $pp->y);
$this->assertSame(0, $drop1->position->y);
$this->assertLessThanOrEqual($pr, abs($drop1->position->x - $pp->x));
$this->assertLessThanOrEqual($pr, abs($drop1->position->z - $pp->z));
$this->assertSame($pp->y, $drop2->position->y);
$this->assertSame(0, $drop2->position->y);
$this->assertLessThanOrEqual($pr, abs($drop2->position->x - $pp->x));
$this->assertLessThanOrEqual($pr, abs($drop2->position->z - $pp->z));
}
@ -176,10 +177,10 @@ class RoundTest extends BaseTestCase
{
$maxRounds = 5;
$game = $this->createGame([
GameProperty::MAX_ROUNDS => $maxRounds,
GameProperty::ROUND_TIME_MS => 1,
GameProperty::MAX_ROUNDS => $maxRounds,
GameProperty::ROUND_TIME_MS => 1,
GameProperty::HALF_TIME_FREEZE_SEC => 0,
GameProperty::START_MONEY => 3000,
GameProperty::START_MONEY => 3000,
]);
$game->setTickMax($maxRounds * 2);

View File

@ -252,6 +252,28 @@ class SimpleInventoryTest extends BaseTestCase
$this->assertSame(800 - 200, $player->getMoney());
}
public function testDropItemWhenDiedMidAir(): void
{
$property = $this->createNoPauseGameProperty(5);
$property->round_end_cool_down_sec = 2;
$game = $this->createTestGame(null, $property);
$game->getPlayer(1)->setPosition(new Point(100, 200, 100));
$this->playPlayer($game, [
fn(Player $p) => $this->assertTrue($p->isAlive()),
fn(Player $p) => $this->assertEmpty($game->getWorld()->getDropItems()),
fn(Player $p) => $p->suicide(),
fn(Player $p) => $this->assertGreaterThan(0, $p->getPositionClone()->y),
fn(Player $p) => $this->assertFalse($p->isAlive()),
fn(Player $p) => $this->assertNotEmpty($game->getWorld()->getDropItems()),
$this->endGame(),
]);
$this->assertCount(2, $game->getWorld()->getDropItems());
foreach ($game->getWorld()->getDropItems() as $item) {
$this->assertSame(0, $item->getPosition()->y);
}
}
public function testPlayerBuyWeapons(): void
{
$startMoney = 10901;