]> git.eli173.com Git - hexmata/commitdiff
save/load, rotation, copy/paste
authorElijah Cohen <eli@eli173.com>
Fri, 1 Apr 2022 19:15:07 +0000 (14:15 -0500)
committerElijah Cohen <eli@eli173.com>
Fri, 1 Apr 2022 19:15:07 +0000 (14:15 -0500)
grid.js
index.html
play.js
wireworld.js

diff --git a/grid.js b/grid.js
index 69c88a3af329fc8f4a7cb049e2a0b80b7e690455..14e1940d9837a342f56c03d3a2f6299269292b2a 100644 (file)
--- a/grid.js
+++ b/grid.js
@@ -53,6 +53,21 @@ class Hex {
                                return {x: xi+xorig, y: yi+yorig}
                }
 
+
+
+               rotate(q,r, ccw=true) {
+                               // rotates (ccw default) about the q,r points given
+                               var retitem = this.clone();
+                               var trq = q-this.q;
+                               var trr = r-this.r;
+                               var trs = -trq-trr;
+                               var rq = ccw ? -trs : -trr;
+                               var rr = ccw ? -trq : -trs;
+                               retitem.q = q+rq;
+                               retitem.r = r+rr;
+                               return retitem;
+               }
+               
 }
 
 
index 944f861168ff3f3bb920a11f2949951a2e53d234..b7cf1c65d8b285c64ec24622e4151719fdc0e11c 100644 (file)
@@ -8,9 +8,15 @@
                <script>
                        function run() {
                                        lc();
-                                       console.log("hello");
+                                       //console.log("hello");
                        }
                </script>
+               <style>
+                       input[type=number] {
+                                       width: 30px;
+                                       margin-left: 15px;
+                       }
+               </style>
     <title>hww</title>
   </head>
   <body onload="run()" style="background-color: #f8f8ff;">
                        <input type="button" id="reset" value="reset"/>
                        <input type="button" id="es" value="export state"/>
                        <input type="button" id="is" value="import state"/>
+                       <input type="number" id="rad" value="5" min="1" max="30" step="1"/>
+                       <input type="button" id="rot" value="rotate hex"/>
+                       <input type="button" id="copy" value="copy hex"/>
+                       <input type="button" id="paste" value="paste hex"/>
+                       <input type="number" id="slot" value="1" min="1" max="9" step="1"/>
+                       <input type="button" id="save" value="save state"/>
+                       <input type="button" id="load" value="load state"/>
                        <details><summary></summary><span id="outzone"></span></details>
                </div>
     <div><canvas width="2000" height="2000" id="c"></canvas></div>
diff --git a/play.js b/play.js
index 7ff42971aa8a43d831f8bc4d346c21f39ec66bc7..6a86af5ca4a0d8483feb4a5e358cea8274ca63d4 100644 (file)
--- a/play.js
+++ b/play.js
@@ -2,30 +2,21 @@
 
 
 g = new WWGrid();
+copybuf = [];
 
 //params
 xcenter = 200
 ycenter = 200
 scale = 10
 
-g.change(WWState.OFF, 0,0);
-g.change(WWState.OFF, 0,1);
 g.change(WWState.OFF, 1,1);
-g.change(WWState.HEAD, -1,0);
-g.change(WWState.OFF, 1,-1);
+g.change(WWState.OFF, 1,4);
+g.change(WWState.TAIL, 4,1);
+g.change(WWState.EMPTY, 4,-2);
+g.change(WWState.HEAD, -2,4);
+g.change(WWState.HEAD, 1,-2);
+g.change(WWState.HEAD, -2,1);
 
-/*
-for(var i=-5; i<5; i++) {
-               for(var j=-5; j<5; j++) {
-                               g.change(WWState.EMPTY, i, j);
-               }
-}*/
-
-g.change(WWState.EMPTY, 0,0);
-g.change(WWState.HEAD, 1,0);
-g.change(WWState.HEAD, 2,0);
-g.change(WWState.TAIL, 0,1);
-g.change(WWState.TAIL, 0,2);
 
 var ctx = null;
 var c = null;
