From 7ea0001618ea58d3af46229576d767ef34e08ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wojciech=20=C5=81ukasik?= Date: Mon, 6 Apr 2020 20:09:50 +0200 Subject: [PATCH] poprawa poruszania --- frontend/script.js | 72 ++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 25 deletions(-) diff --git a/frontend/script.js b/frontend/script.js index 061c18e..29cbec8 100644 --- a/frontend/script.js +++ b/frontend/script.js @@ -5,9 +5,25 @@ class Agent { this.yField = y; } - getid(){ + getId(){ return this.xField + "-" + this.yField; } + + right(){ + this.yField++; + } + + left(){ + this.yField--; + } + + up(){ + this.xField--; + } + + down(){ + this.xField++; + } } class Field { @@ -61,14 +77,14 @@ class Shelf { //Objekty i zmienne var regaly = ['1-1', '1-3', '1-4', '1-6', '1-7', '1-9', '2-1', '2-9', '3-3', '3-4', '3-6', '3-7', '4-1', '4-9', '5-1', '5-3', '5-4', '5-6', '5-7', '5-9'] -const agent = new Agent(3, 0); +const agent1 = new Agent(3, 0); //Funkcja uruchamiajÄ…ca prace calego scriptu function start(){ - ponumerujPola(); + //ponumerujPola(); pokolorujRegaly(); - umiescAgenta(); - droga(); + umiescAgenta(agent1); + droga(agent1); } //Funkcja kolorujaca miejsca na planszy gdzie znajduja sie regaly @@ -90,37 +106,43 @@ function ponumerujPola(){ } } +//Funkcja usuwajÄ…ca agenta z pola, przed przemieszczeniem +function usunAgenta(agent){ + document.getElementById(agent.getId()).style.backgroundImage = "none"; +} + + //Funckja wyswietlajaca agenta gdy ten zmieni polozenie -function umiescAgenta() { - document.getElementById(id).style.backgroundImage = "none"; - id = agent.getid(); - document.getElementById(id).style.backgroundImage = "url('Agent.jpg')"; +function umiescAgenta(agent) { + document.getElementById(agent.getId()).style.backgroundImage = "url('Agent.jpg')"; } //Funkcja zmieniajaca polozenie agenta o 1 pole -function przemieszczenie(side, time){ +function przemieszczenie(side, time, agent){ + const lastPosition = agent.getId(); setTimeout(function(){ + usunAgenta(agent) if(side == "left") - agent.yField--; + agent.left(); if(side == "right") - agent.yField++; + agent.right(); if(side == "up") - agent.xField--; + agent.up(); if(side == "down") - agent.xField++; - umiescAgenta(); + agent.down(); + umiescAgenta(agent); }, time); } //Funkcja ktora ustala droge agenta do przebycia -function droga() { - przemieszczenie("right", 1000); - przemieszczenie("right", 2000); - przemieszczenie("up", 3000); - przemieszczenie("right", 4000); - przemieszczenie("right", 5000); - przemieszczenie("right", 6000); - przemieszczenie("right", 7000); - przemieszczenie("right", 8000); - przemieszczenie("right", 9000); +function droga(agent) { + przemieszczenie("right", 1000, agent); + przemieszczenie("right", 2000, agent); + przemieszczenie("up", 3000, agent); + przemieszczenie("right", 4000, agent); + przemieszczenie("right", 5000, agent); + przemieszczenie("right", 6000, agent); + przemieszczenie("right", 7000, agent); + przemieszczenie("right", 8000, agent); + przemieszczenie("right", 9000, agent); }