Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- statemachine class CKNGWISP extends W3UsableItem {
- private var wispCurrentRotationCircleAngle : float;
- private var wispCurrentRotation : EulerAngles;
- private var wispCurrentPosition : Vector;
- private var wispCurrentVelocity : Vector;
- private var wispCurrentAcceleration : Vector;
- private var wispGoalPositionOffset : Vector;
- private var wispGoalPositionOffset_regenerateInSeconds : float;
- private var isActive : bool;
- private var entity : CEntity;
- private var entityTemplate : CEntityTemplate;
- private var resourcePath : string;
- default itemType = UI_Apple;
- default isActive = false;
- event OnUsed(usedBy : CEntity) {
- if(usedBy == thePlayer && !isActive) {
- SpawnWisp();
- }
- }
- event OnHidden(hiddenBy : CEntity) {
- if(hiddenBy == thePlayer && isActive) {
- entity.RemoveTimer('FollowPlayer');
- entity.StopAllEffects();
- entity.DestroyAfter(0.1);
- isActive = false;
- }
- }
- event OnSpawned( spawnData : SEntitySpawnData ) {
- entity.AddTimer('CheckIfFactExists', 2, false);
- }
- timer function CheckIfFactExists(deltaTime : float, id : int) {
- theGame.GetGuiManager().ShowNotification(FactsDoesExist("kngwisp_used"));
- if(FactsDoesExist("kngwisp_used")) {
- theGame.GetGuiManager().ShowNotification("Fact does exist!");
- }
- }
- private function SpawnWisp() {
- resourcePath = "dlc\dlckngwisp\data\w2ent\kngwisp.w2ent";
- entityTemplate = (CEntityTemplate)LoadResource(resourcePath,true);
- entity = theGame.CreateEntity(entityTemplate, thePlayer.GetWorldPosition()+Vector(0,0,2), thePlayer.GetWorldRotation());
- entity.AddTag('KNGWISP');
- isActive = true;
- thePlayer.PlayVoiceset( 100, "Swears" );
- entity.AddTimer('FollowPlayer', 0.0001, true);
- // set wisp initial position
- wispCurrentPosition = thePlayer.GetWorldPosition()+Vector(0,0,2);
- // set wisp initial velocity to be like it was released upwards
- wispCurrentVelocity.X = 0;
- wispCurrentVelocity.Y = 0;
- wispCurrentVelocity.Z = 10;
- }
- timer function FollowPlayer(deltaTime : float, id : int) {
- var playerPos : Vector;
- var playerRot : EulerAngles;
- var wispGoalPos : Vector;
- var goalAcceleration : Vector;
- var navigationComputeZReturn : float;
- // CONSTANTS start
- var wispRotationCircleRadius : float;
- var wispSpeed : float;
- var rotationSpeed : float;
- var wispAccelerationSpeed : float;
- var maxWispAcceleration : float;
- var maxWispVelocity : float;
- wispSpeed = 25;
- wispRotationCircleRadius = 0.75;
- rotationSpeed = 0.3;
- wispAccelerationSpeed = 100;
- maxWispAcceleration = 100;
- maxWispVelocity = 20;
- // CONSTANTS end
- // increase
- wispCurrentRotationCircleAngle += deltaTime * wispSpeed;
- // if rotation incresed over 360 degrees make sure its under 360
- while(wispCurrentRotationCircleAngle > 360) {
- wispCurrentRotationCircleAngle -= 360;
- }
- playerPos = thePlayer.GetWorldPosition();
- playerRot = thePlayer.GetWorldRotation();
- // calculate wisp Goal position
- wispGoalPos.X = playerPos.X + CosF(Deg2Rad(wispCurrentRotationCircleAngle)) * wispRotationCircleRadius;
- wispGoalPos.Y = playerPos.Y + SinF(Deg2Rad(wispCurrentRotationCircleAngle)) * wispRotationCircleRadius;
- wispGoalPos.Z = playerPos.Z + 1.25;
- wispGoalPos += wispGoalPositionOffset;
- // rotate wisp it self over time
- wispCurrentRotation.Yaw += deltaTime * rotationSpeed;
- // see if we can set new random wisp position offset
- wispGoalPositionOffset_regenerateInSeconds -= deltaTime;
- if(wispGoalPositionOffset_regenerateInSeconds < 0) {
- wispGoalPositionOffset_regenerateInSeconds = RandRangeF(1, 0.1);
- wispGoalPositionOffset = VecRand() * 0.1;
- }
- // find the Goal acceleration
- goalAcceleration = wispGoalPos - wispCurrentPosition;
- // accelerate towards Goal movement vector
- wispCurrentAcceleration = VecInterpolate(wispCurrentAcceleration, goalAcceleration, deltaTime * wispAccelerationSpeed);
- // clamp wisp acceleration, so its not too fast
- if(VecLength(wispCurrentAcceleration) > maxWispAcceleration) {
- wispCurrentAcceleration = VecNormalize(wispCurrentAcceleration) * maxWispAcceleration;
- }
- // simulate acceleration and velocity for more natural movement
- wispCurrentVelocity += wispCurrentAcceleration * deltaTime;
- // clamp wisp velocity, so its not too fast
- if(VecLength(wispCurrentVelocity) > maxWispVelocity) {
- wispCurrentVelocity = VecNormalize(wispCurrentVelocity) * maxWispVelocity;
- }
- // get the height
- if (theGame.GetWorld().NavigationComputeZ( wispCurrentPosition, wispCurrentPosition.Z - 1, wispCurrentPosition.Z + 1, navigationComputeZReturn ) )
- {
- // if wisp is too close to collision, bounce off it and loose some velocity
- if(AbsF(wispCurrentPosition.Z - navigationComputeZReturn) < 0.1) {
- wispCurrentVelocity.Z *= -0.9;
- }
- }
- // simulate acceleration and velocity for more natural movement
- wispCurrentPosition += wispCurrentVelocity * deltaTime;
- // if wisp is too far away, just teleport it back to player
- if(VecDistance(wispCurrentPosition, wispGoalPos) > 100) {
- wispCurrentAcceleration.X = 0;
- wispCurrentAcceleration.Y = 0;
- wispCurrentAcceleration.Z = 0;
- wispCurrentVelocity.X = 0;
- wispCurrentVelocity.Y = 0;
- wispCurrentVelocity.Z = 0;
- wispCurrentPosition = wispGoalPos;
- }
- // finall set the wisp position and rotation
- this.TeleportWithRotation(wispCurrentPosition, wispCurrentRotation);
- }
- }
- exec function spawnWisp() {
- // var mKNGWISP : CKNGWISP;
- // mKNGWISP = new CKNGWISP in theGame;
- // mKNGWISP.Spawn();
- var entity : CEntity;
- var entityTemplate : CEntityTemplate;
- var resourcePath : string;
- resourcePath = "dlc\dlckngwisp\data\w2ent\kngwisp.w2ent";
- entityTemplate = (CEntityTemplate)LoadResource(resourcePath,true);
- entity = theGame.CreateEntity(entityTemplate,thePlayer.GetWorldPosition()+Vector(0,0,2), thePlayer.GetWorldRotation());
- entity.AddTag('KNGWISP');
- }
Advertisement
Add Comment
Please, Sign In to add comment