Skip to content

Commit

Permalink
Code cleaning. With the latest "@newkrok/three-game" there was a poss…
Browse files Browse the repository at this point in the history
…ibility to remove a lot of non-generic code. Check the generic solution in the three-tps-demo project. https://github.com/NewKrok/three-tps-demo

Dependency update.
  • Loading branch information
NewKrok committed Apr 3, 2022
1 parent 7e420c2 commit c418c6e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 283 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.2.1",
"version": "0.3.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.2.1"
"@newkrok/three-game": "0.3.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
87 changes: 0 additions & 87 deletions src/js/newkrok/three-tps/control/unit-action-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ export const UnitAction = {
Left: "Left",
Right: "Right",
Run: "Run",
Jump: "Jump",
Attack: "Attack",
AttackFinish: "AttackFinish",
Crouch: "Crouch",
Aim: "Aim",
RotateCamera: "RotateCamera",
Interaction: "Interaction",
Pause: "Pause",
Expand All @@ -38,11 +33,6 @@ export const unitActionState = {
left: { pressed: false, value: 0 },
right: { pressed: false, value: 0 },
run: { pressed: false, value: 0 },
jump: { pressed: false, value: 0 },
attack: { pressed: false, value: 0 },
attackFinish: { pressed: false, value: 0 },
crouch: { pressed: false, value: 0 },
aim: { pressed: false, value: 0 },
interaction: { pressed: false, value: 0 },
pause: { pressed: false, value: 0 },
...Array.from({ length: 10 }).reduce(
Expand Down Expand Up @@ -164,39 +154,6 @@ const updateRunState = () => {
});
};

const updateJumpState = () => {
unitActionState.jump = calculateState({
prevState: unitActionState.jump,
keys: [keys.space],
gamepadButton: ButtonKey.ActionBottom,
action: UnitAction.Jump,
});
};

const updateAttackState = () => {
unitActionState.attack = calculateState({
prevState: unitActionState.attack,
keys: [],
gamepadButton: ButtonKey.ActionLeft,
action: UnitAction.Attack,
});
unitActionState.attackFinish = calculateState({
prevState: unitActionState.attackFinish,
keys: [],
gamepadButton: ButtonKey.AttackFinish,
action: UnitAction.AttackFinish,
});
};

const updateAimState = () => {
unitActionState.aim = calculateState({
prevState: unitActionState.aim,
keys: [],
gamepadButton: ButtonKey.RightTrigger,
action: UnitAction.Aim,
});
};

const updateChooseToolState = () => {
for (let i = 0; i < 10; i++) {
unitActionState[`chooseTool${i}`] = calculateState({
Expand All @@ -208,15 +165,6 @@ const updateChooseToolState = () => {
}
};

const updateCrouchState = () => {
unitActionState.crouch = calculateState({
prevState: unitActionState.crouch,
keys: [keys.control],
gamepadButton: ButtonKey.RightAxisButton,
action: UnitAction.Crouch,
});
};

const updateInteractionState = () => {
unitActionState.interaction = calculateState({
prevState: unitActionState.interaction,
Expand All @@ -243,10 +191,6 @@ export const updateUnitActions = () => {
updateLeftState();
updateRightState();
updateRunState();
updateJumpState();
updateAttackState();
updateCrouchState();
updateAimState();
updateChooseToolState();
updateInteractionState();
updatePauseState();
Expand Down Expand Up @@ -290,37 +234,6 @@ export const initUnitActions = () => {
value: { x: movementX / 350, y: movementY / 350 },
});
});
document.addEventListener("mousedown", (e) => {
switch (e.button) {
case 0:
trigger({
action: UnitAction.Attack,
value: 1,
});
break;

case 2:
trigger({
action: UnitAction.Aim,
value: 1,
});
break;

default:
}
});
document.addEventListener("mouseup", (e) => {
switch (e.button) {
case 0:
trigger({
action: UnitAction.AttackFinish,
value: 1,
});
break;

default:
}
});
};

export const onUnitAction = ({ action, callback }) => {
Expand Down
196 changes: 2 additions & 194 deletions src/js/newkrok/three-tps/control/unit-controller.js
Original file line number Diff line number Diff line change
@@ -1,166 +1,21 @@
import * as THREE from "three";

import {
UnitAction,
onUnitAction,
unitActionState,
} from "./unit-action-manager.js";
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 = {
Unarmed: 0,
Machete: 1,
Pistol: 2,
RIFLE: 3,
};
import { unitActionState } from "./unit-action-manager.js";

let _target = null;
let _world = null;

let isMovementBlocked = false;
let selectedWeaponType = WeaponType.Unarmed;

let stamina = 100;

const slashTimeout = 1000;
const shootTimeout = 200;

let crosshairs = null;
let isShootingActive = false;

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

export const setUnitControllerTarget = ({ target, world }) => {
_target = target;
_world = world;

onUnitAction({
action: UnitAction.Jump,
callback: () => {
if (_target.onGround) _target.jump();
},
});

onUnitAction({
action: UnitAction.Attack,
callback: () => (isShootingActive = true),
});

onUnitAction({
action: UnitAction.AttackFinish,
callback: () => (isShootingActive = false),
});

const chooseRifle = () => {
if (!_target.isWeaponChangeTriggered) {
// setTimeout(() => {
if (_target) {
if (selectedWeaponType !== WeaponType.RIFLE) {
selectedWeaponType = WeaponType.RIFLE;
//_target.usePistol();

/*playAudio({
audioId: AudioId.ChangeToPistol,
cacheId: AudioId.ChangeToPistol,
});*/
} else {
selectedWeaponType = WeaponType.Unarmed;
_target.useUnarmed();
clearAimState();
}
}
// }, 500);
if (selectedWeaponType === WeaponType.RIFLE) {
/*playAudio({
audioId: AudioId.ChangeToPistol,
cacheId: AudioId.ChangeToPistol,
});*/
}
_target.isWeaponChangeTriggered = true;
}
};

onUnitAction({
action: UnitAction.Aim,
callback: () => {
const zoom = () => {
_target.userData.useAim = !_target.userData.useAim;
if (_target.userData.useAim) _world.tpsCamera.useAimZoom();
else clearAimState();
/*playAudio({
audioId: AudioId.Aim,
cacheId: AudioId.Aim,
});*/
};

if (selectedWeaponType !== WeaponType.RIFLE) {
chooseRifle();
zoom();
} else zoom();
},
});

onUnitAction({
action: UnitAction.ChooseWeapon1,
callback: () => {
if (!_target.isWeaponChangeTriggered && _target.onGround) {
setTimeout(() => {
if (_target) {
if (selectedWeaponType !== WeaponType.Machete) {
selectedWeaponType = WeaponType.Machete;
_target.useMachete();
clearAimState();
/*playAudio({
audioId: AudioId.ChangeToMachete,
cacheId: AudioId.ChangeToMachete,
});*/
} else {
selectedWeaponType = WeaponType.Unarmed;
_target.useUnarmed();
clearAimState();
}
}
}, 300);
if (selectedWeaponType === WeaponType.Machete) {
/*playAudio({
audioId: AudioId.ChangeToMachete,
cacheId: AudioId.ChangeToMachete,
});*/
}
_target.isWeaponChangeTriggered = true;
}
},
});

onUnitAction({
action: UnitAction.ChooseWeapon2,
callback: () => {
if (!_target.isWeaponChangeTriggered && _target.onGround) {
chooseRifle();
}
},
});
};

export const updateUnitController = ({ now, delta }) => {
if (_target) {
const {
viewRotation,
wasSlashTriggered,
isSlashTriggered,
slashStartTime,
wasShootTriggered,
isShootTriggered,
shootStartTime,
userData: { useAim },
} = _target;

Expand All @@ -181,9 +36,7 @@ export const updateUnitController = ({ now, delta }) => {
: _target.config.speedInAir;

const velocity =
(stamina > 0 && unitActionState.run.pressed
? baseSpeed * 2.5
: baseSpeed) *
(unitActionState.run.pressed ? baseSpeed * 2.5 : baseSpeed) *
Math.max(
unitActionState.forward.value,
unitActionState.backward.value,
Expand Down Expand Up @@ -221,17 +74,11 @@ export const updateUnitController = ({ now, delta }) => {
}
_target.setRotation(_target.viewRotation + diff * (delta / 0.1));

/*physics.quaternion.setFromAxisAngle(
new CANNON.Vec3(0, 1, 0),
-_target.viewRotation
);*/

let normalizedDiff = Math.abs(diff);
normalizedDiff -= normalizedDiff > Math.PI ? Math.PI : 0;

velocityMultiplier =
normalizedDiff > 0.9 ? 0 : (Math.PI - normalizedDiff) / Math.PI;
if (selectedWeaponType != WeaponType.Unarmed) velocityMultiplier *= 0.9;

let noramalizedTargetRotation = Math.PI * 2 - targetRotation;
let relativeVector;
Expand Down Expand Up @@ -303,44 +150,5 @@ export const updateUnitController = ({ now, delta }) => {
}

_target.turn = 0;

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

if (
isSlashTriggered &&
wasSlashTriggered &&
now - slashStartTime > slashTimeout
) {
_target.wasSlashTriggered = false;
_target.isSlashTriggered = false;
}

if (
isShootTriggered &&
wasShootTriggered &&
now - shootStartTime > shootTimeout
) {
_target.wasShootTriggered = false;
_target.isShootTriggered = false;
}

const selectedTool = _target.getSelectedTool();
if (
selectedTool &&
isShootingActive &&
!_target.isShootTriggered &&
useAim &&
selectedWeaponType === WeaponType.RIFLE
) {
_target.isShootTriggered = true;
const position = _target
.getRegisteredObject("projectileStart")
.object.getWorldPosition(new THREE.Vector3());
const direction = selectedTool.object.getWorldDirection(
new THREE.Vector3()
);
selectedTool?.on?.activate({ world: _world, position, direction });
}
}
};

0 comments on commit c418c6e

Please sign in to comment.