forked from lieuzhenghong/discord-spatial-layer
-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
ee9e92b
commit 125a23a
Showing
4 changed files
with
71 additions
and
56 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,64 +1,10 @@ | ||
import GameClient from './GameClient' | ||
import instantiateClient from './instantiateClient' | ||
|
||
const SECRET_KEY_FORM_ID = 'secret-key-form' | ||
import runClient from './runClient'; | ||
|
||
window.onload = function () { | ||
const container = document.getElementById('container') | ||
container.style.height = '100%' | ||
container.style.width = '100%' | ||
container.style.position = 'absolute' | ||
|
||
container.insertAdjacentHTML('beforeend', renderSecretKeyForm()) | ||
} | ||
|
||
function renderSecretKeyForm() { | ||
return ` | ||
<form id="${SECRET_KEY_FORM_ID}"> | ||
<label for="secret-key">Secret Key:</label><br> | ||
<input type="text" id="secret-key" name="secret-key"><br> | ||
<input type="submit" value="Submit"> | ||
</form> | ||
` | ||
} | ||
|
||
let gameClient | ||
|
||
function handleSecretKeyFormSubmit(e) { | ||
const secretKey = document.getElementById('secret-key').value | ||
|
||
instantiateClient(secretKey) | ||
.then(client => { | ||
const canvas = '<canvas id=\'main-canvas\' style="height: 100%; width: 100%"></canvas>' | ||
container.innerText = '' | ||
container.insertAdjacentHTML('beforeend', canvas) | ||
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(res => { | ||
alert('shit secret key') | ||
}) | ||
runClient(container); | ||
} | ||
|
||
document.addEventListener('submit', e => { | ||
e.preventDefault() | ||
|
||
const form = e.target | ||
const formId = form.getAttribute('id') | ||
|
||
if (formId === SECRET_KEY_FORM_ID) { | ||
handleSecretKeyFormSubmit(e) | ||
} | ||
}, false) |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
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 ` | ||
<form id="${SECRET_KEY_FORM_ID}"> | ||
<label for="secret-key">Secret Key:</label><br> | ||
<input type="text" id="secret-key" name="secret-key"><br> | ||
<input type="submit" value="Submit"> | ||
</form> | ||
` | ||
} | ||
|
||
function handleSecretKeyFormSubmit(e) { | ||
const secretKey = document.getElementById('secret-key').value; | ||
console.log(secretKey); | ||
|
||
instantiateClient(secretKey) | ||
.then(client => { | ||
console.log("success"); | ||
const canvas = '<canvas id=\'main-canvas\' style="height: 100%; width: 100%"></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++ | ||
|
||
gameClient.update(delta, tick, now) | ||
} | ||
|
||
loop() | ||
}) | ||
.catch(err => { | ||
console.log(`error: ${err}`); | ||
alert('shit secret key') | ||
}) | ||
} | ||
|
||
|
||
document.addEventListener('submit', e => { | ||
e.preventDefault() | ||
|
||
const form = e.target | ||
const formId = form.getAttribute('id') | ||
|
||
if (formId === SECRET_KEY_FORM_ID) { | ||
handleSecretKeyFormSubmit(e) | ||
} | ||
}, false) |
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