Skip to content

Commit

Permalink
Tuning
Browse files Browse the repository at this point in the history
  • Loading branch information
solcloud committed Oct 22, 2022
1 parent 891bbe5 commit 01c03b8
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 256 deletions.
243 changes: 26 additions & 217 deletions www/assets/js/Control.js
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())
}
}
19 changes: 19 additions & 0 deletions www/assets/js/Enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export {
BuyMenuItem,
InventorySlot,
ItemId,
Action,
}

// server/src/Event/EventList.php
Expand Down Expand Up @@ -133,3 +134,21 @@ const ItemId = {
Molotov: 35,
Smoke: 36,
}

// PlayerAction.js
const Action = {
MOVE_FORWARD: 1,
MOVE_LEFT: 2,
MOVE_BACK: 3,
MOVE_RIGHT: 4,
JUMP: 5,
CROUCH: 6,
WALK: 7,
RELOAD: 8,
EQUIP_KNIFE: 9,
EQUIP_PRIMARY: 10,
EQUIP_SECONDARY: 11,
EQUIP_BOMB: 12,
BUY_MENU: 13,
SCORE_BOARD: 14,
}
29 changes: 26 additions & 3 deletions www/assets/js/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import {SoundType} from "./Enums.js";
import {SoundRepository} from "./SoundRepository.js";

export class Game {
#world
#hud
#stats
#pointer
#round = 1
#paused = false
#started = false
#options = false
#readyCallback
#endCallback
#hud
#stats
#world
#soundRepository
#hudDebounceTicks = 1
eventProcessor
Expand Down Expand Up @@ -235,6 +236,28 @@ export class Game {
return this.playerMe.isAlive()
}

setPointer(pointer) {
this.#pointer = pointer
}

getPlayerMeRotation() {
return threeRotationToServer(this.#pointer.getObject().rotation)
}

requestPointerLock() {
if (this.#pointer.isLocked) {
return
}
this.#pointer.lock()
}

requestPointerUnLock() {
if (!this.#pointer.isLocked) {
return
}
this.#pointer.unlock()
}

#render() {
if (this.#started && --this.#hudDebounceTicks === 0) {
this.#hudDebounceTicks = 4
Expand Down
Loading

0 comments on commit 01c03b8

Please sign in to comment.