aeroson

Untitled

Jul 21st, 2016
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.70 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 = 10;
  93. wispCurrentVelocity.Z = 0;
  94.  
  95.  
  96. }
  97.  
  98. timer function FollowPlayer(deltaTime : float, id : int) {
  99.  
  100. var playerPos : Vector;
  101. var playerRot : EulerAngles;
  102. var wispGoalPos : Vector;
  103. var goalAcceleration : Vector;
  104.  
  105.  
  106. // CONSTANTS start
  107. var wispRotationCircleRadius : float;
  108. var wispSpeed : float;
  109. var rotationSpeed : float;
  110. var wispAccelerationSpeed : float;
  111. var maxWispAcceleration : float;
  112. var maxWispVelocity : float;
  113. wispSpeed = 50;
  114. wispRotationCircleRadius = 0.75;
  115. rotationSpeed = 0.3;
  116. wispAccelerationSpeed = 3;
  117. maxWispAcceleration = 5;
  118. maxWispVelocity = 20;
  119. // CONSTANTS end
  120.  
  121. // increase
  122. wispCurrentRotationCircleAngle += deltaTime * wispSpeed;
  123. // if rotation incresed over 360 degrees make sure its under 360
  124. while(wispCurrentRotationCircleAngle > 360) {
  125. wispCurrentRotationCircleAngle -= 360;
  126. }
  127.  
  128. playerPos = thePlayer.GetWorldPosition();
  129. playerRot = thePlayer.GetWorldRotation();
  130.  
  131. // calculate wisp Goal position
  132. wispGoalPos.X = playerPos.X + CosF(Deg2Rad(wispCurrentRotationCircleAngle)) * wispRotationCircleRadius;
  133. wispGoalPos.Y = playerPos.Y + SinF(Deg2Rad(wispCurrentRotationCircleAngle)) * wispRotationCircleRadius;
  134. wispGoalPos.Z = playerPos.Z + 1.25;
  135. wispGoalPos += wispGoalPositionOffset;
  136.  
  137. // rotate wisp it self over time
  138. wispCurrentRotation.Yaw += deltaTime * rotationSpeed;
  139.  
  140. // see if we can set new random wisp position offset
  141. wispGoalPositionOffset_regenerateInSeconds -= deltaTime;
  142. if(wispGoalPositionOffset_regenerateInSeconds < 0) {
  143. wispGoalPositionOffset_regenerateInSeconds = RandRangeF(1, 0.1);
  144. wispGoalPositionOffset = VecRand() * 0.1;
  145. }
  146.  
  147. // find the Goal acceleration
  148. goalAcceleration = wispGoalPos - wispCurrentPosition;
  149. // accelerate towards Goal movement vector
  150. wispCurrentAcceleration = VecInterpolate(wispCurrentAcceleration, goalAcceleration, deltaTime * wispAccelerationSpeed);
  151.  
  152. // clamp wisp acceleration, so its not too fast
  153. while(AbsF(wispCurrentAcceleration.X) > maxWispAcceleration) wispCurrentAcceleration.X *= 0.9;
  154. while(AbsF(wispCurrentAcceleration.Y) > maxWispAcceleration) wispCurrentAcceleration.Y *= 0.9;
  155. while(AbsF(wispCurrentAcceleration.Z) > maxWispAcceleration) wispCurrentAcceleration.Z *= 0.9;
  156.  
  157. // simulate acceleration and velocity for more natural movement
  158. wispCurrentVelocity += wispCurrentAcceleration * deltaTime;
  159.  
  160. // clamp wisp velocity, so its not too fast
  161. while(AbsF(wispCurrentVelocity.X) > maxWispVelocity) wispCurrentVelocity.X *= 0.9;
  162. while(AbsF(wispCurrentVelocity.Y) > maxWispVelocity) wispCurrentVelocity.Y *= 0.9;
  163. while(AbsF(wispCurrentVelocity.Z) > maxWispVelocity) wispCurrentVelocity.Z *= 0.9;
  164.  
  165. // simulate acceleration and velocity for more natural movement
  166. wispCurrentPosition += wispCurrentVelocity * deltaTime;
  167.  
  168. // if wisp is too far away, just eleport it back to player
  169. if(VecDistance(wispCurrentPosition, wispGoalPos) > 10) {
  170. wispCurrentPosition = wispGoalPos;
  171. }
  172.  
  173. this.TeleportWithRotation(wispCurrentPosition, wispCurrentRotation);
  174.  
  175. }
  176.  
  177. }
  178.  
  179. exec function spawnWisp() {
  180.  
  181. // var mKNGWISP : CKNGWISP;
  182. // mKNGWISP = new CKNGWISP in theGame;
  183. // mKNGWISP.Spawn();
  184.  
  185. var entity : CEntity;
  186. var entityTemplate : CEntityTemplate;
  187. var resourcePath : string;
  188.  
  189. resourcePath = "dlc\dlckngwisp\data\w2ent\kngwisp.w2ent";
  190.  
  191. entityTemplate = (CEntityTemplate)LoadResource(resourcePath,true);
  192.  
  193. entity = theGame.CreateEntity(entityTemplate,thePlayer.GetWorldPosition()+Vector(0,0,2), thePlayer.GetWorldRotation());
  194.  
  195. entity.AddTag('KNGWISP');
  196.  
  197.  
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment