1
0
forked from mirrors/0ad

Move SimpleGroup and RandomGroup from placers.js to a new file, because they aren't Placers, refs #4804.

Document what a Group is, refs #4831.
Remove duplicate place function by calling the SimpleGroup from the
RandomGroup, refs #4805.
Move the default parameters from the function body to the function
header, remove duplicate CELL_SIZE division, pointless comment and use
let instead of var.

This was SVN commit r20355.
This commit is contained in:
elexis
2017-10-26 16:47:07 +00:00
parent 77f28c5a56
commit f6c2ea6b0e
2 changed files with 73 additions and 90 deletions
@@ -0,0 +1,73 @@
/**
* A Group tests if a set of entities specified in the constructor can be placed and
* potentially places some of them (typically all or none).
*
* The location is defined by the x and z property of the Group instance and can be modified externally.
* The Group is free to determine whether, where exactly and how many entities to place.
*
* The Constraint to test against and the future owner of the entities are passed by the caller.
* Typically Groups are called from createObjectGroup with the location set in the constructor or
* from createObjectGroups that randomizes the x and z property of the Group before calling place.
*/
/**
* Places all of the given Objects.
*
* @param objects - An array of Objects, for instance SimpleObjects.
* @param avoidSelf - Objects will not overlap.
* @param tileClass - Optional TileClass that tiles with placed entities are marked with.
* @param x, z - The location the group is placed around. Can be omitted if the x and z properties are set externally.
*/
function SimpleGroup(objects, avoidSelf = false, tileClass = undefined, x = -1, z = -1)
{
this.objects = objects;
this.tileClass = tileClass;
this.avoidSelf = avoidSelf;
this.x = x;
this.z = z;
}
SimpleGroup.prototype.place = function(player, constraint)
{
let resultObjs = [];
// Test if the Objects can be placed at the given location
// Place none of them if one can't be placed.
for (let object of this.objects)
{
let objs = object.place(this.x, this.z, player, this.avoidSelf, constraint);
if (objs === undefined)
return undefined;
resultObjs = resultObjs.concat(objs);
}
// Add all objects to the map
for (let obj of resultObjs)
{
let x = obj.position.x / CELL_SIZE;
let z = obj.position.z / CELL_SIZE;
if (g_Map.validT(x, z))
g_Map.addObject(obj);
if (this.tileClass !== undefined)
getTileClass(this.tileClass).add(Math.floor(x), Math.floor(z));
}
return resultObjs;
};
/**
* Randomly choses one of the given Objects and places it just like the SimpleGroup.
*/
function RandomGroup(objects, avoidSelf = false, tileClass = undefined, x = -1, z = -1)
{
this.simpleGroup = new SimpleGroup([pickRandom(objects)], avoidSelf, tileClass, x, z);
}
RandomGroup.prototype.place = function(player, constraint)
{
return this.simpleGroup.place(player, constraint);
};
@@ -483,93 +483,3 @@ RandomObject.prototype.place = function(cx, cz, player, avoidSelf, constraint, m
return resultObjs;
};
/////////////////////////////////////////////////////////////////////////////////////////
// SimpleGroup
//
// Class for placing groups of different objects
//
// elements: Array of SimpleObjects
// avoidSelf: Objects will not overlap
// tileClass: Optional tile class to add with these objects
// x,z: Tile coordinates of center of placer
//
/////////////////////////////////////////////////////////////////////////////////////////
function SimpleGroup(elements, avoidSelf, tileClass, x, z)
{
this.elements = elements;
this.tileClass = tileClass !== undefined ? getTileClass(tileClass) : undefined;
this.avoidSelf = avoidSelf !== undefined ? avoidSelf : false;
this.x = x !== undefined ? x : -1;
this.z = z !== undefined ? z : -1;
}
SimpleGroup.prototype.place = function(player, constraint)
{
var resultObjs = [];
// Try placement of objects
for (let element of this.elements)
{
var objs = element.place(this.x, this.z, player, this.avoidSelf, constraint);
if (objs === undefined)
return undefined;
resultObjs = resultObjs.concat(objs);
}
// Add placed objects to map
for (let obj of resultObjs)
{
if (g_Map.validT(obj.position.x / CELL_SIZE, obj.position.z / CELL_SIZE))
g_Map.addObject(obj);
// Convert position to integer number of tiles
if (this.tileClass !== undefined)
this.tileClass.add(Math.floor(obj.position.x/CELL_SIZE), Math.floor(obj.position.z/CELL_SIZE));
}
return resultObjs;
};
/////////////////////////////////////////////////////////////////////////////////////////
// RandomGroup
//
// Class for placing group of a random simple object
//
// elements: Array of SimpleObjects
// avoidSelf: Objects will not overlap
// tileClass: Optional tile class to add with these objects
// x,z: Tile coordinates of center of placer
//
/////////////////////////////////////////////////////////////////////////////////////////
function RandomGroup(elements, avoidSelf, tileClass, x, z)
{
this.elements = elements;
this.tileClass = tileClass !== undefined ? getTileClass(tileClass) : undefined;
this.avoidSelf = avoidSelf !== undefined ? avoidSelf : false;
this.x = x !== undefined ? x : -1;
this.z = z !== undefined ? z : -1;
}
RandomGroup.prototype.place = function(player, constraint)
{
var resultObjs = pickRandom(this.elements).place(this.x, this.z, player, this.avoidSelf, constraint);
if (resultObjs === undefined)
return undefined;
// Add placed objects to map
for (let obj of resultObjs)
{
g_Map.addObject(obj);
// Convert position to integer number of tiles
if (this.tileClass !== undefined)
this.tileClass.add(Math.floor(obj.position.x/CELL_SIZE), Math.floor(obj.position.z/CELL_SIZE));
}
return resultObjs;
};