mirror of
https://github.com/solcloud/Counter-Strike.git
synced 2025-02-24 11:52:23 +01:00
103 lines
3.2 KiB
JavaScript
103 lines
3.2 KiB
JavaScript
import {InventorySlot} from "./Enums.js";
|
|
|
|
export class Control {
|
|
#game
|
|
#action
|
|
#setting
|
|
|
|
constructor(game, action) {
|
|
this.#game = game
|
|
this.#action = action
|
|
}
|
|
|
|
init(pointer, setting) {
|
|
this.#setting = setting
|
|
const self = this
|
|
const action = this.#action
|
|
const game = this.#game
|
|
const sprayEnableSlots = [InventorySlot.SLOT_KNIFE, InventorySlot.SLOT_PRIMARY, InventorySlot.SLOT_BOMB]
|
|
|
|
document.addEventListener("mouseup", function (event) {
|
|
event.preventDefault()
|
|
|
|
action.sprayingDisable()
|
|
})
|
|
document.addEventListener("mousedown", function (event) {
|
|
event.preventDefault()
|
|
|
|
action.sprayingDisable()
|
|
if (!game.isPlaying() || game.isPaused()) {
|
|
return
|
|
}
|
|
if (game.meIsSpectating() && (event.buttons === 1 || event.buttons === 2)) {
|
|
game.spectatePlayer(event.buttons === 1)
|
|
return
|
|
}
|
|
if (!pointer.isLocked || !game.playerMe.data.canAttack) {
|
|
return;
|
|
}
|
|
|
|
if (event.buttons === 2) {
|
|
action.attack2(game.getPlayerMeRotation())
|
|
}
|
|
if (event.buttons === 1) {
|
|
if (sprayEnableSlots.includes(game.playerMe.getEquippedSlotId())) {
|
|
action.sprayingEnable()
|
|
}
|
|
action.attack(game.getPlayerMeRotation())
|
|
}
|
|
})
|
|
document.addEventListener('wheel', (event) => {
|
|
if (!game.isPlaying() || !game.meIsAlive()) {
|
|
return
|
|
}
|
|
|
|
if (event.deltaY > 0) { // wheel down
|
|
if (game.playerMe.data.slots[InventorySlot.SLOT_SECONDARY]) {
|
|
action.equip(InventorySlot.SLOT_SECONDARY)
|
|
} else {
|
|
action.equip(InventorySlot.SLOT_KNIFE)
|
|
}
|
|
} else { // wheel up
|
|
if (game.playerMe.data.slots[InventorySlot.SLOT_PRIMARY]) {
|
|
action.equip(InventorySlot.SLOT_PRIMARY)
|
|
} else {
|
|
action.equip(InventorySlot.SLOT_KNIFE)
|
|
}
|
|
}
|
|
})
|
|
document.addEventListener('keydown', function (event) {
|
|
event.preventDefault()
|
|
|
|
if (!game.isPlaying() || !game.meIsAlive()) {
|
|
return
|
|
}
|
|
|
|
self.#processKeyboardEvent(event, true)
|
|
});
|
|
document.addEventListener('keyup', function (event) {
|
|
event.preventDefault()
|
|
|
|
if (!(game.isPlaying() && game.meIsAlive())) {
|
|
return
|
|
}
|
|
|
|
self.#processKeyboardEvent(event, false)
|
|
});
|
|
}
|
|
|
|
#processKeyboardEvent(event, isKeyDown) {
|
|
const actionIndex = this.#setting.getBinds()[event.code]
|
|
if (actionIndex !== undefined) {
|
|
this.#action.actionCallback[actionIndex](isKeyDown)
|
|
}
|
|
}
|
|
|
|
getTickAction() {
|
|
if (this.#game.isPlaying() && this.#game.meIsAlive()) {
|
|
return this.#action.getPlayerAction(this.#game, this.#setting.getSprayTriggerDeltaMs())
|
|
}
|
|
return ''
|
|
}
|
|
}
|