Skip to content

Instantly share code, notes, and snippets.

@zachwlewis
Last active August 29, 2015 14:00
Show Gist options
  • Save zachwlewis/c70a782b4dc29e5018eb to your computer and use it in GitHub Desktop.
Save zachwlewis/c70a782b4dc29e5018eb to your computer and use it in GitHub Desktop.
tilemap slowdown
package game_handling
{
import assets.hp_globe.HpGlobe;
import assets.hp_globe.HpGlobe_HP;
import assets.stamina_bar.StaminaBarGraphic;
import assets.stamina_bar.StaminaGraphic;
import assets.xp_bar.XpBarGraphic;
import assets.xp_bar.XpGraphic;
import blocks.*;
import blocks.chests.Chest_Wooden;
import enemies.Goblin_Thief;
import events.Change_Room;
import flash.display.Bitmap;
import flash.geom.Point;
import hero.*;
import levels.level1.*;
import lit.Light;
import lit.Lighting;
import net.flashpunk.Entity;
import net.flashpunk.FP;
import net.flashpunk.graphics.Graphiclist;
import net.flashpunk.graphics.Image;
import net.flashpunk.graphics.Stamp;
import net.flashpunk.graphics.Tilemap;
import net.flashpunk.masks.Grid;
import net.flashpunk.World;
import talents.*;
public class GameWorld extends World
{
[Embed(source="../assets/circle_gradient2.png")]
public static const SPR_LIGHT_CIRCLE_GRADIENT:Class;
private var stats:HeroStats = new HeroStats();
private var xpbar:XpBarGraphic = new XpBarGraphic(470, 1040);
private var xpbar_xp:XpGraphic = new XpGraphic(470, 1040);
private var hpglobe:HpGlobe = new HpGlobe(30, 30);
private var hpglobe_hp:HpGlobe_HP = new HpGlobe_HP(30, 30);
private var staminabar:StaminaBarGraphic = new StaminaBarGraphic(250, 30);
private var staminabar_stamina:StaminaGraphic = new StaminaGraphic(250, 30);
private var gui:HUD = new HUD();
private var sounds:Sounds = new Sounds();
public var lighting:Lighting;
public var mouseLight:Light;
private var _map_Ground_Accessories_2:Tilemap;
private var _map_Ground_Accessories:Tilemap;
private var _map_Ground:Tilemap;
private var _map_BG_Accessories_2:Tilemap;
private var _map_BG_Accessories:Tilemap;
private var _map_BG:Tilemap;
public var collisionData:Grid;
public static var currentLevelIsScrollable:Boolean = false;
public static var currentLevel:int = 0;
private var _rawMapData:Class;
private var _scrollable:Boolean;
private var _tileset:Class;
public function GameWorld(map:Class, tileset:Class, isScrollable:Boolean)
{
_rawMapData = map
_tileset = tileset
_scrollable = isScrollable
}
override public function begin():void
{
super.begin();
//Hero stats
add(stats);
//Hp and xp graphics
add(hpglobe);
add(hpglobe_hp);
add(xpbar);
add(xpbar_xp);
add(staminabar);
add(staminabar_stamina);
//HUD
add(gui);
add(sounds);
loadLevel(_rawMapData, _tileset, _scrollable);
}
override public function update():void
{
super.update();
}
public function setNewMap(map:Class, tileset:Class, isScrollable:Boolean):void
{
_rawMapData = map;
loadLevel(_rawMapData, _tileset, _scrollable);
}
public function loadLevel(levelNumber:Class, tileset:Class, scrollable:Boolean):void
{
currentLevelIsScrollable = scrollable;
var tileSet:Class = tileset
var mapXML:XML = FP.getXML(levelNumber);
var mapWidth:uint = uint(mapXML.@width);
var mapHeight:uint = uint(mapXML.@height);
var tileX:uint = 0;
var tileY:uint = 0;
collisionData = new Grid(mapWidth * 128, mapHeight * 128, 128, 128);
_map_Ground_Accessories_2 = new Tilemap(tileSet, mapWidth * 128, mapHeight * 128, 128, 128);
_map_Ground_Accessories = new Tilemap(tileSet, mapWidth * 128, mapHeight * 128, 128, 128);
_map_Ground = new Tilemap(tileSet, mapWidth * 128, mapHeight * 128, 128, 128);
_map_BG_Accessories_2 = new Tilemap(tileSet, mapWidth * 128, mapHeight * 128, 128, 128);
_map_BG_Accessories = new Tilemap(tileSet, mapWidth * 128, mapHeight * 128, 128, 128);
_map_BG = new Tilemap(tileSet, mapWidth * 128, mapHeight * 128, 128, 128);
var amountOfTiles:int = 0;
for each (var layer:XML in mapXML.layer)
{
var XMLlayers:String = layer.attribute("name");
switch (XMLlayers)
{
case "Ground_Accessories_2":
tileX = 0;
tileY = 0;
for each (var tile:XML in layer.data.tile)
{
if (tileX >= mapWidth)
{
tileX = 0;
tileY++;
}
if (tile.@gid != 0)
{
_map_Ground_Accessories_2.setTile(tileX, tileY, uint(tile.@gid - 1));
}
tileX++;
}
break;
case "Ground_Accessories":
tileX = 0;
tileY = 0;
for each ( tile in layer.data.tile)
{
if (tileX >= mapWidth)
{
tileX = 0;
tileY++;
}
if (tile.@gid != 0)
{
_map_Ground_Accessories.setTile(tileX, tileY, uint(tile.@gid - 1));
}
tileX++;
}
break;
case "Ground":
tileX = 0;
tileY = 0;
for each (tile in layer.data.tile)
{
if (tileX >= mapWidth)
{
tileX = 0;
tileY++;
}
if (tile.@gid != 0)
{
collisionData.setTile(tileX, tileY, true);
}
if (tile.@gid != 0)
{
_map_Ground.setTile(tileX, tileY, uint(tile.@gid - 1));
}
tileX++;
}
break;
case "BG_Accessories_2":
tileX = 0;
tileY = 0;
for each (tile in layer.data.tile)
{
if (tileX >= mapWidth)
{
tileX = 0;
tileY++;
}
if (tile.@gid != 0)
{
_map_BG_Accessories_2.setTile(tileX, tileY, uint(tile.@gid - 1));
}
tileX++;
}
break;
case "BG_Accessories":
tileX = 0;
tileY = 0;
for each (tile in layer.data.tile)
{
if (tileX >= mapWidth)
{
tileX = 0;
tileY++;
}
if (tile.@gid != 0)
{
_map_BG_Accessories.setTile(tileX, tileY, uint(tile.@gid - 1));
}
tileX++;
}
break;
case "BG":
tileX = 0;
tileY = 0;
for each (tile in layer.data.tile)
{
if (tileX >= mapWidth)
{
tileX = 0;
tileY++;
}
if (tile.@gid != 0)
{
_map_BG.setTile(tileX, tileY, uint(tile.@gid - 1));
}
tileX++;
}
break;
}
}
trace(amountOfTiles)
for each (var objectgroup:XML in mapXML.objectgroup)
{
var groupname:String = objectgroup.attribute("name");
switch (groupname)
{
case "Spawn":
for each (var object:XML in objectgroup.object)
{
var spawnname:String = object.attribute("name");
switch (spawnname)
{
case "Hero":
FP.world.add(new Hero(new Point(int(object.@x * 8), int(object.@y * 8))));
break;
case "Goblin_Thief":
FP.world.add(new Goblin_Thief(new Point(int(object.@x * 8), int(object.@y * 8))));
break;
}
}
case "Chests":
for each (object in objectgroup.object)
{
var chestname:String = object.attribute("name");
switch (chestname)
{
case "Wooden":
FP.world.add(new Chest_Wooden(new Point(int(object.@x * 8), int(object.@y * 8))));
break;
}
}
case "Events":
for each (object in objectgroup.object)
{
var eventname:String = object.attribute("name");
switch (eventname)
{
case "Change_Room":
FP.world.add(new Change_Room(new Point(int(object.@x * 8), int(object.@y * 8))));
break;
}
}
}
}
//_map_BG.scrollX = 0.2;
//addGraphic(_map_Ground)
addGraphic(_map_BG, 1);
addGraphic(_map_BG_Accessories);
addGraphic(_map_BG_Accessories_2);
addGraphic(_map_Ground_Accessories, -2)
addGraphic(_map_Ground_Accessories_2, -3)
// Debug map dimensions.
trace("----- Tilemap Layer Dimensions -----");
trace("_map_BG: width =>", _map_BG.width, "height =>", _map_BG.height);
trace("_map_BG_Accessories: width =>", _map_BG_Accessories.width, "height =>", _map_BG_Accessories.height);
trace("_map_BG_Accessories_2: width =>", _map_BG_Accessories_2.width, "height =>", _map_BG_Accessories_2.height);
trace("_map_Ground_Accessories: width =>", _map_Ground_Accessories.width, "height =>", _map_Ground_Accessories.height);
trace("_map_Ground_Accessories_2: width =>", _map_Ground_Accessories_2.width, "height =>", _map_Ground_Accessories_2.height);
trace("------------------------------------");
updateCollision();
}
public function updateCollision():void
{
var collision_:Collision = FP.world.getInstance("collision");
if (collision_ == null)
FP.world.add(new Collision(0, 0, _map_Ground, collisionData))
else
{
FP.world.remove(collision_)
trace("updating collision")
}
}
//End
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment