Parcourir la source

new algo for grass to beach

Johannes Zellner il y a 11 ans
Parent
commit
19007c07aa
1 fichiers modifiés avec 153 ajouts et 5 suppressions
  1. 153 5
      index.js

+ 153 - 5
index.js

@@ -71,13 +71,161 @@ function setupWindow() {
     window.addEventListener('resize', resize, false);
 }
 
+function getMapTile(pos) {
+    if (!gMap[pos.y]) return null;
+    if (!gMap[pos.y][pos.x]) return null;
+    return gMap[pos.y][pos.x];
+}
+
 function setMapTile(pos, tile) {
-    if (tile.name === 'beach') {
-        if (gMap[pos.y-1][pos.x].name === 'grass') gMap[pos.y][pos.x] = new Tile('grass-beach', 'straight', 45);
-        else  gMap[pos.y][pos.x] = tile;
+    if (!tile) return;
+    if (!gMap[pos.y]) return;
+    if (!gMap[pos.y][pos.x]) return;
+    gMap[pos.y][pos.x] = tile;
+}
+
+function calculateMapTile(pos) {
+    var tile = getMapTile(pos);
+
+    // top row
+    var tl = getMapTile({ y: pos.y-1, x: pos.x-1 });
+    var to = getMapTile({ y: pos.y-1, x: pos.x });
+    var tr = getMapTile({ y: pos.y-1, x: pos.x+1 });
+
+    // middle row
+    var le = getMapTile({ y: pos.y, x: pos.x-1 });
+    var re = getMapTile({ y: pos.y, x: pos.x+1 });
+
+    // bottom row
+    var bl = getMapTile({ y: pos.y+1, x: pos.x-1 });
+    var bo = getMapTile({ y: pos.y+1, x: pos.x });
+    var br = getMapTile({ y: pos.y+1, x: pos.x+1 });
+
+    // top edge
+    var toe;
+    if (!to) {
+        toe = '';
+    } else if (to.name === 'grass' || to.name === 'beach') {
+        toe = to.name;
+    } else if (to.name === 'grass-beach' && to.type === 'straight') {
+        if (to.orientation === 225) toe = 'grass';
+        else toe = 'beach';
+    } else if (to.name === 'grass-beach' && to.type === 'curve_in') {
+        if (to.orientation === 45 || to.orientation === 135) toe = 'beach';
+        else toe = 'grass';
     } else {
-        gMap[pos.y][pos.x] = tile;
+        toe = to.name;
     }
+
+    // bottom edge
+    var boe;
+    if (!bo) {
+        boe = '';
+    } else if (bo.name === 'grass' || bo.name === 'beach') {
+        boe = bo.name;
+    } else if (bo.name === 'grass-beach' && bo.type === 'straight') {
+        if (bo.orientation === 45) boe = 'grass';
+        else boe = 'beach';
+    } else if (bo.name === 'grass-beach' && bo.type === 'curve_in') {
+        if (bo.orientation === 45 || bo.orientation === 135) boe = 'grass';
+        else boe = 'beach';
+    } else {
+        boe = bo.name;
+    }
+
+    // left edge
+    var lee;
+    if (!le) {
+        lee = '';
+    } else if (le.name === 'grass' || le.name === 'beach') {
+        lee = le.name;
+    } else if (le.name === 'grass-beach' && le.type === 'straight') {
+        if (le.orientation === 315) lee = 'grass';
+        else lee = 'beach';
+    } else if (le.name === 'grass-beach' && le.type === 'curve_in') {
+        if (le.orientation === 45 || le.orientation === 315) lee = 'grass';
+        else lee = 'beach';
+    } else {
+        lee = le.name;
+    }
+
+    // right edge
+    var ree;
+    if (!re) {
+        ree = '';
+    } else if (re.name === 'grass' || re.name === 'beach') {
+        ree = re.name;
+    } else if (re.name === 'grass-beach' && re.type === 'straight') {
+        if (re.orientation === 135) ree = 'grass';
+        else ree = 'beach';
+    } else if (re.name === 'grass-beach' && re.type === 'curve_in') {
+        if (re.orientation === 45 || re.orientation === 315) ree = 'beach';
+        else ree = 'grass';
+    } else {
+        ree = re.name;
+    }
+
+    console.log(pos, toe, boe, lee, ree);
+
+    if (toe === 'grass' && lee === 'grass' && boe === 'grass' && ree === 'grass') tile = new Tile('grass', 'straight', 45);
+    if (toe === 'beach' && lee === 'beach' && boe === 'beach' && ree === 'beach') tile = new Tile('beach', 'straight', 45);
+
+    if (toe === 'beach' && lee === 'grass' && boe === 'grass' && ree === 'grass') tile = new Tile('grass-beach', 'straight', 225);
+    if (toe === 'beach' && lee === 'beach' && boe === 'grass' && ree === 'beach') tile = new Tile('grass-beach', 'straight', 225);
+    if (toe === 'grass' && lee === 'beach' && boe === 'grass' && ree === 'grass') tile = new Tile('grass-beach', 'straight', 315);
+    if (toe === 'grass' && lee === 'grass' && boe === 'beach' && ree === 'grass') tile = new Tile('grass-beach', 'straight', 45);
+    if (toe === 'grass' && lee === 'beach' && boe === 'beach' && ree === 'beach') tile = new Tile('grass-beach', 'straight', 45);
+    if (toe === 'grass' && lee === 'grass' && boe === 'grass' && ree === 'beach') tile = new Tile('grass-beach', 'straight', 135);
+    if (toe === 'beach' && lee === 'grass' && boe === 'beach' && ree === 'beach') tile = new Tile('grass-beach', 'straight', 135);
+    if (toe === 'beach' && lee === 'beach' && boe === 'beach' && ree === 'grass') tile = new Tile('grass-beach', 'straight', 315);
+
+    if (toe === 'grass' && lee === 'grass' && boe === 'beach' && ree === 'beach') tile = new Tile('grass-beach', 'curve_in', 135);
+    if (toe === 'grass' && lee === 'beach' && boe === 'grass' && ree === 'beach') tile = new Tile('grass-beach', 'curve_in', 45);
+    if (toe === 'grass' && lee === 'beach' && boe === 'beach' && ree === 'grass') tile = new Tile('grass-beach', 'curve_in', 45);
+    if (toe === 'beach' && lee === 'beach' && boe === 'grass' && ree === 'grass') tile = new Tile('grass-beach', 'curve_in', 315);
+    if (toe === 'beach' && lee === 'grass' && boe === 'grass' && ree === 'beach') tile = new Tile('grass-beach', 'curve_in', 225);
+
+    return tile;
+}
+
+function resetMapTile(pos) {
+    setMapTile(pos, calculateMapTile(pos));
+}
+
+function putMapTile(pos, tile) {
+    setMapTile(pos, tile);
+
+    // Iteration one!
+
+    // cross
+    resetMapTile({ y: pos.y-1, x: pos.x });
+    resetMapTile({ y: pos.y, x: pos.x-1 });
+    resetMapTile({ y: pos.y, x: pos.x+1 });
+    resetMapTile({ y: pos.y+1, x: pos.x });
+
+    // top corners
+    resetMapTile({ y: pos.y-1, x: pos.x-1 });
+    resetMapTile({ y: pos.y-1, x: pos.x+1 });
+
+    // bottom corners
+    resetMapTile({ y: pos.y+1, x: pos.x-1 });
+    resetMapTile({ y: pos.y+1, x: pos.x+1 });
+
+    // Iteration two!
+
+    // cross
+    resetMapTile({ y: pos.y-1, x: pos.x });
+    resetMapTile({ y: pos.y, x: pos.x-1 });
+    resetMapTile({ y: pos.y, x: pos.x+1 });
+    resetMapTile({ y: pos.y+1, x: pos.x });
+
+    // top corners
+    resetMapTile({ y: pos.y-1, x: pos.x-1 });
+    resetMapTile({ y: pos.y-1, x: pos.x+1 });
+
+    // bottom corners
+    resetMapTile({ y: pos.y+1, x: pos.x-1 });
+    resetMapTile({ y: pos.y+1, x: pos.x+1 });
 }
 
 function setupCanvas() {
@@ -104,7 +252,7 @@ function setupCanvas() {
             gMouseDrag = { x: gMousePos.x, y: gMousePos.y };
             gCamera.drag = { x: gCamera.x, y: gCamera.y };
         } else if (e.button === 0 && gTool) {
-            setMapTile(gMapHover, gTool.tile);
+            putMapTile(gMapHover, gTool.tile);
         }
     }, false);
     gMapCanvas.addEventListener('mouseup', function () {