Skip to content

Instantly share code, notes, and snippets.

@jakevsrobots
Created August 11, 2010 23:12
Show Gist options
  • Save jakevsrobots/519985 to your computer and use it in GitHub Desktop.
Save jakevsrobots/519985 to your computer and use it in GitHub Desktop.
// InfiniteOutsideState.as
package beulah {
import org.flixel.*;
public class InfiniteOutsideState extends FlxState {
private var worldChunks:FlxGroup;
private var worldChunkMap:Object;
private var currentWorld:Object;
private var playerCurrentRoomCoordinates:FlxPoint;
private var playerCurrentRoomKey:String;
private var playerInitialStartPosition:FlxPoint;
override public function create():void {
Main.world.updateWorldBounds();
Main.world.updateCameraBounds();
playerCurrentRoomCoordinates = Main.world.getWorldCoordinates(activePlayer.x, activePlayer.y);
// World chunks
worldChunks = new FlxGroup();
worldChunkMap = {
'top': worldChunks.add(new WorldChunk(season, this)),
'middle': worldChunks.add(new WorldChunk(season, this)),
'bottom': worldChunks.add(new WorldChunk(season, this)),
'left': worldChunks.add(new WorldChunk(season, this)),
'right': worldChunks.add(new WorldChunk(season, this)),
'topLeft': worldChunks.add(new WorldChunk(season, this)),
'topRight': worldChunks.add(new WorldChunk(season, this)),
'bottomLeft': worldChunks.add(new WorldChunk(season, this)),
'bottomRight': worldChunks.add(new WorldChunk(season, this))
};
refreshWorldChunks();
add(worldChunks);
}
private function refreshWorldChunks():void {
var targetWorld:Object = getTargetCurrentWorld();
if(!compareWorldObjects(targetWorld, currentWorld)) {
loadTargetWorld(targetWorld);
}
}
private function compareWorldObjects(objOne:Object, objTwo:Object):Boolean {
for(var property:String in objOne) {
if(objOne[property] != objTwo[property]) {
return false;
}
}
return true;
}
private function getTargetCurrentWorld():Object {
var targetWorld:Object = {};
targetWorld['top'] = Main.world.getKey(
playerCurrentRoomCoordinates.x,
playerCurrentRoomCoordinates.y - 1
);
targetWorld['middle'] = Main.world.getKey(
playerCurrentRoomCoordinates.x,
playerCurrentRoomCoordinates.y
);
targetWorld['bottom'] = Main.world.getKey(
playerCurrentRoomCoordinates.x,
playerCurrentRoomCoordinates.y + 1
);
targetWorld['left'] = Main.world.getKey(
playerCurrentRoomCoordinates.x - 1,
playerCurrentRoomCoordinates.y
);
targetWorld['right'] = Main.world.getKey(
playerCurrentRoomCoordinates.x + 1,
playerCurrentRoomCoordinates.y
);
targetWorld['topLeft'] = Main.world.getKey(
playerCurrentRoomCoordinates.x - 1,
playerCurrentRoomCoordinates.y - 1
);
targetWorld['topRight'] = Main.world.getKey(
playerCurrentRoomCoordinates.x + 1,
playerCurrentRoomCoordinates.y - 1
);
targetWorld['bottomLeft'] = Main.world.getKey(
playerCurrentRoomCoordinates.x - 1,
playerCurrentRoomCoordinates.y + 1
);
targetWorld['bottomRight'] = Main.world.getKey(
playerCurrentRoomCoordinates.x + 1,
playerCurrentRoomCoordinates.y + 1
);
return targetWorld;
}
private function loadTargetWorld(targetWorld:Object):void {
for(var positionName:String in targetWorld) {
if(targetWorld[positionName] != currentWorld[positionName]) {
worldChunkMap[positionName].loadMap(Main.world.getMapFromKey(targetWorld[positionName]),
targetWorld[positionName]);
}
}
currentWorld = targetWorld;
}
override public function update():void {
refreshWorldChunks();
FlxU.collide(worldChunkMap['middle'].walls, activePlayer);
super.update();
}
}
}
////////////////////////////////////////////////////////////////////
// WorldChunk.as
package beulah {
import org.flixel.*;
public class WorldChunk extends FlxGroup {
public var position:FlxPoint;
public var roomName:String = '';
public var frontTileMap:FlxTilemap;
public var backTileMap:FlxTilemap;
public var walls:FlxGroup;
public var key:String;
private var gameState:InfiniteOutsideState;
public function WorldChunk(season:String, gameState:InfiniteOutsideState):void {
super();
this.gameState = gameState;
position = new FlxPoint(-1,-1); // Initialize this way so 0,0 will not give false positives
frontTileMap = new FlxTilemap();
backTileMap = new FlxTilemap();
walls = new FlxGroup();
add(backTileMap);
add(frontTileMap);
frontTileMap.kill();
backTileMap.kill();
}
private function resetData():void {
frontTileMap.kill();
backTileMap.kill();
// also clear out any sprites, etc.
}
public function loadMap(mapData:XML, key:String):void {
this.key = key;
if(mapData == null) {
resetData();
return;
}
this.roomName = mapData.@roomName;
// Global coordinates
position.x = mapData.@worldX;
position.y = mapData.@worldY;
// Absolute position
x = position.x * Number(Main.world.worldChunkWidth);
y = position.y * Number(Main.world.worldChunkHeight);
reset(x,y);
// Tiles
if(mapData.foregroundTiles.toString()) {
frontTileMap.reset(x,y);
frontTileMap.loadMap(mapData.foregroundTiles.toString(),
Main.library.getAsset(season + 'Tiles'), 16, 16);
} else {
frontTileMap.kill();
}
if(mapData.backgroundTiles.toString()) {
backTileMap.reset(x,y);
backTileMap.loadMap(mapData.backgroundTiles.toString(),
Main.library.getAsset(season + 'Tiles'), 16, 16);
} else {
backTileMap.kill();
}
// Walls and platforms
walls.destroy();
for each(var wallRectNode:XML in mapData.walls.rect) {
var wallBlock:FlxTileblock = new FlxTileblock(x + int(wallRectNode.@x), y + int(wallRectNode.@y), wallRectNode.@w, wallRectNode.@h);
if(showWalls) {
wallBlock.createGraphic(wallRectNode.@w, wallRectNode.@h, 0xff000000);
}
walls.add(wallBlock);
}
// Also load any sprites.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment