0ad/binaries/data/mods/public/simulation/components/tests/test_UnitMotionFlying.js
Itms c0708da215 Fix the Mustang template. Add tests for the UnitMotionFlying component.
Patch by bb.
Reviewers: Itms

Differential Revision: https://code.wildfiregames.com/D139
This was SVN commit r19373.
2017-04-03 09:58:01 +00:00

146 lines
4.8 KiB
JavaScript

Engine.LoadComponentScript("UnitMotionFlying.js");
Engine.LoadComponentScript("interfaces/Health.js");
Engine.LoadComponentScript("interfaces/GarrisonHolder.js");
let entity = 1;
let target = 2;
let height = 5;
AddMock(SYSTEM_ENTITY, IID_Pathfinder, {
GetPassabilityClass: (name) => 1 << 8
});
let cmpUnitMotionFlying = ConstructComponent(entity, "UnitMotionFlying", {
"MaxSpeed": 1.0,
"TakeoffSpeed": 0.5,
"LandingSpeed": 0.5,
"AccelRate": 0.0005,
"SlowingRate": 0.001,
"BrakingRate": 0.0005,
"TurnRate": 0.1,
"OvershootTime": 10,
"FlyingHeight": 100,
"ClimbRate": 0.1,
"DiesInWater": false,
"PassabilityClass": "unrestricted"
});
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetWalkSpeed(), 1.0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetRunSpeed(), 1.0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0);
cmpUnitMotionFlying.SetSpeed(2.0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetWalkSpeed(), 1.0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetRunSpeed(), 1.0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetPassabilityClassName(), "unrestricted");
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetPassabilityClass(), 1 << 8);
AddMock(entity, IID_Position, {
"IsInWorld": () => true,
"GetPosition2D": () => { return { "x": 50, "y": 100 }; },
"GetPosition": () => { return { "x": 50, "y": height, "z": 100 }; },
"GetRotation": () => { return { "y": 3.14 }; },
"SetHeightFixed": (y) => height = y,
"TurnTo": () => {},
"SetXZRotation": () => {},
"MoveTo": () => {}
});
AddMock(target, IID_Position, {
"IsInWorld": () => true,
"GetPosition2D": () => { return { "x": 100, "y": 200 }; }
});
TS_ASSERT_EQUALS(cmpUnitMotionFlying.IsInTargetRange(target, 10, 112), true);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.IsInTargetRange(target, 50, 111), false);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.IsInTargetRange(target, 112, 200), false);
AddMock(entity, IID_GarrisonHolder, {
"AllowGarrisoning": () => {}
});
AddMock(entity, IID_Health, {
});
AddMock(entity, IID_RangeManager, {
"GetLosCircular": () => true
});
AddMock(entity, IID_Terrain, {
"GetGroundLevel": () => 4,
"GetTilesPerSide": () => 5
});
AddMock(entity, IID_WaterManager, {
"GetWaterLevel": () => 5
});
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 500 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.MoveToTargetRange(target, 0, 10), true);
TS_ASSERT_EQUALS(cmpUnitMotionFlying.MoveToPointRange(100, 200, 0, 20), true);
// Take Off
cmpUnitMotionFlying.OnUpdate({ "turnLength": 500 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0.25);
TS_ASSERT_EQUALS(height, 5);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 500 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0.5);
TS_ASSERT_EQUALS(height, 5);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 0 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0.5);
TS_ASSERT_EQUALS(height, 5);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 500 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0.75);
TS_ASSERT_EQUALS(height, 55);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 500 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 1);
TS_ASSERT_EQUALS(height, 105);
// Fly
cmpUnitMotionFlying.OnUpdate({ "turnLength": 100 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 1);
TS_ASSERT_EQUALS(height, 105);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 500 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 1);
TS_ASSERT_EQUALS(height, 105);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 0 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 1);
TS_ASSERT_EQUALS(height, 105);
// Land
cmpUnitMotionFlying.StopMoving();
cmpUnitMotionFlying.OnUpdate({ "turnLength": 0 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 1);
TS_ASSERT_EQUALS(height, 105);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 500 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0.5);
TS_ASSERT_EQUALS(height, 5);
// Slide
cmpUnitMotionFlying.OnUpdate({ "turnLength": 500 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0.25);
TS_ASSERT_EQUALS(height, 5);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 0 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0.25);
TS_ASSERT_EQUALS(height, 5);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 500 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0);
TS_ASSERT_EQUALS(height, 5);
// Stay
cmpUnitMotionFlying.OnUpdate({ "turnLength": 300 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0);
TS_ASSERT_EQUALS(height, 5);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 0 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0);
TS_ASSERT_EQUALS(height, 5);
cmpUnitMotionFlying.OnUpdate({ "turnLength": 900 });
TS_ASSERT_EQUALS(cmpUnitMotionFlying.GetCurrentSpeed(), 0);
TS_ASSERT_EQUALS(height, 5);