2 * @file A Group tests if a set of entities specified in the constructor can be placed and
3 * potentially places some of them (typically all or none).
5 * The location is defined by the x and z property of the Group instance and can be modified externally.
6 * The Group is free to determine whether, where exactly and how many entities to place.
8 * The Constraint to test against and the future owner of the entities are passed by the caller.
9 * Typically Groups are called from createObjectGroup with the location set in the constructor or
10 * from createObjectGroups that randomizes the x and z property of the Group before calling place.
14 * Places all of the given Objects.
16 * @param objects - An array of Objects, for instance SimpleObjects.
17 * @param avoidSelf - Objects will not overlap.
18 * @param tileClass - Optional TileClass that tiles with placed entities are marked with.
19 * @param centerPosition - The location the group is placed around. Can be omitted if the property is set externally.
21 function SimpleGroup(objects, avoidSelf = false, tileClass = undefined, centerPosition = undefined)
23 this.objects = objects;
24 this.tileClass = tileClass;
25 this.avoidSelf = avoidSelf;
26 this.centerPosition = undefined;
29 this.setCenterPosition(centerPosition);
32 SimpleGroup.prototype.setCenterPosition = function(position)
34 this.centerPosition = deepfreeze(position.clone().round());
37 SimpleGroup.prototype.place = function(player, constraint)
39 let entitySpecsResult = [];
41 // Test if the Objects can be placed at the given location
42 // Place none of them if one can't be placed.
43 for (let object of this.objects)
45 let entitySpecs = object.place(this.centerPosition, player, this.avoidSelf, constraint);
50 entitySpecsResult = entitySpecsResult.concat(entitySpecs);
54 // Create and place entities as specified
56 for (let entitySpecs of entitySpecsResult)
58 // The Object must ensure that non-actor entities are not placed at the impassable map-border
60 g_Map.placeEntityAnywhere(entitySpecs.templateName, entitySpecs.playerID, entitySpecs.position, entitySpecs.angle));
63 this.tileClass.add(entitySpecs.position.clone().floor());
70 * Randomly choses one of the given Objects and places it just like the SimpleGroup.
72 function RandomGroup(objects, avoidSelf = false, tileClass = undefined, centerPosition = undefined)
74 this.simpleGroup = new SimpleGroup([pickRandom(objects)], avoidSelf, tileClass, centerPosition);
77 RandomGroup.prototype.setCenterPosition = function(position)
79 this.simpleGroup.setCenterPosition(position);
82 RandomGroup.prototype.place = function(player, constraint)
84 return this.simpleGroup.place(player, constraint);