Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Roguelike test - Room building (fully random)
- * @author Dan Hett (hellodanhett@gmail.com)
- **/
- package
- {
- import flash.display.DisplayObject;
- import flash.display.MovieClip;
- import flash.display.Sprite;
- import flash.events.KeyboardEvent;
- import flash.ui.Keyboard;
- [SWF (width="1024", height="768", backgroundColor="0x000000")]
- public class Roguelike4 extends Sprite
- {
- // Glyph size
- public static var TILE:int = 32
- // Room boundaries
- public static var MAX_ROOM_SIZE:int = 15
- public static var MIN_ROOM_SIZE:int = 5
- // Room objects
- private var _blank:int = 0
- private var _wall:int = 1
- private var _floor:int = 2
- // Player position variables
- private var PLAYER_ROW:int
- private var PLAYER_COL:int
- // Scene
- private var level:MovieClip
- private var char:player
- // init
- public function Roguelike4()
- {
- // Holder clip for test level, offset from the edge by one tile width
- level = new MovieClip()
- level.x = TILE
- level.y = TILE
- stage.addChild(level)
- // Enable keyboard
- stage.addEventListener(KeyboardEvent.KEY_DOWN, moveCharacter)
- // Begin the process
- generateLevel()
- }
- // Generate a random array that will be used to construct the room
- private var tileSet:Array
- public function generateLevel():void
- {
- // Array of tile values for test level
- tileSet = new Array;
- // Push a random number of objects into the array to give us VERTICAL size
- var random_height:int = Math.random() * (MAX_ROOM_SIZE - MIN_ROOM_SIZE) + MIN_ROOM_SIZE
- var random_width:int = Math.random() * (MAX_ROOM_SIZE - MIN_ROOM_SIZE) + MIN_ROOM_SIZE
- // Trace our values
- trace("Generating room: HEIGHT: " + random_height + ", WIDTH: " + random_width);
- // First we need to create a top row made entirely of wall tiles
- var wall_row:Array = new Array()
- for(var t:int = 0; t < random_width; t++)
- {
- wall_row.push(1)
- }
- // Add the top row to the main array before generating the middle tiles
- tileSet.push(wall_row)
- // Create the MIDDLE ROWS
- for(var i:int = 0; i < random_height - 2; i++)
- {
- // Create a row instance
- var mid_row:Array = new Array()
- // Add a wall on the LEFT SIDE of the row instance
- mid_row.push(1)
- // Add the filler tiles to form the floor
- for(var j:int = 0; j < random_width - 2; j++)
- {
- mid_row.push(2)
- }
- // Finally add a wall tile on the RIGHT side of the row instance
- mid_row.push(1)
- // Add the newly created row to the main set of tiles
- tileSet.push(mid_row)
- }
- // Add another wall row to the bottom so the room is sealed
- tileSet.push(wall_row)
- // Take the level array and create a physical level
- buildLevel(tileSet)
- }
- // Physically constructs the level from our random array
- public function buildLevel(target:Array):void
- {
- // Loop through the row, from top to bottom
- for(var v:int = 0; v < target.length; v++)
- {
- // Loop through all horizontal tiles in each vertical row
- for(var h:int = 0; h < target[v].length; h++)
- {
- // Create generic tile object
- var _tile:DisplayObject;
- // Figure out what the tile actually is
- switch(target[v][h])
- {
- case 0: // BLANK
- _tile = new tile_blank();
- break;
- case 1: // WALL
- _tile = new tile_wall();
- break;
- case 2: // FLOOR
- _tile = new tile_floor();
- break;
- }
- // Add the tile to the display list
- _tile.y = (v) * TILE
- _tile.x = (h) * TILE
- level.addChild(_tile)
- }
- }
- // Now the level is complete, we can place the player character
- setupPlayer(target)
- }
- private var randX:int
- private var randY:int
- public function setupPlayer(level_array:Array):void
- {
- // Get a random X and Y value based on the level's construction array
- randY = Math.random() * level_array.length
- randX = Math.random() * level_array[randY].length
- // Attempt to spawn the player
- spawnPlayer(level_array)
- }
- public function spawnPlayer(level_array:Array):void
- {
- // Ensure the desination is a walkable surface
- if(level_array[randY][randX] == 2)
- {
- // Create a physical instance of the character
- char = new player()
- level.addChild(char)
- // Place the player character at the correct co-ordinates
- char.y = randY * TILE
- char.x = randX * TILE
- // Check player position (apply the vars so we know where we are numerically!)
- PLAYER_ROW = randX
- PLAYER_COL = randY
- check_player_pos()
- }
- // If the surface ISN'T walkable, generate another random square
- else
- {
- setupPlayer(level_array)
- }
- }
- // Get the type of tile we're standing on when we first spawn
- private function check_player_pos():void
- {
- trace("Currently stood on: " + tileSet[PLAYER_COL][PLAYER_ROW])
- }
- /**
- * MAIN CHARACTER CONTROL FUNCTIONALITY
- * This functionality will check where we're *trying* to move to,
- * and if the square is valid then will allow us to proceed.
- */
- private function moveCharacter(e:KeyboardEvent):void
- {
- switch(e.keyCode)
- {
- case Keyboard.LEFT:
- if(tileSet[PLAYER_COL][PLAYER_ROW - 1] == _floor)
- {
- PLAYER_ROW--
- char.x -= TILE
- }
- break;
- case Keyboard.RIGHT:
- if(tileSet[PLAYER_COL][PLAYER_ROW + 1] == _floor)
- {
- PLAYER_ROW++
- char.x += TILE
- }
- break;
- case Keyboard.UP:
- if(tileSet[PLAYER_COL-1][PLAYER_ROW] == _floor)
- {
- PLAYER_COL--
- char.y -= TILE
- }
- break;
- case Keyboard.DOWN:
- if(tileSet[PLAYER_COL+1][PLAYER_ROW] == _floor)
- {
- PLAYER_COL++
- char.y += TILE
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement