MultiplayerMinesweeper/www/script.js

285 lines
6.1 KiB
JavaScript
Raw Normal View History

2022-06-19 20:21:33 +00:00
// const perlinScale = 3;
const IP_ADDRESS = 'http://127.0.0.1:8082/'
var SERVER_CONNECTION = "disconnected"
var IN_GAME = false
const tileSize = 24;
const gridSize = [18, 18];
var world
var tiles = []
var button
var canvas = document.getElementById('canvas');
var hud = document.getElementById('hud');
var ctx = canvas.getContext('2d');
var hctx = hud.getContext('2d');
var socket = io.connect(IP_ADDRESS);
function joinGame(socket, data){
socket.emit('joinGame', data);
}
function leaveGame(socket){
socket.emit('leaveGame', {});
}
socket.on('connect', function(data){
SERVER_CONNECTION = "connected"
updateConnectionStatus();
})
socket.on('disconnect', function(data){
SERVER_CONNECTION = "disconnected"
updateConnectionStatus();
})
socket.on('illegalAction', function(data){
let action
switch (data) {
case 1:
action = "You must be in game to do this."
break;
case 20:
action = "That name is already taken."
break;
case 23:
action = "Invalid Placement location."
break;
default:
action = "Unknown action."
}
console.log(`Illegal Action. ${action}`)
alert(`Illegal Action, ${action}`)
})
function updateConnectionStatus() {
let obj = document.getElementById("status")
obj.textContent = `Server Connection: ${SERVER_CONNECTION}`
switch (SERVER_CONNECTION) {
case "connected":
obj.style.color = "green";
break;
case "disconnected":
obj.style.color = "red";
break;
}
}
class Cursor {
constructor(){
this.x = 0
this.y = 0
this.xold = 0
this.yold = 0
}
}
var cursor = new Cursor();
window.onbeforeunload = function(){
socket.disconnect();
world = []
}
socket.on('playerList', function(data){
console.log(data)
});
socket.on('inGame', function(data){
if (!IN_GAME) {changeScene()}
IN_GAME = true;
})
socket.on('sync', function (sync){
world = sync.world;
render()
})
function changeScene() {
document.getElementById('menu').style = "display: none;"
document.getElementById('container').style = ""
for(i=0;i < 5;i++) {
let span = document.createElement('span')
span.className = "button"
let n;
switch (i) {
case (1):
n = 4
break;
case (2):
n = 32
break;
case (3):
n = 33
break;
case (4):
n = 34
break;
default:
n = 5
}
span.appendChild(tiles[n]);
document.getElementById("buttonBar").appendChild(span);
span.no = i;
span.addEventListener('click', e => {
clickSelector(e)
})
}
}
function loadSprites() {
var spriteJank = document.getElementById('spriteJank');
var ctxj = spriteJank.getContext('2d');
var spriteSheet = new Image();
spriteSheet.src = '/sheet.png'
spriteSheet.onload = function() {
for (y = 0; y < 16; y++) {
for (x = 0; x < 16; x++) {
ctxj.drawImage(spriteSheet, -x*24,-y*24)
var tmp = new Image();
tmp.src = spriteJank.toDataURL();
ctxj.clearRect(0, 0, 24, 24);
tiles.push(tmp)
}
}
}
spriteJank.remove();
}
function submit(event) {
const room = document.getElementById("room").value
const name = document.getElementById("name").value
if (room == '' || name == '' || SERVER_CONNECTION == 'disconnected') return
joinGame(socket, {room: room, name: name})
event.preventDefault();
}
function clickSelector(e) {
document.querySelectorAll('.button').forEach(item => {
item.style ="";
})
event.target.style = "background: lightslategray;"
button = event.target.no
}
window.onload = function () {
document.getElementById("submit").addEventListener("click", e => {
submit(e)
});
hud.addEventListener('mousemove', e => {
mouseMoved(e)
});
hud.addEventListener("mousedown", function(e)
{
getMousePosition(canvas, e);
});
//Set canvases to be ready
canvas.width = tileSize*gridSize[0];
canvas.height = tileSize*gridSize[1];
hud.width = canvas.width
hud.height = canvas.height
//Load all sprites into memory
loadSprites()
}
function randomNumber(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function render() { // DRAW THE IMAGE TO THE CANVAS.
let x, y = 0
for(x = 0; x < gridSize[0]; x++){
for(y = 0; y < gridSize[1]; y++){
const xu = x*tileSize;
const yu = y*tileSize;
// Draw buildings
switch (world[x][y].type) {
case (0):
ctx.drawImage(tiles[0], xu,yu)
break;
case (1):
ctx.drawImage(tiles[2], xu,yu)
break;
case (2):
ctx.drawImage(tiles[1], xu,yu)
break;
}
// Draw Structures
switch (world[x][y].structure) {
case (1):
ctx.drawImage(tiles[4], xu,yu)
break;
case (2):
ctx.drawImage(tiles[32], xu,yu)
break;
case (3):
ctx.drawImage(tiles[33], xu,yu)
break;
case (4):
ctx.drawImage(tiles[34], xu,yu)
break;
}
// Draw Property overlays
if (world[x][y].color != null){
ctx.beginPath();
ctx.fillStyle = world[x][y].color;
ctx.rect(xu, yu, tileSize, tileSize)
ctx.globalAlpha = 0.6;
ctx.fill()
ctx.globalAlpha = 1;
}
}
}
console.log("image Rendered")
}
function getMousePosition(canvas, event) {
// Get mouse position on canvas
const rect = canvas.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
// const xu = Math.floor(x/tileSize)
// const yu = Math.floor(y/tileSize)
const xu = cursor.x
const yu = cursor.y
// Convert canvas coordinates to usable coordinates within the coordinate system
console.log("Click!")
console.log(xu,yu)
socket.emit('clickCanvas', {tilePosition: [xu,yu], structure: button*1});
}
function mouseMoved(event) {
const x = Math.floor(event.offsetX/tileSize)
const y = Math.floor(event.offsetY/tileSize)
if (cursor.xold != x || cursor.yold != y) {
cursor.x = x
cursor.y = y
hctx.clearRect(cursor.xold*tileSize, cursor.yold*tileSize, tileSize, tileSize)
hctx.drawImage(tiles[80], x*tileSize,y*tileSize)
cursor.xold = x
cursor.yold = y
}
}