Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // --------------------[Инициализация]-----------------------
- const myWs = new WebSocket('ws://localhost:9000');
- document.oncontextmenu = function() { return false; }
- // --------------------[Контроллеры объектов]-----------------------
- const field_con = {
- fig_list: [],
- addFigure(obj){
- if (obj.id != undefined) {
- this.fig_list[obj.id] = document.createElement('img');
- that = this.fig_list[obj.id];
- } else {
- this.fig_list[this.fig_list.length] = document.createElement('img');
- that = this.fig_list[this.fig_list.length-1];
- that.id = this.fig_list.length-1;
- }
- that.orig = obj;
- that.name = obj.name;
- that.title = obj.name;
- that.src = "http://127.0.0.1:8000/static/" + obj.src;
- that.orig.rota = (that.orig.rota == undefined) ? (0) : that.orig.rota;
- that.eventAnchor = 'figure';
- that.x_cord = obj.x;
- that.y_cord = obj.y;
- console.log(obj.x);
- that.style.top = that.y_cord + 'px';
- that.style.left = that.x_cord + 'px';
- that.style.position = 'absolute';
- that.style.transform = `rotate(${that.orig.rota}deg)`;
- that.bound_fig = obj.bound_fig;
- if (that.bound_fig != undefined) that.bound_fig.onField = this.fig_list[that.id];
- that.bound_unit = obj.bound_unit;
- that.onmousedown = mouseDownWrapperUnit;
- if (that.bound_unit != undefined) that.onmouseover = highlight_unit;
- that.onmouseout = highlight_unit_stop;
- document.body.append(that);
- return that;
- },
- delFigure() {
- console.log('Вызов')
- }
- }
- const game_con = {
- ruleset: {
- },
- units: [],
- actions: [],
- cur_round: 1,
- cur_phase: -1,
- maxRounds: null,
- unitMoverActive: false,
- startGame() {
- this.cur_round = 1;
- this.cur_phase = 0;
- },
- initPhase() {
- this.cur_phase += 1
- if (typeof courseOfPlay.gameCycle[this.cur_phase].prepare != 'undefined') {
- for (c of courseOfPlay.gameCycle[this.cur_phase].prepare) {
- c();
- }
- }
- },
- spawnUnit(obj) {
- this.units[this.units.length] = Object.assign({}, obj);
- that2 = this.units[this.units.length-1];
- that2.mousedowner = {parent: that2, lbm: dragndrop};
- for (let c of that2.figures) {
- x = {
- name: c.name,
- src: c.img,
- bound_fig: c,
- bound_unit: that2,
- x: 600,
- y: 200,
- }
- field_con.addFigure(x);
- for (let c2 of c.weapons) {
- c.weapons[c2] = Object.assign({}, prime_weapons[c2]);
- }
- }
- },
- }
- const win_con = {
- list: [],
- actionWindow: null,
- createWindow(header, context, targ=null) {
- this.list[this.list.length] = document.createElement('div');
- that = this.list[this.list.length-1];
- that.target = targ;
- that.classList.add('window');
- str = '<div>' + header + '<button id="selectme" style="float: right;">X</button></div><hr><div class="hider"><p>' + context + '</p></div>';
- that.innerHTML = str;
- that.close_bt = that.querySelector('button');
- that.close_bt.onclick = function() {
- win_con.list[win_con.list.length-1].remove();
- }
- that.eventAnchor = 'window';
- that.onmousedown = mouseDownWrapperWindow;
- that.mousedowner = {
- lbm: dragndrop
- }
- document.body.append(that);
- return that;
- }
- }
- // --------------------[Вспомогательные функции]-----------------------
- function send_wses(targ, inp, type = "message") {
- let outp = {
- json_type: type,
- hash: 'placeholder',
- yumies: inp
- }
- targ.send(JSON.stringify(outp));
- }
- function dice(inp) {
- c1 = inp.indexOf('d');
- c2 = inp.indexOf(' ');
- num = inp.slice(0, c1);
- sides = inp.slice(c1+1);
- x = 0;
- log = "";
- for (let c = 1; c<=num; c++) {
- y = Math.floor(Math.random() * sides + 1);
- log += y + " ";
- x += y;
- }
- console.log('⚄', log);
- return x;
- }
- function rangeMeasure(x1, y1, x2, y2) {
- r = Math.sqrt((x2-x1)**2+(y2-y1)**2);
- return r;
- }
- function parentRecursive(x, n) {
- if (n>1) {
- return parentRecursive(x, n-1).parentElement;
- } else {
- return x.parentElement;
- }
- console.log(y);
- }
- function findAngle(x0, y0, x, y) {
- r = rangeMeasure(x0, y0, x, y);
- cx = Math.acos((x - x0) / r);
- cy = Math.asin((y - y0) / r);
- // console.log(Math.trunc(cx*57,2958), ' ', Math.trunc(cy*57,2958));
- if (r>100) {
- r = 100;
- };
- zx = r * Math.cos(cx) + x0;
- zy = r * Math.sin(cy) + y0;
- obj = {
- src: 'dot.png',
- x: zx,
- y: zy,
- };
- console.log(obj.x, ' ', obj.y);
- if (r>=100) {
- rra = field_con.addFigure(obj);
- console.log(rra);
- if (rangeMeasure(200, 200, rra.x, rra.y)>r) rra.remove();
- }
- }
- // --------------------[WebSocket]-----------------------
- myWs.onopen = function () {
- console.log('Подключился');
- };
- myWs.onmessage = function (smth) {
- let x = JSON.parse(smth.data);
- switch (x.json_type) {
- case 'message':
- // console.log('Сообщение: %s', x.yumies);
- console.log(x.yumies);
- break;
- case 'game_data_init':
- init = JSON.parse(x.yumies);
- for (let c of init) {
- field_con.addFigure(c);
- }
- console.log(init);
- break;
- case 'rule_data_init':
- game_con.ruleset = JSON.parse(x.yumies);
- break;
- case 'codex_data_init':
- len = game_con.codexes.length;
- game_con.codexes[len] = JSON.parse(x.yumies);
- break;
- case 'roster1_data_init':
- len = game_con.rosters.length;
- game_con.rosters[len] = JSON.parse(x.yumies);
- break;
- case 'updateXY':
- field_con.fig_list[x.yumies.id].style.top = x.yumies.y + 'px';
- field_con.fig_list[x.yumies.id].style.left = x.yumies.x + 'px';
- break;
- }
- };
- function log_field() {
- console.log(JSON.stringify(field_con.fig_list))
- }
- // ----------------[Обёртки]---------------------------------
- function mouseDownWrapperUnit(event) {
- target = this;
- if (event.button == 0) target.bound_unit.mousedowner.lbm(event, target);
- if (event.button == 2) target.bound_unit.mousedowner.rbm(event, target);
- }
- function mouseDownWrapperWindow(event) {
- target = this;
- if (event.button == 0) target.mousedowner.lbm(event, target);
- if (event.button == 2) target.mousedowner.rbm(event, target);
- }
- // ----------------[Операции с фигурами]----------------------
- function dragndrop(event, target) {
- that = target;
- that.ondragstart = function() {return false;};
- that.style.position = 'absolute';
- that.style.zIndex = 1000;
- document.body.append(that);
- corr_x = event.pageX - that.getBoundingClientRect().x;
- corr_y = event.pageY - that.getBoundingClientRect().y;
- moveAt(event.pageX, event.pageY);
- if (game_con.unitMoverActive == true) {
- for (c of game_con.initials)
- if (c.whom == that) {
- startpoint = c;
- }
- };
- function moveAt(pageX, pageY) {
- that.x_cord = (pageX - corr_x);
- that.style.left = that.x_cord + 'px';
- that.y_cord = (pageY - corr_y);
- that.style.top = that.y_cord + 'px';
- findAngle(200, 200, that.x_cord, that.y_cord);
- }
- function onMouseMove(event) {
- if ((game_con.unitMoverActive == false)||(rangeMeasure(that.initialMovementX, that.initialMovementY, that.x_cord, that.y_cord)<that.bound_unit.move)) {
- moveAt(event.pageX, event.pageY, that);
- }
- }
- document.addEventListener('mousemove', onMouseMove);
- that.onwheel = wheel_rotate;
- that.onmouseup = function() {
- document.removeEventListener('mousemove', onMouseMove);
- // send_wses(myWs, that.orig, 'updateXY');
- that.style.zIndex = 500;
- that.onmouseup = null;
- that.onwheel = null;
- };
- };
- function wheel_rotate(event) {
- this.orig.rota += event.deltaY / Math.abs(event.deltaY) * 15;
- this.style.transform = `rotate(${this.orig.rota}deg)`;
- }
- function highlight_unit(event){
- that = this;
- highlight(that);
- for (c of field_con.fig_list) {
- if (c.bound_unit == that.bound_unit) {
- highlight(c);
- }
- }
- }
- function highlight_unit_stop(event){
- that = this;
- highlight(that);
- for (c of field_con.fig_list) {
- if (c.bound_unit == that.bound_unit) {
- highlight_stop(c);
- }
- }
- }
- function highlight(target) {
- target.classList.add('highlight');
- }
- function highlight_stop(target) {
- target.classList.remove('highlight');
- }
- // ----------------[Геометрические функции]----------------------
- function elips(a, b, x0, y0, r) {
- mas = []
- for (let c = 0 ; c<18; c++) {
- x = a * Math.cos(c * Math.PI / 9) + x0;
- y = b * Math.sin(c * Math.PI / 9) + y0;
- xy = rotation(x, y, x0, y0, r);
- mas[c] = xy;
- }
- return mas;
- }
- function rotation(x, y, x0, y0, r) {
- let xr = (x - x0) * Math.cos(r * Math.PI / 180) - (y - y0) * Math.sin(r * Math.PI / 180) + x0;
- let yr = (x - x0) * Math.sin(r * Math.PI / 180) + (y - y0) * Math.cos(r * Math.PI / 180) + y0;
- let xy = {
- x: xr,
- y: yr
- }
- return xy;
- }
- function make_aura(who, type, a, b = null) {
- if (b == null)
- b = a;
- cen_x = who.orig.x + who.offsetWidth / 2;
- cen_y = who.orig.y + who.offsetHeight / 2;
- who.aura = elips(a, b, cen_x, cen_y, who.orig.rota, true);
- }
- function upd_aura(who, type, a, b, com = 'upd') {
- for (let c in who.aura) {
- who.aura[c].remove();
- }
- if (com != 'rem') {
- make_aura(who, type, a, b);
- for (let c in who.aura) {
- document.body.append(who.aura[c]);
- }
- }
- }
- // ----------------[Обработка логики]----------------------
- function unitMover(targ) {
- game_con.initials = [];
- for (c of targ.figures) {
- c.onField.initialMovementX = c.onField.x_cord;
- c.onField.initialMovementY = c.onField.y_cord;
- console.log(c);
- game_con.initials[game_con.initials.length] = document.createElement('img');
- that = game_con.initials[game_con.initials.length-1]
- that.src = "http://127.0.0.1:8000/static/dot.png";
- that.style.position = 'absolute';
- that.style.left = c.onField.initialMovementX + 'px';
- that.style.top = c.onField.initialMovementY + 'px';
- that.whom = c;
- document.body.append(that);
- targ.mousedowner.lbm = dragndrop;
- }
- game_con.unitMoverActive = true;
- }
- // ------------------[Дебажные хуйни]------------------------
- function dd_avral(event) { // (1) отследить нажатие
- ball = this;
- that.ondragstart = function() {return false;};
- ball.style.position = 'absolute';
- ball.style.zIndex = 1000;
- document.body.append(ball);
- moveAt(event.pageX, event.pageY);
- function moveAt(pageX, pageY) {
- ball.style.left = pageX - ball.offsetWidth / 2 + 'px';
- ball.style.top = pageY - ball.offsetHeight / 2 + 'px';
- }
- function onMouseMove(event) {
- moveAt(event.pageX, event.pageY);
- }
- document.addEventListener('mousemove', onMouseMove);
- ball.onmouseup = function() {
- document.removeEventListener('mousemove', onMouseMove);
- ball.onmouseup = null;
- };
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement