Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public function move(dirX:int, dirY:int):void {
  2.             //Ensure you're already locked into the last desired position.
  3.             if(destinationX == x && destinationY == y) {
  4.                 directionX = dirX;
  5.                 directionY = dirY;
  6.                 if(environment.checkBounds(x, y, directionX, directionY)) {
  7.                     var nextTile:MasterTile;
  8.                     //Check to see if the desired location is passable.
  9.                     nextTile = environment.getTile(tileGridPositions, directionX, directionY)
  10.                     if(nextTile != null && nextTile.getPassable()) {
  11.                         destinationX += tileWidth * directionX;
  12.                         destinationY += tileHeight * directionY;
  13.                     }
  14.                 }
  15.             }
  16.         }
  17.        
  18.         protected function movement():void {
  19.             if(destinationX != x) x += directionX * speed;
  20.             if(destinationY != y) y += directionY * speed;
  21.             if(destinationX == x && destinationY == y) {
  22.                 //Update where you're standing (tile under you, grid position, so on).
  23.                 updateTileGridPositions();
  24.                 updateCurrentMapTile();
  25.                 directionX = 0;
  26.                 directionY = 0;
  27.             }
  28.         }
  29.  
  30. protected function updateTileGridPositions():void {
  31.             environment.getTileGridPositions(x, y, verticalHeight, tileGridPositions);
  32.         }
  33.        
  34.         protected function updateCurrentMapTile():void {
  35.             var previousMapTile:MasterTile;
  36.             previousMapTile = currentMapTile;
  37.            
  38.             if(previousMapTile != null) {
  39.                 previousMapTile.setPassable(true);
  40.             }
  41.             //Switch currentMapTile to the new tile.
  42.             setCurrentMapTile(environment.getTile(tileGridPositions, 0, 0));
  43.             currentMapTile.setPassable(passable);
  44.             currentMapTile.updateTileInfo(this);
  45.         }
  46.  
  47. //Pass in the objects vertical height as well. Subtract that from the py - vertical offset.
  48.         public function getTileGridPositions(px:int, py:int, verticalHeight:int, gridPosition:Point):void {
  49.             gridPosition.x = floor((px - horizontalOffset) / mapTilewidth);
  50.             gridPosition.y = floor((py - verticalOffset + verticalHeight) / mapTileHeight);
  51.         }
  52.        
  53.         public function getTile(tileGridPositions:Point, dirX:int, dirY:int):MasterTile {
  54.             var returnTile:MasterTile;
  55.             var vectorPosition:int;
  56.             vectorPosition = ((tileGridPositions.y + dirY) * mapTileCountX) + tileGridPositions.x + dirX;
  57.             if(vectorPosition >= 0 && vectorPosition < mapTiles.length) {
  58.                 return mapTiles[vectorPosition];
  59.             } else {
  60.                 return null;
  61.             }
  62.         }
  63.  
  64. //DEPTH SORTING
  65.         private function entityDepthSorting():void {
  66.             //Sort the objects in the vector.
  67.             entitiesLayer.getDisplayList().sort(sortEntities);
  68.         }
  69.        
  70.         public function sortEntities(objectA:Entity, objectB:Entity):Number {
  71.             if(objectA.getZ() < objectB.getZ()) return -1;
  72.             else if(objectA.getZ() > objectB.getZ()) return 1;
  73.             else return 0;
  74.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement