cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <div class="preload">
	<img src="http://irresponsibleart.com/wp-content/uploads/2014/05/east.png">
	<img src="http://irresponsibleart.com/wp-content/uploads/2014/05/west.png">
	<img src="http://irresponsibleart.com/wp-content/uploads/2014/05/south.png">
	<img src="http://irresponsibleart.com/wp-content/uploads/2014/05/north.png">
</div>
<div id="html5Game" class="layer">
	<div class="layer grid">
    
  </div>
  <div class="layer character">
		<a id="claptrap"
       href="#claptrap"
       class="south"
       data-row="5"
       data-col="5"
       data-dest-row="5"
       data-dest-col="5"><span class="sprite"></span></a>
	</div>
</div>


            
          
!
            
              $grid-size : 50 ;

body {
  background-color: #232e64;
}
.preload {
  overflow: hidden;
  height: 1px;
  width:1px;
  position: absolute;
  
}

.layer {
  position: absolute;
  width:  $grid-size * 11px;
  height: $grid-size * 11px;
}
#html5Game {
	position: relative;
	margin: auto;
	border-top: 1px solid #000000;
	border-left: 1px solid #000000;
}

.character {
	z-index: 100;
	pointer-events: none;
}

.grid {
	z-index: 1;
}

.tile {
	position: absolute;
	display:block;
	width: $grid-size - 1px;
	height: $grid-size - 1px;
	text-decoration: none;
	border-bottom: 1px solid rgba(0,0,0,0.6);
	border-right: 1px solid rgba(0,0,0,0.6);
	background: transparent url(http://irresponsibleart.com/wp-content/uploads/2014/05/tiles.png) 0 0 no-repeat;
	cursor: default;
  
  &[data-cost="1"].type-1 {
	  background-position: 0 0;
  }
  
  &[data-cost="1"].type-2 {
    background-position: $grid-size * -1px 0;
  }
  
  &[data-cost="1"].type-3 {
    background-position: $grid-size * -2px 0;
  }
  
  &[data-cost="2"].type-1{
    background-position: $grid-size * -3px 0;
  }
  
  &[data-cost="2"].type-2{
    background-position: $grid-size * -4px 0;
  }

  &[data-cost="2"].type-3{
    background-position: $grid-size * -5px 0;
  }
  
  &.can-move-to:before {
    content: "";
    position: absolute;
    display:block;
    width: $grid-size - 1px;
    height: $grid-size - 1px;
    top: 0;
    left: 0;
    z-index: 1;
    background-color: rgba( 65, 94, 195 , 0.4 );
    cursor: pointer;
  }
  
  &.can-move-to:hover:before {
    content: "";
    position: absolute;
    display:block;
    width: $grid-size - 1px;
    height: $grid-size - 1px;
    top: 0;
    left: 0;
    z-index: 1;
    cursor: pointer;
    background-color: rgba( 65, 94, 195 , 0.8 );
  }
  
  &.can-attack:before {
    content: "";
    position: absolute;
    display:block;
    width: $grid-size - 1px;
    height: $grid-size - 1px;
    top: 0;
    left: 0;
    z-index: 1;
    cursor: pointer;
    background-color: rgba( 255,169,0 , 0.4 );
  }
  
  &.enemy-can-attack {
    background: rgba( 244, 5, 5 , 0.6 );
  }

}

#claptrap {
	position: absolute;
	display:block;
	width: $grid-size * 1px;
	height: $grid-size * 1px;
	text-decoration: none;
	pointer-events: auto !important;
	transition: all .4s linear;
  
  .sprite {
    display: block;
    position: relative;
    width: 80px;
    height: 80px;
    overflow: hidden;
    top: -30px;
    left: -15px;
  }
}

@for $i from 0 through 10 {
  [data-col="#{$i}"]  { left: $grid-size * $i * 1px; }
}

