aeroson

Untitled

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