| 1 | Engine.LoadComponentScript("Visibility.js"); |
| 2 | |
| 3 | const VIS_HIDDEN = 0; |
| 4 | const VIS_FOGGED = 1; |
| 5 | const VIS_VISIBLE = 2; |
| 6 | |
| 7 | let maxPlayers = 8; |
| 8 | let owner = 3; |
| 9 | |
| 10 | function testVisbility(template, test) |
| 11 | { |
| 12 | ResetState(); |
| 13 | |
| 14 | AddMock(SYSTEM_ENTITY, IID_RangeManager, { |
| 15 | "ActivateScriptedVisibility": (entity, status) => {} |
| 16 | }); |
| 17 | |
| 18 | let entityID = 804; |
| 19 | |
| 20 | AddMock(entityID, IID_Ownership, { |
| 21 | "GetOwner": () => owner |
| 22 | }); |
| 23 | |
| 24 | let cmpVisibility = ConstructComponent(entityID, "Visibility", template); |
| 25 | |
| 26 | TS_ASSERT_EQUALS(cmpVisibility.GetRetainInFog(), template.RetainInFog == "true"); |
| 27 | TS_ASSERT_EQUALS(cmpVisibility.GetAlwaysVisible(), template.AlwaysVisible == "true"); |
| 28 | |
| 29 | TS_ASSERT_EQUALS(cmpVisibility.IsActivated(), template.Corpse == "true" || template.Preview == "true"); |
| 30 | for (let value of [true, false, true]) |
| 31 | { |
| 32 | cmpVisibility.SetActivated(value); |
| 33 | TS_ASSERT_EQUALS(cmpVisibility.IsActivated(), value); |
| 34 | } |
| 35 | |
| 36 | test(cmpVisibility); |
| 37 | } |
| 38 | |
| 39 | // Test corpse |
| 40 | testVisbility({ |
| 41 | "RetainInFog": "true", |
| 42 | "AlwaysVisible": "false", |
| 43 | "Corpse": "true", |
| 44 | "Preview": "false" |
| 45 | }, cmpVisibility => { |
| 46 | |
| 47 | for (let [isVisible, isExplored] of [[true, true], [false, false], [false, true]]) |
| 48 | for (let playerID = 0; playerID <= maxPlayers; ++playerID) |
| 49 | TS_ASSERT_EQUALS( |
| 50 | cmpVisibility.GetVisibility(playerID, isVisible, isExplored), |
| 51 | isVisible ? VIS_VISIBLE : |
| 52 | isExplored && playerID == owner ? VIS_FOGGED: VIS_HIDDEN |
| 53 | ); |
| 54 | }); |
| 55 | |
| 56 | // Preview should be visible only to the owner |
| 57 | testVisbility({ |
| 58 | "RetainInFog": "true", |
| 59 | "AlwaysVisible": "false", |
| 60 | "Corpse": "false", |
| 61 | "Preview": "true" |
| 62 | }, cmpVisibility => { |
| 63 | |
| 64 | for (let [isVisible, isExplored] of [[true, true], [false, false], [false, true]]) |
| 65 | for (let playerID = 0; playerID <= maxPlayers; ++playerID) |
| 66 | TS_ASSERT_EQUALS( |
| 67 | cmpVisibility.GetVisibility(playerID, isVisible, isExplored), |
| 68 | playerID == owner ? |
| 69 | (isVisible ? |
| 70 | VIS_VISIBLE : |
| 71 | isExplored ? |
| 72 | VIS_FOGGED : |
| 73 | VIS_HIDDEN) : |
| 74 | VIS_HIDDEN); |
| 75 | }); |
| 76 | |
| 77 | // Test regular entity |
| 78 | // Notice GetVisibility returns VIS_VISIBLE even if isVisible and isExplored are false |
| 79 | // TODO: can [false, false] occur even or should an argument be removed? |
| 80 | testVisbility({ |
| 81 | "RetainInFog": "true", |
| 82 | "AlwaysVisible": "false", |
| 83 | "Corpse": "false", |
| 84 | "Preview": "false" |
| 85 | }, cmpVisibility => { |
| 86 | |
| 87 | for (let [isVisible, isExplored] of [[true, true], [false, false], [false, true]]) |
| 88 | for (let playerID = 0; playerID <= maxPlayers; ++playerID) |
| 89 | TS_ASSERT_EQUALS(cmpVisibility.GetVisibility(playerID, isVisible, isExplored), VIS_VISIBLE); |
| 90 | }); |
| 91 | |
| 92 | // Test GetAlwaysVisible |
| 93 | testVisbility({ |
| 94 | "RetainInFog": "true", |
| 95 | "AlwaysVisible": "true", |
| 96 | "Corpse": "false", |
| 97 | "Preview": "false" |
| 98 | }, function() {}); |