From c2424dfcc5f856d8a159ab0d98576b0ce9797c0c Mon Sep 17 00:00:00 2001 From: Dexter Leng Date: Sat, 9 Jan 2021 02:23:18 +0800 Subject: [PATCH] oop that shit --- client/clientMain.js | 5 +- client/runClient.js | 108 +++++++++++++++++++++++-------------------- 2 files changed, 60 insertions(+), 53 deletions(-) diff --git a/client/clientMain.js b/client/clientMain.js index 868f943..46b7e7d 100644 --- a/client/clientMain.js +++ b/client/clientMain.js @@ -1,4 +1,4 @@ -import runClient from './runClient'; +import UI from './runClient'; window.onload = function () { const container = document.getElementById('container') @@ -6,5 +6,6 @@ window.onload = function () { container.style.width = '100%' container.style.position = 'absolute' - runClient(container); + const ui = new UI(container); + ui.start(); } diff --git a/client/runClient.js b/client/runClient.js index cb64149..ae79c36 100644 --- a/client/runClient.js +++ b/client/runClient.js @@ -1,62 +1,68 @@ import GameClient from './GameClient' import instantiateClient from './instantiateClient' -export default function runClient(container) { - container.innerText = ''; - container.insertAdjacentHTML('beforeend', renderSecretKeyForm()) -} - const SECRET_KEY_FORM_ID = 'secret-key-form' -function renderSecretKeyForm() { - return ` -
-
-
- -
- ` -} - -function handleSecretKeyFormSubmit(e) { - const secretKey = document.getElementById('secret-key').value; - console.log(secretKey); - - instantiateClient(secretKey) - .then(client => { - console.log("success"); - const canvas = '' - container.innerText = '' - container.insertAdjacentHTML('beforeend', canvas) - const gameClient = new GameClient(client) - let tick = 0 - let previous = performance.now() - const loop = function () { - window.requestAnimationFrame(loop) - const now = performance.now() - const delta = (now - previous) / 1000 - previous = now - tick++ +class UI { + constructor(container) { + this.container = container; + } - gameClient.update(delta, tick, now) + start() { + this.container.innerText = ''; + this.container.insertAdjacentHTML('beforeend', this.renderSecretKeyForm()); + document.addEventListener('submit', e => { + e.preventDefault() + + const form = e.target + const formId = form.getAttribute('id') + + if (formId === SECRET_KEY_FORM_ID) { + this.handleSecretKeyFormSubmit(e) } + }, false) + } - loop() - }) - .catch(err => { - console.log(`error: ${err}`); - alert('shit secret key') - }) -} - + renderSecretKeyForm() { + return ` +
+
+
+ +
+ ` + } -document.addEventListener('submit', e => { - e.preventDefault() + handleSecretKeyFormSubmit(e) { + const secretKey = document.getElementById('secret-key').value; + + instantiateClient(secretKey) + .then(client => { + console.log("success"); + const canvas = '' + this.container.innerText = '' + this.container.insertAdjacentHTML('beforeend', canvas) + const gameClient = new GameClient(client) + let tick = 0 + let previous = performance.now() + const loop = function () { + window.requestAnimationFrame(loop) + const now = performance.now() + const delta = (now - previous) / 1000 + previous = now + tick++ + + gameClient.update(delta, tick, now) + } + + loop() + }) + .catch(err => { + console.log(`error: ${err}`); + alert('shit secret key') + }) + } +} - const form = e.target - const formId = form.getAttribute('id') +export default UI; - if (formId === SECRET_KEY_FORM_ID) { - handleSecretKeyFormSubmit(e) - } -}, false) \ No newline at end of file