前言
本版本在之前版本的基础上进行了动作分离,引入了SSAction,SSActionManager,CCActionManager等类,使代码的可复用性更高。
详细代码
SSAction
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SSAction : ScriptableObject
{
public bool enable = true;
public bool destroy = false;
public GameObject gameObject { get; set; }
public Transform transform { get; set; }
public ISSActionCallback callback { get; set; }
protected SSAction()
{
}
// Start is called before the first frame update
public virtual void Start()
{
throw new System.NotImplementedException();
}
// Update is called once per frame
public virtual void Update()
{
throw new System.NotImplementedException();
}
}
ISSActionCallback
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum SSActionEventType:int {Started, Completed}
public interface ISSActionCallback
{
//回调函数
void SSActionEvent(SSAction source,
SSActionEventType events = SSActionEventType.Completed,
int intParam = 0,
string strParam = null,
Object objectParam = null);
}
SSActionManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SSActionManager : MonoBehaviour
{
private Dictionary<int, SSAction> actions = new Dictionary<int, SSAction>();
private List<SSAction> waitingAdd = new List<SSAction> ();
private List<int> waitingDelete = new List<int>();
// Start is called before the first frame update
protected void Start()
{
}
// Update is called once per frame
protected void Update()
{
foreach(SSAction ac in waitingAdd){
actions[ac.GetInstanceID()] = ac;
}
waitingAdd.Clear();
foreach(KeyValuePair<int, SSAction> kv in actions){
SSAction ac = kv.Value;
if(ac.destroy){
waitingDelete.Add(ac.GetInstanceID());
}
else if(ac.enable){
ac.Update();
}
}
foreach(int key in waitingDelete){
SSAction ac = actions[key];
actions.Remove(key);
Destroy(ac);
}
waitingDelete.Clear();
}
public void RunAction(GameObject gameobject, SSAction action, ISSActionCallback manager){
action.gameObject = gameobject;
action.transform = gameobject.transform;
action.callback = manager;
waitingAdd.Add(action);
action.Start();
}
}
CCMoveToAction
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CCMoveToAction : SSAction
{
public Vector3 target; // 移动后的目标位置
public float speed;
private CCMoveToAction(){
}
// Start is called before the first frame update
public override void Start()
{
}
// Update is called once per frame
public override void Update()
{
this.transform.localPosition = Vector3.MoveTowards(this.transform.localPosition, target, speed * Time.deltaTime);
// 如果游戏对象不存在或者当前位置已在目标位置上,则不移动
if(this.transform.localPosition == target || this.gameobject == null){
this.destroy = true; // 标记为销毁
this.callback.SSActionEvent(this); // 回调函数
return;
}
}
public static CCMoveToAction GetSSAction(Vector3 target, float speed){
CCMoveToAction action = ScriptableObject.CreateInstance<CCMoveToAction>();
action.target = target;
action.speed = speed;
return action;
}
}
CCSequenceAction
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CCSequenceAction : SSAction, ISSActionCallback
{
public List<SSAction> sequence;
public int repeat = -1;
public int start = 0;
// Start is called before the first frame update
public override void Start()
{
// 初始化列表中的动作
foreach(SSAction action in sequence){
action.gameObject = this.gameObject;
action.transform = this.transform;
action.callback = this;
action.Start();
}
}
// Update is called once per frame
public override void Update()
{
if(sequence.Count <= 0){
return;
}
if(sequence.Count > 0 && start < sequence.Count){
sequence[start].Update();
}
else{
return;
}
}
public static CCSequenceAction GetSSAction(int repeat, int start, List<SSAction> sequence){
CCSequenceAction action = ScriptableObject.CreateInstance<CCSequenceAction>();
action.repeat = repeat;
action.start = start;
action.sequence = sequence;
return action;
}
public void SSActionEvent(SSAction source,
SSActionEventType events = SSActionEventType.Completed,
int intParam = 0, string strParam = null, Object objectParam = null){
source.destroy = false;
this.start++;
if(this.start >= sequence.Count){
this.start = 0;
if(this.repeat > 0){
this.repeat--;
}
else{
this.destroy = true;
this.callback.SSActionEvent(this);
}
}
}
void OnDestroy(){
}
}
CCActionManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CCActionManager : SSActionManager, ISSActionCallback
{
public CCMoveToAction boatMovement;
public CCSequenceAction roleMovement;
public FirstController controller;
private bool isMoving = false;
protected new void Start()
{
controller = (FirstController)SSDirector.GetInstance().CurrentSceneController;
controller.actionManager = this;
}
public bool CheckMoving()
{
return isMoving;
}
public void MoveBoat(GameObject boat, Vector3 target, float speed)
{
if (isMoving)
return;
isMoving = true;
boatMovement = CCMoveToAction.GetSSAction(target, speed);
this.RunAction(boat, boatMovement, this);
}
public void MoveRole(GameObject role, Vector3 middle_pos, Vector3 target, float speed)
{
if (isMoving)
return;
isMoving = true;
SSAction ac1 = CCMoveToAction.GetSSAction(middle_pos, speed);
SSAction ac2 = CCMoveToAction.GetSSAction(target, speed);
roleMovement = CCSequenceAction.GetSSAction(0, 0, new List<SSAction> {CCMoveToAction.GetSSAction(middle_pos, speed), CCMoveToAction.GetSSAction(target, speed)});
this.RunAction(role, roleMovement, this);
}
public void SSActionEvent(SSAction source,
SSActionEventType events = SSActionEventType.Completed,
int intParam = 0,
string strParam = null,
Object objectParam = null)
{
isMoving = false;
}
}
FirstController的修改部分
public void MoveBoat() {
if (isRunning == false || actionManager.CheckMoving() == true) return;
Vector3 target;
if(boatController.GetBoatModel().isRight){
target = Position.leftBoat;
}
else{
target = Position.rightBoat;
}
actionManager.MoveBoat(boatController.GetBoatModel().boat, target, speed);
boatController.GetBoatModel().isRight = !boatController.GetBoatModel().isRight;
}
public void MoveRole(Role roleModel) {
if (isRunning == false || actionManager.CheckMoving() == true) return;
Vector3 middle_pos;
Vector3 target;
if(roleModel.inBoat){
if(boatController.GetBoatModel().isRight){
target = rightLandController.AddRole(roleModel);
}
else{
target = leftLandController.AddRole(roleModel);
}
middle_pos = new Vector3(roleModel.role.transform.localPosition.x, target.y, target.z);
actionManager.MoveRole(roleModel.role, middle_pos, target, speed);
roleModel.onRight = boatController.GetBoatModel().isRight;
boatController.RemoveRole(roleModel);
}
else{
if (boatController.GetBoatModel().isRight == roleModel.onRight){
if (roleModel.onRight) {
rightLandController.RemoveRole(roleModel);
}
else {
leftLandController.RemoveRole(roleModel);
}
target = boatController.AddRole(roleModel);
middle_pos = new Vector3(target.x, roleModel.role.transform.localPosition.y, target.z);
actionManager.MoveRole(roleModel.role, middle_pos, target, speed);
}
}
}