Skip to content

Commit

Permalink
little refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
starswaitforus committed Aug 28, 2024
1 parent f9aa662 commit bc28967
Show file tree
Hide file tree
Showing 22 changed files with 76 additions and 62 deletions.
8 changes: 8 additions & 0 deletions server/src/Core/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ public function set(int $x, int $y, int $z): self
return $this;
}

public function setScalar(int $xyz): self
{
$this->x = $xyz;
$this->y = $xyz;
$this->z = $xyz;
return $this;
}

public function __toString(): string
{
return "Point({$this->x},{$this->y},{$this->z})";
Expand Down
4 changes: 2 additions & 2 deletions server/src/Core/World.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public function tryPickDropItems(Player $player): void
public function dropItem(Player $player, Item $item): void
{
$dropEvent = new DropEvent($player, $item, $this);
$dropEvent->onLand(function (DropEvent $event): void {
$dropEvent->onFloorLand(function (DropEvent $event): void {
$this->dropItems[] = $event->getDropItem();
});
$this->game->addDropEvent($dropEvent);
Expand Down Expand Up @@ -631,7 +631,7 @@ public function isCollisionWithMolotov(Point $pos): bool
}

foreach ($molotov->parts as $flame) {
if ($flame->active && Collision::pointWithCylinder($pos, $flame->center, 3 * $flame->radius, $flame->height)) {
if ($flame->active && Collision::pointWithBoxBoundary($pos, $flame->boundaryMin, $flame->boundaryMax)) {
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/src/Event/DropEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ public function __construct(private readonly Player $player, private readonly It
$this->angleHorizontal = $player->getSight()->getRotationHorizontal();
$this->angleVertical = $player->getSight()->getRotationVertical();
$this->velocity = ($player->isMoving() || $player->isJumping()) ? 30.0 : 20.0;
$this->timeIncrement = 1 / Util::millisecondsToFrames(100);
$this->timeIncrement = 1 / $this->timeMsToTick(100);
if (!$this->player->isAlive()) {
$this->velocity = 7;
$this->timeIncrement = 7;
}
}

public function onLand(Closure $callback): void
public function onFloorLand(Closure $callback): void
{
$this->onLand = $callback;
}
Expand Down
8 changes: 3 additions & 5 deletions server/src/Event/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class Event implements NetSerializable

abstract public function process(int $tick): void;

public function timeMsToTick(int $timeMs): int
public final function timeMsToTick(int $timeMs): int
{
return Util::millisecondsToFrames($timeMs);
}
Expand All @@ -26,7 +26,7 @@ public function reset(): void
$this->onComplete = [];
}

public final function runOnCompleteHooks(): void
protected final function runOnCompleteHooks(): void
{
foreach ($this->onComplete as $func) {
call_user_func($func, $this);
Expand All @@ -40,9 +40,7 @@ public function getCode(): int

public function serialize(): array
{
return [
'class' => get_class($this),
];
return [];
}

}
3 changes: 1 addition & 2 deletions server/src/Event/GrillEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use cs\Core\Column;
use cs\Core\Point;
use cs\Core\Util;
use cs\Enum\SoundType;
use cs\Interface\Flammable;

Expand All @@ -18,7 +17,7 @@ final class GrillEvent extends VolumetricEvent

protected function setup(): void
{
$this->damageCoolDownTickCount = Util::millisecondsToFrames(self::DAMAGE_COOL_DOWN_TIME_MS);
$this->damageCoolDownTickCount = $this->timeMsToTick(self::DAMAGE_COOL_DOWN_TIME_MS);
}

protected function onProcess(int $tick): void
Expand Down
4 changes: 2 additions & 2 deletions server/src/Event/ThrowEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public function __construct(
$this->floorCandidate = $origin->clone();
$this->ball = new BallCollider($this->world, $origin, $radius);
$this->needsToLandOnFloor = !($this->item instanceof Flashbang || $this->item instanceof HighExplosive);
$this->timeIncrement = 1 / Util::millisecondsToFrames(150); // fixme some good value or velocity or gravity :)
$this->tickMax = $this->getTickId() + Util::millisecondsToFrames($this->needsToLandOnFloor ? 99999 : 1200);
$this->timeIncrement = 1 / $this->timeMsToTick(150); // fixme some good value or velocity or gravity :)
$this->tickMax = $this->getTickId() + $this->timeMsToTick($this->needsToLandOnFloor ? 99999 : 1200);
}

private function makeEvent(Point $point, SoundType $type): Event
Expand Down
15 changes: 10 additions & 5 deletions server/src/Event/VolumetricEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use cs\Core\Player;
use cs\Core\Point;
use cs\Core\Sequence;
use cs\Core\Util;
use cs\Core\World;
use cs\Interface\ForOneRoundMax;
use cs\Interface\Volumetric;
Expand Down Expand Up @@ -54,8 +53,8 @@ public function __construct(
$this->id = Sequence::next();
$this->partSize = $this->partRadius * 2 + 1;
$this->startedTickId = $this->world->getTickId();
$this->spawnTickCount = Util::millisecondsToFrames(20);
$this->maxTicksCount = Util::millisecondsToFrames($this->item->getMaxTimeMs());
$this->spawnTickCount = $this->timeMsToTick(20);
$this->maxTicksCount = $this->timeMsToTick($this->item->getMaxTimeMs());

$partArea = ($this->partSize) ** 2;
$this->spawnPartCount = (int)ceil($this->item->getSpawnAreaMetersSquared() * 100 / $partArea);
Expand Down Expand Up @@ -121,7 +120,13 @@ public function process(int $tick): void

private function expand(int $tick): void
{
foreach ($this->loadParts() as $candidate) {
$candidates = $this->loadParts();
if ($candidates === []) {
$this->lastPartSpawnTickId = $tick + $this->maxTicksCount;
return;
}

foreach ($candidates as $candidate) {
$this->boundaryMin->set(
min($this->boundaryMin->x, $candidate->x - $this->partRadius),
min($this->boundaryMin->y, $candidate->y - 0),
Expand All @@ -133,9 +138,9 @@ private function expand(int $tick): void
max($this->boundaryMax->z, $candidate->z + $this->partRadius),
);

$this->lastPartSpawnTickId = $tick;
$this->parts[] = $this->expandPart($candidate);
}
$this->lastPartSpawnTickId = $tick;
}

/** @return Point[] */
Expand Down
4 changes: 3 additions & 1 deletion server/src/HitGeometry/HitBoxBack.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

class HitBoxBack extends SphereGroupHitBox
{
private Point $centerPoint;

public function __construct()
{
$this->centerPoint = new Point();
parent::__construct(function (Player $player): Point {
return (new Point())->addY($player->getHeadHeight());
return $this->centerPoint->setScalar(0)->addY($player->getHeadHeight());
});

$this->createBackLeft();
Expand Down
5 changes: 4 additions & 1 deletion server/src/HitGeometry/HitBoxChest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@

class HitBoxChest extends SphereGroupHitBox
{
private Point $centerPoint;

public function __construct()
{
$this->centerPoint = new Point();
parent::__construct(function (Player $player): Point {
return (new Point())->addY($player->getHeadHeight());
return $this->centerPoint->setScalar(0)->addY($player->getHeadHeight());
});

$this->createChestLeft();
Expand Down
4 changes: 3 additions & 1 deletion server/src/HitGeometry/HitBoxHead.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

class HitBoxHead extends SphereGroupHitBox
{
private Point $centerPoint;

public function __construct()
{
$this->centerPoint = new Point();
parent::__construct(function (Player $player): Point {
return (new Point())->addY($player->getHeadHeight());
return $this->centerPoint->setScalar(0)->addY($player->getHeadHeight());
});

$this->addHitBox(new Point(0, -8, 1), 8);
Expand Down
4 changes: 3 additions & 1 deletion server/src/HitGeometry/HitBoxStomach.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

class HitBoxStomach extends SphereGroupHitBox
{
private Point $centerPoint;

public function __construct()
{
$this->centerPoint = new Point();
parent::__construct(function (Player $player): Point {
return (new Point())->addY($player->getHeadHeight());
return $this->centerPoint->setScalar(0)->addY($player->getHeadHeight());
});

$this->createFrontRight();
Expand Down
4 changes: 2 additions & 2 deletions server/src/HitGeometry/SphereGroupHitBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(public readonly ?Closure $centerPointModifier = null
public function intersect(Player $player, Point $point): bool
{
/** @var Point $modifier */
$modifier = $this->centerPointModifier ? call_user_func($this->centerPointModifier, $player) : new Point();
$modifier = $this->centerPointModifier ? call_user_func($this->centerPointModifier, $player) : null;
foreach ($this->getParts($player) as $part) {
$center = $part->calculateWorldCoordinate($player, $modifier);
if (Collision::pointWithSphere($point, $center, $part->radius)) {
Expand All @@ -36,7 +36,7 @@ public function intersect(Player $player, Point $point): bool

public function addHitBox(Point $relativeCenter, int $radius): self
{
$this->parts[] = new SphereHitBox($relativeCenter, $radius);
$this->parts[] = $this->createHitBox($relativeCenter, $radius);
return $this;
}

Expand Down
12 changes: 5 additions & 7 deletions server/src/HitGeometry/SphereHitBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,17 @@ public function intersect(Player $player, Point $point): bool
return Collision::pointWithSphere($point, $center, $this->radius);
}

public function calculateWorldCoordinate(Player $player, Point $centerModifier = new Point()): Point
public function calculateWorldCoordinate(Player $player, ?Point $centerModifier = null): Point
{
$angle = $player->getSight()->getRotationHorizontal();
$center = $player->getPositionClone()->add($centerModifier);
$center = $player->getPositionClone();
if ($centerModifier) {
$center->add($centerModifier);
}
$point = $center->clone()->add($this->relativeCenter);

[$x, $z] = Util::rotatePointY($angle, $point->x, $point->z, $center->x, $center->z);
return $point->setX($x)->setZ($z);
}

public function getRelativeCenter(): Point
{
return $this->relativeCenter;
}

}
6 changes: 0 additions & 6 deletions server/src/Interface/Flammable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@
interface Flammable extends Volumetric
{

public function getMaxAreaMetersSquared(): int;

public function getSpawnAreaMetersSquared(): int;

public function getMaxTimeMs(): int;

public function getBoundingRadius(): int;

public function calculateDamage(bool $hasKevlar): int;
Expand Down
13 changes: 4 additions & 9 deletions server/src/Net/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@

namespace cs\Net;

class Client
final class Client
{

public function __construct(
private PlayerControl $playerControl,
public readonly string $ip,
public readonly int $port
public readonly PlayerControl $playerControl,
public readonly string $ip,
public readonly int $port
)
{
}

public function getPlayerControl(): PlayerControl
{
return $this->playerControl;
}

}
8 changes: 4 additions & 4 deletions server/src/Net/Protocol.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
abstract class Protocol
{

/** @var array<string,int> [methodName => maxCallCountPerTick] */
/** @var array<string,positive-int> [methodName => maxCallCountPerTick] */
public const playerControlMethods = [
'attack' => 1,
'attack2' => 1,
Expand All @@ -29,7 +29,7 @@ abstract class Protocol
'walk' => 1,
];

/** @var array<string,int> [methodName => paramCount] */
/** @var array<string,non-negative-int> [methodName => paramCount] */
public const playerControlMethodParamCount = [
'attack' => 0,
'attack2' => 0,
Expand Down Expand Up @@ -58,7 +58,7 @@ abstract class Protocol
],
];

/** @var array<string,int> [methodName => methodNumber] */
/** @var array<string,non-negative-int> [methodName => methodNumber] */
public const playerMethodByName = [
'attack' => 0,
'attack2' => 1,
Expand All @@ -79,7 +79,7 @@ abstract class Protocol
'walk' => 18,
];

/** @var array<int,string> [methodNumber => methodName] */
/** @var array<non-negative-int,string> [methodNumber => methodName] */
public const playerMethodByNumber = [
0 => 'attack',
1 => 'attack2',
Expand Down
2 changes: 1 addition & 1 deletion server/src/Net/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private function playerCreate(int $playerId, bool $attackerSide): PlayerControl
private function gameTick(int $tickId): ?GameOverEvent
{
foreach ($this->tickCommands as $playerId => $callback) {
call_user_func($callback, $this->clients[$playerId]->getPlayerControl());
call_user_func($callback, $this->clients[$playerId]->playerControl);
}
$this->tickCommands = [];
return $this->game->tick($tickId);
Expand Down
2 changes: 1 addition & 1 deletion server/src/Net/ServerSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(
public readonly int $warmupWaitSec = 60,
)
{
Util::$TICK_RATE = ($tickMs > 0 ? $tickMs : Util::$TICK_RATE);
Util::$TICK_RATE = ($tickMs > 0 ? $tickMs : Util::$TICK_RATE); // side effect
$this->warmupWaitSecRemains = $this->warmupWaitSec;
}

Expand Down
9 changes: 2 additions & 7 deletions test/og/Unit/HitBoxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,14 @@ public function testSphereHitBoxInvalidRadius(): void
new SphereHitBox(new Point(), 0);
$this->fail('Should throw');
} catch (GameException $ex) {
$this->assertStringContainsStringIgnoringCase('bigger than zero', $ex->getMessage());
}
try {
new SphereHitBox(new Point(), -2);
$this->fail('Should throw');
} catch (GameException $ex) {
$this->assertStringContainsStringIgnoringCase('bigger than zero', $ex->getMessage());
}

// Just grinding code coverage...
$point = new Point2D(1, 1);
$hit = new SphereHitBox(new Point($point->clone()->setY(-1)->addY(1)->setX(0)->x, 0), 2);
$point->setFrom(new Point2D());
$this->assertTrue($hit->getRelativeCenter()->to2D('xz')->equals($point));
$this->assertTrue($hit->getRelativeCenter()->to2D('xy')->equals($point));
}

public function testSphereWorldPointCenter(): void
Expand Down
9 changes: 8 additions & 1 deletion www/assets/js/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,14 @@ export class Game {
const range = this.#volumetrics[smokeId]['range']
const mesh = this.#volumetrics[smokeId]['mesh']

this.#roundIntervalIds.push(setInterval(() => mesh.geometry.setDrawRange(0, mesh.geometry.drawRange.count - range), 50))
const intervalId = setInterval(function() {
const newRange = mesh.geometry.drawRange.count - range
if (newRange <= 0) {
clearInterval(intervalId)
}
mesh.geometry.setDrawRange(0, newRange)
}, 50)
this.#roundIntervalIds.push(intervalId)
}

bombPlanted(timeMs, position) {
Expand Down
1 change: 1 addition & 0 deletions www/assets/js/Setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export class Setting {
'KeyB': Action.BUY_MENU,
'Tab': Action.SCORE_BOARD,
'Backquote': Action.GAME_MENU,
'IntlBackslash': Action.GAME_MENU,
'Enter': Action.CLEAR_DECALS,
'CapsLock': Action.CLEAR_DECALS,
'ArrowLeft': `buy ${BuyMenuItem.RIFLE_AK},${BuyMenuItem.RIFLE_M4A4}`,
Expand Down
Loading

0 comments on commit bc28967

Please sign in to comment.