Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5.  
  6. [System.Serializable]
  7. public class MovementPattern {
  8.     public static Dictionary<string, MovementPattern> patterns;
  9.     public static void Init() {
  10.         TextAsset anims = Resources.Load<TextAsset>("Animations");
  11.         Debug.Log(anims);
  12.         patterns = new Dictionary<string, MovementPattern>();
  13.         string[] lines = anims.text.Split('\n');
  14.        
  15.         MovementPattern pt = new MovementPattern();
  16.        
  17.         for (int i = 0; i < lines.Length; i++) {
  18.             string line = lines[i];
  19.             if (line.Length == 0) { continue; }
  20.             if (line[0] == '#') { continue; }
  21.             if (line[0] == ':') {
  22.                 //Declaration of a new movement Pattern
  23.                 pt = new MovementPattern();
  24.                 patterns.Add(line.Substring(1), pt);
  25.                 Debug.Log("Added new pattern: " + line);
  26.             } else {
  27.                 try {
  28.                     //This might be position information
  29.                     string[] content = line.Split(',');
  30.                     if (content.Length < 3) { continue; }
  31.                     float t = float.Parse(content[0]);
  32.                     float x = float.Parse(content[1]);
  33.                     float y = float.Parse(content[2]);
  34.                     pt.x.AddKey(t, x);
  35.                     pt.y.AddKey(t, y);
  36.                     Debug.Log("Added key to active pattern: " + line);
  37.                    
  38.                 } catch (Exception e) {
  39.                     Debug.Log(e);
  40.                     Debug.Log("Error loading key: " + line);
  41.                     //Something went wrong? Skip.
  42.                     continue;
  43.                 }
  44.                 //*/
  45.             }
  46.            
  47.            
  48.            
  49.         }
  50.        
  51.        
  52.     }
  53.    
  54.     public MovementPattern() {
  55.         x = new AnimationCurve();
  56.         y = new AnimationCurve();
  57.     }
  58.    
  59.     public AnimationCurve x;
  60.     public AnimationCurve y;
  61.  
  62.     public float timeLength { get { return x[x.length-1].time; } }
  63.    
  64.     public Vector2 Evaluate(float time) {
  65.         return new Vector2(x.Evaluate(time), y.Evaluate(time));
  66.     }
  67.    
  68. }
  69.  
  70.  
  71. public class EnemyMover : MonoBehaviour {
  72.     public MovementPattern currentPattern;
  73.     public float timeout = 0;
  74.     public Vector3 currentAnchor;
  75.    
  76.     void Start() {
  77.         currentAnchor = transform.position;
  78.     }
  79.    
  80.     void Update() {
  81.         timeout += Time.deltaTime;
  82.         if (timeout >= currentPattern.timeLength) {
  83.             timeout = 0;
  84.             //currentPattern = NextPattern();
  85.         } else {
  86.             Vector2 offset = currentPattern.Evaluate(timeout);
  87.         }
  88.     }
  89.    
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement