Advertisement
infinite_ammo

Player.cs excerpt (from Alone in Dreams)

Apr 17th, 2012
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.87 KB | None | 0 0
  1.    void OnStateEnter(State state)
  2.     {
  3.         switch (state)
  4.         {
  5.             case State.Idle:
  6.                 modelAnimation.CrossFade("idle" + Random.Range(1, 3), 0.25f);
  7.                 break;
  8.  
  9.             case State.Elevator:
  10.                 if (walk)
  11.                 {
  12.                     walk.Stop();
  13.                     walk.ToggleAgent(false);
  14.                 }
  15.  
  16.                 modelAnimation.CrossFade("idle" + Random.Range(1, 3), 0.25f);
  17.                 break;
  18.  
  19.             case State.PushPull:
  20.                 if (walk)
  21.                 {
  22.                     walk.Stop();
  23.                 }
  24.                 modelAnimation.CrossFade("idle" + Random.Range(1, 3), 0.25f);
  25.                 float gridSize = 1.0f;
  26.  
  27.                 Vector3 dir = transform.position - pushPull.transform.position;
  28.                 dir.y = 0.0f;
  29.  
  30.                 Vector3 newPosition = transform.position;
  31.  
  32.                 if (Mathf.Abs(dir.x) > Mathf.Abs(dir.z))
  33.                 {
  34.                     float oldZ = dir.z;
  35.                     dir.z = 0.0f;
  36.                     dir.Normalize();
  37.                     newPosition = new Vector3(pushPull.transform.position.x + dir.x, transform.position.y, pushPull.transform.position.z + dir.z);
  38.                     if (pushPull.grid.IsObstructed(newPosition))
  39.                     {
  40.                         dir.x = 0.0f;
  41.                         dir.z = oldZ;
  42.                         dir.Normalize();
  43.                         newPosition = new Vector3(pushPull.transform.position.x + dir.x, transform.position.y, pushPull.transform.position.z + dir.z);
  44.                     }
  45.                 }
  46.                 else
  47.                 {
  48.                     float oldX = dir.x;
  49.                     dir.x = 0.0f;
  50.                     dir.Normalize();
  51.                     newPosition = new Vector3(pushPull.transform.position.x + dir.x, transform.position.y, pushPull.transform.position.z + dir.z);
  52.                     if (pushPull.grid.IsObstructed(newPosition))
  53.                     {
  54.                         dir.z = 0.0f;
  55.                         dir.x = oldX;
  56.                         dir.Normalize();
  57.                         newPosition = new Vector3(pushPull.transform.position.x + dir.x, transform.position.y, pushPull.transform.position.z + dir.z);
  58.                     }
  59.                 }
  60.                 //dir.Normalize();
  61.  
  62.                 transform.position = newPosition;
  63.  
  64.                 //transform.position = new Vector3((int)(transform.position.x/gridSize) * gridSize, transform.position.y, (int)(transform.position.z/gridSize) * gridSize)
  65.                  //+ new Vector3(1, 0, 1) * gridSize * 0.5f;
  66.                
  67.                 model.transform.forward = -dir;
  68.                 pushPullOffset = transform.position - pushPull.transform.position;
  69.                 break;
  70.  
  71.             case State.StandOn:
  72.                 if (walk)
  73.                 {
  74.                     walk.ToggleAgent(false);
  75.                 }
  76.                 modelAnimation.CrossFade("idle" + Random.Range(1, 3), 0.25f);
  77.                 break;
  78.  
  79.             case State.Sit:
  80.                 savePosition = transform.position;
  81.                
  82.                 if (walk)
  83.                 {
  84.                     walk.Stop();
  85.                     walk.ToggleAgent(false);
  86.                 }
  87.                 modelAnimation.CrossFade("idle" + Random.Range(1, 3), 0.25f);
  88.                 break;
  89.  
  90.             case State.Walk:
  91.                 modelAnimation.CrossFade("walk", 0.25f);
  92.                 if (fat.state == Fat.State.Fat)
  93.                 {
  94.                     modelAnimation["walk"].speed = fatWalkAnimationSpeed;
  95.                 }
  96.                 else
  97.                 {
  98.                     modelAnimation["walk"].speed = walkAnimationSpeed;
  99.                 }
  100.                 break;
  101.  
  102.             case State.Run:
  103.                 modelAnimation.CrossFade("run", 0.25f);
  104.                 modelAnimation["run"].speed = runAnimationSpeed;
  105.                 break;
  106.  
  107.             case State.Slide:
  108.                 Global.cinematicCamera.state = CinematicCamera.State.TrackPlayerOnXY;
  109.                 Global.cinematicCamera.transform.forward = Vector3.forward;
  110.                 model.localEulerAngles = Vector3.zero;
  111.                 modelAnimation.CrossFade("idle1", 0.25f);
  112.             break;
  113.  
  114.             case State.InsidePlant:
  115.                 if (walk)
  116.                 {
  117.                     walk.ToggleAgent(false);
  118.                 }
  119.                 modelAnimation.CrossFade("idle1", 0.25f);
  120.             break;
  121.  
  122.             case State.FiredFromPlant:
  123.                 if (walk)
  124.                 {
  125.                     walk.ToggleAgent(false);
  126.                 }
  127.                
  128.             break;
  129.         }
  130.     }
  131.  
  132.     void OnStateExit(State state)
  133.     {
  134.         switch (state)
  135.         {
  136.             case State.FiredFromPlant:
  137.                 if (walk)
  138.                 {
  139.                     walk.ToggleAgent(true);
  140.                 }
  141.                 break;
  142.  
  143.             case State.Elevator:
  144.                 if (walk)
  145.                 {
  146.                     walk.ToggleAgent(true);
  147.                 }
  148.                 break;
  149.  
  150.             case State.Sit:
  151.                 transform.position = savePosition;
  152.                 if (walk)
  153.                 {
  154.                     walk.ToggleAgent(true);
  155.                 }
  156.                 //transform.position = savePosition;
  157.                 break;
  158.  
  159.             case State.Slide:
  160.                 Global.cinematicCamera.state = CinematicCamera.State.Static;
  161.             break;
  162.  
  163.             case State.Fall:
  164.                 walk.Warp(transform);
  165.             break;
  166.  
  167.             case State.PushPull:
  168.                 if (touchPushPull)
  169.                 {
  170.                     touchPushPull.Done();
  171.                 }
  172.                 touchPushPull = null;
  173.                 pushPull = null;
  174.             break;
  175.         }
  176.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement