Move your ship with mouse, click to shoot.
A fork of Multiplayer Games with Socket.io by Sinan Ascioglu
//var socket = io.connect(":30000?sketch=1372150"); //connect to the socket server (old method)
var legacyServer = false;
var socket = io.connect(legacyServer ? "wss://echo.openprocessing.org?sketch=1372150" : $OP.getEchoServerURL(1372150)); //connect to the socket server (new method)
window.fwidth = 2000;
window.fheight = 2000;
window.cht = {};
window.ldrboard = true;
window.nameshow = true;
window.cursorsh = true;
var easing = 0.05;
var up10 = false;
var sensitivity = {
spinning: 10,
}
OPC.slider({
name: 'sensitivityspinning',
value: 10,
min: 0,
max: 20.1,
step: 0.1,
label: 'Spinning Speed',
description: 'Sensitivity for rotations (rotation speed).'
});
OPC.slider({
name: 'quality',
value: 2,
min: 0.01,
max: 10.1,
step: 0.01,
label: 'Quality',
description: 'Lower Quality Options'
});
/*OPC.slider({
name: 'frameps',
value: 20,
min: 1,
max: 1024,
step: 1,
label: 'FPS',
description: 'Maximum frames rendered per second'
});*/
var frameps = 20;
OPC.toggle({
name: 'expergfx',
value: false,
label: 'Experimental Graphical Options',
description: 'Some experimental graphic options that may slow down your computer.\nLower graphical quality is recomended for not as powerful machines.'
});
OPC.toggle({
name: 'devSecretMode',
value: false,
label: 'Developer Mode',
description: 'Developer mode enables some annoying overlays.\nUnless you want to be annoyed with debugging info that you can\'t understand, don\'t enable.'
});
var reconnecting = false;
var disconnected = false;
let credits = [
"SUPPORTERS",
"Duckbox_boy (Lincoln Duck)", "Devangee", "Neelan Ganeshan", "Lachie", "Benjini THE GREAT (Ben Murdocca)", "Eason (Yuchen) Wu", "EBHS TCC3 Class of 2024 (playtesting)", "All my hearters and views for this project and the followers for this account.", "diep.io", "YOU!!!!",
"CREATORS",
"CODER: Max Parry",
"DESIGNERS: Max Parry, Yukiharu Asahi, Lincoln Duck, Luke Hawkins, Emmanuel Lee, Benjini THE GREAT (Ben Murdocca)",
"ORIGINAL CREATOR AND ORIGINAL IDEA: Max Parry",
"Owned by Import/Nyanport Studios Productions 2021-" + new Date().getFullYear() +".",
"BORING LEGAL STUFF",
"You can distribute this game wherever you want, copy the game wherever you want but NOT modify the game whenever you want. For more boring legal info visit https://creativecommons.org/licenses/by-nc-nd/4.0/legalcode"
];
let battleships = []; //our array of all battleships
let myShip; //this is ours
let lasers = []; //no lasers shot yet
let shapes = []; // Shapes for killing
let bosses = []; // Bosses for killing
// Tanks
let basic = {"blasters": [{
rotation: 0,
td: 0.1,
sd: 5
}], regen:0.001};
let flank = {"blasters": [
{
rotation: 1*(360/3),
td: 1,
sd: 10
},
{
rotation: 2*(360/3),
td: 1,
sd: 10
},
{
rotation: 3*(360/3),
td: 0.1,
sd: 10
}
], regen:0.1};
let twin = {"blasters": [
{
rotation: 360-1,
td: 2,
sd: 5
},
{
rotation: 1,
td: 2,
sd: 5
}
], regen:0.2};
let sniper = {"blasters": [{
rotation: 0,
td: 5,
sd: 5
}], regen:0.05};
// Bosses
// Fallen
// blasters
let fallen_bl = [{
rotation: 0,
td: 100,
sd: 0
}];
//build
let fallen_bu = {
'x': fwidth/2,
'y': fheight/2,
'rotation': 0,
'worth': 100000,
'health': 100/2,
'maxhealth': 100/2,
'dead': false,// Don't get these two
'died': false,// confused.
'murderer': null,
'blasters': fallen_bl,
'name': "The Fallen Basic"
};
// Copycat
//build
let cpycat_bu = {
'x': fwidth/2,
'y': fheight/2,
'rotation': 0,
'worth': 1000000000,
'health': null, // REPLACE WITH PLAYER/2
'maxhealth': null, // REPLACE WITH PLAYER/2
'dead': false, // Don't get these two
'died': false, // confused.
'murderer': "", // REPLACE WITH PLAYER NAME
'blasters': null,
'name': "Copycat"
};
//////////////////////////////
//var kP = false;
var changingName = false;
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
frameRate(20); // slightly slow for no maaaaaad laaaaagggg
window.canvas = $OP.getCanvas();
canvas.addEventListener("click", async () => {
canvas.requestFullscreen();
});
document.addEventListener('keydown', function (e) {
if (e.key == "Control") {
inchat = !inchat;
}
}
);
canvas.style.cursor = "none";
canvas.style.imageRendering = "pixelated";
initTests();
if(localStorage.hasOwnProperty("laserio_save")) {
myShip = JSON.parse(localStorage.getItem("laserio_save"));
} else {
let tmpName = prompt('Name for your battleship?');
myShip = {
'x':0,
'y':0,
'rotation': 0,
'score': 0,
'health': 100,
'maxhealth': 100,
'dead': false,// Don't get these two
'died': false,// confused.
'murderer': null,
'visible': true,
'shapes': [],
'bosses': [],
'blasters': basic,
'name': ( (tmpName == null || ( (tmpName != null) && ( tmpName.trim() == "" ) ) ) ? ("Unnamed") : (tmpName) )
};
}
battleships.push(myShip);
//console.log(myShip.blasters);
//if(changingName == false) {
socket.on('update', updateBattleship);
socket.on('chat', cht.handleMessage);
socket.on('quake', quake);
socket.on('stopquake', stopQuake);
socket.on('score', updateScore);
socket.on('ban', ban);
//socket.on('update', connectedToServer);
//}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
}
function draw() {
if(!nameshow) {
window.text = function(args) {};
}
pixelDensity(quality);
frameRate(frameps);
sensitivity.spinning = sensitivityspinning;
chat = chat.slice(-10);
if(checkban()) {
background(255,0,0); // extreme ban red
fill(0);
strokeWeight(5);
stroke(255);
textSize(25);
var bannedText = myShip.name + ", you've been banned.";
text(bannedText, (width/2)-(textWidth(bannedText)/2), (height/2)+(25/2));
return;
}
if(isHeld("0") && !reconnecting) {
reconnect();
}
/*
//Testquake
quakejson = { density: 5 };
quake = true;
*/
if(reconnecting) return;
background(100);
if(quake) {
translate(int(random(-quakejson.density, quakejson.density+1)), int(random(-quakejson.density, quakejson.density+1)));
}
let tmpshipsbam = battleships.filter(
function(d) {
if(d.name == myShip.name) return true;
return ((millis()-d.lastping) <= 5000)
}
);
battleships = tmpshipsbam;
if(!myShip.dead && cursorsh) {
fill(255, 0, 0);
noStroke();
ellipse(mouseX, mouseY, 10, 10);
}
if(ldrboard) {
push();
fill(150, 100);
strokeWeight(5);
stroke(0);
rectMode(CENTER);
let v1 = "Players online:";
let v2 = battleships.filter(function (ship) { return !ship.dead; }).length;
let v3 = "Leader:";
let v4 = (getLeader() != null) ? (getLeader().name) : ("No-one");
textSize(30);
let s1 = textWidth(v1);
let s3 = textWidth(v3);
textSize(50);
let s2 = textWidth(v2);
let s4 = textWidth(v4);
rect(width/2, 160, s1 + s2 + s3 + s4, 300);
textAlign(CENTER, CENTER);
fill(255);
textSize(30);
text(v1, width/2, 50);
text(v3, width/2, 200);
textSize(50);
text(v2, width/2, 125);
text(v4, width/2, 250);
pop();
}
if(myShip.died && !myShip.dead) {
fill(0);
strokeWeight(5);
stroke(255);
textSize(14);
text("Main objective: Get " + ((myShip.murderer == myShip.name) ? ("everyone") : (myShip.murderer)) + " dead.", width/2-(textWidth("Main objective: Get " + myShip.murderer + " dead.")/2), 10);
}
// Upgrades
if(myShip.score > 10000 && !up10) {
myShip.shooting = false;
if((!disconnected || !reconnecting) && myShip.name != "spectator"/*litle easter egg lol*/) { socket.emit('update', myShip); } //let others know of our new position -- if we are connected of course
alert("New tank classes are now avalible\nThese are:\n• Flank\n• Twin\n• Sniper\nAccess them using 1 for flank, 2 for twin, and 3 for sniper.");
up10 = true;
}
// Flank
if(!inchat) {
if(isHeld('1') && up10) {
myShip.blasters = flank;
myShip.health = (myShip.health/myShip.maxhealth)*200;
myShip.maxhealth = 200;
}
// Twin
if(isHeld('2') && up10) {
myShip.blasters = twin;
myShip.health = (myShip.health/myShip.maxhealth)*175;
myShip.maxhealth = 175;
}
// Sniper
if(isHeld('3') && up10) {
myShip.blasters = sniper;
myShip.health = (myShip.health/myShip.maxhealth)*150;
myShip.maxhealth = 150;
}
}
////////////////////////////////////////////////
if(myShip.health < myShip.maxhealth && !myShip.dead && (int(random(100+1))) === 1) {
myShip.health+=myShip.blasters.regen; // health regeneration :D
}
if(myShip.health > myShip.maxhealth) {
myShip.health = myShip.maxhealth;
}
if((myShip.health <= 0)) {
//keysHeld = "";
myShip.health = 0;
myShip.dead = true;
myShip.died = true;
}
if(!inchat) {
if(isHeld("O")) {
myShip.health = 0;
myShip.murderer = myShip.name;
myShip.dead = true;
myShip.died = true;
keysHeld = "";
}
if(isHeld("C")) {
console.log("Thanks to all our contributors for helping us make a reeeeaaalllly fun game");
for(let word of credits) {
console.log(word);
}
keysHeld = "";
}
}
// BOSS AI
for(let i = 0; i < myShip.bosses.length; i++) {
if(!myShip.bosses[i].dead) {
let target = aiTarget(myShip.bosses[i]);
if(target != null) {
var btargetX = target.x;
var bdx = btargetX - myShip.bosses[i].x;
myShip.bosses[i].x += bdx * easing;
var btargetY = target.y;
var bdy = btargetY - myShip.bosses[i].y;
myShip.bosses[i].y += bdy * easing;
let angle = atan2(myShip.bosses[i].x-target.x, myShip.bosses[i].y-target.y); // Complex algorithm for the rotation (that's math for ya)
let angle2 = -angle-(Math.PI/2);
let angle2deg = angle2.derad();
myShip.bosses[i].rotation = (angle2deg) + 90;
myShip.bosses[i].shooting = true;
} else {
// Spin gracefully ;D
myShip.bosses[i].rotation += 1;
myShip.bosses[i].shooting = false;
}
}
}
//update our own ship position
myShip.name = ( ( ( (myShip.name != null) && ( myShip.name.trim() == "" ) ) ) ? ("Unnamed") : (myShip.name) ); // An unnamed tank (diep.io reference)
if(!myShip.dead && !inchat) {
mX = mouseX+(myShip.x-width/2);
mY = mouseY+(myShip.y-height/2);
// Worst physics mechinism (too fast)
/*var targetX = mX;
var dx = targetX - myShip.x;
myShip.x += dx * easing;
var targetY = mY;
var dy = targetY - myShip.y;
myShip.y += dy * easing;*/
// BRAND NEW PHYSICS MECHINISM!
// BRANDER NEW (1.06)
let nothingelse = false;
if(dist(mX, mY, myShip.x, myShip.y) < 10) { myShip.x = mX; myShip.y = mY; nothingelse = true; }
if(!nothingelse) {
//myShip.rotation = degrees(atan2(mX-myShip.x, mY-myShip.y))-180; // Welcome to my world Processing, where everything's calculated in degrees.
myShip.x += sin(radians(degrees(atan2(mX-myShip.x, mY-myShip.y)))) * 10;
myShip.y += cos(radians(degrees(atan2(mX-myShip.x, mY-myShip.y)))) * 10;
}
myShip.x = constrain(myShip.x, 0, fwidth);
myShip.y = constrain(myShip.y, 0, fheight);
myShip.shooting = mouseIsPressed;
// OLD RUBBISHY PHYSICS (thats actually good)
if(isHeld("d")) {
myShip.rotation+=sensitivity.spinning;
//kP = false;
}
if(isHeld("a")) {
myShip.rotation-=sensitivity.spinning;
//kP = false;
}
}
if(int(random(100+1)) === 1 && myShip.shapes.filter(function (shape) { return shape.alive; }).length < 25) {
let circle = {
x: random(1, fwidth),
y: random(1, fheight),
health: 100,
alive: true,
type: int(random(5))
};
myShip.shapes.push(circle);
//console.log(myShip.shapes.length);
}
if(int(random(2000+1)) === 1 && myShip.bosses.filter(function (boss) { return !boss.dead; }).length < 5) {
myShip.bosses.push(fallen_bu);
//console.log(myShip.shapes.length);
}
if(int(random(2000+1)) === 1) {
quakejson = { density: int(random(0, 10+1)) };
quake = true;
socket.emit('quake', quakejson);
//console.log(myShip.shapes.length);
}
if(int(random(500+1)) === 1) {
quake = false;
socket.emit('stopquake');
//console.log(myShip.shapes.length);
}
if((!disconnected || !reconnecting) && myShip.name != "spectator"/*litle easter egg lol*/) { socket.emit('update', myShip); } //let others know of our new position -- if we are connected of course
//console.log(lasers.length);
//draw all battleships
translate(-myShip.x, -myShip.y);
translate(width/2, height/2);
noFill();
stroke(200, 10)
strokeWeight(10)
grid({x:0,y:0}, {x:fwidth,y:fheight}, 100);
for (let ship of battleships) { //draw each ship
if((ship.visible) ? (!ship.dead) : (false)) {
push();
translate(ship.x, ship.y);
rectMode(CENTER);
rotate(radians(ship.rotation));
noStroke();
fill(30, 30, 30, 90);
rect(0, 0, 20, 20);
pop();
fill(0);
strokeWeight(5);
stroke(255);
if((getLeader() != null) ? (getLeader().name == ship.name) : (false) /*This is THE LEADER!*/) { stroke(0, 255, 0); }
textSize(25);
text(ship.name, ship.x-(textWidth(ship.name)/2), ship.y+50);
textSize(15);
text(ship.score, ship.x-(textWidth(ship.score)/2), ship.y+75);
stroke(0);
strokeWeight(10);
line((ship.x-(100/2)), (ship.y+100), (ship.x-(100/2))+100, (ship.y+100));
stroke(0, 255, 0);
strokeWeight(5);
line((ship.x-(100/2)), (ship.y+100), (ship.x-(100/2))+((ship.health/ship.maxhealth)*100), (ship.y+100));
strokeWeight(1);
noStroke();
// Simple logic for NPEs (non-player entities)
shapes.push(ship.shapes);
if(ship.shooting){ //add lasers to the screen
for(let i = 0; i < ship.blasters.blasters.length; i++) {
let newLaser = {
x: ship.x,
y: ship.y,
distance: 0,
owner: ship.name,
rotation: ship.rotation + ship.blasters.blasters[i].rotation,
td: ship.blasters.blasters[i].td,
sd: ship.blasters.blasters[i].sd
};
lasers.push(newLaser);
}
}
pop();
}
bosses.push(ship.bosses);
}
// Draw Artificial Intelligence NPEs
for (let boss_c of bosses) {
for (let boss of boss_c) {
if(!boss.dead) {
push();
translate(boss.x, boss.y);
rectMode(CENTER);
rotate(radians(boss.rotation));
noStroke();
fill(30, 30, 30);
rect(0, 0, 20, 20);
pop();
fill(0);
strokeWeight(5);
stroke(255, 0, 0);
textSize(25);
text(boss.name, boss.x-(textWidth(boss.name)/2), boss.y+50);
textSize(15);
text(boss.worth, boss.x-(textWidth(boss.worth)/2), boss.y+75);
stroke(0);
strokeWeight(10);
line((boss.x-(100/2)), (boss.y+100), (boss.x-(100/2))+100, (boss.y+100));
stroke(0, 255, 0);
strokeWeight(5);
line((boss.x-(100/2)), (boss.y+100), (boss.x-(100/2))+((boss.health/boss.maxhealth)*100), (boss.y+100));
strokeWeight(1);
noStroke();
if(boss.shooting){ //add lasers to the screen
for(let i = 0; i < boss.blasters.length; i++) {
let newLaser = {
x: boss.x,
y: boss.y,
distance: 0,
owner: boss.name,
rotation: boss.rotation + boss.blasters[i].rotation,
td: boss.blasters[i].td,
sd: boss.blasters[i].sd
};
lasers.push(newLaser);
}
}
pop();
}
}
}
//draw any lasers that are shot
for (let laser of lasers) {
push();
laser.distance -= 10;
let v0 = createVector(laser.x, laser.y);
//let v1 = createVector(laser.x, 0);
//translate(laser.x, laser.y);
//v1.rotate(radians(laser.rotation));
drawLaser(v0, laser, 'black'); // Drawlaser function
for(let i = 0; i < myShip.shapes.length; i++) {
if(shapeCollision(i, v0, laser) && myShip.shapes[i].alive && myShip.name != "spectator") {
myShip.shapes[i].health -= laser.sd;
let aScore = {
"owner": laser.owner,
"xp": (myShip.shapes[i].type+1)*10
};
socket.emit('score', aScore);
if(laser.owner == myShip.name) {
updateScore(aScore);
}
if(myShip.shapes[i].health <= 0) {
myShip.shapes[i].health = 0;
myShip.shapes[i].alive = false;
}
}
}
for(let i = 0; i < myShip.bosses.length; i++) {
if(bossCollision(i, v0, laser) && !myShip.bosses[i].dead && myShip.name != "spectator" & laser.owner != myShip.bosses[i].name) {
myShip.bosses[i].health -= laser.td;
let aScore = {
"owner": laser.owner,
"xp": 100
};
socket.emit('score', aScore);
if(laser.owner == myShip.name) {
updateScore(aScore);
}
if(myShip.bosses[i].health <= 0) {
myShip.bosses[i].health = 0;
myShip.bosses[i].dead = true;
let bossScore = {
"owner": laser.owner,
"xp": myShip.bosses[i].worth
};
socket.emit('score', bossScore);
if(laser.owner == myShip.name) {
updateScore(bossScore);
}
}
}
}
pop();
}
// Generate shapes after lasers
for (let shape_c of shapes) {
push();
for (let shape of shape_c) {
if(shape.alive) {
push();
strokeWeight(5);
stroke(0);
if(shape.type == 0) {
fill(255);
}
if(shape.type == 1) {
fill(0, 255, 0);
}
if(shape.type == 2) {
fill(255, 255, 0);
}
if(shape.type == 3) {
fill(255, 0, 0);
}
if(shape.type == 4) {
fill(0, 0, 255);
}
ellipseMode(CENTER);
ellipse(shape.x, shape.y, 50, 50);
stroke(0);
strokeWeight(10);
line((shape.x-(100/2)), (shape.y+100), (shape.x-(100/2))+100, (shape.y+100));
stroke(0, 255, 0);
strokeWeight(5);
line((shape.x-(100/2)), (shape.y+100), (shape.x-(100/2))+shape.health, (shape.y+100));
pop();
}
}
pop();
}
/*part of the REALLY IMPORTANT update*/
myShip.shapes = myShip.shapes.filter(function (obj) {
return (obj.alive);
});
myShip.bosses = myShip.bosses.filter(function (obj) {
return !(obj.dead);
});
battleships = battleships.filter(function (obj) {
return !(obj.dead);
});
shapes = []; // No more shapes
bosses = []; // No more bosses
//remove lasers out of screen, for performance
lasers = lasers.filter(function (laser) {
return (laser.x+createVector(0, laser.distance).rotate(radians(laser.rotation)).x > 0) && (laser.x+createVector(0, laser.distance).rotate(radians(laser.rotation)).x < fwidth) && (laser.y+createVector(0, laser.distance).rotate(radians(laser.rotation)).y > 0) && (laser.y+createVector(0, laser.distance).rotate(radians(laser.rotation)).y < fheight);
});
chat = chat.filter(function(a) {
console.log(`(${millis()} - ${a.time}) < ${cht.time}*1000`);
return (millis() - a.time) < cht.time*1000
})
push();
fill(255);
noStroke();
translate((myShip.x-width/2), (myShip.y-height/2));
textAlign(LEFT, BOTTOM)
textSize(30)
text(chat.map(a => a.msg).join('\n'), 0, height-(inchat ? 30 : 0));
fill(0);
if(inchat)
text(cht.keysLog + ((millis()%2000 < 1000) ? "|" : ""), 0, height);
pop();
if(devSecretMode) {
push();
fill(0)
noStroke();
translate((myShip.x-width/2), (myShip.y-height/2));
textAlign(LEFT, TOP)
textSize(15)
text(`
current fps: ${frameRate()}
client socket id: ${socket.id}
connected: ${socket.connected}
connected to server
`, 0, 0);
pop();
}
if(!myShip.dead) {
return;
}
if(expergfx)
filter(BLUR, 3);
push();
translate((myShip.x-width/2), (myShip.y-height/2));
noStroke();
fill(100, 100);
rectMode(CORNER);
rect(0, 0, width, height);
fill(0);
strokeWeight(5);
stroke(255);
textSize(50);
text("YOU DIED!", width/2-(textWidth("YOU DIED!")/2), height/3);
textSize(25);
text("You were killed by " + myShip.murderer, width/2-(textWidth("You were killed by " + myShip.murderer)/2), height/2);
text("R to respawn", width/2-(textWidth("R to respawn")/2), height/1.75);
pop();
if(isHeld("r") && !inchat) {
respawn();
}
}
function updateBattleship(ship) {
//see if ship already exists
if(disconnected) {
return;
}
let bs = battleships.filter(d=>d.name == ship.name);
if(myShip.name == ship.name && changingName == false) {
myShip.visible = false;
socket.emit('update', myShip);
let oldName = myShip.name;
//if(changingName == false) {
console.log("Error: there are 2 players with the name \'" + myShip.name + "\' in the server");
sameNames(oldName);
//}
}
if(bs.length == 0){ //if a new ship, create a new one
ship.lastping = millis();
battleships.push(ship);
}else{ //if exists, then update its data
bs[0].x = ship.x;
bs[0].y = ship.y;
bs[0].rotation = ship.rotation;
bs[0].shooting = ship.shooting;
bs[0].score = ship.score;
bs[0].maxhealth = ship.maxhealth;
bs[0].health = ship.health;
bs[0].dead = ship.dead;
bs[0].visible = ship.visible;
bs[0].died = ship.died;
bs[0].shapes = ship.shapes;
bs[0].bosses = ship.bosses;
bs[0].blasters = ship.blasters;
bs[0].murderer = ship.murderer;
bs[0].lastping = millis();
}
}
function updateScore(score) {
if(score.owner == myShip.name /*This score belongs to me*/) {
myShip.score+=score.xp;
}
}
function laserRemovalBoolean(laser) {
return (laser.x+createVector(0, laser.distance).rotate(radians(laser.rotation)).x > 0) && (laser.x+createVector(0, laser.distance).rotate(radians(laser.rotation)).x < width) && (laser.y+createVector(0, laser.distance).rotate(radians(laser.rotation)).y > 0) && (laser.y+createVector(0, laser.distance).rotate(radians(laser.rotation)).y < height);
}
function sameNames(oldName) {
changingName = true;
let newName = prompt("Sorry, but something went wrong when you entered your name.\nEnter a new name!");
newName = ( ( ( (newName != null) && ( myShip.name.trim() == "" ) ) ) ? ("Unnamed") : (newName) ); // An unnamed tank (diep.io reference)
if(newName == oldName || newName == null) {
sameNames(oldName);
return;
}
myShip.name = newName;
battleships = [ myShip ];
myShip.visible = true;
changingName = false;
}
function getLeader() {
if(battleships.isEmpty()) {
return null;
}
let scorearray = [];
let bs2 = [];
//let namearray = [];
for(let ship of battleships) {
if( (ship.visible) ? (!ship.dead) : (false) ) { scorearray.push(ship.score); bs2.push(ship); }
//namearray.push(ship.name);
}
if(scorearray.isEmpty()) {
return null;
}
let highestscore = scorearray.max();
let highestship = bs2[scorearray.indexOf(highestscore)];
return highestship;
}
function aiTarget(boss) {
if(battleships.isEmpty()) {
return null;
}
let distarray = [];
let bs2 = [];
//let namearray = [];
for(let ship of battleships) {
if( (ship.visible) ? (!ship.dead) : (false) ) { distarray.push(dist(boss.x, boss.y, ship.x, ship.y)); bs2.push(ship); }
//namearray.push(ship.name);
}
if(distarray.isEmpty()) {
return null;
}
let lowestdist = distarray.min();
let targetship = bs2[distarray.indexOf(lowestdist)];
return targetship;
}
function reconnect() { // Reconnect to server
reconnecting = true;
if(socket.connected)
socket.disconnect();
alert("Are you having trouble? Well, we\'re reconnecting you to the server!\nDon't fret, all your old progress will still be there.\nPress ok to continue the reconnection.");
battleships = [ myShip ]; // Delete all other players
keysHeld = "";
socket = io.connect($OP.getEchoServerURL(1372150));
//socket.emit('update', myShip);
//socket.on('update', updateBattleship);
lasers = [];
connectedToServer();
}
function connectedToServer() {
reconnecting = false;
}
function disconnect() {
// give them a creepy error then shut the connection off.
alert("Disconnected from the server.");
socket.disconnect();
disconnected = true;
}
function respawn() {
lasers = [];
myShip.x = fwidth/2;
myShip.y = fheight/2;
myShip.dead = false;
myShip.health = myShip.maxhealth;
myShip.rotation = 0;
battleships.push(myShip);
}
// draw an laser for a vector at a given base position
function drawLaser(base, laser, myColor) {
push();
if(laser.owner != myShip.name) { stroke(myColor); } else { stroke(255); }
strokeWeight(1);
if(laser.owner != myShip.name) { fill(myColor); } else { fill(255); }
translate(base.x, base.y);
line(
createVector(0, laser.distance).rotate(radians(laser.rotation)).x,
createVector(0, laser.distance).rotate(radians(laser.rotation)).y,
createVector(0, laser.distance+10).rotate(radians(laser.rotation)).x,
createVector(0, laser.distance+10).rotate(radians(laser.rotation)).y);
pop();
rectMode(CENTER);
if(
laser.owner != myShip.name /* It's not my laser*/&& !myShip.dead && collideLineRect( // Hopefully this is a function
base.x+createVector(0, laser.distance).rotate(radians(laser.rotation)).x,
base.y+createVector(0, laser.distance).rotate(radians(laser.rotation)).y,
base.x+createVector(0, laser.distance+10).rotate(radians(laser.rotation)).x,
base.y+createVector(0, laser.distance+10).rotate(radians(laser.rotation)).y,
myShip.x-10,
myShip.y-10,
10,
10
)
)
{
myShip.health-=laser.td;
let aScore = {
"owner": laser.owner,
"xp": 10
};
socket.emit('score', aScore);
if(myShip.health <= 0) {
let myScore = {
"owner": laser.owner,
"xp": myShip.score
};
socket.emit('score', myScore);
myShip.murderer = laser.owner;
}
}
}
function shapeCollision(i, base, laser) {
if(
collideLineCircle( // Hopefully this is a function
base.x+createVector(0, laser.distance).rotate(radians(laser.rotation)).x,
base.y+createVector(0, laser.distance).rotate(radians(laser.rotation)).y,
base.x+createVector(0, laser.distance+10).rotate(radians(laser.rotation)).x,
base.y+createVector(0, laser.distance+10).rotate(radians(laser.rotation)).y,
myShip.shapes[i].x,
myShip.shapes[i].y,
50,
50
)
) { return true; } else { return false; }
}
function bossCollision(i, base, laser) {
if(
collideLineRect( // Hopefully this is a function
base.x+createVector(0, laser.distance).rotate(radians(laser.rotation)).x,
base.y+createVector(0, laser.distance).rotate(radians(laser.rotation)).y,
base.x+createVector(0, laser.distance+10).rotate(radians(laser.rotation)).x,
base.y+createVector(0, laser.distance+10).rotate(radians(laser.rotation)).y,
myShip.bosses[i].x-10,
myShip.bosses[i].y-10,
20,
20
)
) { return true; } else { return false; }
}
///////////////////////adminConsole commands////////////////
function checkban() {
if(window.localStorage.hasOwnProperty("laserio-banned")) {
return true;
} else {
return false;
}
}
function ban(name) {
if(name == myShip.name) {
localStorage.setItem("laserio-banned", "true");
// You've been banned
console.error("LaserIoException:\nBANNED_BY_ADMIN");
alert("You\'ve got banned.\nError code:\nBANNED_BY_ADMIN");
}
}
window.cht.handleMessage = function(msg) {
chat.push({
msg: `${msg.name}: ${msg.content}`,
time: millis()
});
}
window.cht.sendMessage = function(msgstr) {
let msg = {
"content": msgstr,
"name": myShip.name
};
chat.push({
msg: `${msg.name}: ${msg.content}`,
time: millis()
});
socket.emit("chat", msg)
}
var socket = io.connect($OP.getEchoServerURL(1370168));
Learn more See an example