Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var ws = new WebSocket("ws://daydun.com:4444");
- var id = -1;
- var entity;
- ws.addEventListener("open", function() {
- console.log("[GlobeSim] WebSocket Open");
- if (localStorage.globesimCode) {
- ws.send(JSON.stringify({
- type: "login",
- code: localStorage.globesimCode
- }));
- } else {
- OWOP.windowSys.addWindow(loginWindow);
- }
- });
- ws.addEventListener("message", function(event) {
- var msg = JSON.parse(event.data);
- switch(msg.type) {
- case "login":
- if (msg.success) {
- localStorage.globesimCode = msg.entity.code;
- id = msg.id;
- entity = msg.entity;
- } else {
- OWOP.windowSys.addWindow(loginWindow);
- }
- break;
- }
- });
- ws.addEventListener("close", function() {
- console.log("[GlobeSim] WebSocket Close");
- });
- function darken(color) {
- let r = color >> 16 & 0xff;
- let g = color >> 8 & 0xff;
- let b = color >> 0 & 0xff;
- let darken = 50;
- if ((r + g + b) / 3 < 100) {
- darken = -50;
- }
- r = Math.min(Math.max(r - darken, 0), 255);
- g = Math.min(Math.max(g - darken, 0), 255);
- b = Math.min(Math.max(b - darken, 0), 255);
- return (r << 16) | (g << 8) | (b << 0);
- }
- function placeLand(x, y, remove) {
- ws.send(JSON.stringify({
- type: "placeLand",
- x: x,
- y: y,
- remove: remove
- }));
- var c = [
- entity.color >> 16 & 0xff,
- entity.color >> 8 & 0xff,
- entity.color >> 0 & 0xff
- ];
- [
- [0, 0],
- [1, 0], [0, 1], [-1, 0], [0, -1]
- ].forEach(function(i) {
- //OWOP.world.setPixel(x + i[0], y + i[1], c);
- });
- }
- OWOP.tool.addToolObject(new OWOP.tool.class("Territory", OWOP.cursors.cursor, OWOP.fx.player.RECT_SELECT_ALIGNED(1), OWOP.RANK.USER, function (tool) {
- var lastX, lastY;
- tool.setEvent("mousedown mousemove", function (mouse, event) {
- var remove = mouse.buttons === 2;
- switch (mouse.buttons) {
- case 1:
- case 2:
- if (!lastX || !lastY) {
- lastX = mouse.tileX;
- lastY = mouse.tileY;
- }
- OWOP.util.line(lastX, lastY, mouse.tileX, mouse.tileY, 1, function (x, y) {
- placeLand(x, y, remove);
- });
- lastX = mouse.tileX;
- lastY = mouse.tileY;
- break;
- }
- return 3;
- });
- tool.setEvent("mouseup", function (mouse) {
- lastX = null;
- lastY = null;
- });
- }));
- var loginWindow = new OWOP.windowSys.class.window("Login", {closeable: true}, function(wdow) {
- wdow.container.style.width = "120px";
- wdow.container.style.textAlign = "center";
- var register = document.createElement("button");
- register.innerHTML = "Register";
- register.style.width = "120px";
- register.style.display = "block";
- register.addEventListener("click", function() {
- wdow.container.removeChild(register);
- wdow.container.removeChild(login);
- var info = document.createElement("span");
- info.style.color = "#fff";
- info.innerHTML = "Choose a color for your land.";
- wdow.container.appendChild(info);
- var color = document.createElement("input");
- color.type = "color";
- color.style.height = "32px";
- color.style.marginTop = "8px";
- color.addEventListener("input", function(event) {
- OWOP.windowSys.delWindow(loginWindow);
- ws.send(JSON.stringify({
- type: "register",
- color: parseInt(event.target.value.slice(1), 16)
- }));
- });
- wdow.container.appendChild(color);
- });
- wdow.container.appendChild(register);
- var login = document.createElement("button");
- login.innerHTML = "Login";
- login.style.width = "120px";
- wdow.container.appendChild(login);
- }).move(window.innerWidth - 195, 32);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement