Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
starswaitforus committed Aug 26, 2024
1 parent 2e5b950 commit 87d788e
Show file tree
Hide file tree
Showing 27 changed files with 141 additions and 202 deletions.
13 changes: 13 additions & 0 deletions server/src/Core/Sequence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace cs\Core;

final class Sequence
{
private static int $value = 0;

public static function next(): string
{
return 'id-' . ++self::$value;
}
}
8 changes: 5 additions & 3 deletions server/src/Core/World.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,14 +547,14 @@ public function smokeTryToExtinguishFlames(Column $smoke): void
}

foreach ($fire->parts as $flame) {
if (Collision::boxWithBox($smoke->boundaryMin, $smoke->boundaryMax, $flame->boundaryMin, $flame->boundaryMax)) {
if ($flame->active && Collision::boxWithBox($smoke->boundaryMin, $smoke->boundaryMax, $flame->boundaryMin, $flame->boundaryMax)) {
$fire->extinguish($flame);
}
}
}
}

public function tryToExtinguishFire(GrillEvent $fire, Column $flame): void
public function flameCanIgnite(Column $flame): bool
{
foreach ($this->activeSmokes as $smoke) {
if (!Collision::boxWithBox($smoke->boundaryMin, $smoke->boundaryMax, $flame->boundaryMin, $flame->boundaryMax)
Expand All @@ -564,10 +564,12 @@ public function tryToExtinguishFire(GrillEvent $fire, Column $flame): void

foreach ($smoke->parts as $smokePart) {
if (Collision::boxWithBox($smokePart->boundaryMin, $smokePart->boundaryMax, $flame->boundaryMin, $flame->boundaryMax)) {
$fire->extinguish($flame);
return false;
}
}
}

return true;
}

public function checkFlameDamage(GrillEvent $fire, int $tickId): void
Expand Down
3 changes: 2 additions & 1 deletion server/src/Event/DropEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use cs\Core\Item;
use cs\Core\Player;
use cs\Core\Point;
use cs\Core\Sequence;
use cs\Core\Util;
use cs\Core\World;
use cs\Enum\SoundType;
Expand All @@ -27,7 +28,7 @@ class DropEvent extends Event implements ForOneRoundMax

public function __construct(private readonly Player $player, private readonly Item $item, private readonly World $world)
{
$this->id = "drop-{$this->player->getId()}-{$this->item->getId()}-{$this->world->getTickId()}";
$this->id = Sequence::next();
$this->origin = $this->player->getSightPositionClone();
$this->dropItem = new DropItem($this->id, $this->item, $this->origin->clone());
$this->angleHorizontal = $player->getSight()->getRotationHorizontal();
Expand Down
19 changes: 12 additions & 7 deletions server/src/Event/GrillEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ final class GrillEvent extends VolumetricEvent
{
public const DAMAGE_COOL_DOWN_TIME_MS = 100;

protected string $eventName = 'grill';
/** @var array<int,int> [playerId => tick] */
private array $playerTickHits = [];
private int $damageCoolDownTickCount;
Expand All @@ -37,18 +36,24 @@ protected function shrinkPart(Column $column): void
protected function expandPart(Point $center): Column
{
$flame = new Column($center, $this->partRadius, $this->partHeight);
$sound = new SoundEvent($flame->center, SoundType::FLAME_SPAWN);
$sound->addExtra('id', $this->id);
$sound->addExtra('height', $flame->height);
$this->world->makeSound($sound);

$this->world->tryToExtinguishFire($this, $flame);
if ($this->world->flameCanIgnite($flame)) {
$sound = new SoundEvent($flame->center, SoundType::FLAME_SPAWN);
$sound->addExtra('id', $this->id);
$sound->addExtra('height', $flame->height);
$this->world->makeSound($sound);
} else {
$flame->active = false;
}

return $flame;
}

public function extinguish(Column $flame): void
{
if (!$flame->active) {
return;
}

$flame->active = false;
$this->shrinkPart($flame);
}
Expand Down
1 change: 0 additions & 1 deletion server/src/Event/SmokeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ final class SmokeEvent extends VolumetricEvent
public const MAX_CORNER_HEIGHT = 270;

private int $maxHeight = self::MAX_HEIGHT;
protected string $eventName = 'smoke';

protected function setup(): void
{
Expand Down
5 changes: 3 additions & 2 deletions server/src/Event/ThrowEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use cs\Core\Item;
use cs\Core\Player;
use cs\Core\Point;
use cs\Core\Sequence;
use cs\Core\Util;
use cs\Core\World;
use cs\Enum\SoundType;
Expand All @@ -22,7 +23,7 @@
final class ThrowEvent extends Event implements Attackable, ForOneRoundMax
{

private string $id;
private readonly string $id;
private float $time = 0.0;
private float $timeIncrement;
private Point $position;
Expand All @@ -49,6 +50,7 @@ public function __construct(
throw new GameException("Velocity needs to be positive"); // @codeCoverageIgnore
}

$this->id = Sequence::next();
$this->position = $origin->clone();
$this->lastEventPosition = $origin->clone();
$this->floorCandidate = $origin->clone();
Expand Down Expand Up @@ -179,7 +181,6 @@ public function process(int $tick): void

public function fire(): AttackResult
{
$this->id = "throw-{$this->player->getId()}-{$this->getTickId()}";
$this->player->getInventory()->removeEquipped();
$this->player->equip($this->player->getEquippedItem()->getSlot());
$this->velocity *= $this->item->getSpeedMultiplier();
Expand Down
4 changes: 2 additions & 2 deletions server/src/Event/VolumetricEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use cs\Core\Graph;
use cs\Core\Player;
use cs\Core\Point;
use cs\Core\Sequence;
use cs\Core\Util;
use cs\Core\World;
use cs\Interface\ForOneRoundMax;
Expand All @@ -26,7 +27,6 @@ abstract class VolumetricEvent extends Event implements ForOneRoundMax
private int $maxTicksCount;
private readonly int $partSize;
private readonly int $maxPartCount;
protected string $eventName;

public readonly Point $boundaryMin;
public readonly Point $boundaryMax;
Expand All @@ -51,6 +51,7 @@ public function __construct(
throw new GameException("No node for start point: " . $start->hash());
}

$this->id = Sequence::next();
$this->partSize = $this->partRadius * 2 + 1;
$this->startedTickId = $this->world->getTickId();
$this->spawnTickCount = Util::millisecondsToFrames(20);
Expand All @@ -61,7 +62,6 @@ public function __construct(
$this->maxPartCount = (int)ceil($this->item->getMaxAreaMetersSquared() / $partArea);

$this->setup();
$this->id = "{$this->eventName}-{$initiator->getId()}-{$this->world->getTickId()}";
$this->boundaryMin = $start->clone();
$this->boundaryMax = $start->clone();
$this->queue = new SplQueue();
Expand Down
4 changes: 2 additions & 2 deletions test/og/Shooting/MolotovGrenadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public function testSmokeExtinguishFlames(): void
$game->getWorld()->addBox(new Box(new Point(), 1000, 2000, 1000));
$health = 100;

$this->playPlayerDebug($game, [
$this->playPlayer($game, [
fn(Player $p) => $p->setPosition(new Point(500, 0, 500)),
fn(Player $p) => $this->assertTrue($p->buyItem(BuyMenuItem::GRENADE_MOLOTOV)),
fn(Player $p) => $this->assertTrue($p->buyItem(BuyMenuItem::GRENADE_SMOKE)),
Expand All @@ -244,7 +244,7 @@ public function testSmokeExtinguishFlames(): void
fn(Player $p) => $this->assertNotNull($p->attackSecondary()),
fn(Player $p) => $this->assertTrue($p->equip(InventorySlot::SLOT_GRENADE_MOLOTOV)),
$this->waitNTicks(Molotov::equipReadyTimeMs),
$this->waitNTicks(ceil(Smoke::MAX_TIME_MS / 3)),
$this->waitNTicks((int)ceil(Smoke::MAX_TIME_MS / 3)),
fn(Player $p) => $p->getSight()->look(0, -90),
fn(Player $p) => $this->assertNotNull($p->attack()),
fn(Player $p) => $this->assertSame($health, $p->getHealth()),
Expand Down
6 changes: 4 additions & 2 deletions www/assets/js/Game.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as THREE from 'three' // fixme try remove from game, maybe only allow import Vec3...
import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js' // todo remove
import {EventProcessor} from "./EventProcessor.js";
import {Player} from "./Player.js";
import {InventorySlot, SoundType} from "./Enums.js";
Expand Down Expand Up @@ -353,8 +355,8 @@ export class Game {
return
}

let geometry= THREE.BufferGeometryUtils.mergeBufferGeometries(this.#volumetrics[smokeId]['geometries']);
geometry = THREE.BufferGeometryUtils.mergeVertices(geometry, 2);
let geometry= BufferGeometryUtils.mergeGeometries(this.#volumetrics[smokeId]['geometries']);
geometry = BufferGeometryUtils.mergeVertices(geometry, 2);
geometry.computeBoundingBox();
let mesh = this.#volumetrics[smokeId]['mesh']
if (mesh) {
Expand Down
11 changes: 7 additions & 4 deletions www/assets/js/ModelRepository.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import * as THREE from 'three'
import {GLTFLoader} from 'three/addons/loaders/GLTFLoader.js';
import * as SkeletonUtils from 'three/addons/utils/SkeletonUtils.js';
import {ItemId} from "./Enums.js";

export class ModelRepository {
Expand All @@ -17,7 +20,7 @@ export class ModelRepository {
#mapObjects = [];

constructor() {
this.#gltfLoader = new THREE.GLTFLoader()
this.#gltfLoader = new GLTFLoader()
this.#textureLoader = new THREE.TextureLoader()
}

Expand Down Expand Up @@ -54,7 +57,7 @@ export class ModelRepository {
}
})

const sun = new THREE.DirectionalLight(0xffeac2, .9)
const sun = new THREE.DirectionalLight(0xffeac2, 4)
sun.position.set(4000, 4999, -4000)
sun.castShadow = true
sun.shadow.mapSize.width = 4096
Expand All @@ -64,7 +67,7 @@ export class ModelRepository {
sun.shadow.camera.right = 2000
sun.shadow.camera.top = 0
sun.shadow.camera.bottom = -3000
model.scene.add(sun, new THREE.AmbientLight(0xcfe4bb, .4))
model.scene.add(sun, new THREE.AmbientLight(0xcfe4bb, 1))
return model.scene
})
}
Expand All @@ -78,7 +81,7 @@ export class ModelRepository {
}

getPlayer(colorIndex, isOpponent) {
const clone = THREE.SkeletonUtils.clone(this.#models.player)
const clone = SkeletonUtils.clone(this.#models.player)
const player = clone.getObjectByName('player')

const headWear = player.getObjectByName('Wolf3D_Headwear')
Expand Down
6 changes: 4 additions & 2 deletions www/assets/js/Player.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {AnimationMixer, Vector3} from 'three'

export class Player {
data = {
id: null,
Expand Down Expand Up @@ -45,7 +47,7 @@ export class Player {

setAnimations(animations) {
animations.forEach((clip) => {
const mixer = new THREE.AnimationMixer(this.#threeObject)
const mixer = new AnimationMixer(this.#threeObject)
const action = mixer.clipAction(clip);
if (clip.name === 'crouch') {
action.play()
Expand Down Expand Up @@ -114,7 +116,7 @@ export class Player {
}

getSightPositionThreeVector() {
return new THREE.Vector3(
return new Vector3(
this.data.position.x,
this.data.position.y + this.data.sight,
-this.data.position.z,
Expand Down
17 changes: 7 additions & 10 deletions www/assets/js/World.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as Enum from "./Enums.js";
import {ModelRepository} from "./ModelRepository.js";
import * as THREE from 'three'
import * as Enum from "./Enums.js"
import {RGBELoader} from "three/addons/loaders/RGBELoader.js"
import {ModelRepository} from "./ModelRepository.js"

export class World {
#scene
Expand All @@ -9,13 +11,11 @@ export class World {
#audioLoader
#modelRepository
#decals = []
#flames = []
#cache = {}
volume = 30

constructor() {
THREE.Cache.enabled = true
THREE.ColorManagement.legacyMode = false

this.#audioLoader = new THREE.AudioLoader()
this.#modelRepository = new ModelRepository()
Expand Down Expand Up @@ -51,15 +51,15 @@ export class World {
}
const renderer = new THREE.WebGLRenderer(glParameters)
renderer.outputEncoding = THREE.sRGBEncoding
renderer.toneMapping = THREE.ACESFilmicToneMapping
renderer.toneMapping = THREE.CineonToneMapping
renderer.toneMappingExposure = setting.getExposure()
renderer.physicallyCorrectLights = false
renderer.setSize(window.innerWidth, window.innerHeight)
if (!setting.shouldPreferPerformance()) {
new THREE.RGBELoader().load('./resources/img/orlando_stadium_1k.hdr', function (texture) {
new RGBELoader().load('./resources/img/orlando_stadium_1k.hdr', function (texture) {
texture.mapping = THREE.EquirectangularReflectionMapping
texture.toneMapped = true
scene.environment = texture
scene.environmentIntensity = 0.4
})
renderer.shadowMap.enabled = true
renderer.shadowMap.type = THREE.PCFSoftShadowMap
Expand Down Expand Up @@ -196,7 +196,6 @@ export class World {
mesh.receiveShadow = true

this.#scene.add(mesh)
this.#flames.push(mesh)
return mesh
}

Expand Down Expand Up @@ -240,8 +239,6 @@ export class World {

reset() {
this.clearDecals()
this.#flames.forEach((item) => this.destroyObject(item))
this.#flames = []
const bomb = this.#modelRepository.getBomb()
if (bomb.parent && bomb.parent.name === 'MainScene') {
bomb.visible = false
Expand Down
1 change: 1 addition & 0 deletions www/assets/js/hud/Radar.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as THREE from 'three'
import {Color, InventorySlot} from "../Enums.js";

export class Radar {
Expand Down
3 changes: 2 additions & 1 deletion www/assets/js/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {World} from "./World.js";
import Stats from "../threejs/Stats.js";
import {Setting} from "./Setting.js";
import {PlayerAction} from "./PlayerAction.js";
import {PointerLockControls} from "three/addons/controls/PointerLockControls.js";

let launchGame
(function () {
Expand Down Expand Up @@ -51,7 +52,7 @@ let launchGame

const setting = new Setting(settingString)
const canvas = await world.init(map, setting)
const pointerLock = new THREE.PointerLockControls(world.getCamera(), canvasParent)
const pointerLock = new PointerLockControls(world.getCamera(), canvasParent)
pointerLock.pointerSpeed = setting.getSensitivity()

hud.createHud(elementHud, map, setting)
Expand Down
12 changes: 6 additions & 6 deletions www/assets/js/utils.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
function degreeToRadian(degree) {
return THREE.MathUtils.degToRad(degree)
return degree * Math.PI / 180
}

function radianToDegree(radian) {
return Math.round(THREE.MathUtils.radToDeg(radian))
return Math.round(radian * 180 / Math.PI)
}

function threeRotationToServer(eulerYXZ) {
const horizontal = THREE.MathUtils.radToDeg(eulerYXZ.y)
const vertical = THREE.MathUtils.radToDeg(eulerYXZ.x)
const horizontal = eulerYXZ.y * 180 / Math.PI
const vertical = eulerYXZ.x * 180 / Math.PI

if (horizontal === 0) {
return [0, vertical]
Expand Down Expand Up @@ -38,11 +38,11 @@ function scopeLevelToZoom(scopeLevel) {
}

function randomInt(start, end) {
return THREE.MathUtils.randInt(start, end)
return start + Math.floor(Math.random() * (end - start + 1));
}

function lerp(start, end, percentage) {
return THREE.MathUtils.lerp(start, end, percentage)
return ( 1 - percentage ) * start + percentage * end;
}

function msToTick(timeMs) {
Expand Down
Loading

0 comments on commit 87d788e

Please sign in to comment.