Skip to content

Commit

Permalink
Moved map code to seperate file
Browse files Browse the repository at this point in the history
  • Loading branch information
ethransom committed Mar 12, 2012
1 parent 3abce86 commit 7669e53
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 209 deletions.
209 changes: 0 additions & 209 deletions src/flotsam.js
Expand Up @@ -254,215 +254,6 @@ function initThing(thing)
thing.autoroll = 0;
}

// Parses level XML
function LoadCollisFromXML(levelName)
{
//var levelName = "levels/test_level.xml";
//if (!isNaN(levelNum))
// levelName = "levels/" + levels[levelNum].map;
var realName = "levels/" + levelName;
//var realName = "levels/free_for_all.xml";

xmlDoc = loadXMLDoc(realName);
if (xmlDoc == null)
{
alert("BAD LEVEL XML");
return;
}

var test = xmlDoc.childNodes;

var layerList = xmlDoc.getElementsByTagName("Layer");

var itemList = layerList[0].getElementsByTagName("Item");

// COLLISION

var loopCount = itemList.length;
for (var i = 0; i < loopCount /*itemList.length*/; i++)
{
var Positions = itemList[i].getElementsByTagName("Position");

var xval = Positions[0].getElementsByTagName("X")[0].firstChild.nodeValue;
xxval = parseInt(xval);

var yval = Positions[0].getElementsByTagName("Y")[0].firstChild.nodeValue;
yyval = parseInt(yval);

// probably faster to declare these outside this loop?
var fixDef = new b2FixtureDef;
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = 0.2;
fixDef.filter.categoryBits = COLLIS_WORLD;

if (itemList[i].getAttribute('xsi:type') == "RectangleItem")
{
var width = parseInt(itemList[i].getElementsByTagName("Width")[0].firstChild.nodeValue);
var height = parseInt(itemList[i].getElementsByTagName("Height")[0].firstChild.nodeValue);

fixDef.shape = new b2PolygonShape;
fixDef.shape.SetAsBox(width/gPhysScale/2, height/gPhysScale/2);

var bodyDef = new b2BodyDef;
//create object
bodyDef.type = b2Body.b2_staticBody;
bodyDef.position.x = xxval/gPhysScale + width/gPhysScale/2;
bodyDef.position.y = yyval/gPhysScale + height/gPhysScale/2;

world.CreateBody(bodyDef).CreateFixture(fixDef);
}
else if (itemList[i].getAttribute('xsi:type') == "CircleItem")
{
var radius = parseInt(itemList[i].getElementsByTagName("Radius")[0].firstChild.nodeValue);

fixDef.shape = new b2CircleShape(
radius / gPhysScale
);

var bodyDef = new b2BodyDef;
//create object
bodyDef.type = b2Body.b2_staticBody;
bodyDef.position.x = xxval/gPhysScale;// + radius/gPhysScale/2;
bodyDef.position.y = yyval/gPhysScale;// + radius/gPhysScale/2;

world.CreateBody(bodyDef).CreateFixture(fixDef);

} else {
alert("BOGUS COLLIS ITEM!");
}

//itemList[i]
}

// GRAPHICS

var itemList = layerList[1].getElementsByTagName("Item");

var loopEnd = itemList.length;

for (var i = 0; i < loopEnd; i++)
{
var Positions = itemList[i].getElementsByTagName("Position");

var xval = Positions[0].getElementsByTagName("X")[0].firstChild.nodeValue;
var xxval = parseInt(xval);

var yval = Positions[0].getElementsByTagName("Y")[0].firstChild.nodeValue;
var yyval = parseInt(yval);

var rotation = parseFloat(itemList[i].getElementsByTagName("Rotation")[0].firstChild.nodeValue);

var Scales = itemList[i].getElementsByTagName("Scale");

var prexscale = Scales[0].getElementsByTagName("X")[0].firstChild.nodeValue;
var xscale = parseFloat(prexscale);

var yscale = parseFloat(Scales[0].getElementsByTagName("Y")[0].firstChild.nodeValue);

if (prexscale == "NaN")
{
//eh... just skip this one.
} else if (itemList[i].getAttribute('xsi:type') == "TextureItem")
{
var imageAsset = itemList[i].getElementsByTagName("texture_filename")[0].firstChild.nodeValue;
var pointer = imageAsset.lastIndexOf('\\');

imageAsset = imageAsset.slice(pointer+1, imageAsset.length);
imageAsset = "art/" + imageAsset;

thing = new Object;
initThing(thing);
thing.name = "worldart" + i;

thing.pos = new RVector(xxval, yyval);
thing.angle = rotation;
thing.xscale = xscale;
thing.yscale = yscale;

thing.typeID = OBJECT_WORLD_GRAPHIC;
thing.sprite = new Object;
thing.sprite.scale = 1.0;
thing.sprite.image = new Image();
thing.sprite.image.src = imageAsset;//new String(imageAsset);

gThings[gThings.length] = thing;

} else {
alert("BOGUS GRAPHICS ITEM!");
}

//itemList[i]
}

// OBJECTS
var itemList = layerList[2].getElementsByTagName("Item");


var loopEnd = itemList.length;
for (var i = 0; i < loopEnd; i++)
{

var Positions = itemList[i].getElementsByTagName("Position");

var xval = Positions[0].getElementsByTagName("X")[0].firstChild.nodeValue;
xxval = parseInt(xval);

var yval = Positions[0].getElementsByTagName("Y")[0].firstChild.nodeValue;
yyval = parseInt(yval);


if (itemList[i].getAttribute('xsi:type') == "PathItem")
{
// ignore for a bit...
}
else if (itemList[i].getAttribute('xsi:type') == "CircleItem")
{
var radius = parseInt(itemList[i].getElementsByTagName("Radius")[0].firstChild.nodeValue);

var hp = 30; // default hp
var range = 200; // default range for firing
var rate = 30; // default cannon cooldown
var strength = 10; // default damage of balls
var shipClass = "";

var Properties = itemList[i].getElementsByTagName("Property");

for (var pIndex = 0; pIndex < Properties.length; pIndex++)
{

if (Properties[pIndex].getAttribute('Name') == "hp")
{
var theStrings = Properties[pIndex].getElementsByTagName("string");
if (theStrings != undefined && theStrings.length > 0)
hp = parseInt(theStrings[0].firstChild.nodeValue);
} else if (Properties[pIndex].getAttribute('Name') == "range")
{
var theStrings = Properties[pIndex].getElementsByTagName("string");
if (theStrings != undefined && theStrings.length > 0)
range = parseInt(theStrings[0].firstChild.nodeValue);
} else if (Properties[pIndex].getAttribute('Name') == "rate")
{
var theStrings = Properties[pIndex].getElementsByTagName("string");
if (theStrings != undefined && theStrings.length > 0)
rate = parseFloat(theStrings[0].firstChild.nodeValue);
} else if (Properties[pIndex].getAttribute('Name') == "object_type")
{
var theStrings = Properties[pIndex].getElementsByTagName("string");
if (theStrings != undefined && theStrings.length > 0)
shipClass = theStrings[0].firstChild.nodeValue;
}
}

CreateBadguy(xxval, yyval, radius, hp, range, rate, strength, shipClass);

} else {
alert("BOGUS OBJECT ITEM!");
}
}

}

function CreateBadguy(posx, posy, radius, hp, range, rate, strength, shipClass)
{
var thing = new Object;
Expand Down

0 comments on commit 7669e53

Please sign in to comment.