Skip to content

Commit

Permalink
*Update 主要还是改了下脚本
Browse files Browse the repository at this point in the history
Assets Version: v0.0.2
Script Version: v0.0.3
  • Loading branch information
dogdie233 committed Nov 7, 2020
1 parent 1dd8692 commit 58cf875
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 87 deletions.
66 changes: 16 additions & 50 deletions Assets/DLFMSample/Scripts/CameraFollower.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,15 @@ namespace Level
{
public class CameraFollower : MonoBehaviour
{
public class Task
{
public Vector3 startRotation;
public Vector3 targetRotation;
public float startDistance;
public float targetDistance;
public AnimationCurve curve;
public float startTime = 0f;

public Task(Vector3 arg1, Vector3 arg2, float arg3, float arg4, AnimationCurve arg5)
{
this.startRotation = arg1;
this.targetRotation = arg2;
this.startDistance = arg3;
this.targetDistance = arg4;
this.curve = arg5;
}
}

public Transform line;
public float distance;
private Vector3 currentVelocity = Vector3.zero;
public float smoothTime;
public bool enable;
private Vector3 followPoint;
private Vector3 vector;
private Task runningTask;
private Tweener rotaterX;
private Tweener rotaterY;
private Tweener rotaterZ;
private Tweener distanceChanger;

void Start()
Expand All @@ -47,22 +27,12 @@ void Start()
vector = (transform.position - followPoint) / Vector3.Distance(transform.position, followPoint) * distance;
}

void Update()
void Update()
{
if (!enable) return;
followPoint = Vector3.SmoothDamp(followPoint, line.position, ref currentVelocity, smoothTime);
transform.LookAt(followPoint);
transform.position = followPoint + vector;
/*
if (runningTask != null)
{
if (runningTask.startTime == 0f)
runningTask.startTime = Time.time;
Rotate(Vector3.Lerp(runningTask.startRotation, runningTask.targetRotation, runningTask.curve.Evaluate(Time.time - runningTask.startTime)), Mathf.Lerp(runningTask.startDistance, runningTask.targetDistance, runningTask.curve.Evaluate(Time.time - runningTask.startTime)));
distance = Vector3.Distance(followPoint, transform.position);
if (Time.time >= runningTask.startTime + runningTask.curve.length)
runningTask = null;
}
*/
}

/// <summary>
Expand All @@ -74,32 +44,28 @@ void Update()
/// <param name="curve">时间曲线</param>
public void Rotate(Vector2 target, float distance, float duration, AnimationCurve curve)
{
if (!enable) return;
if (rotaterX != null)
{
{
rotaterX.Kill(false);
rotaterX = null;
}
}
if (rotaterY != null)
{
rotaterY.Kill(false);
rotaterY = null;
}
if (rotaterZ != null)
{
rotaterZ.Kill(false);
rotaterZ = null;
}
if (distanceChanger != null)
{
{
distanceChanger.Kill(false);
distanceChanger = null;
}
if (target.x != transform.eulerAngles.x)
rotaterX = DOTween.To(() => transform.eulerAngles.x, x => { transform.RotateAround(followPoint, Vector3.right, x - transform.eulerAngles.x); }, target.x, duration).SetEase(curve);
if (target.y != transform.eulerAngles.y)
rotaterY = DOTween.To(() => transform.eulerAngles.y, y => { transform.RotateAround(followPoint, Vector3.up, y - transform.eulerAngles.y); }, target.y, duration).SetEase(curve);
if (Vector3.Distance(transform.position, followPoint) != distance)
distanceChanger = DOTween.To(() => Vector3.Distance(transform.position, followPoint), d => vector = (transform.position - followPoint) / Vector3.Distance(transform.position, followPoint) * d, distance, duration).SetEase(curve);
}
}
}
if (target.x != transform.eulerAngles.x)
rotaterX = DOTween.To(() => transform.eulerAngles.x, x => { transform.RotateAround(followPoint, Vector3.right, x - transform.eulerAngles.x); }, target.x, duration).SetEase(curve);
if (target.y != transform.eulerAngles.y)
rotaterY = DOTween.To(() => transform.eulerAngles.y, y => { transform.RotateAround(followPoint, Vector3.up, y - transform.eulerAngles.y); }, target.y, duration).SetEase(curve);
if (Vector3.Distance(transform.position, followPoint) != distance)
distanceChanger = DOTween.To(() => Vector3.Distance(transform.position, followPoint), d => vector = (transform.position - followPoint) / Vector3.Distance(transform.position, followPoint) * d, distance, duration).SetEase(curve);
}
}
}
38 changes: 38 additions & 0 deletions Assets/DLFMSample/Scripts/Events/EventManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace Level.Event
{
public static class EventManager
{
public delegate void StateChange(ref StateChangeEvent arg);
public delegate void GameOver(ref GameOverEvent arg);
public static UnityEvent onStart = new UnityEvent();
public static UnityEvent onBGButtonClick = new UnityEvent();
public static StateChange OnStateChange;
public static GameOver onGameOver;

public static void RunStateChangeEvent(GameState oldState, GameState newState, UnityAction<StateChangeEvent> callback)
{
StateChangeEvent arg = new StateChangeEvent(oldState, newState);
try {
IAsyncResult iar = OnStateChange.BeginInvoke(ref arg, ar => { callback.Invoke(arg); }, null);
OnStateChange.EndInvoke(ref arg, iar);
}
catch (NullReferenceException) { callback.Invoke(arg); }
}

public static void RunGameOverEvent(DeathCause deathCause, UnityAction<GameOverEvent> callback)
{
GameOverEvent arg = new GameOverEvent(deathCause);
try {
IAsyncResult iar = onGameOver.BeginInvoke(ref arg, ar => { callback.Invoke(arg); }, null);
onGameOver.EndInvoke(ref arg, iar);
}
catch (NullReferenceException) { callback.Invoke(arg); }
}
}
}
11 changes: 11 additions & 0 deletions Assets/DLFMSample/Scripts/Events/EventManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Assets/DLFMSample/Scripts/Events/GameOverEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Level;
using System.Collections;
using System.Collections.Generic;

