Advertisement
Guest User

Roguelike - simplified

a guest
Jun 29th, 2010
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Roguelike test - Room building (fully random)
  3.  * @author Dan Hett (hellodanhett@gmail.com)
  4.  **/
  5.  
  6. package
  7. {
  8.     import flash.display.DisplayObject;
  9.     import flash.display.MovieClip;
  10.     import flash.display.Sprite;
  11.     import flash.events.KeyboardEvent;
  12.     import flash.ui.Keyboard;
  13.    
  14.     [SWF (width="1024", height="768", backgroundColor="0x000000")]
  15.    
  16.     public class Roguelike4 extends Sprite
  17.     {
  18.         // Glyph size
  19.         public static var TILE:int = 32
  20.        
  21.         // Room boundaries
  22.         public static var MAX_ROOM_SIZE:int = 15
  23.         public static var MIN_ROOM_SIZE:int = 5
  24.            
  25.         // Room objects
  26.         private var _blank:int = 0
  27.         private var _wall:int = 1
  28.         private var _floor:int = 2
  29.            
  30.         // Player position variables
  31.         private var PLAYER_ROW:int
  32.         private var PLAYER_COL:int
  33.        
  34.         // Scene
  35.         private var level:MovieClip
  36.         private var char:player
  37.        
  38.         // init
  39.         public function Roguelike4()
  40.         {
  41.             // Holder clip for test level, offset from the edge by one tile width
  42.             level = new MovieClip()
  43.             level.x = TILE
  44.             level.y = TILE
  45.             stage.addChild(level)
  46.            
  47.             // Enable keyboard
  48.             stage.addEventListener(KeyboardEvent.KEY_DOWN, moveCharacter)
  49.                
  50.             // Begin the process
  51.             generateLevel()
  52.         }
  53.        
  54.         // Generate a random array that will be used to construct the room
  55.         private var tileSet:Array
  56.         public function generateLevel():void
  57.         {
  58.             // Array of tile values for test level
  59.             tileSet = new Array;
  60.            
  61.             // Push a random number of objects into the array to give us VERTICAL size
  62.             var random_height:int = Math.random() * (MAX_ROOM_SIZE - MIN_ROOM_SIZE) + MIN_ROOM_SIZE
  63.             var random_width:int = Math.random() * (MAX_ROOM_SIZE - MIN_ROOM_SIZE) + MIN_ROOM_SIZE
  64.                
  65.             // Trace our values
  66.             trace("Generating room: HEIGHT: " + random_height + ", WIDTH: " + random_width);
  67.            
  68.             // First we need to create a top row made entirely of wall tiles
  69.             var wall_row:Array = new Array()
  70.             for(var t:int = 0; t < random_width; t++)
  71.             {
  72.                 wall_row.push(1)
  73.             }
  74.            
  75.             // Add the top row to the main array before generating the middle tiles
  76.             tileSet.push(wall_row)
  77.            
  78.             // Create the MIDDLE ROWS
  79.             for(var i:int = 0; i < random_height - 2; i++)
  80.             {
  81.                 // Create a row instance
  82.                 var mid_row:Array = new Array()
  83.                
  84.                 // Add a wall on the LEFT SIDE of the row instance
  85.                 mid_row.push(1)
  86.                    
  87.                 // Add the filler tiles to form the floor
  88.                 for(var j:int = 0; j < random_width - 2; j++)
  89.                 {
  90.                     mid_row.push(2)
  91.                 }
  92.                
  93.                 // Finally add a wall tile on the RIGHT side of the row instance
  94.                 mid_row.push(1)
  95.                
  96.                 // Add the newly created row to the main set of tiles
  97.                 tileSet.push(mid_row)
  98.             }
  99.  
  100.             // Add another wall row to the bottom so the room is sealed
  101.             tileSet.push(wall_row)
  102.            
  103.             // Take the level array and create a physical level
  104.             buildLevel(tileSet)
  105.         }
  106.        
  107.        
  108.         // Physically constructs the level from our random array
  109.         public function buildLevel(target:Array):void
  110.         {
  111.             // Loop through the row, from top to bottom
  112.             for(var v:int = 0; v < target.length; v++)
  113.             {
  114.                 // Loop through all horizontal tiles in each vertical row
  115.                 for(var h:int = 0; h < target[v].length; h++)
  116.                 {
  117.                     // Create generic tile object
  118.                     var _tile:DisplayObject;
  119.                    
  120.                     // Figure out what the tile actually is
  121.                     switch(target[v][h])
  122.                     {
  123.                         case 0: // BLANK
  124.                         _tile = new tile_blank();
  125.                         break;
  126.                        
  127.                         case 1: // WALL
  128.                         _tile = new tile_wall();
  129.                         break;
  130.                        
  131.                         case 2: // FLOOR
  132.                         _tile = new tile_floor();
  133.                         break;
  134.                     }
  135.                    
  136.                     // Add the tile to the display list
  137.                     _tile.y = (v) * TILE
  138.                     _tile.x = (h) * TILE
  139.                     level.addChild(_tile)
  140.                    
  141.                 }
  142.                
  143.             }
  144.            
  145.             // Now the level is complete, we can place the player character
  146.             setupPlayer(target)
  147.            
  148.         }
  149.        
  150.         private var randX:int
  151.         private var randY:int
  152.         public function setupPlayer(level_array:Array):void
  153.         {              
  154.             // Get a random X and Y value based on the level's construction array
  155.             randY = Math.random() * level_array.length
  156.             randX = Math.random() * level_array[randY].length
  157.                
  158.             // Attempt to spawn the player
  159.             spawnPlayer(level_array)   
  160.         }
  161.        
  162.        
  163.         public function spawnPlayer(level_array:Array):void
  164.         {
  165.             // Ensure the desination is a walkable surface
  166.             if(level_array[randY][randX] == 2)
  167.             {
  168.                 // Create a physical instance of the character
  169.                 char = new player()
  170.                 level.addChild(char)
  171.                
  172.                 // Place the player character at the correct co-ordinates
  173.                 char.y = randY * TILE
  174.                 char.x = randX * TILE
  175.                    
  176.                 // Check player position (apply the vars so we know where we are numerically!)
  177.                 PLAYER_ROW = randX
  178.                 PLAYER_COL = randY
  179.                 check_player_pos()
  180.             }
  181.            
  182.             // If the surface ISN'T walkable, generate another random square
  183.             else
  184.             {
  185.                 setupPlayer(level_array)
  186.             }
  187.         }
  188.        
  189.         // Get the type of tile we're standing on when we first spawn
  190.         private function check_player_pos():void
  191.         {
  192.             trace("Currently stood on: " + tileSet[PLAYER_COL][PLAYER_ROW])
  193.         }
  194.        
  195.         /**
  196.          * MAIN CHARACTER CONTROL FUNCTIONALITY
  197.          * This functionality will check where we're *trying* to move to,
  198.          * and if the square is valid then will allow us to proceed.
  199.          */
  200.         private function moveCharacter(e:KeyboardEvent):void
  201.         {
  202.             switch(e.keyCode)
  203.             {
  204.                 case Keyboard.LEFT:
  205.                 if(tileSet[PLAYER_COL][PLAYER_ROW - 1] == _floor)
  206.                 {
  207.                     PLAYER_ROW--
  208.                     char.x -= TILE
  209.                 }
  210.                 break;
  211.                
  212.                 case Keyboard.RIGHT:
  213.                 if(tileSet[PLAYER_COL][PLAYER_ROW + 1] == _floor)
  214.                 {
  215.                     PLAYER_ROW++
  216.                     char.x += TILE
  217.                 }
  218.                 break;
  219.                
  220.                 case Keyboard.UP:
  221.                 if(tileSet[PLAYER_COL-1][PLAYER_ROW] == _floor)
  222.                 {
  223.                     PLAYER_COL--
  224.                     char.y -= TILE
  225.                 }
  226.                 break;
  227.                
  228.                 case Keyboard.DOWN:
  229.                 if(tileSet[PLAYER_COL+1][PLAYER_ROW] == _floor)
  230.                 {
  231.                     PLAYER_COL++
  232.                     char.y += TILE
  233.                 }
  234.                 break;
  235.             }
  236.         }
  237.     }
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement