Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function move(dirX:int, dirY:int):void {
- //Ensure you're already locked into the last desired position.
- if(destinationX == x && destinationY == y) {
- directionX = dirX;
- directionY = dirY;
- if(environment.checkBounds(x, y, directionX, directionY)) {
- var nextTile:MasterTile;
- //Check to see if the desired location is passable.
- nextTile = environment.getTile(tileGridPositions, directionX, directionY)
- if(nextTile != null && nextTile.getPassable()) {
- destinationX += tileWidth * directionX;
- destinationY += tileHeight * directionY;
- }
- }
- }
- }
- protected function movement():void {
- if(destinationX != x) x += directionX * speed;
- if(destinationY != y) y += directionY * speed;
- if(destinationX == x && destinationY == y) {
- //Update where you're standing (tile under you, grid position, so on).
- updateTileGridPositions();
- updateCurrentMapTile();
- directionX = 0;
- directionY = 0;
- }
- }
- protected function updateTileGridPositions():void {
- environment.getTileGridPositions(x, y, verticalHeight, tileGridPositions);
- }
- protected function updateCurrentMapTile():void {
- var previousMapTile:MasterTile;
- previousMapTile = currentMapTile;
- if(previousMapTile != null) {
- previousMapTile.setPassable(true);
- }
- //Switch currentMapTile to the new tile.
- setCurrentMapTile(environment.getTile(tileGridPositions, 0, 0));
- currentMapTile.setPassable(passable);
- currentMapTile.updateTileInfo(this);
- }
- //Pass in the objects vertical height as well. Subtract that from the py - vertical offset.
- public function getTileGridPositions(px:int, py:int, verticalHeight:int, gridPosition:Point):void {
- gridPosition.x = floor((px - horizontalOffset) / mapTilewidth);
- gridPosition.y = floor((py - verticalOffset + verticalHeight) / mapTileHeight);
- }
- public function getTile(tileGridPositions:Point, dirX:int, dirY:int):MasterTile {
- var returnTile:MasterTile;
- var vectorPosition:int;
- vectorPosition = ((tileGridPositions.y + dirY) * mapTileCountX) + tileGridPositions.x + dirX;
- if(vectorPosition >= 0 && vectorPosition < mapTiles.length) {
- return mapTiles[vectorPosition];
- } else {
- return null;
- }
- }
- //DEPTH SORTING
- private function entityDepthSorting():void {
- //Sort the objects in the vector.
- entitiesLayer.getDisplayList().sort(sortEntities);
- }
- public function sortEntities(objectA:Entity, objectB:Entity):Number {
- if(objectA.getZ() < objectB.getZ()) return -1;
- else if(objectA.getZ() > objectB.getZ()) return 1;
- else return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement