aeroson

Untitled

Jul 21st, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. statemachine class CKNGWISP extends W3UsableItem {
  2.  
  3. private var wispCurrentRotationCircleAngle : float;
  4.  
  5. private var wispCurrentRotation : EulerAngles;
  6.  
  7. private var wispCurrentPosition : Vector;
  8. private var wispCurrentVelocity : Vector;
  9. private var wispCurrentAcceleration : Vector;
  10.  
  11. private var wispGoalPositionOffset : Vector;
  12. private var wispGoalPositionOffset_regenerateInSeconds : float;
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. private var isActive : bool;
  20.  
  21. private var entity : CEntity;
  22. private var entityTemplate : CEntityTemplate;
  23. private var resourcePath : string;
  24.  
  25.  
  26.  
  27. default itemType = UI_Apple;
  28. default isActive = false;
  29.  
  30. event OnUsed(usedBy : CEntity) {
  31.  
  32. if(usedBy == thePlayer && !isActive) {
  33.  
  34. SpawnWisp();
  35.  
  36. }
  37. }
  38.  
  39. event OnHidden(hiddenBy : CEntity) {
  40.  
  41. if(hiddenBy == thePlayer && isActive) {
  42.  
  43. entity.RemoveTimer('FollowPlayer');
  44. entity.StopAllEffects();
  45. entity.DestroyAfter(0.1);
  46.  
  47. isActive = false;
  48.  
  49. }
  50. }
  51.  
  52. event OnSpawned( spawnData : SEntitySpawnData ) {
  53.  
  54. entity.AddTimer('CheckIfFactExists', 2, false);
  55. }
  56.  
  57. timer function CheckIfFactExists(deltaTime : float, id : int) {
  58.  
  59. theGame.GetGuiManager().ShowNotification(FactsDoesExist("kngwisp_used"));
  60.  
  61. if(FactsDoesExist("kngwisp_used")) {
  62.  
  63. theGame.GetGuiManager().ShowNotification("Fact does exist!");
  64.  
  65. }
  66.  
  67.  
  68. }
  69.  
  70.  
  71. private function SpawnWisp() {
  72.  
  73. resourcePath = "dlc\dlckngwisp\data\w2ent\kngwisp.w2ent";
  74.  
  75. entityTemplate = (CEntityTemplate)LoadResource(resourcePath,true);
  76.  
  77. entity = theGame.CreateEntity(entityTemplate, thePlayer.GetWorldPosition()+Vector(0,0,2), thePlayer.GetWorldRotation());
  78.  
  79. entity.AddTag('KNGWISP');
  80.  
  81. isActive = true;
  82.  
  83. thePlayer.PlayVoiceset( 100, "Swears" );
  84.  
  85. entity.AddTimer('FollowPlayer', 0.0001, true);
  86.  
  87. // set wisp initial position
  88. wispCurrentPosition = thePlayer.GetWorldPosition()+Vector(0,0,2);
  89.  
  90. // set wisp initial velocity to be like it was released upwards
  91. wispCurrentVelocity.X = 0;
  92. wispCurrentVelocity.Y = 0;
  93. wispCurrentVelocity.Z = 10;
  94. }
  95.  
  96. timer function FollowPlayer(deltaTime : float, id : int) {
  97.  
  98. var playerPos : Vector;
  99. var playerRot : EulerAngles;
  100. var wispGoalPos : Vector;
  101. var goalAcceleration : Vector;
  102. var navigationComputeZReturn : float;
  103.  
  104.  
  105. // CONSTANTS start
  106. var wispRotationCircleRadius : float;
  107. var wispSpeed : float;
  108. var rotationSpeed : float;
  109. var wispAccelerationSpeed : float;
  110. var maxWispAcceleration : float;
  111. var maxWispVelocity : float;
  112. wispSpeed = 25;
  113. wispRotationCircleRadius = 0.75;
  114. rotationSpeed = 0.3;
  115. wispAccelerationSpeed = 100;
  116. maxWispAcceleration = 100;
  117. maxWispVelocity = 20;
  118. // CONSTANTS end
  119.  
  120. // increase
  121. wispCurrentRotationCircleAngle += deltaTime * wispSpeed;
  122. // if rotation incresed over 360 degrees make sure its under 360
  123. while(wispCurrentRotationCircleAngle > 360) {
  124. wispCurrentRotationCircleAngle -= 360;
  125. }
  126.  
  127. playerPos = thePlayer.GetWorldPosition();
  128. playerRot = thePlayer.GetWorldRotation();
  129.  
  130. // calculate wisp Goal position
  131. wispGoalPos.X = playerPos.X + CosF(Deg2Rad(wispCurrentRotationCircleAngle)) * wispRotationCircleRadius;
  132. wispGoalPos.Y = playerPos.Y + SinF(Deg2Rad(wispCurrentRotationCircleAngle)) * wispRotationCircleRadius;
  133. wispGoalPos.Z = playerPos.Z + 1.25;
  134. wispGoalPos += wispGoalPositionOffset;
  135.  
  136. // rotate wisp it self over time
  137. wispCurrentRotation.Yaw += deltaTime * rotationSpeed;
  138.  
  139. // see if we can set new random wisp position offset
  140. wispGoalPositionOffset_regenerateInSeconds -= deltaTime;
  141. if(wispGoalPositionOffset_regenerateInSeconds < 0) {
  142. wispGoalPositionOffset_regenerateInSeconds = RandRangeF(1, 0.1);
  143. wispGoalPositionOffset = VecRand() * 0.1;
  144. }
  145.  
  146. // find the Goal acceleration
  147. goalAcceleration = wispGoalPos - wispCurrentPosition;
  148. // accelerate towards Goal movement vector
  149. wispCurrentAcceleration = VecInterpolate(wispCurrentAcceleration, goalAcceleration, deltaTime * wispAccelerationSpeed);
  150.  
  151. // clamp wisp acceleration, so its not too fast
  152. if(VecLength(wispCurrentAcceleration) > maxWispAcceleration) {
  153. wispCurrentAcceleration = VecNormalize(wispCurrentAcceleration) * maxWispAcceleration;
  154. }
  155.  
  156. // simulate acceleration and velocity for more natural movement
  157. wispCurrentVelocity += wispCurrentAcceleration * deltaTime;
  158.  
  159. // clamp wisp velocity, so its not too fast
  160. if(VecLength(wispCurrentVelocity) > maxWispVelocity) {
  161. wispCurrentVelocity = VecNormalize(wispCurrentVelocity) * maxWispVelocity;
  162. }
  163.  
  164. // get the height
  165. if (theGame.GetWorld().NavigationComputeZ( wispCurrentPosition, wispCurrentPosition.Z - 1, wispCurrentPosition.Z + 1, navigationComputeZReturn ) )
  166. {
  167. // if wisp is too close to collision, bounce off it and loose some velocity
  168. if(AbsF(wispCurrentPosition.Z - navigationComputeZReturn) < 0.1) {
  169. wispCurrentVelocity.Z *= -0.9;
  170. }
  171. }
  172.  
  173. // simulate acceleration and velocity for more natural movement
  174. wispCurrentPosition += wispCurrentVelocity * deltaTime;
  175.  
  176. // if wisp is too far away, just teleport it back to player
  177. if(VecDistance(wispCurrentPosition, wispGoalPos) > 100) {
  178. wispCurrentAcceleration.X = 0;
  179. wispCurrentAcceleration.Y = 0;
  180. wispCurrentAcceleration.Z = 0;
  181. wispCurrentVelocity.X = 0;
  182. wispCurrentVelocity.Y = 0;
  183. wispCurrentVelocity.Z = 0;
  184. wispCurrentPosition = wispGoalPos;
  185. }
  186.  
  187. // finall set the wisp position and rotation
  188. this.TeleportWithRotation(wispCurrentPosition, wispCurrentRotation);
  189.  
  190. }
  191.  
  192.  
  193. }
  194.  
  195. exec function spawnWisp() {
  196.  
  197. // var mKNGWISP : CKNGWISP;
  198. // mKNGWISP = new CKNGWISP in theGame;
  199. // mKNGWISP.Spawn();
  200.  
  201. var entity : CEntity;
  202. var entityTemplate : CEntityTemplate;
  203. var resourcePath : string;
  204.  
  205. resourcePath = "dlc\dlckngwisp\data\w2ent\kngwisp.w2ent";
  206.  
  207. entityTemplate = (CEntityTemplate)LoadResource(resourcePath,true);
  208.  
  209. entity = theGame.CreateEntity(entityTemplate,thePlayer.GetWorldPosition()+Vector(0,0,2), thePlayer.GetWorldRotation());
  210.  
  211. entity.AddTag('KNGWISP');
  212.  
  213.  
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment