Advertisement
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 and initial velocity to be like it was released upwards
- wispCurrentPosition = thePlayer.GetWorldPosition() + Vector(0,0,2);
- wispCurrentVelocity = Vector(0,0,10);
- wispCurrentAcceleration = Vector(0,0,0);
- }
- timer function FollowPlayer(deltaTime : float, id : int) {
- var playerPosition : Vector;
- var playerRotation : EulerAngles;
- var wispGoalPosition : Vector;
- var goalAcceleration : Vector;
- var navigationComputeZReturn : float;
- // CONFIG CONSTANTS start
- var rotationCircleRadius : float;
- var rotationCircleMovementSpeed : float;
- var selfRotationSpeed : float;
- var maxAcceleration : float;
- var maxVelocity : float;
- var velocityDampeningFactor : float;
- rotationCircleMovementSpeed = 25;
- rotationCircleRadius = 0.75;
- selfRotationSpeed = 0.3;
- maxAcceleration = 100;
- maxVelocity = 20;
- velocityDampeningFactor = 0.9;
- // CONFIG CONSTANTS end
- // increase current angle in the circle around player
- wispCurrentRotationCircleAngle += deltaTime * rotationCircleMovementSpeed;
- // if rotation incresed over 360 degrees make sure its under 360
- while(wispCurrentRotationCircleAngle > 360) {
- wispCurrentRotationCircleAngle -= 360;
- }
- playerPosition = thePlayer.GetWorldPosition();
- playerRotation = thePlayer.GetWorldRotation();
- // calculate wisp Goal position
- wispGoalPosition = playerPosition;
- wispGoalPosition += wispGoalPositionOffset;
- wispGoalPosition.X += CosF(Deg2Rad(wispCurrentRotationCircleAngle)) * rotationCircleRadius;
- wispGoalPosition.Y += SinF(Deg2Rad(wispCurrentRotationCircleAngle)) * rotationCircleRadius;
- wispGoalPosition.Z += 2;
- // 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() * rotationCircleRadius * 0.25;
- }
- // accelerate towards goal position
- wispCurrentAcceleration = wispGoalPosition - wispCurrentPosition;
- // clamp wisp acceleration, so its not too fast
- if(VecLength(wispCurrentAcceleration) > maxAcceleration) {
- wispCurrentAcceleration = VecNormalize(wispCurrentAcceleration) * maxAcceleration;
- }
- // simulate acceleration and velocity for more natural movement
- wispCurrentVelocity += wispCurrentAcceleration * deltaTime;
- // dampen velocity
- wispCurrentVelocity *= velocityDampeningFactor;
- // clamp wisp velocity, so its not too fast
- if(VecLength(wispCurrentVelocity) > maxVelocity) {
- wispCurrentVelocity = VecNormalize(wispCurrentVelocity) * maxVelocity;
- }
- // 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, wispGoalPosition) > 100) {
- wispCurrentPosition = wispGoalPosition;
- wispCurrentVelocity = Vector(0,0,0);
- wispCurrentAcceleration = Vector(0,0,0);
- }
- // rotate wisp it self over time
- wispCurrentRotation.Pitch += deltaTime * selfRotationSpeed;
- // 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
Advertisement