Skip to content

Commit

Permalink
From now TPS and World camera is available too on the world object.
Browse files Browse the repository at this point in the history
Aiming logic was moved into the @newkrok/three-game's aiming module.
Dependency update.
  • Loading branch information
NewKrok committed Mar 27, 2022
1 parent a9b83c8 commit 8f4be23
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 55 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@newkrok/three-tps",
"version": "0.1.0",
"version": "0.2.0",
"description": "TPS extension for THREE Game",
"exports": {
"./src/*": "./src/*"
Expand All @@ -23,7 +23,7 @@
},
"homepage": "https://github.com/NewKrok/three-tps#readme",
"dependencies": {
"@newkrok/three-game": "0.1.0"
"@newkrok/three-game": "0.2.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
45 changes: 11 additions & 34 deletions src/js/newkrok/three-tps/control/unit-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,12 @@ import {
onUnitAction,
unitActionState,
} from "./unit-action-manager.js";

import { MODULE_ID } from "@newkrok/three-game/src/js/newkrok/three-game/modules/modules.js";
import { ModelSocketId } from "@newkrok/three-game/src/js/newkrok/three-game/unit/unit-enums.js";

//import { EffectId, effectsConfig } from "../../effects-config.js";

/* import {
createParticleSystem,
destroyParticleSystem,
} from "@newkrok/three-particles/src/js/effects/three-particles"; */

/* import { MODULE_ID } from "../../three-game/world.js";
import { MathUtils } from "three"; */
import {
UnitModuleId,
WorldModuleId,
} from "@newkrok/three-game/src/js/newkrok/three-game/modules/module-enums.js";

//import { AudioId } from "../../assets-config.js";

//import { playAudio } from "../../game-engine/audio/audio.js";

const WeaponType = {
Expand All @@ -45,7 +35,7 @@ let crosshairs = null;
let isShootingActive = false;

const clearAimState = () => {
_world.camera.disableAimZoom();
_world.tpsCamera.disableAimZoom();
_target.useAim = false;
};

Expand Down Expand Up @@ -103,8 +93,8 @@ export const setUnitControllerTarget = ({ target, world }) => {
action: UnitAction.Aim,
callback: () => {
const zoom = () => {
_target.useAim = !_target.useAim;
if (_target.useAim) _world.camera.useAimZoom();
_target.userData.useAim = !_target.userData.useAim;
if (_target.userData.useAim) _world.tpsCamera.useAimZoom();
else clearAimState();
/*playAudio({
audioId: AudioId.Aim,
Expand Down Expand Up @@ -171,10 +161,10 @@ export const updateUnitController = ({ now, delta }) => {
wasShootTriggered,
isShootTriggered,
shootStartTime,
useAim,
userData: { useAim },
} = _target;

const cameraRotation = _world.camera.getRotation().clone();
const cameraRotation = _world.tpsCamera.getRotation().clone();

isMovementBlocked = false;

Expand Down Expand Up @@ -314,7 +304,7 @@ export const updateUnitController = ({ now, delta }) => {

_target.turn = 0;

if (crosshairs) crosshairs.style.opacity = _target.useAim ? 1 : 0;
if (crosshairs) crosshairs.style.opacity = useAim ? 1 : 0;
else crosshairs = document.querySelector("#crosshairs");

if (
Expand All @@ -335,25 +325,12 @@ export const updateUnitController = ({ now, delta }) => {
_target.isShootTriggered = false;
}

const cameraPosition = _world.camera.instance.getWorldPosition(
new THREE.Vector3()
);
const cameraDirection = _world.camera.instance.getWorldDirection(
new THREE.Vector3()
);
const aimingRayResult = _world
.getModule(MODULE_ID.OCTREE)
.worldOctree.rayIntersect(new THREE.Ray(cameraPosition, cameraDirection));
_target.aimingPosition =
aimingRayResult?.position ||
cameraPosition.add(cameraDirection.setLength(15));

const selectedTool = _target.getSelectedTool();
if (
selectedTool &&
isShootingActive &&
!_target.isShootTriggered &&
_target.useAim &&
useAim &&
selectedWeaponType === WeaponType.RIFLE
) {
_target.isShootTriggered = true;
Expand Down
37 changes: 18 additions & 19 deletions src/js/newkrok/three-tps/tps-world.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ import {
updateUnitActions,
} from "./control/unit-action-manager";

import { MODULE_ID } from "@newkrok/three-game/src/js/newkrok/three-game/modules/modules.js";
import { WorldModuleId } from "@newkrok/three-game/src/js/newkrok/three-game/modules/module-enums.js";
import { createTPSCamera } from "./tps-camera";
import { createWorld } from "@newkrok/three-game/src/js/newkrok/three-game/world.js";
import { deepMerge } from "@newkrok/three-utils/src/js/newkrok/three-utils/object-utils.js";
import { updateUnitController } from "./control/unit-controller";

export const createTPSWorld = ({
target,
assetsConfig,
worldConfig,
characterConfig,
unitConfig,
}) => {
let _onUpdate;

const camera = createTPSCamera();
const tpsCamera = createTPSCamera();
const onLoaded = worldConfig.onLoaded;

const promise = new Promise((resolve, reject) => {
Expand All @@ -27,24 +28,19 @@ export const createTPSWorld = ({
target,
assetsConfig,
worldConfig: { ...worldConfig, onLoaded: null },
characterConfig,
camera: camera.instance,
characterTickRoutine: (character) => {
character.updateAimPosition(
character.useAim ? character.aimingPosition : null
);
},
unitConfig,
camera: tpsCamera.instance,
})
.then((world) => {
world.renderer.domElement.onclick =
world.renderer.domElement.requestPointerLock;

camera.init({
worldOctree: world.getModule(MODULE_ID.OCTREE).worldOctree,
tpsCamera.init({
worldOctree: world.getModule(WorldModuleId.OCTREE).worldOctree,
});

const update = (cycleData) => {
camera.update();
tpsCamera.update();
updateUnitActions();
updateUnitController(cycleData);
_onUpdate && _onUpdate(cycleData);
Expand All @@ -54,14 +50,17 @@ export const createTPSWorld = ({
initUnitActions();
onUnitAction({
action: UnitAction.RotateCamera,
callback: ({ x, y }) => camera.updateRotation({ x, y }),
callback: ({ x, y }) => tpsCamera.updateRotation({ x, y }),
});

const tpsWorld = {
...world,
camera,
onUpdate: (onUpdate) => (_onUpdate = onUpdate),
};
const tpsWorld = deepMerge(
world,
{
tpsCamera,
onUpdate: (onUpdate) => (_onUpdate = onUpdate),
},
{ applyToFirstObject: true }
);
onLoaded && onLoaded(tpsWorld);
resolve(tpsWorld);
})
Expand Down

0 comments on commit 8f4be23

Please sign in to comment.