Advertisement
aeroson

Untitled

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