-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
331 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,274 +1,83 @@ | ||
import {InventorySlot} from "./Enums.js"; | ||
|
||
export class Control { | ||
#game; | ||
#hud; | ||
#pointerLock; | ||
#localPlayerActions; | ||
#game | ||
#action | ||
#setting | ||
|
||
constructor(game, hud) { | ||
constructor(game, action, setting) { | ||
this.#game = game | ||
this.#hud = hud | ||
this.#action = action | ||
this.#setting = setting | ||
} | ||
|
||
getRotation() { | ||
return threeRotationToServer(this.#pointerLock.getObject().rotation) | ||
} | ||
|
||
init(camera) { | ||
let shootLookAt = '' | ||
let lastLookAt = '' | ||
let moveForward = false | ||
let moveBackward = false | ||
let moveLeft = false | ||
let moveRight = false | ||
let jumping = false | ||
let crouching = false | ||
let standing = false | ||
let attack = false | ||
let shifting = false | ||
let running = false | ||
let reload = false | ||
let equip = false | ||
let drop = false | ||
let spraying = false | ||
|
||
init(pointer) { | ||
const self = this | ||
const action = this.#action | ||
const game = this.#game | ||
const hud = this.#hud | ||
this.#pointerLock = new THREE.PointerLockControls(camera, document.body) | ||
const pointer = this.#pointerLock | ||
let sprayTriggerStartMs = null; | ||
const sprayTriggerDeltaMs = 80; // TODO settings | ||
const sprayEnableSlots = [InventorySlot.SLOT_KNIFE, InventorySlot.SLOT_PRIMARY] | ||
|
||
// todo: use binds object for action shortcut and allow changing in settings | ||
document.addEventListener("mouseup", function (event) { | ||
event.preventDefault() | ||
spraying = false | ||
sprayTriggerStartMs = null | ||
|
||
action.sprayingDisable() | ||
}) | ||
document.addEventListener("mousedown", function (event) { | ||
event.preventDefault() | ||
if (spraying) { | ||
spraying = false | ||
} | ||
|
||
action.sprayingDisable() | ||
if (!game.isPlaying() || game.isPaused() || !game.meIsAlive()) { | ||
return | ||
} | ||
|
||
if (!pointer.isLocked || !game.playerMe.data.canAttack) { | ||
return; | ||
} | ||
|
||
if (sprayEnableSlots.includes(game.playerMe.getEquippedSlotId())) { | ||
sprayTriggerStartMs = Date.now() | ||
spraying = true | ||
action.sprayingEnable() | ||
} | ||
|
||
attack = true | ||
let lookAt = self.getRotation() | ||
shootLookAt = `lookAt ${lookAt[0]} ${lookAt[1]}` | ||
action.attack(game.getPlayerMeRotation()) | ||
}) | ||
document.addEventListener('wheel', (event) => { | ||
event.preventDefault() | ||
|
||
if (!(game.isPlaying() && game.meIsAlive())) { | ||
return | ||
} | ||
|
||
if (event.deltaY > 0) { | ||
equip = InventorySlot.SLOT_SECONDARY | ||
action.equip(InventorySlot.SLOT_SECONDARY) | ||
} else { | ||
equip = InventorySlot.SLOT_PRIMARY | ||
action.equip(InventorySlot.SLOT_PRIMARY) | ||
} | ||
}) | ||
document.addEventListener('keydown', function (event) { | ||
event.preventDefault() | ||
|
||
switch (event.code) { | ||
case 'Tab': | ||
hud.showScore() | ||
break; | ||
} | ||
|
||
if (!(game.isPlaying() && game.meIsAlive())) { | ||
if (!game.isPlaying() || !game.meIsAlive()) { | ||
return | ||
} | ||
|
||
switch (event.code) { | ||
case 'KeyW': | ||
moveForward = true; | ||
break; | ||
case 'KeyA': | ||
moveLeft = true; | ||
break; | ||
case 'KeyS': | ||
moveBackward = true; | ||
break; | ||
case 'KeyD': | ||
moveRight = true; | ||
break; | ||
case 'KeyG': | ||
drop = true; | ||
break; | ||
case 'Space': | ||
jumping = true; | ||
break; | ||
case 'CapsLock': | ||
case 'ControlLeft': | ||
crouching = true; | ||
break; | ||
case 'ShiftLeft': | ||
shifting = true; | ||
break; | ||
const actionIndex = self.#setting.getBinds()[event.key] | ||
if (actionIndex !== undefined) { | ||
self.#action.actionCallback[actionIndex](true) | ||
} | ||
}); | ||
document.addEventListener('keyup', function (event) { | ||
event.preventDefault() | ||
|
||
switch (event.code) { | ||
case 'Tab': | ||
hud.hideScore() | ||
break; | ||
} | ||
|
||
if (!(game.isPlaying() && game.meIsAlive())) { | ||
return | ||
} | ||
|
||
switch (event.code) { | ||
case 'KeyW': | ||
moveForward = false; | ||
break; | ||
case 'KeyA': | ||
moveLeft = false; | ||
break; | ||
case 'KeyS': | ||
moveBackward = false; | ||
break; | ||
case 'KeyD': | ||
moveRight = false; | ||
break; | ||
case 'KeyR': | ||
reload = true; | ||
break; | ||
case 'KeyQ': | ||
equip = InventorySlot.SLOT_KNIFE; | ||
break; | ||
case 'Digit1': | ||
equip = InventorySlot.SLOT_PRIMARY; | ||
break; | ||
case 'Digit2': | ||
equip = InventorySlot.SLOT_SECONDARY; | ||
break; | ||
case 'Digit5': | ||
equip = InventorySlot.SLOT_BOMB; | ||
break; | ||
case 'CapsLock': | ||
case 'ControlLeft': | ||
standing = true; | ||
break; | ||
case 'ShiftLeft': | ||
running = true; | ||
break; | ||
case 'KeyB': | ||
hud.toggleBuyMenu() | ||
break; | ||
const actionIndex = self.#setting.getBinds()[event.key] | ||
if (actionIndex !== undefined) { | ||
self.#action.actionCallback[actionIndex](false) | ||
} | ||
}); | ||
|
||
this.#localPlayerActions = function () { | ||
let serverAction = [] | ||
|
||
if (game.buyList.length) { | ||
game.buyList.forEach(function (buyMenuItemId) { | ||
serverAction.push('buy ' + buyMenuItemId) | ||
}) | ||
game.buyList = [] | ||
} | ||
if (moveForward) { | ||
serverAction.push('forward') | ||
} | ||
if (moveLeft) { | ||
serverAction.push('left') | ||
} | ||
if (moveRight) { | ||
serverAction.push('right') | ||
} | ||
if (moveBackward) { | ||
serverAction.push('backward') | ||
} | ||
if (jumping) { | ||
serverAction.push('jump') | ||
jumping = false | ||
} | ||
if (crouching) { | ||
serverAction.push('crouch') | ||
crouching = false | ||
} | ||
if (standing) { | ||
serverAction.push('stand') | ||
standing = false | ||
} | ||
if (shifting) { | ||
serverAction.push('walk') | ||
shifting = false | ||
} | ||
if (running) { | ||
serverAction.push('run') | ||
running = false | ||
} | ||
if (reload) { | ||
serverAction.push('reload') | ||
reload = false | ||
} | ||
if (drop) { | ||
serverAction.push('drop') | ||
drop = false | ||
} | ||
if (equip !== false) { | ||
serverAction.push('equip ' + equip) | ||
equip = false | ||
} | ||
|
||
if (attack) { | ||
game.attack() | ||
serverAction.push(shootLookAt) | ||
serverAction.push('attack') | ||
attack = false | ||
} else if (spraying && sprayTriggerStartMs && sprayTriggerStartMs + sprayTriggerDeltaMs < Date.now()) { | ||
game.attack() | ||
let rotation = self.getRotation() | ||
serverAction.push(`lookAt ${rotation[0]} ${rotation[1]}`) | ||
serverAction.push('attack') | ||
} else { | ||
let horizontal, vertical | ||
[horizontal, vertical] = self.getRotation() | ||
let action = `lookAt ${horizontal} ${vertical}` | ||
if (lastLookAt !== action) { | ||
serverAction.push(action) | ||
lastLookAt = action | ||
} | ||
} | ||
|
||
return serverAction.join('|') | ||
} | ||
} | ||
|
||
requestLock() { | ||
if (this.#pointerLock.isLocked) { | ||
return | ||
} | ||
this.#pointerLock.lock() | ||
} | ||
|
||
requestUnLock() { | ||
if (!this.#pointerLock.isLocked) { | ||
return | ||
} | ||
this.#pointerLock.unlock() | ||
} | ||
|
||
getTickAction() { | ||
return this.#localPlayerActions() | ||
return this.#action.getPlayerAction(this.#setting.getSprayTriggerDeltaMs()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.