Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- using System;
- [System.Serializable]
- public class MovementPattern {
- public static Dictionary<string, MovementPattern> patterns;
- public static void Init() {
- TextAsset anims = Resources.Load<TextAsset>("Animations");
- Debug.Log(anims);
- patterns = new Dictionary<string, MovementPattern>();
- string[] lines = anims.text.Split('\n');
- MovementPattern pt = new MovementPattern();
- for (int i = 0; i < lines.Length; i++) {
- string line = lines[i];
- if (line.Length == 0) { continue; }
- if (line[0] == '#') { continue; }
- if (line[0] == ':') {
- //Declaration of a new movement Pattern
- pt = new MovementPattern();
- patterns.Add(line.Substring(1), pt);
- Debug.Log("Added new pattern: " + line);
- } else {
- try {
- //This might be position information
- string[] content = line.Split(',');
- if (content.Length < 3) { continue; }
- float t = float.Parse(content[0]);
- float x = float.Parse(content[1]);
- float y = float.Parse(content[2]);
- pt.x.AddKey(t, x);
- pt.y.AddKey(t, y);
- Debug.Log("Added key to active pattern: " + line);
- } catch (Exception e) {
- Debug.Log(e);
- Debug.Log("Error loading key: " + line);
- //Something went wrong? Skip.
- continue;
- }
- //*/
- }
- }
- }
- public MovementPattern() {
- x = new AnimationCurve();
- y = new AnimationCurve();
- }
- public AnimationCurve x;
- public AnimationCurve y;
- public float timeLength { get { return x[x.length-1].time; } }
- public Vector2 Evaluate(float time) {
- return new Vector2(x.Evaluate(time), y.Evaluate(time));
- }
- }
- public class EnemyMover : MonoBehaviour {
- public MovementPattern currentPattern;
- public float timeout = 0;
- public Vector3 currentAnchor;
- void Start() {
- currentAnchor = transform.position;
- }
- void Update() {
- timeout += Time.deltaTime;
- if (timeout >= currentPattern.timeLength) {
- timeout = 0;
- //currentPattern = NextPattern();
- } else {
- Vector2 offset = currentPattern.Evaluate(timeout);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement