Skip to content

Commit

Permalink
Add player shot movement slowdown aka tagging
Browse files Browse the repository at this point in the history
  • Loading branch information
solcloud committed Mar 28, 2023
1 parent 94b61d6 commit 6e6954e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
9 changes: 6 additions & 3 deletions server/src/Core/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use cs\Enum\Color;
use cs\Enum\InventorySlot;
use cs\Event\Event;
use cs\Event\TimeoutEvent;
use cs\Traits\Player as PlayerTrait;
use cs\Weapon\AmmoBasedWeapon;

Expand Down Expand Up @@ -38,9 +39,10 @@ final class Player
private int $eventIdPrimary = 0;
private int $eventIdJump = 1;
private int $eventIdCrouch = 2;
private int $eventIdMovement = 3;
private int $eventIdGravity = 4;
private int $eventIdOther = 5; // last
private int $eventIdShotSlowdown = 3;
private int $eventIdMovement = 4;
private int $eventIdGravity = 5;
private int $eventIdOther = 6; // last

public function __construct(
private int $id,
Expand Down Expand Up @@ -195,6 +197,7 @@ public function lowerArmor(int $armorDamage): void

public function lowerHealth(int $healthDamage): void
{
$this->addEvent(new TimeoutEvent(null, 70), $this->eventIdShotSlowdown);
$this->health -= abs($healthDamage);
if ($this->health <= 0) {
$this->health = 0;
Expand Down
8 changes: 5 additions & 3 deletions server/src/Event/TimeoutEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
class TimeoutEvent extends Event
{
protected int $tickCountTimeout = 0;
protected Closure $callback;
protected ?Closure $callback = null;

public function __construct(Closure $callback, protected int $timeoutMs)
public function __construct(?Closure $callback, protected int $timeoutMs)
{
$this->callback = $callback;
$this->tickCountTimeout = $this->timeMsToTick($this->timeoutMs);
Expand All @@ -21,7 +21,9 @@ final public function process(int $tick): void
return;
}

call_user_func($this->callback, $this, $tick);
if ($this->callback) {
call_user_func($this->callback, $this, $tick);
}
if ($this->onComplete !== []) {
foreach ($this->onComplete as $func) {
call_user_func($func, $this);
Expand Down
3 changes: 3 additions & 0 deletions server/src/Traits/Player/MovementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ private function getMoveSpeed(): int
} elseif ($this->isFlying()) {
$speed *= Setting::flyingMovementSpeedMultiplier();
}
if (isset($this->events[$this->eventIdShotSlowdown])) {
$speed *= .4;
}

return (int)ceil($speed);
}
Expand Down
21 changes: 21 additions & 0 deletions test/og/Movement/MovementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,27 @@ public function testPlayerSlowMovementWhenHaveGun(): void
$this->assertLessThan(Setting::moveDistancePerTick(), $game->getPlayer(1)->getPositionClone()->z);
}

public function testPlayerSlowMovementWhenShot(): void
{
$tickMax = 5;
$game = $this->createTestGame($tickMax);
$p2 = new Player(2, Color::YELLOW, false);
$game->addPlayer($p2);
$p2->getSight()->look(180, -10);
$p2->setPosition($p2->getPositionClone()->addZ(Setting::moveDistancePerTick() * 10));
$game->onTick(function (GameState $state) {
$state->getPlayer(1)->moveForward();
if ($state->getTickId() === 2) {
$this->assertNotNull($state->getPlayer(2)->attack());
$this->assertLessThan(100, $state->getPlayer(1)->getHealth());
$this->assertTrue($state->getPlayer(1)->isAlive());
}
});
$game->start();
$this->assertGreaterThan(Setting::moveDistancePerTick() * 3, $game->getPlayer(1)->getPositionClone()->z);
$this->assertLessThan(Setting::moveDistancePerTick() * $tickMax, $game->getPlayer(1)->getPositionClone()->z);
}

public function testPlayerSlowMovementWhenFlying(): void
{
$game = $this->createOneRoundGame();
Expand Down

0 comments on commit 6e6954e

Please sign in to comment.