namespace Level.Event
{
public class GameOverEvent
{
public readonly DeathCause deathCause;
public bool canceled = false;

public GameOverEvent(DeathCause deathCause)
{
this.deathCause = deathCause;
}
}
}
11 changes: 11 additions & 0 deletions Assets/DLFMSample/Scripts/Events/GameOverEvent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Assets/DLFMSample/Scripts/Events/StateChange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
using System.Collections;
using System.Collections.Generic;

namespace Event
namespace Level.Event
{
public class StateChangeEvent
{
public readonly GameState lastState;
public readonly GameState newState;
public bool canceled = false;
public string test;

public StateChangeEvent(GameState lastState, GameState newState)
{
Expand Down
39 changes: 11 additions & 28 deletions Assets/DLFMSample/Scripts/GameController.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Event;
using Level.Event;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -28,60 +28,43 @@ public enum GameState

public class GameController : MonoBehaviour
{
[Serializable]
public class EventsClass
{
public delegate void StateChange(ref StateChangeEvent arg);
public UnityEvent onStart = new UnityEvent();
public StateChange OnStateChange;
}

[HideInInspector] public static GameController instance = null;
public Button bgButton;
public static EventsClass events = new EventsClass();
private static GameState _state = GameState.SelectingSkins;

public static GameState State
{
get { return _state; }
set { RunStateChangeEvent(value); }
set { EventManager.RunStateChangeEvent(_state, value, StateChangeEventCompleted); }
}

public static bool IsStarted
{
get { return !(_state == GameState.SelectingSkins || _state == GameState.WaitingStart); }
}

private static void RunStateChangeEvent()
{
RunStateChangeEvent((GameState)((int)_state + 1));
}

private static void RunStateChangeEvent(GameState newState)
{
StateChangeEvent arg = new StateChangeEvent(_state, newState);
try { events.OnStateChange.BeginInvoke(ref arg, ar => { events.OnStateChange.EndInvoke(ref arg, ar); StateChangeEventCompleted(arg); }, null); }
catch (NullReferenceException) { StateChangeEventCompleted(arg); }
}

private static void StateChangeEventCompleted(StateChangeEvent arg)
{
Debug.Log("test: " + arg.test);
if (arg.canceled)
return;
_state = arg.newState;
switch (_state)
{
case GameState.Playing:
events.onStart.Invoke();
instance.bgButton.onClick.RemoveListener(RunStateChangeEvent);
EventManager.onStart.Invoke();
break;
}
}

private void Awake()
{
bgButton.onClick.AddListener(RunStateChangeEvent);
{
bgButton.onClick.AddListener(() => { EventManager.onBGButtonClick.Invoke(); });
bgButton.onClick.AddListener(() => {
if (_state != GameState.Playing && _state != GameState.WaitingRespawn && (int)_state + 1 < Enum.GetNames(typeof(GameState)).Length)
{
EventManager.RunStateChangeEvent(_state, (GameState)((int)_state + 1), StateChangeEventCompleted);
}
});
if (instance == null && instance != this)
instance = this;
else
Expand Down
16 changes: 10 additions & 6 deletions Assets/DLFMSample/Scripts/Line.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Level.Event;
using System;
using System.Collections;
using System.Collections.Generic;
Expand Down Expand Up @@ -62,20 +63,22 @@ public bool IsGrounded
private void UpdateTurnListener(bool value)
{
if (value)
GameController.instance.bgButton.onClick.AddListener(() => { Turn(false); });
else
GameController.instance.bgButton.onClick.RemoveListener(() => { Turn(false); });
if (GameController.IsStarted) EventManager.onBGButtonClick.AddListener(() => { Turn(false); });
else
if (GameController.IsStarted) EventManager.onBGButtonClick.RemoveListener(() => { Turn(false); });
}

void Awake()
{
rigidbody = GetComponent<Rigidbody>();
GameController.events.onStart.AddListener(() => { if (_controlled) moving = true;});
EventManager.onStart.AddListener(() => {
moving = _controlled;
if (_controlled) EventManager.onBGButtonClick.AddListener(() => { Turn(false); });
});
}

void Start()
{
UpdateTurnListener(_controlled);
bodiesParent = new GameObject("Bodies").transform;
previousFrameIsGrounded = IsGrounded;
}
Expand Down Expand Up @@ -129,7 +132,7 @@ private void CreateBody()
/// <param name="focus">强制转弯(即无视controlled, IsGrounded, State)</param>
public void Turn(bool focus)
{
if ((_controlled && IsGrounded && GameController.State == GameState.Playing) || focus)
if ((IsGrounded && GameController.State == GameState.Playing) || focus)
{
(transform.localEulerAngles, nextWay) = (nextWay, transform.localEulerAngles);
CreateBody();
Expand All @@ -149,6 +152,7 @@ public void Die(DeathCause deathCause)
rigidbody.isKinematic = true;
break;
}
IsControlled = false;
events.onDie.Invoke(deathCause);
}

Expand Down
3 changes: 2 additions & 1 deletion Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,8 @@ MonoBehaviour:
m_EditorClassIdentifier:
line: {fileID: 711418601}
distance: 30
smoothTime: 0.3
smoothTime: 0.5
enable: 1
--- !u!1 &1231657479
GameObject:
m_ObjectHideFlags: 0
Expand Down

0 comments on commit 58cf875

Please sign in to comment.