@for $i from 0 through 10 {
  [data-row="#{$i}"]  { top: $grid-size * $i * 1px; }
}
// HERE BEGINS THE SPRITES!!!!
/* ******************************************** EAST */
.east .sprite {
	background: transparent url(http://irresponsibleart.com/wp-content/uploads/2014/05/east.png) 0 0 no-repeat;
}

.east.moving .sprite {
  animation: east-walk .4s steps(39) infinite;
}

@keyframes east-walk {
	from { background-position:   -80px; }
	to { background-position: -3200px; }
}

/* ******************************************** SOUTH */
.south .sprite {
	background: transparent url(http://irresponsibleart.com/wp-content/uploads/2014/05/south.png) 0 0 no-repeat;
}

.south.moving .sprite {
	animation: south-walk .8s steps(39) infinite;
}

@keyframes south-walk {
	from { background-position:   -80px; }
	to { background-position: -3200px; }
}
/* ******************************************** WEST */
.west .sprite {
	background: transparent url(http://irresponsibleart.com/wp-content/uploads/2014/05/west.png) 0 0 no-repeat;
}

.west.moving .sprite {
	animation: west-walk .8s steps(39) infinite;
}

@keyframes west-walk {
	from { background-position:   -80px; }
	to { background-position: -3200px; }
}

.north .sprite {
	background: transparent url(http://irresponsibleart.com/wp-content/uploads/2014/05/north.png) 0 0 no-repeat;
}

.north.moving .sprite {
	animation: north-walk .8s steps(39) infinite;
}

@keyframes north-walk {
	from { background-position:   -80px; }
	to { background-position: -3200px; }
}

            
          
!
            
              $(function(){
	"use strict";
  
	var area = 11 ,
    $grid = $(".grid"),
		grid = [[],[],[],[],[],[],[],[],[],[],[]],
		movement = 5,
		attack = 1,
		claptrap = $( "#claptrap" ) ,
		GPoint = function( row , col, cost ){
			this.row = row ;
			this.col = col ;
			this.terrain = cost ;
			this.cost = -1 ;
		};

	for(var i = 0; i < area; i++ ){
		for(var j = 0; j < area; j++ ){
      var $html = $('<a href="#" class="tile" data-cost="1"></a>').attr('data-row', i ).attr('data-col', j);
			grid[ i ][ j ] = new GPoint( i, j, 1 );
      $grid.append($html);
		}
	}

	$(".tile" ).each( function( ){
		$( this ).attr( 'data-cost' , getRandomInt( 1, 2 ) ).addClass( 'type-2' ) ;
	} );


	$(".tile" ).each( function( ){
		var target = $( this ),
			tile = new GPoint(
				parseInt( target.attr( 'data-row' ), 10 ),
				parseInt( target.attr( 'data-col' ), 10 ),
				parseInt( target.attr( 'data-cost' ), 10 )
			);
		grid[ tile.row ][ tile.col ] = tile ;
	} );





	claptrap.on( 'transitionend webkittransitionend' , doAnimationFrame )
		.on('click' , onClaptrapClick );

	function onClaptrapClick( e ){
		e.preventDefault() ;

		resetGrid() ;
		var target = $( e.currentTarget ),
			origin = grid[ parseInt( target.attr('data-row'), 10 ) ][ parseInt( target.attr('data-col'), 10 ) ],
			openTiles = [ origin ],
			selectedTiles = [ ] ,
			locationCost = 0,
			current, adjacent, currentAdjacent , index ;
		origin.cost = 0 ;
		selectedTiles.push( origin ) ;

		while( openTiles.length > 0 ){
			current = openTiles.pop() ;
			adjacent = getAdjacent( current ) ;

			for( var i = 0; i < adjacent.length; i++ ){
				currentAdjacent = adjacent[ i ] ;
				locationCost = current.cost + currentAdjacent.terrain ;

				if( locationCost < currentAdjacent.cost || currentAdjacent.cost === -1 ){
					currentAdjacent.cost = locationCost ;
					if( openTiles.indexOf( currentAdjacent ) === -1 ){
						openTiles.push( currentAdjacent ) ;
					}
				}

				if( locationCost <= movement  && selectedTiles.indexOf( currentAdjacent ) === -1 ){
					selectedTiles.push( currentAdjacent )  ;
				}
			}
		}

		renderSelected( selectedTiles  );
		renderAttack( selectedTiles , 2 ) ;
	}
	function resetGrid() {
		for(var i = 0; i < area; i++ ){
			for(var j = 0; j < area; j++ ){
				grid[i][j].cost = -1 ;
			}
		}
	}

	function renderSelected( selected  ){
		var key, el , cls,
			len = selected.length ;
		$(".tile" ).removeClass( "can-move-to can-attack" );

		for( key in selected ){
			if( selected.hasOwnProperty( key ) ){
				el = selected[ key ];
				cls = "can-move-to";
				$(".tile[data-row='"+  el.row +"'][data-col='"+ el.col +"']" ).addClass( cls );
			}
		}
	}
	function renderAttack( selected , attackRange ){
		var openTiles = _.clone( selected ),
			attackTiles = [ ] ,
			locationCost = 0,
			current, adjacent, currentAdjacent , index ;

		for( var i = 0; i < openTiles.length; i++ ){
			openTiles[i].cost = 0 ;
		}

		while( openTiles.length > 0 ){
			current = openTiles.pop() ;
			adjacent = getAdjacent( current ) ;

			for( i = 0; i < adjacent.length; i++ ){
				currentAdjacent = adjacent[ i ] ;
				locationCost = current.cost + 1 ;

				if( locationCost <= attackRange &&
					attackTiles.indexOf( currentAdjacent ) === -1 &&
					selected.indexOf( currentAdjacent ) === -1 ){
					attackTiles.push( currentAdjacent )  ;
				}
			}
		}
		for( i = 0; i < attackTiles.length; i++ ){
			var el = attackTiles[i] ;
			$(".tile[data-row='"+  el.row +"'][data-col='"+ el.col +"']" ).addClass( "can-attack" );
		}

		return;
	}


	function getAdjacent( pt ){
		var result = [];

		if( pt.row < area - 1 ){
			result.push( grid[ pt.row + 1][ pt.col ] ) ;
		}
		if( pt.row > 0 ) {
			result.push( grid[ pt.row - 1][ pt.col ] ) ;
		}
		if( pt.col < area - 1 ){
			result.push( grid[ pt.row][ pt.col + 1 ] );
		}
		if( pt.col > 0 ){
			result.push( grid[ pt.row ][ pt.col - 1 ] );
		}
		return result ;
	}

	function getGridCost( row , col ){
		return parseInt( getGridLocation(row, col).attr('data-cost') , 10 ) ;
	}

	function getGridLocation( row , col ){
		return $(".tile[data-row='"+row+"'][data-col='"+col+"']" ) ;
	}

	function getGridLocationFromGPoint( gpt ){
		return getGridLocation( gpt.row, gpt.col );
	}

	function getCArrayID( pt ){
		return pt.row * area + pt.col;
	}

	$( ".tile" ).click( function( e ){
		e.preventDefault();

		var target =  $( e.currentTarget ) ;
		if( target.hasClass( 'can-move-to' ) ) {
			claptrap
				.attr( "data-dest-col", parseInt( target.attr( 'data-col' ), 10 ) )
				.attr( "data-dest-row", parseInt( target.attr( 'data-row' ), 10 ) );
			$( ".tile" ).removeClass( "can-move-to can-attack" );
			doAnimationFrame();
		}

	});


	function doAnimationFrame(){
		var row = parseInt( claptrap.attr( 'data-row' ), 10 ),
			dest_row = parseInt( claptrap.attr( 'data-dest-row' ), 10 ),
			col = parseInt( claptrap.attr( 'data-col' ), 10 ),
			dest_col = parseInt( claptrap.attr( 'data-dest-col' ), 10 );
		if( row < dest_row ){
			claptrap
				.removeClass('north east west')
				.addClass("south moving" )
				.attr( 'data-row' , ++row );
		} else if( row > dest_row ){
			claptrap
				.removeClass('south east west')
				.addClass("north moving" )
				.attr( 'data-row' , --row );
		} else if( col < dest_col ){
			claptrap
				.removeClass('south north west')
				.addClass( "east moving" )
				.attr( 'data-col' , ++col );
		} else if( col > dest_col ){
			claptrap
				.removeClass('south north east')
				.addClass("west moving" )
				.attr( 'data-col' , --col );
		} else {
			claptrap.removeClass('moving');
		}
	}

	function getRandomInt( min, max ) {
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}
});
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console