1.init
scripts/runtime/view/actor
/view
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum StateDef
{
Idle,
Run,
Attack
}
public class ViewActor : MonoBehaviour
{
public ViewAnimatorComponent animatorComponent;
public float speed;
public StateDef state
{
get => m_state;
set
{
m_state = value;
switch (m_state)
{
case StateDef.Idle:
animatorComponent.Play("idle");
break;
case StateDef.Run:
animatorComponent.Play("run");
break;
case StateDef.Attack:
animatorComponent.Play("attack");
break;
}
}
}
private StateDef m_state;
// Start is called before the first frame update
void Start()
{
}
private float attackTime = 0;
// Update is called once per frame
void Update()
{
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
if(state != StateDef.Attack || attackTime > 1.0f)
{
if (inputX > 0.1f || inputY > 0.1f)
{
state = StateDef.Run;
transform.forward = new Vector3(inputX, 0, inputY).normalized;
transform.position += new Vector3(inputX, 0, inputY).normalized * speed;
}
else
{
state = StateDef.Idle;
}
}
if (Input.GetKeyDown(KeyCode.J))
{
state = StateDef.Attack;
attackTime = 0;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ViewComponentBase : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
11 changes: 11 additions & 0 deletions11
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ViewAnimatorComponent : ViewComponentBase
{
public Animator animator;
///
/// 播放指定动画
///
///
public void Play(string stateName)
{
animator.Play(stateName, 0);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SimpleAI : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
2.能动先
case StateDef.Attack:
animatorComponent.Play("attack");
animatorComponent.Play("attack_01_1");
break;
3.场景资源 代码框架
using System.Collections.Generic;
using UnityEngine;
public class SimpleAI : MonoBehaviour
namespace Runtime.View
{
// Start is called before the first frame update
void Start()
public class SimpleAI : MonoBehaviour
{
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
// Update is called once per frame
void Update()
{
}
}
}
using System.Collections.Generic;
using UnityEngine;
public class ViewAnimatorComponent : ViewComponentBase
namespace Runtime.View
{
public Animator animator;
///
/// 播放指定动画
///
///
public void Play(string stateName)
public class ViewAnimatorComponent : ViewComponentBase
{
animator.Play(stateName, 0);
public Animator animator;
///
/// 播放指定动画
///
///
public void Play(string stateName)
{
animator.Play(stateName, 0);
}
}
}
using System.Collections.Generic;
using UnityEngine;
public enum StateDef
namespace Runtime.View
{
Idle,
Run,
Attack
}
public enum StateDef
{
Idle,
Run,
Attack
}
public class ViewActor : MonoBehaviour
{
public ViewAnimatorComponent animatorComponent;
public class ViewActor : MonoBehaviour
{
public ViewAnimatorComponent animatorComponent;
public float speed;
public float speed;
public StateDef state
{
get => m_state;
set
public StateDef state
{
m_state = value;
switch (m_state)
get => m_state;
set
{
case StateDef.Idle:
animatorComponent.Play("idle");
break;
case StateDef.Run:
animatorComponent.Play("run");
break;
case StateDef.Attack:
animatorComponent.Play("attack_01_1");
break;
m_state = value;
switch (m_state)
{
case StateDef.Idle:
animatorComponent.Play("idle");
break;
case StateDef.Run:
animatorComponent.Play("run");
break;
case StateDef.Attack:
animatorComponent.Play("attack_01_1");
break;
}
}
}
}
private StateDef m_state;
// Start is called before the first frame update
void Start()
{
private StateDef m_state;
}
private float attackTime = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
private float attackTime = 0;
if(state != StateDef.Attack || attackTime > 1.0f)
// Update is called once per frame
void Update()
{
if (inputX > 0.1f || inputY > 0.1f)
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
if(state != StateDef.Attack || attackTime > 1.0f)
{
state = StateDef.Run;
transform.forward = new Vector3(inputX, 0, inputY).normalized;
transform.position += new Vector3(inputX, 0, inputY).normalized * speed;
if (inputX > 0.1f || inputY > 0.1f)
{
state = StateDef.Run;
transform.forward = new Vector3(inputX, 0, inputY).normalized;
transform.position += new Vector3(inputX, 0, inputY).normalized * speed;
}
else
{
state = StateDef.Idle;
}
}
else
if (Input.GetKeyDown(KeyCode.J))
{
state = StateDef.Idle;
state = StateDef.Attack;
attackTime = 0;
}
}
if (Input.GetKeyDown(KeyCode.J))
{
state = StateDef.Attack;
attackTime = 0;
}
}
}
using System.Collections;
using System.Collections.Generic;
namespace Runtime.Logic
{
// 逻辑基类
public class LogicActor
{
public LAnimatorComponent aniCom;
// 当前动作
public LogicActionBase CurAction => m_curAction;
private LogicActionBase m_curAction;
// 角色持有的所有动作
private Dictionary<int, LogicActionBase> m_actions = new Dictionary<int, LogicActionBase>();
public LogicActor()
{
aniCom = new LAnimatorComponent();
InitActions();
m_actions.TryGetValue(LogicActionIDDef.IDLE, out m_curAction);
if (m_curAction != null)
m_curAction.ActiveAction();
}
// 初始化所有动作
private void InitActions()
{
m_actions.Clear();
AddAction(LogicActionIDDef.IDLE);
AddAction(LogicActionIDDef.RUN);
AddAction(LogicActionIDDef.ATTACK_NORMAL);
}
// 添加单个动作, 根据ID可以初始化不同的动作子类
private void AddAction(int actionID)
{
LogicActionBase action = null;
switch (actionID)
{
case LogicActionIDDef.IDLE:
m_actions.Add(actionID, new IdleAction(actionID));
break;
case LogicActionIDDef.RUN:
m_actions.Add(actionID, new RunAction(actionID));
break;
default:
m_actions.Add(actionID, new LogicActionBase(actionID));
break;
}
m_actions.Add(actionID, action);
action.Init();
}
}
}
namespace Runtime.Logic
{
public class LAnimatorComponent
{
}
}
namespace Runtime.Logic
{
public class RunAction : LogicActionBase
{
public RunAction(int actionID) : base(actionID)
{
}
}
}
namespace Runtime.Logic
{
public class LogicActionIDDef
{
public const int IDLE = 1;
public const int RUN = 2;
public const int ATTACK_NORMAL = 101;
}
}
namespace Runtime.Logic
{
public class LogicActionBase
{
private int m_actionID = 0;
public LogicActionBase(int actionID)
{
this.m_actionID = actionID;
}
// 生命周期
public virtual void ActiveAction()
{
}
public virtual void InActiveAction()
{
}
public virtual void Init()
{
}
public virtual void ExecKey(int key)
{
}
}
}
namespace Runtime.Logic
{
public class IdleAction : LogicActionBase
{
public IdleAction(int actionID) : base(actionID)
{
}
}
}
4.基础结构
//logicactor
action.Init();
}
public void Tick()
{
}
}
}
namespace Runtime.Logic
{
public class LogicEngine
{
public LogicPlayerManager PlayerMgr => m_playerManager;
public LogicPlayerManager m_playerManager;
public void Init()
{
m_playerManager = new LogicPlayerManager();
}
public void Tick()
{
m_playerManager.Tick();
}
}
}
namespace Runtime.Logic
{
public class LogicPlayerManager
{
public void Init()
{
}
public void Tick()
{
}
}
}