Line data Source code
1 : /**
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).
4 : *
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.
7 : *
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.
11 : */
12 :
13 : /**
14 : * Places all of the given Objects.
15 : *
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.
20 : */
21 : function SimpleGroup(objects, avoidSelf = false, tileClass = undefined, centerPosition = undefined)
22 : {
23 0 : this.objects = objects;
24 0 : this.tileClass = tileClass;
25 0 : this.avoidSelf = avoidSelf;
26 0 : this.centerPosition = undefined;
27 :
28 0 : if (centerPosition)
29 0 : this.setCenterPosition(centerPosition);
30 : }
31 :
32 7 : SimpleGroup.prototype.setCenterPosition = function(position)
33 : {
34 0 : this.centerPosition = deepfreeze(position.clone().round());
35 : };
36 :
37 7 : SimpleGroup.prototype.place = function(playerID, constraint)
38 : {
39 0 : let entitySpecsResult = [];
40 0 : let avoidPositions = this.avoidSelf ? [] : undefined;
41 :
42 : // Test if the Objects can be placed at the given location
43 : // Place none of them if one can't be placed.
44 0 : for (const object of this.objects)
45 : {
46 0 : const entitySpecs = object.place(this.centerPosition, playerID, avoidPositions, constraint, 30);
47 :
48 0 : if (!entitySpecs)
49 0 : return undefined;
50 :
51 0 : entitySpecsResult = entitySpecsResult.concat(entitySpecs);
52 :
53 0 : if (this.avoidSelf)
54 0 : avoidPositions = avoidPositions.concat(entitySpecs.map(entitySpec => ({
55 : "position": entitySpec.position,
56 : "distanceSquared": object.avoidDistanceSquared
57 : })));
58 : }
59 :
60 : // Create and place entities as specified
61 0 : const entities = [];
62 0 : for (const entitySpecs of entitySpecsResult)
63 : {
64 : // The Object must ensure that non-actor entities are not placed at the impassable map-border
65 0 : entities.push(
66 : g_Map.placeEntityAnywhere(entitySpecs.templateName, entitySpecs.playerID, entitySpecs.position, entitySpecs.angle));
67 :
68 0 : if (this.tileClass)
69 0 : this.tileClass.add(entitySpecs.position.clone().floor());
70 : }
71 :
72 0 : return entities;
73 : };
74 :
75 : /**
76 : * Randomly choses one of the given Objects and places it just like the SimpleGroup.
77 : */
78 : function RandomGroup(objects, avoidSelf = false, tileClass = undefined, centerPosition = undefined)
79 : {
80 0 : this.simpleGroup = new SimpleGroup([pickRandom(objects)], avoidSelf, tileClass, centerPosition);
81 : }
82 :
83 7 : RandomGroup.prototype.setCenterPosition = function(position)
84 : {
85 0 : this.simpleGroup.setCenterPosition(position);
86 : };
87 :
88 7 : RandomGroup.prototype.place = function(playerID, constraint)
89 : {
90 0 : return this.simpleGroup.place(playerID, constraint);
91 : };
|