Skip to content

Instantly share code, notes, and snippets.

@xTCry
Last active February 27, 2019 21:22
Show Gist options
  • Save xTCry/65fa36b9dff8887213cd27e47e0639f2 to your computer and use it in GitHub Desktop.
Save xTCry/65fa36b9dff8887213cd27e47e0639f2 to your computer and use it in GitHub Desktop.
Cursor.io: Save levels

Получение уровней в JSON формат

Скопировать код из "index.js" в консоль страницы cursors.io
var jq = document.createElement('script');
jq.src = "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(jq);
Array.prototype.remove = function(a) {
a = this.indexOf(a);
return -1 != a ? (this.splice(a, 1), true) : false
};
var pCursor = new Image;
pCursor.src = "http://cursors.io/img/cursor.png";
var isEditor = (-1 != window.location.search.indexOf("editor"));
var isLocalTest = (-1 != window.location.search.indexOf("test"))|| "file:" == window.location.protocol;
var dpr = window.devicePixelRatio;
var cv_Context, // Canvas Context
cv_Container, // Canvas Container
noCursorLock = null,
noDrawings = null
ws = null,
countPlayers = 0,
isMouseDown = false,
gTime = 0,
xxx = 0;
var Ha = pCursor,
clickPoints = [],
drawLinesPoints = [],
ba = 0,
gameID = -1,
playersList = {},
playersIsLimited = false,
N = 0,
O = 0,
da = 0,
isMLocked = false,
isNotEditor = !isEditor && true,
clearedData = new Uint8Array(12e4),
Q = new Uint8Array(12e4),
gObjs = [],
remoteClicks = [];
var u = 0;
var
ha = 0,
posXbin = 0,
posYbin = 0,
posX = 0,
posY = 0,
posXbinTwo = 0,
posYbinTwo = 0,
posXbinZZZ = -1,
posYbinZZZ = -1;
// $(document).ready();
var logWindow = null,
logScrool = true;
function reLog() {
logWindow.document.close()
logCreate()
}
function logCreate() {
if(logWindow == null || logWindow.document === undefined) {
logWindow = window.open('', 'logWindow','left=50,top=20,width=550,height=650,status=0,location=0,menubar=0');
logWindow.onbeforeunload = function () {
logWindow = false;
return "Do you really want to close?";
};
}
}
function inLog(data) {
logCreate();
data = JSON.stringify(data, null, " ").replace(/\"([^(\")"]+)\":/g, "$1:");
data = data.replace(/(?:\r\n|\r|\n)/g, '<br>');
// data = data.replace(/\t/g, "&#8195;");
logWindow.document.write(data+"<hr>");
if(logScrool)
logWindow.scrollBy(0, 10000);
}
inLog("Start log");
function startGame() {
var viewport = $("#canvas").get(0);
cv_Context = viewport.getContext("2d");
cv_Container = $("#canvasContainer").get(0) || viewport;
viewport.width = 800 * dpr;
viewport.height = 600 * dpr;
cv_Context.scale(dpr, dpr);
// Если сайт интегрировали...
try {
if(window.top.location.origin != window.location.origin)
noPlag();
} catch(err) {
noPlag()
}
cv_Container.onmousemove = (a) => {
mouseMoveOrDraw(a)
};
cv_Container.onmouseup = (a) => {
isMouseDown = false
};
cv_Container.onmousedown = mouseDown;
noCursorLock = $("#noCursorLock");
noDrawings = $("#noDrawings");
if(null != localStorage) {
noCursorLock.checked = ("1" == window.localStorage.getItem("noCursorLock"));
noDrawings.checked = ("1" == window.localStorage.getItem("noDrawings"));
}
window.onbeforeunload = () => {
if(window.localStorage && noCursorLock ) {
window.localStorage.setItem("noCursorLock", noCursorLock.checked ? "1" : "0")
window.localStorage.setItem("noDrawings", noDrawings.checked ? "1" : "0")
}
};
cv_Container.requestPointerLock = cv_Container.requestPointerLock || cv_Container.mozRequestPointerLock || cv_Container.webkitRequestPointerLock;
viewport.style.cursor = "none";
InitEditor();
if(!isEditor)
tryConnect();
setInterval(sendMousePos, 50);
setInterval(() => {
for (var a = 0; a < gObjs.length; a++) {
var b = gObjs[a];
if(5 == b.type)
Ka(b)
}
}, 40);
window.requestAnimationFrame(animate);
}
function Ka(a) {
function b(a, b, c) {
e.push([c, a, b]);
l[a + 400 * b] = !0;
g(a, b)
}
function c(a, b, c) {
p.push([c,
a, b
]);
l[a + 400 * b] = !0
}
function d(a, b) {
return 255 != k[4 * (a + 400 * b) + 3] && !l[a + 400 * b]
}
function g(a, b) {
var c = 4 * (a + 400 * b);
k[c + 0] = 255;
k[c + 1] = 153;
k[c + 2] = 153;
k[c + 3] = 255
}
var e = a.queue,
k = a.img.data,
l = a.explored,
p = a.potentialQueue,
r = e.length;
for (var m = 0; m < p.length; m++)
if(!P(p[m][1], p[m][2])) {
g(p[m][1], p[m][2]);
e.push(p[m]), p.splice(m, 1);
--m
}
for (m = 0; m < r; ++m)
if(P(e[m][1], e[m][2])) {
p.push(e[m]);
e.splice(m, 1);
--m;
--r
}
for (r = 0; 50 > r && 0 != e.length; ++r) {
for (var h = Number.POSITIVE_INFINITY, q = [e[0]], m = 1; m < e.length; ++m) {
var x = e[m][0];
.01 > Math.abs(x - h) ? q.push(e[m]) : x < h && (h = x, q = [e[m]])
}
for (m = 0; m < q.length; ++m) {
var x = q[m][0],
h = q[m][1],
s = q[m][2],
qa = e.indexOf(q[m]);
if(1 != qa)
e.splice(qa, 1);
0 < h && d(h - 1, s) && (P(h - 1, s) ? c(h - 1, s, x + 1) : b(h - 1, s, x + 1));
0 < s && d(h, s - 1) && (P(h, s - 1) ? c(h, s - 1, x + 1) : b(h, s - 1, x + 1));
400 > h + 1 && d(h + 1, s) && (P(h + 1, s) ? c(h + 1, s, x + 1) : b(h + 1, s, x + 1));
300 > s + 1 && d(h, s + 1) && (P(h, s + 1) ? c(h, s + 1, x + 1) : b(h, s + 1, x + 1));
0 < h && 0 < s && d(h - 1, s - 1) && (P(h - 1, s - 1) ? c(h - 1, s - 1, x + Math.SQRT2) : b(h - 1, s - 1, x + Math.SQRT2));
0 < h && 300 > s + 1 && d(h - 1, s + 1) && (P(h - 1, s + 1) ? c(h - 1, s + 1, x + Math.SQRT2) :
b(h - 1, s + 1, x + Math.SQRT2));
400 > h + 1 && 0 < s && d(h + 1, s - 1) && (P(h + 1, s - 1) ? c(h + 1, s - 1, x + Math.SQRT2) : b(h + 1, s - 1, x + Math.SQRT2));
400 > h + 1 && 300 > s + 1 && d(h + 1, s + 1) && (P(h + 1, s + 1) ? c(h + 1, s + 1, x + Math.SQRT2) : b(h + 1, s + 1, x + Math.SQRT2))
}
}
a.ctx.putImageData(a.img, 0, 0)
}
function noPlag() {
cv_Context.fillStyle = "#000000";
cv_Context.font = "10px NovaSquare";
cv_Context.fillText("Please do not embed our website, thank you.", 400 - cv_Context.measureText("Please do not embed our website, thank you.").width / 2, 300);
cv_Context.font = "16px NovaSquare";
cv_Context.fillText("Play the game on http://cursors.io/", 400 - cv_Context.measureText("Play the game on http://cursors.io/").width / 2, 330);
// top.location = "http://cursors.io";
throw "Please do not embed our website, thank you.";
}
var dlog = [0,0,0,0,0,0,0]
function tryConnect() {
if(null == ws) {
if(isLocalTest)
connectTo("192.168.1.14:9004", null);
// connectTo(config.ws, null);
else {
m28n.findServerPreference("cursors", function(b, c) {
if(null == ws) {
if(b || 0 == c.length) {
console.error(b);
setTimeout(tryConnect, 1E3)
}
else
connectTo(c[0].ipv4+":2828", c[0].ipv6)
}
});
}
}
function connectTo(b, n) {
// ws = new WebSocket("ws://" + (b || "[" + n + "]") + ":2828");
ws = new WebSocket("ws://" + b);
ws.binaryType = "arraybuffer";
ws.onopen = () => {
resetData();
console.log("Connected!")
};
ws.onmessage = onMessage;
ws.onclose = onClose;
ws.onerror = onError;
}
}
function onClose(a) {
resetData();
console.log("Socket closed: " + a.reason)
}
function onError(a) {
console.log("Socket error", a)
}
function resetData() {
Q.set(clearedData);
gObjs = [];
clickPoints = [];
drawLinesPoints = []
}
function onMessage(a) {
a = a.data;
var dv = new DataView(a);
// inLog(a)
switch(dv.getUint8(0)) {
case 0:
gameID = dv.getUint32(1, true);
break;
// // Spawn doors (но это не точно)
case 1:
var c;
if(a.byteLength > 15 && dlog[1]++ < 5 || dlog[1]<0)
console.log("A1", a);
ha = c = dv.getUint16(1, true);
playersIsLimited = c >= 100;
var d = [],
f;
for(f in playersList)
if(playersList.hasOwnProperty(f))
d.push(f);
for(var e = 0; e < c; e++) {
f = dv.getUint32(3 + 8 * e, true);
var h = dv.getUint16(7 + 8 * e, true),
g = dv.getUint16(9 + 8 * e, true);
if(f != gameID)
if(null != playersList[f]) {
for(var r = 0; r < d.length; r++)
if(d[r] == f) {
d.splice(r, 1);
break
}
f = playersList[f];
f.oldX = f.getX();
f.oldY = f.getY();
f.newX = h;
f.newY = g;
f.time = gTime
}
else
playersList[f] = new PlayerData(h, g)
}
for(e = 0; e < d.length; e++)
delete playersList[d[e]];
var cursor = readRemoteClick(dv, 3 + 8 * c);
f = dv.getUint16(cursor, true);
cursor += 2;
for(var kk = 0; kk < f; kk++) {
var _ID = dv.getUint32(cursor, true);
a: for(var i = 0; i < gObjs.length; i++) {
if(gObjs[i].id == _ID) {
var cObj = gObjs[i];
// if wall
if(cObj.type == 1) {
var _X = cObj.x | 0,
_Y = cObj.y | 0,
_width = cObj.width | 0,
_height = cObj.height | 0;
for(var m = _Y; m < _Y + _height; ++m)
for(var p = _X; p < _X + _width; ++p)
--Q[p + 400 * m];
}
gObjs.splice(i, 1);
break a
}
}
cursor += 4;
}
f = dv.getUint16(cursor, true);
cursor += 2;
for(d = 0; d < f; d++) {
a: {
e = dv.getUint32(cursor, true);
for(h = 0; h < gObjs.length; h++)
if(gObjs[h].id == e) {
e = gObjs[h];
break a
}
e = {
id: e
};
gObjs.push(e)
}
// console.warn("A1: gObj", e)
cursor += 4;
cursor = read_gObjects(dv, cursor, e)
inLog("UPDATE OBJ:");
inLog(e);
}
cursor = drawLines(dv, cursor);
if(a.byteLength < cursor + 4)
break;
countPlayers = dv.getUint32(cursor, true);
break;
// Spawn level
case 4:
if(dlog[4]++ < 5)
console.log("A4", a);
resetData();
inLog({spawn: {x: dv.getUint16(1, true), y: dv.getUint16(3, true)} })
U(dv.getUint16(1, true), dv.getUint16(3, true));
var countObjects = dv.getUint16(5, true);
cursor = 7;
for(var i = 0; i < countObjects; i++) {
var e = {};
e.id = dv.getUint32(cursor, true);
cursor += 4;
cursor = read_gObjects(dv, cursor, e);
gObjs.push(e);
// console.log("EE: ", e);
}
inLog("LEVEL loaded:");
inLog(gObjs);
inLog("END LEVEL");
if(a.byteLength >= cursor + 4)
u = Math.max(u, dv.getUint32(cursor, true))
else if(a.byteLength >= cursor + 2)
u = Math.max(u, dv.getUint16(cursor, true));
Y();
break;
// Spawn and install triggers and buttons
case 5:
if(dlog[5]++ < 5)
console.log("A5", a);
U(dv.getUint16(1, true), dv.getUint16(3, true));
if(9 <= dv.byteLength)
u = Math.max(u, dv.getUint32(5, true));
else (7 <= dv.byteLength)
u = Math.max(u, dv.getUint16(5, true));
Y();
break;
}
}
// Without this, your position only updates if you click or draw
// Отправка позиции курсора
function sendMousePos(a, b) {
if(!isEditor && !isNotEditor && null != ws && ws.readyState == WebSocket.OPEN) {
// console.log("sMP =", a, b, " _ ", posXbinTwo, posYbinTwo)
if("undefined" == typeof a)
a = posXbinTwo;
if("undefined" == typeof b)
b = posYbinTwo;
if(a != posXbinZZZ || b != posYbinZZZ) {
// console.log("QsMP =", a, b, " _ ", posXbinZZZ, posYbinZZZ)
var c = new ArrayBuffer(9),
d = new DataView(c);
// Отправка позиции курсора
d.setUint8(0, 1);
d.setUint16(1, a, true);
d.setUint16(3, b, true);
d.setUint32(5, u, true);
ws.send(c);
posXbinZZZ = a;
posYbinZZZ = b;
}
}
}
function isMLock() {
return document.pointerLockElement === cv_Container || document.mozPointerLockElement === cv_Container || document.webkitPointerLockElement === cv_Container
}
function P(a, b) {
return 0 > a || 400 <= a || 0 > b || 300 <= b ? true : Q[a + 400 * b]
}
function animate() {
cv_Context.clearRect(0, 0, 800, 600);
cv_Context.save();
if(null != ws && ws.readyState != WebSocket.OPEN || isNotEditor) {
var textMessage;
if(null == ws)
textMessage = "Click to begin";
else switch(ws.readyState) {
case WebSocket.CONNECTING:
textMessage = "Connecting";
break;
case WebSocket.CLOSING:
case WebSocket.CLOSED:
textMessage = "Lost connection to server";
break;
default:
textMessage = "Click to begin."
}
cv_Context.font = "60px NovaSquare";
cv_Context.fillText(textMessage, 400 - cv_Context.measureText(textMessage).width / 2, 315);
drawCircles();
drawPlayer(false)
}
else {
cv_Context.fillStyle = "#000000";
cv_Context.save();
cv_Context.globalAlpha = 1;
RenderEditor();
for(var n = 0; n < gObjs.length; n++) {
var b = gObjs[n];
// ObjText
if(0 == b.type) {
cv_Context.font = b.size + "px NovaSquare";
var c = b.x << 1,
d = b.y << 1;
b.isCentered && (c -= cv_Context.measureText(b.text).width / 2);
cv_Context.fillStyle = "#000000";
cv_Context.fillText(b.text, c, d)
}
// ObjWall
else if(1 == b.type) {
cv_Context.fillStyle = b.color;
cv_Context.fillRect(b.x << 1, b.y << 1, b.width << 1, b.height << 1);
cv_Context.strokeStyle = "#000000";
cv_Context.globalAlpha = .2;
cv_Context.lineWidth = 2;
cv_Context.strokeRect((b.x << 1) + 1, (b.y << 1) + 1, (b.width << 1) - 2, (b.height << 1) - 2);
cv_Context.globalAlpha = 1;
}
// ObjTeleport
else if(2 == b.type) {
cv_Context.fillStyle = b.isBad ? "#FF0000" : "#00FF00";
cv_Context.globalAlpha = .2;
cv_Context.fillRect(b.x << 1, b.y << 1, b.width << 1, b.height << 1);
cv_Context.globalAlpha = 1;
}
// ObjAreaCounter
else if(3 == b.type) {
var c = b.x << 1,
d = b.y << 1,
f = b.width << 1,
e = b.height << 1;
cv_Context.fillStyle = b.color;
cv_Context.globalAlpha = .2;
cv_Context.fillRect(c, d, f, e);
cv_Context.globalAlpha = .5;
cv_Context.fillStyle = "#000000";
if(40 > b.width || 40 > b.height) {
cv_Context.font = "30px NovaSquare";
cv_Context.fillText(b.count, c + f / 2 - cv_Context.measureText(b.count).width / 2, d + e / 2 + 10);
}
else {
cv_Context.font = "60px NovaSquare";
cv_Context.fillText(b.count, c + f / 2 - cv_Context.measureText(b.count).width / 2, d + e / 2 + 20);
}
cv_Context.globalAlpha = 1;
}
// ObjClickBox
else if(4 == b.type) {
drawX = b.x << 1;
drawY = b.y << 1;
drawWidth = b.width << 1;
drawHeight = b.height << 1;
cv_Context.fillStyle = b.color;
cv_Context.strokeStyle = b.color;
cv_Context.globalAlpha = 1;
cv_Context.fillRect(drawX, drawY, drawWidth, drawHeight);
cv_Context.globalAlpha = .2;
cv_Context.fillStyle = "#000000";
cv_Context.fillRect(drawX, drawY, drawWidth, drawHeight);
cv_Context.globalAlpha = 1;
cv_Context.fillStyle = b.color;
var isClicking = 150 > gTime - b.lastClickAt,
sizeSBox = isClicking ? 8 : 12;
cv_Context.fillRect(drawX + sizeSBox, drawY + sizeSBox, drawWidth - 2 * sizeSBox, drawHeight - 2 * sizeSBox);
cv_Context.strokeStyle = "#000000";
cv_Context.globalAlpha = .1;
cv_Context.beginPath();
cv_Context.moveTo(drawX, drawY);
cv_Context.lineTo(drawX + sizeSBox, drawY + sizeSBox);
cv_Context.moveTo(drawX + drawWidth, drawY);
cv_Context.lineTo(drawX + drawWidth - sizeSBox, drawY + sizeSBox);
cv_Context.moveTo(drawX, drawY + drawHeight);
cv_Context.lineTo(drawX + sizeSBox, drawY + drawHeight - sizeSBox);
cv_Context.moveTo(drawX + drawWidth, drawY + drawHeight);
cv_Context.lineTo(drawX + drawWidth - sizeSBox, drawY + drawHeight - sizeSBox);
cv_Context.moveTo(drawX, drawY);
cv_Context.rect(drawX, drawY, drawWidth, drawHeight);
cv_Context.rect(drawX + sizeSBox, drawY + sizeSBox, drawWidth - 2 * sizeSBox, drawHeight - 2 * sizeSBox);
cv_Context.stroke();
cv_Context.fillStyle = "#000000";
cv_Context.globalAlpha = .5;
if(50 > b.width || 50 > b.height) {
cv_Context.font = "35px NovaSquare";
cv_Context.fillText(b.count, drawX + drawWidth / 2 - cv_Context.measureText(b.count).width / 2, drawY + drawHeight / 2 + 13);
}
else {
cv_Context.font = "45px NovaSquare";
cv_Context.fillText(b.count, drawX + drawWidth / 2 - cv_Context.measureText(b.count).width / 2, drawY + drawHeight / 2 + 16);
}
if(isClicking) {
cv_Context.fillStyle = "#000000";
cv_Context.globalAlpha = .15;
cv_Context.fillRect(drawX + sizeSBox, drawY + sizeSBox, drawWidth - 2 * sizeSBox, drawHeight - 2 * sizeSBox);
}
cv_Context.globalAlpha = 1;
}
}
cv_Context.restore();
if(!isEditor) {
cv_Context.font = "12px NovaSquare";
cv_Context.strokeStyle = "#000000";
cv_Context.fillStyle = "#FFFFFF";
cv_Context.lineWidth = 2.5;
n = playersIsLimited ? "Area too full, not all cursors are shown" :// Область слишком заполнена, не все курсоры показаны
30 < ha ? "Area too full, drawing is disabled" : // Область слишком заполнена, рисунок отключен
"Use shift+click to draw";
cv_Context.globalAlpha = .5;
cv_Context.strokeText(n, 10, 590);
cv_Context.globalAlpha = 1;
cv_Context.fillText(n, 10, 590);
if(0 != countPlayers) {
n = countPlayers + " players online";
b = cv_Context.measureText(n).width;
cv_Context.globalAlpha = .5;
cv_Context.strokeText(n, 790 - b, 590);
cv_Context.globalAlpha = 1;
cv_Context.fillText(n, 790 - b, 590)
}
}
drawCircles();
// отрисовка линий
if(!noDrawings.checked) {
cv_Context.save();
cv_Context.strokeStyle = "#000000";
cv_Context.lineWidth = 1;
gTime = +new Date;
for(var i = 0; i < drawLinesPoints.length; i++) {
var cElem = drawLinesPoints[i],
alpha = 10 - (gTime - cElem[4]) / 1E3;
if(alpha <= 0) {
drawLinesPoints.splice(i, 1);
--i;
}
else {
if(alpha > 1)
alpha = 1;
cv_Context.globalAlpha = .3 * alpha;
cv_Context.beginPath();
cv_Context.moveTo(cElem[0] - .5, cElem[1] - .5);
cv_Context.lineTo(cElem[2] - .5, cElem[3] - .5);
cv_Context.stroke();
}
}
cv_Context.restore()
}
cv_Context.save();
// Отрисовка игроков
for(var k in playersList) {
if(playersList.hasOwnProperty(k)) {
cv_Context.drawImage(pCursor, (playersList[k].getX() << 1) - 6, (playersList[k].getY() << 1) - 6, 23, 30);
}
}
cv_Context.restore();
drawPlayer(true);
}
cv_Context.restore();
window.requestAnimationFrame(animate)
}
function drawPlayer(drawAreaGlow) {
if(isEditor) {
cv_Context.save();
cv_Context.globalAlpha = 1;
cv_Context.drawImage(pCursor, posX - 5, posY - 5);
}
else {
var _posX = 0,
_posY = 0;
// Если серверная позиция отличается от теккущей
if(posXbin != posXbinTwo || posYbin != posYbinTwo) {
cv_Context.save()
// Курсор "призрак" на красном фоне
if(drawAreaGlow) {
cv_Context.globalAlpha = .2;
cv_Context.fillStyle = "#FF0000";
cv_Context.beginPath();
cv_Context.arc(posX + 2, posY + 8, 20, 0, 2 * Math.PI, false);
cv_Context.fill();
}
cv_Context.globalAlpha = .5;
cv_Context.drawImage(pCursor, posX - 5, posY - 5, 23, 30);
cv_Context.restore();
}
else {
_posX = posX & 1;
_posY = posY & 1;
}
cv_Context.save();
if(drawAreaGlow) {
cv_Context.globalAlpha = .2;
cv_Context.fillStyle = "#FFFF00";
cv_Context.beginPath();
cv_Context.arc((posXbinTwo << 1) + _posX + 2, (posYbinTwo << 1) + _posY + 8, 20, 0, 2 * Math.PI, false);
cv_Context.fill();
}
cv_Context.globalAlpha = 1;
cv_Context.drawImage(Ha, (posXbinTwo << 1) + _posX - 5, (posYbinTwo << 1) + _posY - 5, 23, 30);
}
cv_Context.restore()
}
// Отрисовка кругов при клике
function drawCircles() {
cv_Context.save();
cv_Context.strokeStyle = "#000000";
gTime = +new Date;
for(var i = 0; i < clickPoints.length; i++) {
var cElem = clickPoints[i],
radius = (gTime - cElem[2]) / 1e3,
alpha = 1 - 2 * radius;
if(alpha <= 0) {
clickPoints.splice(i, 1);
--i;
}
else {
radius *= 50;
cv_Context.beginPath();
cv_Context.globalAlpha = .3 * alpha;
cv_Context.arc(cElem[0], cElem[1], radius, 0, 2 * Math.PI, false);
cv_Context.stroke();
}
}
cv_Context.restore()
}
// Отслеживание нажатий
function mouseDown(a) {
if(isEditor) {
isNotEditor = false;
mouseMoveOrDraw(a);
return false;
}
if(isMLock()) {
if(!isMLocked) {
isMLocked = true;
U(posXbinTwo, posYbinTwo);
}
}
else {
isMLocked = false;
if(isEditor || noCursorLock.checked || cv_Container.requestPointerLock)
cv_Container.requestPointerLock();
}
// Возможно ниже косяК!!
if(isNotEditor) {
isNotEditor = false;
sendMousePos();
}
else if((a.ctrlKey || a.shiftKey) && !noDrawings.checked) {
mouseMoveOrDraw(a);
isMouseDown = true;
N = posXbinTwo;
O = posYbinTwo;
}
else if(100 < gTime - ba && posXbin == posXbinTwo && posYbin == posYbinTwo) {
ba = gTime;
// Добавление позиции клика для отрисовки кругов
clickPoints.push([posXbin << 1, posYbin << 1, gTime]);
sendClick(posXbin, posYbin);
var b = [posXbin, posYbin];
remoteClicks.push(b);
setTimeout(function() {
remoteClicks.remove(b)
}, 1e3)
}
return false
}
function sendClick(x, y) {
if(!isEditor && null != ws && ws.readyState == WebSocket.OPEN) {
var c = new ArrayBuffer(9),
d = new DataView(c);
d.setUint8(0, 2);
d.setUint16(1, x, true);
d.setUint16(3, y, true);
d.setUint32(5, u, true);
ws.send(c)
}
}
// Чего-то неважно типа..
function U(x, y) {
console.log("U: ",x,y)
posXbinZZZ = x;
posYbinZZZ = y;
posXbinTwo = posXbin = x;
posYbinTwo = posYbin = y;
posX = posXbin << 1;
posY = posYbin << 1
}
function PlayerData(a, b) {
this.oldX = this.newX = a;
this.oldY = this.newY = b;
this.time = gTime
}
PlayerData.prototype = {
oldX: 0,
oldY: 0,
newX: 0,
newY: 0,
time: 0,
getX: function() {
var a = this.newX - this.oldX,
b = (gTime - this.time) / 100,
b = oa(0 >= b ? 0 : 1 <= b ? 1 : b);
return this.oldX + b * a
},
getY: function() {
var a = this.newY - this.oldY,
b = (gTime - this.time) / 100,
b = oa(0 >= b ? 0 : 1 <= b ? 1 : b);
return this.oldY + b * a
}
};
function oa(a) {
return a * a * (3 - 2 * a)
}
function mouseMoveOrDraw(a) {
if(isMLock()) {
var b = a.webkitMovementX || a.mozMovementX || a.movementX || 0;
a = a.webkitMovementY || a.mozMovementY || a.movementY || 0;
if(300 > Math.abs(b) + Math.abs(a))
setPos(posX + b, posY + a);
}
else if(a.offsetX)
setPos(a.offsetX, a.offsetY)
else if(a.layerX)
setPos(a.layerX, a.layerY);
if(isEditor) {
posXbinTwo = posXbin;
posYbinTwo = posYbin;
}
else if( Y(),
!isMLock() || posXbin == posXbinTwo && posYbin == posYbinTwo || (a = b = 0, posXbin > posXbinTwo && (b = 1), posYbin > posYbinTwo && (a = 1), posXbin = posXbinTwo, posYbin = posYbinTwo, posX = (posXbin << 1) + b, posY = (posYbin << 1) + a),
isMouseDown && (N != posXbinTwo || O != posYbinTwo) && 50 < gTime - da) {
b = N;
a = O;
// Рисование??
var c = posXbinTwo,
d = posYbinTwo;
if(!isEditor && null != ws && ws.readyState == WebSocket.OPEN) {
var f = new ArrayBuffer(9),
e = new DataView(f);
// Рисование
e.setUint8(0, 3);
e.setUint16(1, b, true);
e.setUint16(3, a, true);
e.setUint16(5, c, true);
e.setUint16(7, d, true);
ws.send(f);
}
N = posXbinTwo;
O = posYbinTwo;
da = gTime;
}
}
function Y() {
var a = posXbinTwo,
b = posYbinTwo;
if(P(posXbinTwo, posYbinTwo)) {
var c;
a: {
c = posXbinTwo;
var d = posYbinTwo,
f = [],
e = new Uint8Array(12e4);
f.push([c, d]);
e[c + 400 * d] = 1;
do {
var h = f.shift(),
g = h[0],
h = h[1];
if(!(0 > g || 0 > h || 400 <= g || 300 <= h)) {
if(!P(g, h)) {
c = {
x: g,
y: h
};
break a
}
e[g - 1 + 400 * h] || (f.push([g - 1, h]), e[g - 1 + 400 * h] = 1);
e[g + 1 + 400 * h] || (f.push([g + 1, h]), e[g + 1 + 400 * h] = 1);
e[g + 400 * (h - 1)] || (f.push([g, h - 1]), e[g + 400 * (h - 1)] = 1);
e[g + 400 * (h + 1)] || (f.push([g, h + 1]), e[g + 400 * (h + 1)] = 1)
}
} while (0 < f.length);
c = {
x: c,
y: d
}
}
posXbinTwo = c.x;
posYbinTwo = c.y
}
if(posXbinTwo != posXbin || posYbinTwo != posYbin) {
c = ea(posXbinTwo, posYbinTwo, posXbin, posYbin);
posXbinTwo = c.x;
posYbinTwo = c.y;
}
if(fa(posXbinZZZ, posYbinZZZ, a, b) && !fa(posXbinZZZ, posYbinZZZ, posXbinTwo, posYbinTwo)) {
sendMousePos(a, b);
sendMousePos(posXbinTwo, posYbinTwo);
}
a: {
for(var k = 0; k < gObjs.length; k++) {
var b = gObjs[k];
if(b.type == 2 && !(posXbinTwo < b.x || posYbinTwo < b.y || posXbinTwo >= b.x + b.width || posYbinTwo >= b.y + b.height)) {
// console.warn("PORTAL: "+b.id);
a = true;
break a;
}
a = false;
}
}
if(a)
sendMousePos();
}
function ea(a, b, c, d) {
a |= 0;
b |= 0;
c |= 0;
d |= 0;
if(P(a, b))
return {
x: a,
y: b
};
if(a == c && b == d)
return {
x: c,
y: d
};
var f = a,
e = b;
c = c - a | 0;
d = d - b | 0;
var h = 0,
g = 0,
k = 0,
l = 0;
0 > c ? h = -1 : 0 < c && (h = 1);
0 > d ? g = -1 : 0 < d && (g = 1);
0 > c ? k = -1 : 0 < c && (k = 1);
var m = Math.abs(c) | 0,
p = Math.abs(d) | 0;
if(m <= p) {
m = Math.abs(d) | 0;
p = Math.abs(c) | 0;
if(0 > d)
l = -1
else if(0 < d)
l = 1;
k = 0;
}
c = m >> 1;
for(d = 0; d <= m && !P(a, b); d++) {
f = a;
e = b;
c += p;
if(c >= m) {
c -= m;
a += h;
b += g;
}
else {
a += k;
b += l;
}
}
return {
x: f,
y: e
}
}
function fa(a, b, c, d) {
a = ea(a, b, c, d);
return a.x == c && a.y == d
}
function setPos(x, y) {
posX = x;
posY = y;
posXbin = posX >> 1;
posYbin = posY >> 1
}
function readRemoteClick(a, b) {
setTimeout(function() {
var c = a.getUint16(b, true),
d = 0;
a: for(; d < c; d++) {
var f = a.getUint16(b + 2 + 4 * d, true),
e = a.getUint16(b + 4 + 4 * d, true);
for(var h = 0; h < remoteClicks.length; h++) {
var g = remoteClicks[h];
if(g[0] == f && g[1] == e) {
remoteClicks.splice(h, 1);
continue a
}
}
clickPoints.push([f << 1, e << 1, gTime])
}
}, 100);
return b + 2 + 4 * a.getUint16(b, true)
}
function drawLines(a, b) {
if(!noDrawings.checked) {
setTimeout(function() {
var c = a.getUint16(b, true);
for(var d = 0; d < c; d++) {
var startX = a.getUint16(b + 2 + 8 * d, true),
startY = a.getUint16(b + 4 + 8 * d, true),
endX = a.getUint16(b + 6 + 8 * d, true),
endY = a.getUint16(b + 8 + 8 * d, true);
drawLinesPoints.push([startX << 1, startY << 1, endX << 1, endY << 1, gTime])
}
}, 50);
}
return b + 2 + 8 * a.getUint16(b, true);
}
function read_gObjects(dv, cursor, cObject) {
function readPosSize() {
cObject.x = dv.getUint16(cursor, true);
cursor += 2;
cObject.y = dv.getUint16(cursor, true);
cursor += 2;
cObject.width = dv.getUint16(cursor, true);
cursor += 2;
cObject.height = dv.getUint16(cursor, true);
cursor += 2
}
function readColor() {
for(var d = dv.getUint32(cursor, true).toString(16); 6 > d.length;)
d = "0" + d;
cursor += 4;
cObject.color = "#" + d
}
function getTextData(dv, cursor) {
var res = "",
charCode = 0;
for(var f = 0;(f = dv.getUint8(cursor)) != 0; ++cursor) {
charCode <<= 8;
charCode |= f;
if(!(f & 128)) {
res += String.fromCharCode(charCode);
charCode = 0;
}
}
if(charCode != 0)
res += String.fromCharCode(charCode);
return [res, cursor + 1];
}
var e = dv.getUint8(cursor);
cursor += 1;
cObject.type = e;
switch(e) {
case 255:
break;
// Draws text
case 0:
cObject.x = dv.getUint16(cursor, true);
cursor += 2;
cObject.y = dv.getUint16(cursor, true);
cursor += 2;
cObject.size = dv.getUint8(cursor);
cursor += 1;
cObject.isCentered = !!dv.getUint8(cursor);
cursor += 1;
e = getTextData(dv, cursor);
cObject.text = e[0];
cursor = e[1];
break;
// Draw walls and trigger collision (но это НЕ ТОЧНО!)
case 1:
readPosSize();
var h = !cObject.color;
readColor();
var x = cObject.x | 0,
y = cObject.y | 0,
width = cObject.width | 0,
height = cObject.height | 0;
if(h) {
for(var h = y; h < y + height; ++h)
for(var m = x; m < x + width; ++m)
++Q[m + 400 * h];
}
break;
// Draws teleport (exit points)
case 2:
readPosSize();
cObject.isBad = !!dv.getUint8(cursor);
cursor += 1;
break;
// Handles the area triggers
case 3:
readPosSize();
cObject.count = dv.getUint16(cursor, true);
cursor += 2;
readColor();
break;
// Buttons, doors and area triggers
case 4:
readPosSize();
if(cObject.count) {
if(cObject.count > dv.getUint16(cursor, true))
cObject.lastClickAt = gTime
else
cObject.lastClickAt = 0;
}
cObject.count = dv.getUint16(cursor, true);
cursor += 2;
readColor();
break;
case 5:
console.warn("TYPE 5!!!!!!!!!!", dv)
cObject.x = f.getUint16(cursor, !0);
cursor += 2;
cObject.y = f.getUint16(cursor, !0);
cursor += 2;
cObject.queue = [
[0, cObject.x, cObject.y]
];
cObject.potentialQueue = [];
cObject.explored = new Uint8Array(12e4);
cObject.img = window.createImageData(400, 300);
e = document.createElement("canvas");
e.width = 400;
e.height = 300;
cObject.canvas = e;
cObject.ctx = cObject.canvas.getContext("2d");
break;
default:
console.warn("HZZZ OBJ", dv)
throw Error("Unknown object type " + e);
}
return cursor
}
var levelEditor = function() {
function calculateDrawBox() {
var _posXbinStart = 0,
_posYbinStart = 0,
posXbinCurrent = posXbin / 10,
posYbinCurrent = posYbin / 10;
if(posXbinStart < posXbinCurrent) {
posXbinCurrent = Math.ceil(posXbinCurrent); // В большую
_posXbinStart = Math.floor(posXbinStart); // В меньшую
}
else {
posXbinCurrent = Math.floor(posXbinCurrent); // В меньшую
_posXbinStart = Math.ceil(posXbinStart); // В большую
}
if(posYbinStart < posYbinCurrent) {
posYbinCurrent = Math.ceil(posYbinCurrent); // В большую
_posYbinStart = Math.floor(posYbinStart); // В меньшую
}
else {
posYbinCurrent = Math.floor(posYbinCurrent); // В меньшую
_posYbinStart = Math.ceil(posYbinStart); // В большую
}
if(_posXbinStart > posXbinCurrent)
var e = posXbinCurrent,
posXbinCurrent = _posXbinStart,
_posXbinStart = e;
if(_posYbinStart > posYbinCurrent) {
e = posYbinCurrent;
posYbinCurrent = _posYbinStart;
_posYbinStart = e;
}
return {
sx: _posXbinStart,
sy: _posYbinStart,
fx: posXbinCurrent,
fy: posYbinCurrent
}
}
function onMouseDown() {
isMouseDown = true;
posXbinStart = posXbin / 10;
posYbinStart = posYbin / 10
}
function c(a) {
return "0x" + parseInt(a.slice(1), 16).toString(16).toUpperCase()
}
function create_gObj(width, height, c, d, e) {
// ~~ - делает целое число
var res = {
x: 10 * ~~(posXbinTwo / 10) - ~~(width / 2) + c,
y: 10 * ~~(posYbinTwo / 10) - ~~(height / 2) + d,
width: width,
height: height
};
// Установка значений
for(var f in e)
if(e.hasOwnProperty(f))
res[f] = e[f];
return res;
}
function findByPos(x, y) {
var c = null,
minRadius = Number.POSITIVE_INFINITY;
for(var i = 0; i < gObjs.length; i++) {
var f = gObjs[i];
if(f.hasOwnProperty("x") && f.hasOwnProperty("y") && f.hasOwnProperty("width") && f.hasOwnProperty("height")) {
var wi = f.x + f.width / 2,
he = f.y + f.height / 2,
radius = (x - wi) * (x - wi) + (y - he) * (y - he);
if(radius < minRadius) {
minRadius = radius;
c = f;
}
}
}
return c
}
var isMouseDown = false,
posXbinStart = 0,
posYbinStart = 0,
xBin = 200,
yBin = 150,
v = new Uint8Array(1200),
colorID = 1,
colorList = "#000000 #FF9999 #9999FF #FFFF99 #99FFFF #FF99FF #3333FF".split(" ");
window.addEventListener("mouseup", function() {
// $(document).mouseup(function() {
if(isMouseDown) {
var a = calculateDrawBox(),
b = colorID;
for(var c = a.sy; c < a.fy; ++c)
for(var d = a.sx; d < a.fx; ++d)
v[d + 40 * c] = b;
isMouseDown = false;
}
});
window.generateCode = function() {
var a = "class Level? : public Level {\npublic:\n\tLevel?() : Level(" + xBin + ", " + yBin + "){}\n\n\tvoid OnInit(){\n",
a = a + ("\t\tstd::vector<LevelObject*> wallByColor[" + colorList.length + "];\n"),
b = new Uint8Array(1200),
d = [];
for(var e = 0; 30 > e; ++e) {
for(var f = 0; 40 > f; ++f)
if(!b[f + 40 * e]) {
var g = v[f + 40 * e];
if(0 != g) {
for(var h = f; 40 > f && v[f + 40 * e] == g && !b[f + 40 * e];){
b[f + 40 * e] = true;
++f;
}
var k = f--,
n = k - posXbinStart,
l = e++;
a: for(; 30 > e;) {
for(var p = posXbinStart; p < k; ++p) {
if(v[p + 40 * e] != g)
break a;
if(b[p + 40 * e])
break a;
}
for(p = posXbinStart; p < k; ++p)
b[p + 40 * e] = true;
++e;
}
k = e - l;
e = l;
d.push({
x: 10 * posXbinStart,
y: 10 * l,
width: 10 * n,
height: 10 * k,
color: g - 1
})
}
}
}
for(b = 0; b < d.length; b++) {
var e = d[b];
if(0 == e.color) {
a += "\t\tAddObject(new ObjWall(" + e.x + ", " + e.y + ", " + e.width + ", " + e.height + ", 0x000000));\n"
}
else {
f = c(colorList[e.color]);
a += "\t\twallByColor[" + e.color + "].push_back(AddObject(new ObjWall(" + e.x + ", " + e.y + ", " + e.width + ", " + e.height + ", " + f + ")));\n";
}
}
for(b = 0; b < gObjs.length; b++) {
d = gObjs[b];
if(0 != d.type) {
if(2 == d.type)
a += "\t\tAddObject(new ObjTeleport(LevelManager::GetNextLevel(this), " + d.x + ", " + d.y + ", " + d.width + ", " + d.height + "));\n"
else if(3 == d.type)
a += "\t\tAddObject(new ObjAreaCounter(wallByColor[" + d.colorCode + "], " + d.x + ", " + d.y + ", " + d.width + ", " + d.height + ", ", a += d.count + ", " + c(d.color) + "));\n"
else if(4 == d.type)
a += "\t\tAddObject(new ObjClickBox(wallByColor[" + d.colorCode + "], " + d.x + ", " + d.y + ", " + d.width + ", " + d.height + ", ", a += d.count + ", 1000, " + c(d.color) + "));\n";
}
}
a += "\t}\n};\n"
var showCode = window.open('', 'code','left=50,top=20,width=600,height=800,status=0,location=0,menubar=0');
showCode.document.open();
showCode.document.write(a);
showCode.focus()
// showCode.document.close();
return a;
};
window.addEventListener("keydown", function(a) {
// $(document).keydown(function(event){
if(isEditor) {
var KeyC = event.keyCode;
// key "A"
if(65 == KeyC) {
--colorID;
if(0 > colorID)
colorID = colorList.length;
}
// key "S"
else if (83 == KeyC) {
++colorID;
if(colorID > colorList.length)
colorID = 0;
}
// key "Z"
else if(90 == KeyC) {
gObjs.pop()
}
// key "O"
else if(79 == KeyC) {
xBin = posXbinTwo;
yBin = posYbinTwo;
}
// key "W". Draw ObjTeleport
else if(87 == KeyC) {
gObjs.push(create_gObj(50, 50, -5, -5, {
type: 2,
isBad: false
}));
}
// key "N". Draw ObjAreaCounter
else if (78 == KeyC) {
if(colorID > 1)
gObjs.push(create_gObj(40, 40, 0, 0, {
type: 3,
color: colorList[colorID - 1],
colorCode: colorID - 1,
count: 1
}));
}
// key "V". Draw ObjClickBox
else if(66 == KeyC) {
if(colorID > 1)
gObjs.push(create_gObj(40, 40, 5, 5, {
type: 4,
color: colorList[colorID - 1],
colorCode: colorID - 1,
count: 5
}))
}
// Moving
// key "ArrowLeft"
else if(37 == KeyC) {
var b = findByPos(posXbin, posYbin);
if(null != b) {
if(event.shiftKey)
b.width -= 10;
else {
b.x -= 10;
b.width += 10;
}
// Если размер нулевой, то удаляем
if(0 == b.width)
gObjs.splice(gObjs.indexOf(b), 1);
}
}
// key "ArrowRight"
else if(39 == KeyC) {
var b = findByPos(posXbin, posYbin);
if(null != b) {
if(event.shiftKey) {
b.x += 10;
b.width -= 10;
}
else
b.width += 10;
// Если размер нулевой, то удаляем
if(0 == b.width)
gObjs.splice(gObjs.indexOf(b), 1);
}
}
// key "ArrowUp"
else if(38 == KeyC) {
var b = findByPos(posXbin, posYbin);
if(null != b) {
if(event.shiftKey)
b.height -= 10;
else {
b.y -= 10;
b.height += 10;
}
// Если размер нулевой, то удаляем
if(0 == b.height)
gObjs.splice(gObjs.indexOf(b), 1);
}
}
// key "ArrowDown"
else if(40 == KeyC) {
var b = findByPos(posXbin, posYbin);
if(null != b) {
if(event.shiftKey) {
b.y += 10;
b.height -= 10;
} else
b.height += 10;
// Если размер нулевой, то удаляем
if(0 == b.height)
gObjs.splice(gObjs.indexOf(b), 1);
}
}
}
});
return {
renderEditor: function() {
if(isEditor) {
cv_Context.save();
cv_Context.fillStyle = "#FF0000";
cv_Context.strokeStyle = "#FF0000";
cv_Context.lineWidth = 1;
cv_Context.globalAlpha = .09;
cv_Context.beginPath();
for(var b = 0; 400 > b; b += 10) {
cv_Context.moveTo((b << 1) + .5, 0);
cv_Context.lineTo((b << 1) + .5, 600);
}
for(var c = 0; 300 > c; c += 10) {
cv_Context.moveTo(0, (c << 1) + .5);
cv_Context.lineTo(800, (c << 1) + .5);
}
cv_Context.stroke();
cv_Context.lineWidth = 2;
cv_Context.beginPath();
cv_Context.moveTo(400.5, 0);
cv_Context.lineTo(400.5, 600);
cv_Context.moveTo(0, 300.5, 0);
cv_Context.lineTo(800, 300.5);
cv_Context.stroke();
cv_Context.lineWidth = 1;
cv_Context.globalAlpha = 1;
cv_Context.fillStyle = "#000000";
var d = calculateDrawBox();
for(var c = 0; 300 > c; c += 10) {
for(var b = 0; 400 > b; b += 10) {
var f = b / 10 | 0,
g = c / 10 | 0,
h = v[f + 40 * g];
if(isMouseDown && f >= d.sx && f < d.fx && g >= d.sy && g < d.fy)
h = colorID;
if(h != 0) {
cv_Context.fillStyle = colorList[h - 1];
cv_Context.fillRect(b << 1, c << 1, 20, 20)
}
}
}
cv_Context.save();
cv_Context.globalAlpha = .09;
cv_Context.fillStyle = "#0000FF";
cv_Context.beginPath();
cv_Context.arc(xBin << 1, yBin << 1, 20, 0, 2 * Math.PI, false);
cv_Context.fill();
cv_Context.restore();
cv_Context.save();
cv_Context.fillStyle = "#FFFFFF";
cv_Context.strokeStyle = "#000000";
cv_Context.lineWidth = 2.5;
cv_Context.font = "14px NovaSquare";
cv_Context.globalAlpha = .5;
cv_Context.strokeText("Current color: ", 10, 590);
cv_Context.globalAlpha = 1;
cv_Context.fillText("Current color: ", 10, 590);
if(0 == colorID)
cv_Context.fillText("ESR", 105, 590)
else {
cv_Context.fillStyle = "#000000";
cv_Context.fillRect(104, 575, 22, 22);
cv_Context.fillStyle = colorList[colorID - 1];
cv_Context.fillRect(105, 576, 20, 20);
}
cv_Context.restore();
cv_Context.restore();
}
},
initEditor: function() {
cv_Container.addEventListener("mousedown", onMouseDown)
}
}
}();
var RenderEditor = levelEditor.renderEditor,
InitEditor = levelEditor.initEditor;
setTimeout(function() {
//jQuery.noConflict();
console.log('jQuery loaded');
window.$ = jQuery;
startGame();
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment