tank movement (without collision)
This commit is contained in:
parent
a3bd582b2e
commit
54b840da3e
18 changed files with 321 additions and 254 deletions
|
@ -20,8 +20,6 @@ function drawPixelsToCanvas(pixels: Uint8Array, canvas: HTMLCanvasElement) {
|
|||
const imageData = drawContext.getImageData(0, 0, canvas.width, canvas.height, {colorSpace: 'srgb'});
|
||||
const data = imageData.data;
|
||||
|
||||
console.log('draw', {width: canvas.width, height: canvas.height, dataLength: data.byteLength});
|
||||
|
||||
for (let y = 0; y < canvas.height; y++) {
|
||||
const rowStartPixelIndex = y * pixelsPerRow;
|
||||
for (let x = 0; x < canvas.width; x++) {
|
||||
|
|
|
@ -42,7 +42,6 @@ export default function Controls({playerId}: {
|
|||
return;
|
||||
|
||||
const message = new Uint8Array([typeCode, value]);
|
||||
console.log('input', message);
|
||||
sendMessage(message);
|
||||
};
|
||||
|
||||
|
|
75
tank-frontend/src/controls.js
vendored
75
tank-frontend/src/controls.js
vendored
|
@ -1,75 +0,0 @@
|
|||
import './controls.css';
|
||||
|
||||
const body = document.querySelector('body');
|
||||
const splash = document.querySelector('.splash');
|
||||
|
||||
if (!splash || !body)
|
||||
throw new Error('required element not found');
|
||||
|
||||
splash.addEventListener('transitionend', function () {
|
||||
body.classList.remove('was-killed');
|
||||
});
|
||||
|
||||
const connection = new WebSocket(`ws://${window.location.hostname}:3000`);
|
||||
connection.binaryType = 'blob';
|
||||
|
||||
connection.onmessage = function (message) {
|
||||
message = JSON.parse(message.data);
|
||||
console.log('got message', {message});
|
||||
if (message.type === 'shot')
|
||||
body.classList.add('was-killed');
|
||||
};
|
||||
|
||||
connection.onerror = event => {
|
||||
console.log('error', event);
|
||||
alert('connection error');
|
||||
};
|
||||
|
||||
connection.onclose = event => {
|
||||
console.log('closed', event);
|
||||
alert('connection closed - maybe a player with this name is already connected');
|
||||
};
|
||||
|
||||
const keyEventListener = (type) => (event) => {
|
||||
if (event.defaultPrevented)
|
||||
return;
|
||||
|
||||
const controls = {
|
||||
'ArrowLeft': 'left',
|
||||
'ArrowUp': 'up',
|
||||
'ArrowRight': 'right',
|
||||
'ArrowDown': 'down',
|
||||
'Space': 'shoot',
|
||||
'KeyW': 'up',
|
||||
'KeyA': 'left',
|
||||
'KeyS': 'down',
|
||||
'KeyD': 'right',
|
||||
};
|
||||
|
||||
const value = controls[event.code];
|
||||
if (!value)
|
||||
return;
|
||||
|
||||
send({type, value});
|
||||
};
|
||||
|
||||
connection.onopen = () => {
|
||||
let name = getPlayerName();
|
||||
send({type: 'name', value: name});
|
||||
|
||||
window.onkeyup = keyEventListener('input-off');
|
||||
window.onkeydown = keyEventListener('input-on');
|
||||
|
||||
console.log('connection opened, game ready');
|
||||
};
|
||||
|
||||
function getPlayerName() {
|
||||
let name;
|
||||
while (!name)
|
||||
name = prompt('Player Name');
|
||||
return name;
|
||||
}
|
||||
|
||||
function send(obj) {
|
||||
connection.send(JSON.stringify(obj));
|
||||
}
|
|
@ -1,7 +1,12 @@
|
|||
import { defineConfig } from 'vite'
|
||||
import react from '@vitejs/plugin-react'
|
||||
import {defineConfig} from 'vite';
|
||||
import react from '@vitejs/plugin-react';
|
||||
|
||||
// https://vitejs.dev/config/
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
})
|
||||
plugins: [react()],
|
||||
|
||||
build: {
|
||||
outDir: '../TanksServer/client',
|
||||
emptyOutDir: true
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue