From a21f81037c04b475d1184b7bd29af8eec0be5a91 Mon Sep 17 00:00:00 2001 From: cvoges12 Date: Tue, 18 Jan 2022 20:30:37 -0600 Subject: [PATCH] inventory demo + movement of items --- src/canvas.js | 142 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 103 insertions(+), 39 deletions(-) diff --git a/src/canvas.js b/src/canvas.js index 6aa7b8d..c2cde3b 100644 --- a/src/canvas.js +++ b/src/canvas.js @@ -1,12 +1,21 @@ var canvas = document.querySelector('canvas'); - -canvas.width = window.innerWidth; -canvas.height = window.innerHeight; - - - var c = canvas.getContext('2d'); +window.addEventListener('load', () => { + resize(); +}) + +window.addEventListener('resize', () => { + resize(); +}); + +function resize(){ + canvas.height = window.innerHeight; + canvas.width = window.innerWidth; +} + + + // Rect //c.fillStyle = 'rgba(255,0,255,0.5)'; //c.fillRect(100,200,100,100,0.5); @@ -68,41 +77,96 @@ function coil(x,y, width,od){ c.stroke(); } -for (var i=40; i < (12*40); i=i+40) { - for (var j=50; j < (12*50); j=j+50) { - coil(j,i, 20 + (5 * Math.random()),30 + (5 * Math.random())); +const coils = { + xs: [], + ys: [], + widths: [], + ods: [] +}; + +const field = { + xi: 0, + xf: 12, + yi: 0, + yf: 12, +} + +const initCoil = { + x: 40, + y: 50, + width: 20, + od: 30, +} + +function initCoils(obj, f, r){ + for (var y=f.yi; y { -// const canvas = document.querySelector('#canvas'); -// const ctx = canvas.getContext("2d"); -// -// reloadEvent(canvas, ctx); -//}); -// -//function reloadEvent(x, y){ -// window.addEventListener('resize', () => { -// resize(x, y); -// }); -//} -// -//function draw(x) { -// x.strokeStyle = 'blue'; -// x.lineWidth = '5'; -// x.strokeRect(0, 0, window.innerWidth, window.innerHeight); -//} -// -//function resize(x, y){ -// x.height = window.innerHeight; -// x.width = window.innerWidth; -// draw(y); -//} +function drawZone(x, y, row, length){ + c.strokeRect( + initCoil.x*(x+1) - 5, + initCoil.y*(y+1) - 5, + initCoil.x*row - 5, + initCoil.y*length - 5); +} + +function drawZones(){ + drawZone(0,0,6,6); + drawZone(6,0,6,6); + drawZone(0,6,6,6); + drawZone(6,6,6,6); +} + +function move(i, xf, yf){ + if (coils.xs[i] > initCoil.x*(xf+1)) coils.xs[i]--; + if (coils.xs[i] < initCoil.x*(xf+1)) coils.xs[i]++; + if (coils.ys[i] > initCoil.y*(yf+1)) coils.ys[i]--; + if (coils.ys[i] < initCoil.y*(yf+1)) coils.ys[i]++; +} + +function movements(){ + move(0,2,13); + if ((coils.xs[0] == initCoil.x*(2+1)) + && (coils.ys[0] == initCoil.y*(13+1))) { + move(2,0,0); + if ((coils.xs[2] == initCoil.x*(0+1)) + && (coils.ys[2] == initCoil.y*(0+1))) { + move(0,2,0); + } + } +} + +function animate(){ + requestAnimationFrame(animate); + c.clearRect(0,0,innerWidth,innerHeight); + + drawCoils(); + drawZones(); + + movements(); + //move(0,2,0); +} + +animate();