@@ -43,7 +34,7 @@ function lc() {
                c = document.getElementById('c');
                ctx = c.getContext('2d');
                //c.addEventListener('click', e=>getClickCoords(c, 20, 200, 200, e));
-               c.addEventListener('click', e=>wwonclick(c,ctx,g,xcenter,ycenter,scale,e));
+               c.onclick = e=>wwonclick(c,ctx,g,xcenter,ycenter,scale,e)
                c.oncontextmenu = (e) => {e.preventDefault(); wwonrtclick(c,ctx,g,xcenter,ycenter,scale,e)};
                drawGrid(g, ctx, xcenter,ycenter,scale);
                document.addEventListener('keydown', e=>{
@@ -66,22 +57,95 @@ function lc() {
                es.onclick = exportState;
                is = document.getElementById("is");
                is.onclick = importState;
+               rb = document.getElementById("rot");
+               rb.onclick = rotateButtonHandler;
+               cp = document.getElementById("copy");
+               cp.onclick = copyHandler;
+               ps = document.getElementById("paste");
+               ps.onclick = pasteHandler;
+               sv = document.getElementById("save");
+               sv.onclick = saveState;
+               ld = document.getElementById("load");
+               ld.onclick = loadState;
+}
+
+saveState = function(e) {
+               let slot = document.getElementById("slot").value;
+               let val = g.exportState();
+               localStorage.setItem(slot, val);
+}
+loadState = function(e) {
+               let slot = document.getElementById("slot").value;
+               let res = localStorage.getItem(slot);
+               if(res == null) {
+                               g.loadState("[]");
+                               drawGrid(g, ctx, xcenter,ycenter,scale);
+               }
+               g.loadState(res);
+               drawGrid(g, ctx, xcenter,ycenter,scale);
 }
 
+
 pptoggle = function(e) {
                if(intervalID == null) {
                                g.clean();
                                rstgrid = g.clone();
                                intervalID = setInterval(play, timeout);
-                               playbtn.value = "pause"
+                               playbtn.value = "pause";
                }
                else {
                                clearInterval(intervalID);
                                intervalID = null;
-                               playbtn.value = "play"
+                               playbtn.value = "play";
                }
 };
 
+rotateButtonHandler = function(e) {
+               c.onclick =  e=>rotateThingy(e);
+               // tf are those arguments globals are there for a reason
+               ctx.save();
+               ctx.fillStyle="rgba(0.4,0.4,0.4,0.1)"
+               ctx.fillRect(0,0,ctx.canvas.width,ctx.canvas.height);
+               ctx.restore();
+}
+rotateThingy = function(evt) {
+               let coords = getClickCoords(c,scale,xcenter,ycenter,evt);
+               let rad = parseInt(document.getElementById('rad').value);
+               g.rotateHex(coords.q,coords.r,rad);
+               drawGrid(g, ctx, xcenter,ycenter,scale);
+               c.onclick = e=>wwonclick(c,ctx,g,xcenter,ycenter,scale,e);
+}
+
+copyHandler = function(e) {
+               c.onclick = e=>copyhex(e);
+               ctx.save();
+               ctx.fillStyle="rgba(0.4,0.4,0.4,0.1)"
+               ctx.fillRect(0,0,ctx.canvas.width,ctx.canvas.height);
+               ctx.restore();
+}
+
+copyhex = function(evt) {
+               let coords = getClickCoords(c,scale,xcenter,ycenter,evt);
+               let rad = parseInt(document.getElementById('rad').value);
+               copybuf = g.copyHex(coords.q, coords.r, rad)
+               drawGrid(g, ctx, xcenter,ycenter,scale);
+               c.onclick = e=>wwonclick(c,ctx,g,xcenter,ycenter,scale,e);
+}
+pasteHandler = function(e) {
+               c.onclick = e=>pastehex(e);
+               ctx.save();
+               ctx.fillStyle="rgba(0.4,0.4,0.4,0.1)"
+               ctx.fillRect(0,0,ctx.canvas.width,ctx.canvas.height);
+               ctx.restore();
+}
+pastehex = function(evt) {
+               let coords = getClickCoords(c,scale,xcenter,ycenter,evt);
+               let rad = parseInt(document.getElementById('rad').value);
+               g.pasteHex(coords.q,coords.r,copybuf);
+               drawGrid(g, ctx, xcenter,ycenter,scale);
+               c.onclick = e=>wwonclick(c,ctx,g,xcenter,ycenter,scale,e);
+}
+
 exportState = function() {
                let s = g.exportState();
                deets = document.getElementById("outzone");
index 15ef28c20c0c490de4c6a588b002714dedc73cb5..bd8ceb96d83620e6651dc24957ba8ca6a9cddd6e 100644 (file)
@@ -44,7 +44,6 @@ class WWGrid {
                }
                exportState() {
                                this.clean();
-                               //console.log(JSON.stringify(this.cells));
                                return JSON.stringify(this.cells);
                }
                loadState(str) {
@@ -65,11 +64,16 @@ class WWGrid {
                                                cells[0].state = state;
                                }
                }
-               
-               stateAt(q,r) {
+               cellAt(q,r) {
                                let cells = this.cells.filter(c => c.q==q && c.r==r);
-                               if(cells.length == 0) return WWState.EMPTY;
-                               return cells[0].state;
+                               if(cells.length == 0) return new WWCell(WWState.EMPTY,q,r);
+                               return cells[0];
+               }
+               stateAt(q,r) {
+                               return this.cellAt(q,r).state;
+                               //let cells = this.cells.filter(c => c.q==q && c.r==r);
+                               //if(cells.length == 0) return WWState.EMPTY;
+                               //return cells[0].state;
                }
                
                step() {
@@ -113,9 +117,98 @@ class WWGrid {
                                // setup for an eventual implementation
                                // that said js is probs the worst language for that
                }
-
-
-
+               rotateGrid(q,r,ccw=true) {
+                               let cells2 = this.cells.map((c)=>c.rotate(q,r,ccw));
+                               this.cells = cells2;
+               }
+               rotateHex(q,r,radius,ccw=true) {
+                               var rcells = [this.cellAt(q,r)];
+                               for(let i=1; i<radius; i++) {
+                                               var currcell = this.cellAt(q,r+i);
+                                               for(let j=0; j<6; j++) {
+                                                               let dirv = null;
+                                                               switch(j) {
+                                                               case 0:
+                                                                               dirv = {q: 1, r:-1};
+                                                                               break;
+                                                               case 1:
+                                                                               dirv = {q: 0, r:-1};
+                                                                               break;
+                                                               case 2:
+                                                                               dirv = {q:-1, r: 0};
+                                                                               break;
+                                                               case 3:
+                                                                               dirv = {q:-1, r: 1};
+                                                                               break;
+                                                               case 4:
+                                                                               dirv = {q: 0, r: 1};
+                                                                               break;
+                                                               case 5:
+                                                                               dirv = {q: 1, r: 0};
+                                                                               break;
+                                                               }
+                                                               for(let k=0; k<i; k++) {
+                                                                               rcells.push(currcell);
+                                                                               currcell = this.cellAt(currcell.q + dirv.q, currcell.r + dirv.r);
+                                                               }
+                                               }                                                               
+                               }
+                               
+                               let newcells = rcells.map(x=>x.rotate(q,r));
+                               //this.cells = newcells;
+                               for(let i of newcells) {
+                                               this.change(i.state, i.q, i.r);
+                               }
+                               this.clean();
+               }
+               
+               copyHex(q,r,radius) {
+                               var first = this.cellAt(q,r).clone();
+                               first.q = 0;
+                               first.r = 0;
+                               var rcells = [];
+                               rcells.push(first);
+                               for(let i=1; i<radius; i++) {
+                                               var currcell = this.cellAt(q,r+i);
+                                               for(let j=0; j<6; j++) {
+                                                               let dirv = null;
+                                                               switch(j) {
+                                                               case 0:
+                                                                               dirv = {q: 1, r:-1};
+                                                                               break;
+                                                               case 1:
+                                                                               dirv = {q: 0, r:-1};
+                                                                               break;
+                                                               case 2:
+                                                                               dirv = {q:-1, r: 0};
+                                                                               break;
+                                                               case 3:
+                                                                               dirv = {q:-1, r: 1};
+                                                                               break;
+                                                               case 4:
+                                                                               dirv = {q: 0, r: 1};
+                                                                               break;
+                                                               case 5:
+                                                                               dirv = {q: 1, r: 0};
+                                                                               break;
+                                                               }
+                                                               for(let k=0; k<i; k++) {
+                                                                               let clone = currcell.clone()
+                                                                               clone.q = currcell.q - q;
+                                                                               clone.r = currcell.r - r;
+                                                                               rcells.push(clone);
+                                                                               currcell = this.cellAt(currcell.q + dirv.q, currcell.r + dirv.r);
+                                                               }
+                                               }                                                               
+                               }
+                               return rcells;
+               }
+               pasteHex(q,r,buf) {
+                               for(let i of buf) {
+                                               let cp = i.clone(); // so that one can paste multiple times
+                                               this.change(cp.state, cp.q+q, cp.r+r);
+                               }
+               }
 }