Advertisement
aeroson

Untitled

Jul 21st, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 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 and initial velocity to be like it was released upwards
  88. wispCurrentPosition = thePlayer.GetWorldPosition() + Vector(0,0,2);
  89. wispCurrentVelocity = Vector(0,0,10);
  90. wispCurrentAcceleration = Vector(0,0,0);
  91. }
  92.  
  93. timer function FollowPlayer(deltaTime : float, id : int) {
  94.  
  95. var playerPosition : Vector;
  96. var playerRotation : EulerAngles;
  97. var wispGoalPosition : Vector;
  98. var goalAcceleration : Vector;
  99. var navigationComputeZReturn : float;
  100.  
  101.  
  102. // CONSTANTS start
  103. var rotationCircleRadius : float;
  104. var rotationCircleMovementSpeed : float;
  105. var selfRotationSpeed : float;
  106. var maxAcceleration : float;
  107. var maxVelocity : float;
  108. var velocityDampeningFactor : float;
  109. rotationCircleMovementSpeed = 25;
  110. rotationCircleRadius = 0.75;
  111. selfRotationSpeed = 0.3;
  112. maxAcceleration = 100;
  113. maxVelocity = 20;
  114. velocityDampeningFactor = 0.9;
  115. // CONSTANTS end
  116.  
  117. // increase
  118. wispCurrentRotationCircleAngle += deltaTime * rotationCircleMovementSpeed;
  119. // if rotation incresed over 360 degrees make sure its under 360
  120. while(wispCurrentRotationCircleAngle > 360) {
  121. wispCurrentRotationCircleAngle -= 360;
  122. }
  123.  
  124. playerPosition = thePlayer.GetWorldPosition();
  125. playerRotation = thePlayer.GetWorldRotation();
  126.  
  127. // calculate wisp Goal position
  128. wispGoalPosition = playerPosition;
  129. wispGoalPosition += wispGoalPositionOffset;
  130. wispGoalPosition.X += CosF(Deg2Rad(wispCurrentRotationCircleAngle)) * rotationCircleRadius;
  131. wispGoalPosition.Y += SinF(Deg2Rad(wispCurrentRotationCircleAngle)) * rotationCircleRadius;
  132. wispGoalPosition.Z += 2;
  133.  
  134. // see if we can set new random wisp position offset
  135. wispGoalPositionOffset_regenerateInSeconds -= deltaTime;
  136. if(wispGoalPositionOffset_regenerateInSeconds < 0) {
  137. wispGoalPositionOffset_regenerateInSeconds = RandRangeF(1, 0.1);
  138. wispGoalPositionOffset = VecRand() * rotationCircleRadius * 0.25;
  139. }
  140.  
  141. // accelerate towards goal position
  142. wispCurrentAcceleration = wispGoalPosition - wispCurrentPosition;
  143.  
  144. // clamp wisp acceleration, so its not too fast
  145. if(VecLength(wispCurrentAcceleration) > maxAcceleration) {
  146. wispCurrentAcceleration = VecNormalize(wispCurrentAcceleration) * maxAcceleration;
  147. }
  148.  
  149. // simulate acceleration and velocity for more natural movement
  150. wispCurrentVelocity += wispCurrentAcceleration * deltaTime;
  151.  
  152. // dampen velocity
  153. wispCurrentVelocity *= velocityDampeningFactor;
  154.  
  155. // clamp wisp velocity, so its not too fast
  156. if(VecLength(wispCurrentVelocity) > maxVelocity) {
  157. wispCurrentVelocity = VecNormalize(wispCurrentVelocity) * maxVelocity;
  158. }
  159.  
  160. // get the height
  161. if (theGame.GetWorld().NavigationComputeZ( wispCurrentPosition, wispCurrentPosition.Z - 1, wispCurrentPosition.Z + 1, navigationComputeZReturn ) )
  162. {
  163. // if wisp is too close to collision, bounce off it and loose some velocity
  164. if(AbsF(wispCurrentPosition.Z - navigationComputeZReturn) < 0.1) {
  165. wispCurrentVelocity.Z *= -0.9;
  166. }
  167. }
  168.  
  169. // simulate acceleration and velocity for more natural movement
  170. wispCurrentPosition += wispCurrentVelocity * deltaTime;
  171.  
  172. // if wisp is too far away, just teleport it back to player
  173. if(VecDistance(wispCurrentPosition, wispGoalPosition) > 100) {
  174. wispCurrentPosition = wispGoalPosition;
  175. wispCurrentVelocity = Vector(0,0,0);
  176. wispCurrentAcceleration = Vector(0,0,0);
  177. }
  178.  
  179. // rotate wisp it self over time
  180. wispCurrentRotation.Pitch += deltaTime * selfRotationSpeed;
  181.  
  182. // finall set the wisp position and rotation
  183. this.TeleportWithRotation(wispCurrentPosition, wispCurrentRotation);
  184.  
  185. }
  186.  
  187.  
  188. }
  189.  
  190. exec function spawnWisp() {
  191.  
  192. // var mKNGWISP : CKNGWISP;
  193. // mKNGWISP = new CKNGWISP in theGame;
  194. // mKNGWISP.Spawn();
  195.  
  196. var entity : CEntity;
  197. var entityTemplate : CEntityTemplate;
  198. var resourcePath : string;
  199.  
  200. resourcePath = "dlc\dlckngwisp\data\w2ent\kngwisp.w2ent";
  201.  
  202. entityTemplate = (CEntityTemplate)LoadResource(resourcePath,true);
  203.  
  204. entity = theGame.CreateEntity(entityTemplate,thePlayer.GetWorldPosition()+Vector(0,0,2), thePlayer.GetWorldRotation());
  205.  
  206. entity.AddTag('KNGWISP');
  207.  
  208.  
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement