Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var mapSize = 50;
- var map = [];
- map[0] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
- map[1] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1];
- map[2] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1];
- map[3] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1];
- map[4] = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1];
- map[5] = [1, 3, 3, 3, 0, 0, 0, 0, 0, 1];
- map[6] = [1, 3, 2, 3, 0, 0, 0, 0, 0, 1];
- map[7] = [1, 3, 3, 3, 0, 0, 0, 0, 0, 1];
- map[8] = [1, 3, 3, 3, 0, 0, 0, 0, 0, 1];
- map[9] = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1];
- var elements = [];
- elements[0] = 'space';
- elements[1] = 'wall';
- elements[2] = 'link right';
- elements[3] = 'enemy';
- var directions = [];
- directions[0] = 'top';
- directions[1] = 'left';
- directions[2] = 'bottom';
- directions[3] = 'right';
- var player = {
- x: null,
- y: null,
- lifepoints: 3
- };
- function printElement(value, y, x) {
- var linkSize = '';
- switch (value) {
- case 2:
- player.x = x;
- player.y = y;
- linkSize = 'background-size: ' + mapSize * 3 + 'px; ';
- break;
- }
- var html = '<div style="' + linkSize + 'width: ' + mapSize + 'px; height: ' + mapSize + 'px;" data-x="' + x + '" data-y="' + y + '" class="bloc ' + elements[value] + '"></div>';
- $('#map').append(html);
- }
- function initMap() {
- $.each(map, function(kMap, vMap) {
- $.each(vMap, function(kLine, vLine) {
- printElement(vLine, kMap, kLine);
- });
- $('#map').append('<div></div>');
- });
- }
- function moveElement(coord, className, direction) {
- var allDirections = 'top left right bottom';
- switch (direction) {
- case 'top':
- if (!$('div[data-x="' + coord.x + '"][data-y="' + (coord.y-1) + '"]').hasClass('wall')) {
- if ('enemy' === className && $('div[data-x="' + coord.x + '"][data-y="' + (coord.y-1) + '"]').hasClass('enemy')) {
- return coord;
- }
- if (
- 'enemy' === className && $('div[data-x="' + coord.x + '"][data-y="' + (coord.y-1) + '"]').hasClass('link') ||
- 'link' === className && $('div[data-x="' + coord.x + '"][data-y="' + (coord.y-1) + '"]').hasClass('enemy')
- ) {
- player.lifepoints--;
- $('div[data-x="' + coord.x + '"][data-y="' + (coord.y-1) + '"]').removeClass('enemy');
- }
- $('div[data-x="' + coord.x + '"][data-y="' + coord.y + '"]').removeClass(allDirections + ' ' + className).addClass('space');
- coord.y--;
- }
- break;
- case 'left':
- if (!$('div[data-x="' + (coord.x-1) + '"][data-y="' + coord.y + '"]').hasClass('wall')) {
- if ('enemy' === className && $('div[data-x="' + (coord.x-1) + '"][data-y="' + coord.y + '"]').hasClass('enemy')) {
- return coord;
- }
- if (
- 'enemy' === className && $('div[data-x="' + (coord.x-1) + '"][data-y="' + coord.y + '"]').hasClass('link') ||
- 'link' === className && $('div[data-x="' + (coord.x-1) + '"][data-y="' + coord.y + '"]').hasClass('enemy')
- ) {
- player.lifepoints--;
- $('div[data-x="' + (coord.x-1) + '"][data-y="' + coord.y + '"]').removeClass('enemy');
- }
- $('div[data-x="' + coord.x + '"][data-y="' + coord.y + '"]').removeClass(allDirections + ' ' + className).addClass('space');
- coord.x--;
- }
- break;
- case 'bottom':
- if (!$('div[data-x="' + coord.x + '"][data-y="' + (coord.y+1) + '"]').hasClass('wall')) {
- if ('enemy' === className && $('div[data-x="' + coord.x + '"][data-y="' + (coord.y+1) + '"]').hasClass('enemy')) {
- return coord;
- }
- if (
- 'enemy' === className && $('div[data-x="' + coord.x + '"][data-y="' + (coord.y+1) + '"]').hasClass('link') ||
- 'link' === className && $('div[data-x="' + coord.x + '"][data-y="' + (coord.y+1) + '"]').hasClass('enemy')
- ) {
- player.lifepoints--;
- $('div[data-x="' + coord.x + '"][data-y="' + (coord.y+1) + '"]').removeClass('enemy');
- }
- $('div[data-x="' + coord.x + '"][data-y="' + coord.y + '"]').removeClass(allDirections + ' ' + className).addClass('space');
- coord.y++;
- }
- break;
- case 'right':
- if (!$('div[data-x="' + (coord.x+1) + '"][data-y="' + coord.y + '"]').hasClass('wall')) {
- if ('enemy' === className && $('div[data-x="' + (coord.x+1) + '"][data-y="' + coord.y + '"]').hasClass('enemy')) {
- return coord;
- }
- if (
- 'enemy' === className && $('div[data-x="' + (coord.x+1) + '"][data-y="' + coord.y + '"]').hasClass('link') ||
- 'link' === className && $('div[data-x="' + (coord.x+1) + '"][data-y="' + coord.y + '"]').hasClass('enemy')
- ) {
- player.lifepoints--;
- $('div[data-x="' + (coord.x+1) + '"][data-y="' + coord.y + '"]').removeClass('enemy');
- }
- $('div[data-x="' + coord.x + '"][data-y="' + coord.y + '"]').removeClass(allDirections + ' ' + className).addClass('space');
- coord.x++;
- }
- break;
- }
- $('div[data-x="' + coord.x + '"][data-y="' + coord.y + '"]').addClass(className + ' ' + direction);
- if (0 === player.lifepoints) {
- $('body').html('LOOSER');
- }
- return coord;
- }
- function initPlayer() {
- $('body').keypress(function(event) {
- var link = $('.link');
- var direction = null;
- switch (event.which) {
- case 122:
- //top
- direction = 'top';
- break;
- case 113:
- //left
- direction = 'left';
- break;
- case 115:
- //bottom
- direction = 'bottom';
- break;
- case 100:
- //right
- direction = 'right';
- break;
- default:
- return false;
- }
- player = moveElement(player, 'link', direction);
- });
- }
- function initEnemies() {
- setInterval(function() {
- $('.enemy').each(function() {
- moveElement({x: $(this).data('x'), y: $(this).data('y')}, 'enemy', directions[Math.floor((Math.random() * 4) + 0)]);
- });
- }, 2000);
- }
- $(document).ready(function(){
- initMap();
- initPlayer();
- initEnemies();
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement