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 = 10;
- wispCurrentVelocity.Z = 0;
- }
- timer function FollowPlayer(deltaTime : float, id : int) {
- var playerPos : Vector;
- var playerRot : EulerAngles;
- var wispGoalPos : Vector;
- var goalAcceleration : Vector;
- // CONSTANTS start
- var wispRotationCircleRadius : float;
- var wispSpeed : float;
- var rotationSpeed : float;
- var wispAccelerationSpeed : float;
- var maxWispAcceleration : float;
- var maxWispVelocity : float;
- wispSpeed = 50;
- wispRotationCircleRadius = 0.75;
- rotationSpeed = 0.3;
- wispAccelerationSpeed = 3;
- maxWispAcceleration = 5;
- 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
- while(AbsF(wispCurrentAcceleration.X) > maxWispAcceleration) wispCurrentAcceleration.X *= 0.9;
- while(AbsF(wispCurrentAcceleration.Y) > maxWispAcceleration) wispCurrentAcceleration.Y *= 0.9;
- while(AbsF(wispCurrentAcceleration.Z) > maxWispAcceleration) wispCurrentAcceleration.Z *= 0.9;
- // simulate acceleration and velocity for more natural movement
- wispCurrentVelocity += wispCurrentAcceleration * deltaTime;
- // clamp wisp velocity, so its not too fast
- while(AbsF(wispCurrentVelocity.X) > maxWispVelocity) wispCurrentVelocity.X *= 0.9;
- while(AbsF(wispCurrentVelocity.Y) > maxWispVelocity) wispCurrentVelocity.Y *= 0.9;
- while(AbsF(wispCurrentVelocity.Z) > maxWispVelocity) wispCurrentVelocity.Z *= 0.9;
- // simulate acceleration and velocity for more natural movement
- wispCurrentPosition += wispCurrentVelocity * deltaTime;
- // if wisp is too far away, just eleport it back to player
- if(VecDistance(wispCurrentPosition, wispGoalPos) > 10) {
- wispCurrentPosition = wispGoalPos;
- }
- 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