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;
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=>{
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");
}
exportState() {
this.clean();
- //console.log(JSON.stringify(this.cells));
return JSON.stringify(this.cells);
}
loadState(str) {
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() {
// 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);
+ }
+ }
}