[Engedélyezett] Térkép távolságmérő

öreg

Well-Known Member
A reakció pontszáma
1.721
Használata roppant egyszerű. Rákattintunk a térképen 2 falura, a script kiszámolja minden egységre lebontva a menetidőt és a térkép alatt egy táblázatba rakja.
Ha ismét rákattintunk 2 falura, kiszámolja azoknak is a menetidejét, nem kell frissíteni az oldalt és újra futtatni a scriptet.


Kód beillesztése:
javascript:
var obj = {world: {}};
function getConfigInformation() {
    return $.ajax({
        url: `https://${document.domain}/interface.php?func=get_config`,
        type: 'GET',
        async: true,
        success: function(xml) {
            world_speed = Number($(xml).find("config > speed").text());
            unit_speed = Number($(xml).find("config > unit_speed").text());
            Object.defineProperty(obj.world, "world_speed", {value:world_speed});
            Object.defineProperty(obj.world, "unit_speed", {value:unit_speed});
            console.log(obj.world.world_speed,obj.world.unit_speed);
        },
        error: function(xhr, statusText, error) {}
    })
}
getConfigInformation()

var array = [];
var unitSpeed = {
    spear    : 18 * 60,
    sword    : 22 * 60,
    axe      : 18 * 60,
    archer   : 18 * 60,
    spy      : 9  * 60,
    light    : 10 * 60,
    marcher  : 10 * 60,
    heavy    : 11 * 60,
    ram      : 30 * 60,
    catapult : 30 * 60,
    knight   : 10 * 60,
    snob     : 35 * 60
};

var game = window.image_base;
var imageSrc = {
        spear: game + "unit/unit_spear.png",
        sword: game + "unit/unit_sword.png",
        axe: game + "unit/unit_axe.png",
        archer: game + "unit/unit_archer.png",
        spy: game + "unit/unit_spy.png",
        light: game + "unit/unit_light.png",
        marcher: game + "unit/unit_marcher.png",
        heavy: game + "unit/unit_heavy.png",
        ram: game + "unit/unit_ram.png",
        catapult: game + "unit/unit_catapult.png",
        knight: game + "unit/unit_knight.png",
        snob: game + "unit/unit_snob.png",
};

function time(t) {
    var sec_num = Math.round(t / obj.world.world_speed / obj.world.unit_speed),
        hours   = Math.floor(sec_num / 3600),
        minutes = Math.floor((sec_num - (hours * 3600)) / 60),
        seconds = Math.round(sec_num - (hours * 3600) - (minutes * 60));
console.log(sec_num);
console.log(seconds);
    if (hours   < 10) {hours   =       hours;}
    if (minutes < 10) {minutes = "0"+minutes;}
    if (seconds < 10) {seconds = "0"+seconds;}
    return hours+':'+minutes+':'+seconds;
}

function sq(x) {
    return x * x;
}

function distance(x1, y1, x2, y2) {
    return Math.sqrt((sq(x2 - x1) + sq(y2 - y1)));
}

function handleClick(e) {
    var pos = this.coordByEvent(e);
    var x = pos[0];
    var y = pos[1];
    var a = x * 1000 + y;
    var village = TWMap.villages[a];
    if ((typeof village != "undefined")) {
        var clicks = $(this).data('clicks');
        if (!clicks) {
            console.log(TWMap.CoordByXY(village.xy), 1);
            x1 = TWMap.CoordByXY(village.xy)[0];
            y1 = TWMap.CoordByXY(village.xy)[1];
            array.push(x1, y1)
        } else {
            console.log(TWMap.CoordByXY(village.xy), 2);
            x2 = TWMap.CoordByXY(village.xy)[0];
            y2 = TWMap.CoordByXY(village.xy)[1];
            array.push(x2, y2);
            removetable();
            createTable();
            fillTable();
            array = [];
        }
        $(this).data("clicks", !clicks);
    }
    return false;
}

function fillTable() {
    for (const property in game_data.units) {
        if (game_data.units[property] != "militia") {
            seconds = unitSpeed[game_data.units[property]] * distance(array[0], array[1], array[2], array[3]);
            $('#table > tr:first-child').append(`
                <th width="25%" colspan="1"><img src="${imageSrc[game_data.units[property]]}"></th>
            `);
            $('#table > tr:nth-child(2)').append(`
                <td> ${time(seconds)} </td>
            `);
        }
    }
}

function createTable() {
    $('#map_legend').append(`
        <div id="commands_incomings" style="width: 100%" class="commands-container">
            <table id="unit_speed" class="vis" style="width: 50%; float: left;">
                <tbody id="table">
                    <tr></tr>
                    <tr></tr>
                </tbody>
            </table>
        </div>
    `);
}


function removetable() {
    $('#commands_incomings').remove();
}

TWMap.map._handleClick = handleClick;
void(0);
 
Utoljára szerkesztve:
Fent