grab cursor

This commit is contained in:
Adrien MALINGREY 2023-06-14 01:22:47 +02:00
parent 2ba22ee7da
commit af24f0a6ef
2 changed files with 174 additions and 174 deletions

190
app.js
View File

@ -2,11 +2,11 @@ import * as THREE from 'three'
import { OrbitControls } from 'three/addons/controls/OrbitControls.js' import { OrbitControls } from 'three/addons/controls/OrbitControls.js'
import * as FPS from 'three/addons/libs/stats.module.js'; import * as FPS from 'three/addons/libs/stats.module.js';
let P = (x, y, z=0) => new THREE.Vector3(x, y, z) let P = (x, y, z = 0) => new THREE.Vector3(x, y, z)
Array.prototype.pick = function() { return this.splice(Math.floor(Math.random()*this.length), 1)[0] } Array.prototype.pick = function () { return this.splice(Math.floor(Math.random() * this.length), 1)[0] }
HTMLElement.prototype.addNewChild = function(tag, properties) { HTMLElement.prototype.addNewChild = function (tag, properties) {
let child = document.createElement(tag) let child = document.createElement(tag)
for (let key in properties) { for (let key in properties) {
child[key] = properties[key] child[key] = properties[key]
@ -34,10 +34,10 @@ const FACING = {
} }
const TRANSLATION = { const TRANSLATION = {
NONE: P( 0, 0), NONE: P(0, 0),
LEFT: P(-1, 0), LEFT: P(-1, 0),
RIGHT: P( 1, 0), RIGHT: P(1, 0),
DOWN: P( 0, -1), DOWN: P(0, -1),
} }
const ROTATION = { const ROTATION = {
@ -141,7 +141,7 @@ class Matrix extends THREE.Group {
clearLines() { clearLines() {
let nbClearedLines = 0 let nbClearedLines = 0
for (let y=ROWS-1; y>=0; y--) { for (let y = ROWS - 1; y >= 0; y--) {
let row = this.cells[y] let row = this.cells[y]
if (row.filter(mino => mino).length == COLUMNS) { if (row.filter(mino => mino).length == COLUMNS) {
nbClearedLines++ nbClearedLines++
@ -163,7 +163,7 @@ class Matrix extends THREE.Group {
updateUnlockedMinoes(delta) { updateUnlockedMinoes(delta) {
this.unlockedMinoes.forEach(mino => { this.unlockedMinoes.forEach(mino => {
mino.update(delta) mino.update(delta)
if (Math.sqrt(mino.position.x*mino.position.x + mino.position.z*mino.position.z) > 25) { if (Math.sqrt(mino.position.x * mino.position.x + mino.position.z * mino.position.z) > 25) {
this.remove(mino) this.remove(mino)
this.unlockedMinoes.delete(mino) this.unlockedMinoes.delete(mino)
} }
@ -202,16 +202,16 @@ const GRAVITY = -20
class Mino extends THREE.Mesh { class Mino extends THREE.Mesh {
constructor() { constructor() {
super(Mino.prototype.geometry) super(Mino.prototype.geometry)
this.velocity = P(50-100*Math.random(), 50-100*Math.random(), 50-100*Math.random()) this.velocity = P(50 - 100 * Math.random(), 50 - 100 * Math.random(), 50 - 100 * Math.random())
this.rotationAngle = P(Math.random(), Math.random(), Math.random()).normalize() this.rotationAngle = P(Math.random(), Math.random(), Math.random()).normalize()
this.angularVelocity = 5-10*Math.random() this.angularVelocity = 5 - 10 * Math.random()
scene.add(this) scene.add(this)
} }
update(delta) { update(delta) {
this.velocity.y += delta * GRAVITY this.velocity.y += delta * GRAVITY
this.position.addScaledVector(this.velocity, delta) this.position.addScaledVector(this.velocity, delta)
this.rotateOnWorldAxis(this.rotationAngle, delta*this.angularVelocity) this.rotateOnWorldAxis(this.rotationAngle, delta * this.angularVelocity)
} }
} }
const minoFaceShape = new THREE.Shape() const minoFaceShape = new THREE.Shape()
@ -234,7 +234,7 @@ Mino.prototype.geometry = new THREE.ExtrudeGeometry(minoFaceShape, minoExtrudeSe
class MinoMaterial extends THREE.MeshBasicMaterial { class MinoMaterial extends THREE.MeshBasicMaterial {
constructor( color ) { constructor(color) {
super({ super({
color: color, color: color,
reflectivity: 0.9, reflectivity: 0.9,
@ -249,7 +249,7 @@ class MinoMaterial extends THREE.MeshBasicMaterial {
class GhostMaterial extends THREE.MeshBasicMaterial { class GhostMaterial extends THREE.MeshBasicMaterial {
constructor( color ) { constructor(color) {
super({ super({
side: THREE.DoubleSide, side: THREE.DoubleSide,
color: color, color: color,
@ -274,7 +274,7 @@ class Tetromino extends THREE.Group {
this.rotatedLast = false this.rotatedLast = false
this.rotationPoint4Used = false this.rotationPoint4Used = false
this.holdEnabled = true this.holdEnabled = true
for (let i=0; i<4; i++) { for (let i = 0; i < 4; i++) {
this.add(new Mino()) this.add(new Mino())
} }
this.facing = 0 this.facing = 0
@ -305,7 +305,7 @@ class Tetromino extends THREE.Group {
return this._locked return this._locked
} }
canMove(translation, facing=this.facing) { canMove(translation, facing = this.facing) {
let testPosition = this.position.clone().add(translation) let testPosition = this.position.clone().add(translation)
return this.minoesPosition[facing].every(minoPosition => matrix.cellIsEmpty(minoPosition.clone().add(testPosition))) return this.minoesPosition[facing].every(minoPosition => matrix.cellIsEmpty(minoPosition.clone().add(testPosition)))
} }
@ -356,72 +356,72 @@ class Tetromino extends THREE.Group {
// Super Rotation System // Super Rotation System
// freedom of movement = srs[piece.facing][rotation] // freedom of movement = srs[piece.facing][rotation]
Tetromino.prototype.srs = [ Tetromino.prototype.srs = [
{ [ROTATION.CW]: [P(0, 0), P(-1, 0), P(-1, 1), P(0, -2), P(-1, -2)], [ROTATION.CCW]: [P(0, 0), P( 1, 0), P( 1, 1), P(0, -2), P( 1, -2)] }, { [ROTATION.CW]: [P(0, 0), P(-1, 0), P(-1, 1), P(0, -2), P(-1, -2)], [ROTATION.CCW]: [P(0, 0), P(1, 0), P(1, 1), P(0, -2), P(1, -2)] },
{ [ROTATION.CW]: [P(0, 0), P( 1, 0), P( 1, -1), P(0, 2), P( 1, 2)], [ROTATION.CCW]: [P(0, 0), P( 1, 0), P( 1, -1), P(0, 2), P( 1, 2)] }, { [ROTATION.CW]: [P(0, 0), P(1, 0), P(1, -1), P(0, 2), P(1, 2)], [ROTATION.CCW]: [P(0, 0), P(1, 0), P(1, -1), P(0, 2), P(1, 2)] },
{ [ROTATION.CW]: [P(0, 0), P( 1, 0), P( 1, 1), P(0, -2), P( 1, -2)], [ROTATION.CCW]: [P(0, 0), P(-1, 0), P(-1, 1), P(0, -2), P(-1, -2)] }, { [ROTATION.CW]: [P(0, 0), P(1, 0), P(1, 1), P(0, -2), P(1, -2)], [ROTATION.CCW]: [P(0, 0), P(-1, 0), P(-1, 1), P(0, -2), P(-1, -2)] },
{ [ROTATION.CW]: [P(0, 0), P(-1, 0), P(-1, -1), P(0, 2), P(-1, 2)], [ROTATION.CCW]: [P(0, 0), P(-1, 0), P(-1, -1), P(0, 2), P(-1, 2)] }, { [ROTATION.CW]: [P(0, 0), P(-1, 0), P(-1, -1), P(0, 2), P(-1, 2)], [ROTATION.CCW]: [P(0, 0), P(-1, 0), P(-1, -1), P(0, 2), P(-1, 2)] },
] ]
const minoRenderTarget = new THREE.WebGLCubeRenderTarget(256) const minoRenderTarget = new THREE.WebGLCubeRenderTarget(256)
minoRenderTarget.texture.type = THREE.HalfFloatType minoRenderTarget.texture.type = THREE.HalfFloatType
const minoCamera = new THREE.CubeCamera(1, 1000, minoRenderTarget) const minoCamera = new THREE.CubeCamera(1, 1000, minoRenderTarget)
minoCamera.position.set(5, 10) minoCamera.position.set(5, 10)
Tetromino.prototype.lockedMaterial = new MinoMaterial( 0xffffff ) Tetromino.prototype.lockedMaterial = new MinoMaterial(0xffffff)
class I extends Tetromino {} class I extends Tetromino { }
I.prototype.minoesPosition = [ I.prototype.minoesPosition = [
[P(-1, 0), P(0, 0), P(1, 0), P(2, 0)], [P(-1, 0), P(0, 0), P(1, 0), P(2, 0)],
[P( 1, 1), P(1, 0), P(1, -1), P(1, -2)], [P(1, 1), P(1, 0), P(1, -1), P(1, -2)],
[P(-1, -1), P(0, -1), P(1, -1), P(2, -1)], [P(-1, -1), P(0, -1), P(1, -1), P(2, -1)],
[P( 0, 1), P(0, 0), P(0, -1), P(0, -2)], [P(0, 1), P(0, 0), P(0, -1), P(0, -2)],
] ]
I.prototype.srs = [ I.prototype.srs = [
{ [ROTATION.CW]: [P(0, 0), P(-2, 0), P( 1, 0), P(-2, -1), P( 1, 2)], [ROTATION.CCW]: [P(0, 0), P(-1, 0), P( 2, 0), P(-1, 2), P( 2, -1)] }, { [ROTATION.CW]: [P(0, 0), P(-2, 0), P(1, 0), P(-2, -1), P(1, 2)], [ROTATION.CCW]: [P(0, 0), P(-1, 0), P(2, 0), P(-1, 2), P(2, -1)] },
{ [ROTATION.CW]: [P(0, 0), P(-1, 0), P( 2, 0), P(-1, 2), P( 2, -1)], [ROTATION.CCW]: [P(0, 0), P( 2, 0), P(-1, 0), P( 2, 1), P(-1, -2)] }, { [ROTATION.CW]: [P(0, 0), P(-1, 0), P(2, 0), P(-1, 2), P(2, -1)], [ROTATION.CCW]: [P(0, 0), P(2, 0), P(-1, 0), P(2, 1), P(-1, -2)] },
{ [ROTATION.CW]: [P(0, 0), P( 2, 0), P(-1, 0), P( 2, 1), P(-1, -2)], [ROTATION.CCW]: [P(0, 0), P( 1, 0), P(-2, 0), P( 1, -2), P(-2, 1)] }, { [ROTATION.CW]: [P(0, 0), P(2, 0), P(-1, 0), P(2, 1), P(-1, -2)], [ROTATION.CCW]: [P(0, 0), P(1, 0), P(-2, 0), P(1, -2), P(-2, 1)] },
{ [ROTATION.CW]: [P(0, 0), P( 1, 0), P(-2, 0), P( 1, -2), P(-2, 1)], [ROTATION.CCW]: [P(0, 0), P(-2, 0), P( 1, 0), P(-2, -1), P( 1, 2)] }, { [ROTATION.CW]: [P(0, 0), P(1, 0), P(-2, 0), P(1, -2), P(-2, 1)], [ROTATION.CCW]: [P(0, 0), P(-2, 0), P(1, 0), P(-2, -1), P(1, 2)] },
] ]
I.prototype.material = new MinoMaterial( 0xafeff9 ) I.prototype.material = new MinoMaterial(0xafeff9)
I.prototype.ghostMaterial = new GhostMaterial( 0xafeff9 ) I.prototype.ghostMaterial = new GhostMaterial(0xafeff9)
class J extends Tetromino {} class J extends Tetromino { }
J.prototype.minoesPosition = [ J.prototype.minoesPosition = [
[P(-1, 1), P(-1, 0), P(0, 0), P(1, 0)], [P(-1, 1), P(-1, 0), P(0, 0), P(1, 0)],
[P( 0, 1), P( 1, 1), P(0, 0), P(0, -1)], [P(0, 1), P(1, 1), P(0, 0), P(0, -1)],
[P( 1, -1), P(-1, 0), P(0, 0), P(1, 0)], [P(1, -1), P(-1, 0), P(0, 0), P(1, 0)],
[P( 0, 1), P(-1, -1), P(0, 0), P(0, -1)], [P(0, 1), P(-1, -1), P(0, 0), P(0, -1)],
] ]
J.prototype.material = new MinoMaterial( 0xb8b4ff ) J.prototype.material = new MinoMaterial(0xb8b4ff)
J.prototype.ghostMaterial = new GhostMaterial( 0xb8b4ff ) J.prototype.ghostMaterial = new GhostMaterial(0xb8b4ff)
class L extends Tetromino {} class L extends Tetromino { }
L.prototype.minoesPosition = [ L.prototype.minoesPosition = [
[P(-1, 0), P(0, 0), P(1, 0), P( 1, 1)], [P(-1, 0), P(0, 0), P(1, 0), P(1, 1)],
[P(0, 1), P(0, 0), P(0, -1), P( 1, -1)], [P(0, 1), P(0, 0), P(0, -1), P(1, -1)],
[P(-1, 0), P(0, 0), P(1, 0), P(-1, -1)], [P(-1, 0), P(0, 0), P(1, 0), P(-1, -1)],
[P(0, 1), P(0, 0), P(0, -1), P(-1, 1)], [P(0, 1), P(0, 0), P(0, -1), P(-1, 1)],
] ]
L.prototype.material = new MinoMaterial( 0xfdd0b7 ) L.prototype.material = new MinoMaterial(0xfdd0b7)
L.prototype.ghostMaterial = new GhostMaterial( 0xfdd0b7 ) L.prototype.ghostMaterial = new GhostMaterial(0xfdd0b7)
class O extends Tetromino {} class O extends Tetromino { }
O.prototype.minoesPosition = [ O.prototype.minoesPosition = [
[P(0, 0), P(1, 0), P(0, 1), P(1, 1)] [P(0, 0), P(1, 0), P(0, 1), P(1, 1)]
] ]
O.prototype.srs = [ O.prototype.srs = [
{[ROTATION.CW]: [], [ROTATION.CCW]: []} { [ROTATION.CW]: [], [ROTATION.CCW]: [] }
] ]
O.prototype.material = new MinoMaterial( 0xffedac ) O.prototype.material = new MinoMaterial(0xffedac)
O.prototype.ghostMaterial = new GhostMaterial( 0xffedac ) O.prototype.ghostMaterial = new GhostMaterial(0xffedac)
class S extends Tetromino {} class S extends Tetromino { }
S.prototype.minoesPosition = [ S.prototype.minoesPosition = [
[P(-1, 0), P(0, 0), P( 0, 1), P(1, 1)], [P(-1, 0), P(0, 0), P(0, 1), P(1, 1)],
[P( 0, 1), P(0, 0), P( 1, 0), P(1, -1)], [P(0, 1), P(0, 0), P(1, 0), P(1, -1)],
[P(-1, -1), P(0, 0), P( 1, 0), P(0, -1)], [P(-1, -1), P(0, 0), P(1, 0), P(0, -1)],
[P(-1, 1), P(0, 0), P(-1, 0), P(0, -1)], [P(-1, 1), P(0, 0), P(-1, 0), P(0, -1)],
] ]
S.prototype.material = new MinoMaterial( 0xC8FBA8 ) S.prototype.material = new MinoMaterial(0xC8FBA8)
S.prototype.ghostMaterial = new GhostMaterial( 0xC8FBA8 ) S.prototype.ghostMaterial = new GhostMaterial(0xC8FBA8)
class T extends Tetromino { class T extends Tetromino {
get tSpin() { get tSpin() {
@ -437,29 +437,29 @@ class T extends Tetromino {
} }
} }
T.prototype.minoesPosition = [ T.prototype.minoesPosition = [
[P(-1, 0), P(0, 0), P(1, 0), P( 0, 1)], [P(-1, 0), P(0, 0), P(1, 0), P(0, 1)],
[P( 0, 1), P(0, 0), P(1, 0), P( 0, -1)], [P(0, 1), P(0, 0), P(1, 0), P(0, -1)],
[P(-1, 0), P(0, 0), P(1, 0), P( 0, -1)], [P(-1, 0), P(0, 0), P(1, 0), P(0, -1)],
[P( 0, 1), P(0, 0), P(0, -1), P(-1, 0)], [P(0, 1), P(0, 0), P(0, -1), P(-1, 0)],
] ]
T.prototype.tSlots = [ T.prototype.tSlots = [
[P(-1, 1), P( 1, 1), P( 1, -1), P(-1, -1)], [P(-1, 1), P(1, 1), P(1, -1), P(-1, -1)],
[P( 1, 1), P( 1, -1), P(-1, -1), P(-1, 1)], [P(1, 1), P(1, -1), P(-1, -1), P(-1, 1)],
[P( 1, -1), P(-1, -1), P(-1, 1), P( 1, 1)], [P(1, -1), P(-1, -1), P(-1, 1), P(1, 1)],
[P(-1, -1), P(-1, 1), P( 1, 1), P( 1, -1)], [P(-1, -1), P(-1, 1), P(1, 1), P(1, -1)],
] ]
T.prototype.material = new MinoMaterial( 0xedb2ff ) T.prototype.material = new MinoMaterial(0xedb2ff)
T.prototype.ghostMaterial = new GhostMaterial( 0xedb2ff ) T.prototype.ghostMaterial = new GhostMaterial(0xedb2ff)
class Z extends Tetromino {} class Z extends Tetromino { }
Z.prototype.minoesPosition = [ Z.prototype.minoesPosition = [
[P(-1, 1), P( 0, 1), P(0, 0), P( 1, 0)], [P(-1, 1), P(0, 1), P(0, 0), P(1, 0)],
[P( 1, 1), P( 1, 0), P(0, 0), P( 0, -1)], [P(1, 1), P(1, 0), P(0, 0), P(0, -1)],
[P(-1, 0), P( 0, 0), P(0, -1), P( 1, -1)], [P(-1, 0), P(0, 0), P(0, -1), P(1, -1)],
[P( 0, 1), P(-1, 0), P(0, 0), P(-1, -1)] [P(0, 1), P(-1, 0), P(0, 0), P(-1, -1)]
] ]
Z.prototype.material = new MinoMaterial( 0xffb8c5 ) Z.prototype.material = new MinoMaterial(0xffb8c5)
Z.prototype.ghostMaterial = new GhostMaterial( 0xffb8c5 ) Z.prototype.ghostMaterial = new GhostMaterial(0xffb8c5)
class Ghost extends Tetromino { class Ghost extends Tetromino {
copy(piece) { copy(piece) {
@ -537,7 +537,7 @@ class Settings {
} }
} }
window.changeKey = function(input) { window.changeKey = function (input) {
let prevValue = input.value let prevValue = input.value
input.select() input.select()
input.onkeydown = function (event) { input.onkeydown = function (event) {
@ -591,7 +591,7 @@ class Stats {
set level(level) { set level(level) {
this._level = level this._level = level
this.goal += level * 5 this.goal += level * 5
if (level <= 20){ if (level <= 20) {
this.fallPeriod = 1000 * Math.pow(0.8 - ((level - 1) * 0.007), level - 1) this.fallPeriod = 1000 * Math.pow(0.8 - ((level - 1) * 0.007), level - 1)
} }
if (level > 15) if (level > 15)
@ -683,7 +683,7 @@ class Stats {
} }
this.score += b2bScore this.score += b2bScore
} }
} else if (nbClearedLines && !tSpin ) { } else if (nbClearedLines && !tSpin) {
if (this.b2b >= 1) { if (this.b2b >= 1) {
messagesSpan.addNewChild("div", { messagesSpan.addNewChild("div", {
className: "zoom-in-animation", className: "zoom-in-animation",
@ -728,18 +728,18 @@ Stats.prototype.timeFormat = new Intl.DateTimeFormat("fr-FR", {
/* Scene */ /* Scene */
const manager = new THREE.LoadingManager() const manager = new THREE.LoadingManager()
manager.onStart = function ( url, itemsLoaded, itemsTotal ) { manager.onStart = function (url, itemsLoaded, itemsTotal) {
messagesSpan.innerHTML = 'Chargement : 0%...' messagesSpan.innerHTML = 'Chargement : 0%...'
} }
manager.onLoad = function ( ) { manager.onLoad = function () {
restart() restart()
messagesSpan.innerHTML = "" messagesSpan.innerHTML = ""
animate() animate()
} }
manager.onProgress = function ( url, itemsLoaded, itemsTotal ) { manager.onProgress = function (url, itemsLoaded, itemsTotal) {
messagesSpan.innerHTML = 'Chargement : ' + 100 * itemsLoaded / itemsTotal + '%...' messagesSpan.innerHTML = 'Chargement : ' + 100 * itemsLoaded / itemsTotal + '%...'
} }
manager.onError = function ( url ) { manager.onError = function (url) {
messagesSpan.innerHTML = 'Erreur de chargement' messagesSpan.innerHTML = 'Erreur de chargement'
} }
@ -757,18 +757,21 @@ document.body.appendChild(renderer.domElement)
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000) const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000)
camera.position.set(5, 1, 16) camera.position.set(5, 1, 16)
const controls = new OrbitControls( camera, renderer.domElement ) const controls = new OrbitControls(camera, renderer.domElement)
controls.autoRotate controls.autoRotate
controls.enableDamping controls.enableDamping
controls.maxDistance = 21 controls.maxDistance = 21
controls.keys = {} controls.keys = {}
controls.minPolarAngle = 0.9 controls.minPolarAngle = 0.9
controls.maxPolarAngle = 2.14 controls.maxPolarAngle = 2.14
controls.minAzimuthAngle = 0.9 - Math.PI/2 controls.minAzimuthAngle = 0.9 - Math.PI / 2
controls.maxAzimuthAngle = 2.14 - Math.PI/2 controls.maxAzimuthAngle = 2.14 - Math.PI / 2
controls.target = P(5, 10) controls.target = P(5, 10)
controls.update() controls.update()
controls.addEventListener("start", () => renderer.domElement.style.cursor = "grabbing")
controls.addEventListener("end", () => renderer.domElement.style.cursor = "grab")
const showFPS = window.location.search.includes("fps") const showFPS = window.location.search.includes("fps")
const fps = new FPS.default(); const fps = new FPS.default();
if (showFPS) document.body.appendChild(fps.dom); if (showFPS) document.body.appendChild(fps.dom);
@ -839,12 +842,12 @@ const edgeMaterial = new THREE.MeshBasicMaterial({
const edgeShape = new THREE.Shape() const edgeShape = new THREE.Shape()
edgeShape.moveTo(-.3, SKYLINE) edgeShape.moveTo(-.3, SKYLINE)
edgeShape.lineTo( 0, SKYLINE) edgeShape.lineTo(0, SKYLINE)
edgeShape.lineTo( 0, 0) edgeShape.lineTo(0, 0)
edgeShape.lineTo(COLUMNS, 0) edgeShape.lineTo(COLUMNS, 0)
edgeShape.lineTo(COLUMNS, SKYLINE) edgeShape.lineTo(COLUMNS, SKYLINE)
edgeShape.lineTo(COLUMNS+.3, SKYLINE) edgeShape.lineTo(COLUMNS + .3, SKYLINE)
edgeShape.lineTo(COLUMNS+.3, -.3) edgeShape.lineTo(COLUMNS + .3, -.3)
edgeShape.lineTo(-.3, -.3) edgeShape.lineTo(-.3, -.3)
edgeShape.moveTo(-.3, SKYLINE) edgeShape.moveTo(-.3, SKYLINE)
const edgeExtrudeSettings = { const edgeExtrudeSettings = {
@ -905,7 +908,7 @@ function animate() {
/* Game logic */ /* Game logic */
messagesSpan.onanimationend = function(event) { messagesSpan.onanimationend = function (event) {
event.target.remove() event.target.remove()
} }
@ -915,7 +918,7 @@ let stats = new Stats()
let playing = false let playing = false
//let favicon = document.querySelector("link[rel~='icon']") //let favicon = document.querySelector("link[rel~='icon']")
window.restart = function() { window.restart = function () {
stats.modal.hide() stats.modal.hide()
stats.init() stats.init()
settings.init() settings.init()
@ -941,7 +944,6 @@ function pauseSettings() {
music.pause() music.pause()
document.onkeydown = null document.onkeydown = null
renderer.domElement.style.cursor = "auto"
settings.show() settings.show()
} }
@ -977,6 +979,7 @@ function resume(event) {
if (settings.form.checkValidity()) { if (settings.form.checkValidity()) {
settings.modal.hide() settings.modal.hide()
settings.getInputs() settings.getInputs()
renderer.domElement.focus()
document.onkeydown = onkeydown document.onkeydown = onkeydown
document.onkeyup = onkeyup document.onkeyup = onkeyup
@ -990,8 +993,6 @@ function resume(event) {
music.play() music.play()
} }
renderer.domElement.style.cursor = "move"
if (piece) scheduler.setInterval(fall, stats.fallPeriod) if (piece) scheduler.setInterval(fall, stats.fallPeriod)
else generate() else generate()
} }
@ -1025,18 +1026,18 @@ let playerActions = {
rotateCounterclockwise: () => piece.rotate(ROTATION.CCW), rotateCounterclockwise: () => piece.rotate(ROTATION.CCW),
softDrop: function() { softDrop: function () {
if (piece.move(TRANSLATION.DOWN)) stats.score++ if (piece.move(TRANSLATION.DOWN)) stats.score++
}, },
hardDrop: function() { hardDrop: function () {
scheduler.clearTimeout(lockDown) scheduler.clearTimeout(lockDown)
//hardDropSound.play() //hardDropSound.play()
while (piece.move(TRANSLATION.DOWN)) stats.score +=2 while (piece.move(TRANSLATION.DOWN)) stats.score += 2
lockDown() lockDown()
}, },
hold: function() { hold: function () {
if (piece.holdEnabled) { if (piece.holdEnabled) {
scheduler.clearInterval(fall) scheduler.clearInterval(fall)
scheduler.clearTimeout(lockDown) scheduler.clearTimeout(lockDown)
@ -1075,7 +1076,7 @@ function onkeydown(event) {
actionsQueue.unshift(action) actionsQueue.unshift(action)
scheduler.clearTimeout(repeat) scheduler.clearTimeout(repeat)
scheduler.clearInterval(autorepeat) scheduler.clearInterval(autorepeat)
if (action == playerActions.softDrop) scheduler.setInterval(autorepeat, settings.fallPeriod/20) if (action == playerActions.softDrop) scheduler.setInterval(autorepeat, settings.fallPeriod / 20)
else scheduler.setTimeout(repeat, settings.das) else scheduler.setTimeout(repeat, settings.das)
} }
} }
@ -1129,7 +1130,7 @@ function lockDown() {
if (tetrisSound.volume) tetrisSound.play() if (tetrisSound.volume) tetrisSound.play()
} else if (nbClearedLines || tSpin) { } else if (nbClearedLines || tSpin) {
lineClearSound.currentTime = 0 lineClearSound.currentTime = 0
if(lineClearSound.volume) lineClearSound.play() if (lineClearSound.volume) lineClearSound.play()
} }
stats.lockDown(nbClearedLines, tSpin) stats.lockDown(nbClearedLines, tSpin)
@ -1145,18 +1146,13 @@ function gameOver() {
document.onkeydown = null document.onkeydown = null
onblur = null onblur = null
playing = false playing = false
renderer.domElement.style.cursor = "auto"
music.pause() music.pause()
stats.show() stats.show()
} }
window.onbeforeunload = function(event) { window.onbeforeunload = function (event) {
stats.save() stats.save()
settings.save() settings.save()
if (playing) return false if (playing) return false
} }
/*if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('service-worker.js')
}*/

View File

@ -10,6 +10,10 @@ body {
} }
} }
canvas {
cursor: grab;
}
#titleHeader { #titleHeader {
letter-spacing: 1rem; letter-spacing: 1rem;
} }