Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- enum smer { nic, hore, dole, vpravo, vlavo };
- enum bloky { volno, stena, start, ciel }
- class Bludisko {
- private mapa: bloky[][];
- private poziciaX: number;
- private poziciaY: number;
- private mozemIst: boolean;
- private poslednySmer: smer;
- constructor(mojLevel: bloky[][]) {
- this.mapa = mojLevel;
- }
- private dajCoJeNaSuradnici(x: number, y: number): bloky {
- if (y < 0 || y >= this.mapa.length) { return bloky.stena; }
- if (x < 0 || x >= this.mapa[y].length) { return bloky.stena; }
- return this.mapa[y][x];
- }
- private somNaKonci(): boolean {
- return this.dajCoJeNaSuradnici(this.poziciaX, this.poziciaY) == bloky.ciel;
- }
- private najdiStart() {
- this.poziciaX = 0;
- this.poziciaY = 0;
- for (let y = 0; y < this.mapa.length; y++) {
- for (let x = 0; x < this.mapa[y].length; x++) {
- if (this.dajCoJeNaSuradnici(x, y) == bloky.start) {
- this.poziciaX = x;
- this.poziciaY = y;
- return;
- }
- }
- }
- }
- public start() {
- this.najdiStart();
- this.odblokujPohyb();
- }
- private mozemIstHore(): boolean {
- return this.dajCoJeNaSuradnici(
- this.poziciaX,
- this.poziciaY - 1
- ) != 1;
- }
- private mozemIstDole(): boolean {
- return this.dajCoJeNaSuradnici(
- this.poziciaX,
- this.poziciaY + 1
- ) != 1;
- }
- private mozemIstVPravo(): boolean {
- return this.dajCoJeNaSuradnici(
- this.poziciaX + 1,
- this.poziciaY
- ) != 1;
- }
- private mozemIstVLavo(): boolean {
- return this.dajCoJeNaSuradnici(
- this.poziciaX - 1,
- this.poziciaY
- ) != 1;
- }
- private kresliStenuHore() {
- if (this.mozemIstHore()) {
- led.unplot(1, 0)
- led.unplot(2, 0)
- led.unplot(3, 0)
- } else {
- led.plot(1, 0)
- led.plot(2, 0)
- led.plot(3, 0)
- }
- }
- private kresliStenuDole() {
- if (this.mozemIstDole()) {
- led.unplot(1, 4)
- led.unplot(2, 4)
- led.unplot(3, 4)
- } else {
- led.plot(1, 4)
- led.plot(2, 4)
- led.plot(3, 4)
- }
- }
- private kresliStenuVPravo() {
- if (this.mozemIstVPravo()) {
- led.unplot(4, 1)
- led.unplot(4, 2)
- led.unplot(4, 3)
- } else {
- led.plot(4, 1)
- led.plot(4, 2)
- led.plot(4, 3)
- }
- }
- private kresliStenuVLavo() {
- if (this.mozemIstVLavo()) {
- led.unplot(0, 1)
- led.unplot(0, 2)
- led.unplot(0, 3)
- } else {
- led.plot(0, 1)
- led.plot(0, 2)
- led.plot(0, 3)
- }
- }
- private vykresliSipku() {
- switch (this.poslednySmer) {
- case smer.hore:
- led.plot(2, 1)
- led.plot(1, 2)
- led.plot(3, 2)
- led.unplot(2, 3)
- break;
- case smer.dole:
- led.plot(2, 3)
- led.plot(1, 2)
- led.plot(3, 2)
- led.unplot(2, 1)
- break;
- case smer.vpravo:
- led.plot(2, 3)
- led.plot(2, 1)
- led.plot(3, 2)
- led.unplot(1, 2)
- break;
- case smer.vlavo:
- led.plot(2, 3)
- led.plot(2, 1)
- led.plot(1, 2)
- led.unplot(3, 2)
- break;
- default:
- led.unplot(2, 1)
- led.unplot(1, 2)
- led.unplot(3, 2)
- led.unplot(2, 3)
- break;
- }
- }
- public vykresliBludisko() {
- if (this.somNaKonci()) {
- basic.showIcon(IconNames.Diamond);
- return;
- }
- led.plot(0, 0);
- led.plot(0, 4);
- led.plot(4, 0);
- led.plot(4, 4);
- this.kresliStenuHore();
- this.kresliStenuDole();
- this.kresliStenuVPravo();
- this.kresliStenuVLavo();
- this.vykresliSipku();
- }
- public chodHore() {
- if (this.somNaKonci()) { return; }
- if (!this.mozemIst) { return; }
- if (!this.mozemIstHore()) { return; }
- this.poziciaY--;
- this.mozemIst = false;
- this.poslednySmer = smer.hore;
- }
- public chodDole() {
- if (this.somNaKonci()) { return; }
- if (!this.mozemIst) { return; }
- if (!this.mozemIstDole()) { return; }
- this.poziciaY++;
- this.mozemIst = false;
- this.poslednySmer = smer.dole;
- }
- public chodVPravo() {
- if (this.somNaKonci()) { return; }
- if (!this.mozemIst) { return; }
- if (!this.mozemIstVPravo()) { return; }
- this.poziciaX++;
- this.mozemIst = false;
- this.poslednySmer = smer.vpravo;
- }
- public chodVLavo() {
- if (this.somNaKonci()) { return; }
- if (!this.mozemIst) { return; }
- if (!this.mozemIstVLavo()) { return; }
- this.poziciaX--;
- this.mozemIst = false;
- this.poslednySmer = smer.vlavo;
- }
- public odblokujPohyb() {
- this.mozemIst = true;
- this.poslednySmer = smer.nic;
- }
- }
- let level: bloky[][] = [
- [1, 1, 1, 1, 1, 1, 1, 1],
- [1, 2, 0, 0, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
- [1, 3, 0, 0, 0, 0, 0, 1],
- [1, 1, 1, 1, 1, 1, 1, 1]
- ];
- let bludisko = new Bludisko(level);
- bludisko.start();
- basic.forever(function () {
- bludisko.vykresliBludisko();
- })
- input.onGesture(Gesture.LogoDown, function () {
- bludisko.chodHore();
- })
- input.onGesture(Gesture.LogoUp, function () {
- bludisko.chodDole();
- })
- input.onGesture(Gesture.TiltRight, function () {
- bludisko.chodVPravo();
- })
- input.onGesture(Gesture.TiltLeft, function () {
- bludisko.chodVLavo();
- })
- input.onGesture(Gesture.ScreenUp, function () {
- bludisko.odblokujPohyb();
- })
- input.onButtonPressed(Button.A, function () {
- bludisko.start();
- })
Add Comment
Please, Sign In to add comment