Advertisement
Guest User

Untitled

a guest
Aug 26th, 2023
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.43 KB | Fixit | 0 0
  1. // --------------------[Инициализация]-----------------------
  2. const myWs = new WebSocket('ws://localhost:9000');
  3. document.oncontextmenu = function() { return false; }
  4. // --------------------[Контроллеры объектов]-----------------------
  5. const field_con = {
  6. fig_list: [],
  7. addFigure(obj){
  8. if (obj.id != undefined) {
  9. this.fig_list[obj.id] = document.createElement('img');
  10. that = this.fig_list[obj.id];
  11. } else {
  12. this.fig_list[this.fig_list.length] = document.createElement('img');
  13. that = this.fig_list[this.fig_list.length-1];
  14. that.id = this.fig_list.length-1;
  15. }
  16. that.orig = obj;
  17. that.name = obj.name;
  18. that.title = obj.name;
  19. that.src = "http://127.0.0.1:8000/static/" + obj.src;
  20. that.orig.rota = (that.orig.rota == undefined) ? (0) : that.orig.rota;
  21. that.eventAnchor = 'figure';
  22. that.x_cord = obj.x;
  23. that.y_cord = obj.y;
  24. console.log(obj.x);
  25. that.style.top = that.y_cord + 'px';
  26. that.style.left = that.x_cord + 'px';
  27. that.style.position = 'absolute';
  28. that.style.transform = `rotate(${that.orig.rota}deg)`;
  29. that.bound_fig = obj.bound_fig;
  30. if (that.bound_fig != undefined) that.bound_fig.onField = this.fig_list[that.id];
  31. that.bound_unit = obj.bound_unit;
  32. that.onmousedown = mouseDownWrapperUnit;
  33. if (that.bound_unit != undefined) that.onmouseover = highlight_unit;
  34. that.onmouseout = highlight_unit_stop;
  35. document.body.append(that);
  36. return that;
  37. },
  38.  
  39. delFigure() {
  40. console.log('Вызов')
  41. }
  42. }
  43.  
  44. const game_con = {
  45. ruleset: {
  46. },
  47. units: [],
  48. actions: [],
  49. cur_round: 1,
  50. cur_phase: -1,
  51. maxRounds: null,
  52. unitMoverActive: false,
  53. startGame() {
  54. this.cur_round = 1;
  55. this.cur_phase = 0;
  56. },
  57. initPhase() {
  58. this.cur_phase += 1
  59. if (typeof courseOfPlay.gameCycle[this.cur_phase].prepare != 'undefined') {
  60. for (c of courseOfPlay.gameCycle[this.cur_phase].prepare) {
  61. c();
  62. }
  63. }
  64. },
  65. spawnUnit(obj) {
  66. this.units[this.units.length] = Object.assign({}, obj);
  67. that2 = this.units[this.units.length-1];
  68. that2.mousedowner = {parent: that2, lbm: dragndrop};
  69. for (let c of that2.figures) {
  70. x = {
  71. name: c.name,
  72. src: c.img,
  73. bound_fig: c,
  74. bound_unit: that2,
  75. x: 600,
  76. y: 200,
  77. }
  78. field_con.addFigure(x);
  79. for (let c2 of c.weapons) {
  80. c.weapons[c2] = Object.assign({}, prime_weapons[c2]);
  81. }
  82. }
  83. },
  84. }
  85.  
  86. const win_con = {
  87. list: [],
  88. actionWindow: null,
  89. createWindow(header, context, targ=null) {
  90. this.list[this.list.length] = document.createElement('div');
  91. that = this.list[this.list.length-1];
  92. that.target = targ;
  93. that.classList.add('window');
  94. str = '<div>' + header + '<button id="selectme" style="float: right;">X</button></div><hr><div class="hider"><p>' + context + '</p></div>';
  95. that.innerHTML = str;
  96. that.close_bt = that.querySelector('button');
  97. that.close_bt.onclick = function() {
  98. win_con.list[win_con.list.length-1].remove();
  99. }
  100. that.eventAnchor = 'window';
  101. that.onmousedown = mouseDownWrapperWindow;
  102. that.mousedowner = {
  103. lbm: dragndrop
  104. }
  105. document.body.append(that);
  106. return that;
  107. }
  108. }
  109. // --------------------[Вспомогательные функции]-----------------------
  110. function send_wses(targ, inp, type = "message") {
  111. let outp = {
  112. json_type: type,
  113. hash: 'placeholder',
  114. yumies: inp
  115. }
  116. targ.send(JSON.stringify(outp));
  117. }
  118.  
  119. function dice(inp) {
  120. c1 = inp.indexOf('d');
  121. c2 = inp.indexOf(' ');
  122. num = inp.slice(0, c1);
  123. sides = inp.slice(c1+1);
  124. x = 0;
  125. log = "";
  126. for (let c = 1; c<=num; c++) {
  127. y = Math.floor(Math.random() * sides + 1);
  128. log += y + " ";
  129. x += y;
  130. }
  131. console.log('⚄', log);
  132. return x;
  133. }
  134.  
  135. function rangeMeasure(x1, y1, x2, y2) {
  136. r = Math.sqrt((x2-x1)**2+(y2-y1)**2);
  137. return r;
  138. }
  139.  
  140. function parentRecursive(x, n) {
  141. if (n>1) {
  142. return parentRecursive(x, n-1).parentElement;
  143. } else {
  144. return x.parentElement;
  145. }
  146.  
  147. console.log(y);
  148. }
  149.  
  150. function findAngle(x0, y0, x, y) {
  151. r = rangeMeasure(x0, y0, x, y);
  152. cx = Math.acos((x - x0) / r);
  153. cy = Math.asin((y - y0) / r);
  154. // console.log(Math.trunc(cx*57,2958), ' ', Math.trunc(cy*57,2958));
  155. if (r>100) {
  156. r = 100;
  157. };
  158. zx = r * Math.cos(cx) + x0;
  159. zy = r * Math.sin(cy) + y0;
  160. obj = {
  161. src: 'dot.png',
  162. x: zx,
  163. y: zy,
  164. };
  165. console.log(obj.x, ' ', obj.y);
  166. if (r>=100) {
  167. rra = field_con.addFigure(obj);
  168. console.log(rra);
  169. if (rangeMeasure(200, 200, rra.x, rra.y)>r) rra.remove();
  170. }
  171. }
  172. // --------------------[WebSocket]-----------------------
  173. myWs.onopen = function () {
  174. console.log('Подключился');
  175. };
  176.  
  177. myWs.onmessage = function (smth) {
  178. let x = JSON.parse(smth.data);
  179. switch (x.json_type) {
  180. case 'message':
  181. // console.log('Сообщение: %s', x.yumies);
  182. console.log(x.yumies);
  183. break;
  184. case 'game_data_init':
  185. init = JSON.parse(x.yumies);
  186. for (let c of init) {
  187. field_con.addFigure(c);
  188. }
  189. console.log(init);
  190. break;
  191. case 'rule_data_init':
  192. game_con.ruleset = JSON.parse(x.yumies);
  193. break;
  194. case 'codex_data_init':
  195. len = game_con.codexes.length;
  196. game_con.codexes[len] = JSON.parse(x.yumies);
  197. break;
  198. case 'roster1_data_init':
  199. len = game_con.rosters.length;
  200. game_con.rosters[len] = JSON.parse(x.yumies);
  201. break;
  202. case 'updateXY':
  203. field_con.fig_list[x.yumies.id].style.top = x.yumies.y + 'px';
  204. field_con.fig_list[x.yumies.id].style.left = x.yumies.x + 'px';
  205. break;
  206. }
  207. };
  208.  
  209. function log_field() {
  210. console.log(JSON.stringify(field_con.fig_list))
  211. }
  212.  
  213. // ----------------[Обёртки]---------------------------------
  214. function mouseDownWrapperUnit(event) {
  215. target = this;
  216. if (event.button == 0) target.bound_unit.mousedowner.lbm(event, target);
  217. if (event.button == 2) target.bound_unit.mousedowner.rbm(event, target);
  218. }
  219. function mouseDownWrapperWindow(event) {
  220. target = this;
  221. if (event.button == 0) target.mousedowner.lbm(event, target);
  222. if (event.button == 2) target.mousedowner.rbm(event, target);
  223. }
  224. // ----------------[Операции с фигурами]----------------------
  225. function dragndrop(event, target) {
  226. that = target;
  227. that.ondragstart = function() {return false;};
  228. that.style.position = 'absolute';
  229. that.style.zIndex = 1000;
  230. document.body.append(that);
  231. corr_x = event.pageX - that.getBoundingClientRect().x;
  232. corr_y = event.pageY - that.getBoundingClientRect().y;
  233. moveAt(event.pageX, event.pageY);
  234. if (game_con.unitMoverActive == true) {
  235. for (c of game_con.initials)
  236. if (c.whom == that) {
  237. startpoint = c;
  238. }
  239. };
  240. function moveAt(pageX, pageY) {
  241. that.x_cord = (pageX - corr_x);
  242. that.style.left = that.x_cord + 'px';
  243. that.y_cord = (pageY - corr_y);
  244. that.style.top = that.y_cord + 'px';
  245. findAngle(200, 200, that.x_cord, that.y_cord);
  246. }
  247. function onMouseMove(event) {
  248. if ((game_con.unitMoverActive == false)||(rangeMeasure(that.initialMovementX, that.initialMovementY, that.x_cord, that.y_cord)<that.bound_unit.move)) {
  249. moveAt(event.pageX, event.pageY, that);
  250. }
  251. }
  252. document.addEventListener('mousemove', onMouseMove);
  253. that.onwheel = wheel_rotate;
  254. that.onmouseup = function() {
  255. document.removeEventListener('mousemove', onMouseMove);
  256. // send_wses(myWs, that.orig, 'updateXY');
  257. that.style.zIndex = 500;
  258. that.onmouseup = null;
  259. that.onwheel = null;
  260. };
  261. };
  262.  
  263. function wheel_rotate(event) {
  264. this.orig.rota += event.deltaY / Math.abs(event.deltaY) * 15;
  265. this.style.transform = `rotate(${this.orig.rota}deg)`;
  266. }
  267.  
  268. function highlight_unit(event){
  269. that = this;
  270. highlight(that);
  271. for (c of field_con.fig_list) {
  272. if (c.bound_unit == that.bound_unit) {
  273. highlight(c);
  274. }
  275. }
  276. }
  277.  
  278. function highlight_unit_stop(event){
  279. that = this;
  280. highlight(that);
  281. for (c of field_con.fig_list) {
  282. if (c.bound_unit == that.bound_unit) {
  283. highlight_stop(c);
  284. }
  285. }
  286. }
  287.  
  288. function highlight(target) {
  289. target.classList.add('highlight');
  290. }
  291.  
  292. function highlight_stop(target) {
  293. target.classList.remove('highlight');
  294. }
  295.  
  296. // ----------------[Геометрические функции]----------------------
  297. function elips(a, b, x0, y0, r) {
  298. mas = []
  299. for (let c = 0 ; c<18; c++) {
  300. x = a * Math.cos(c * Math.PI / 9) + x0;
  301. y = b * Math.sin(c * Math.PI / 9) + y0;
  302. xy = rotation(x, y, x0, y0, r);
  303. mas[c] = xy;
  304. }
  305. return mas;
  306. }
  307.  
  308. function rotation(x, y, x0, y0, r) {
  309. let xr = (x - x0) * Math.cos(r * Math.PI / 180) - (y - y0) * Math.sin(r * Math.PI / 180) + x0;
  310. let yr = (x - x0) * Math.sin(r * Math.PI / 180) + (y - y0) * Math.cos(r * Math.PI / 180) + y0;
  311. let xy = {
  312. x: xr,
  313. y: yr
  314. }
  315. return xy;
  316. }
  317.  
  318. function make_aura(who, type, a, b = null) {
  319. if (b == null)
  320. b = a;
  321. cen_x = who.orig.x + who.offsetWidth / 2;
  322. cen_y = who.orig.y + who.offsetHeight / 2;
  323. who.aura = elips(a, b, cen_x, cen_y, who.orig.rota, true);
  324. }
  325.  
  326. function upd_aura(who, type, a, b, com = 'upd') {
  327. for (let c in who.aura) {
  328. who.aura[c].remove();
  329. }
  330. if (com != 'rem') {
  331. make_aura(who, type, a, b);
  332. for (let c in who.aura) {
  333. document.body.append(who.aura[c]);
  334. }
  335. }
  336. }
  337.  
  338. // ----------------[Обработка логики]----------------------
  339. function unitMover(targ) {
  340. game_con.initials = [];
  341. for (c of targ.figures) {
  342. c.onField.initialMovementX = c.onField.x_cord;
  343. c.onField.initialMovementY = c.onField.y_cord;
  344. console.log(c);
  345. game_con.initials[game_con.initials.length] = document.createElement('img');
  346. that = game_con.initials[game_con.initials.length-1]
  347. that.src = "http://127.0.0.1:8000/static/dot.png";
  348. that.style.position = 'absolute';
  349. that.style.left = c.onField.initialMovementX + 'px';
  350. that.style.top = c.onField.initialMovementY + 'px';
  351. that.whom = c;
  352. document.body.append(that);
  353. targ.mousedowner.lbm = dragndrop;
  354. }
  355. game_con.unitMoverActive = true;
  356. }
  357.  
  358. // ------------------[Дебажные хуйни]------------------------
  359. function dd_avral(event) { // (1) отследить нажатие
  360. ball = this;
  361. that.ondragstart = function() {return false;};
  362. ball.style.position = 'absolute';
  363. ball.style.zIndex = 1000;
  364. document.body.append(ball);
  365. moveAt(event.pageX, event.pageY);
  366. function moveAt(pageX, pageY) {
  367. ball.style.left = pageX - ball.offsetWidth / 2 + 'px';
  368. ball.style.top = pageY - ball.offsetHeight / 2 + 'px';
  369. }
  370. function onMouseMove(event) {
  371. moveAt(event.pageX, event.pageY);
  372. }
  373. document.addEventListener('mousemove', onMouseMove);
  374. ball.onmouseup = function() {
  375. document.removeEventListener('mousemove', onMouseMove);
  376. ball.onmouseup = null;
  377. };
  378. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement