基础内容
必要插件安装 缓动曲线和动画基础 ElementFolder,Track与其次级模块,PathNode重构
This commit is contained in:
168
Assets/Feel/FeelDemos/Barbarians/Scripts/Barbarian.cs
Normal file
168
Assets/Feel/FeelDemos/Barbarians/Scripts/Barbarian.cs
Normal file
@@ -0,0 +1,168 @@
|
||||
using MoreMountains.Feedbacks;
|
||||
using MoreMountains.Tools;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MoreMountains.Feel
|
||||
{
|
||||
/// <summary>
|
||||
/// A simple class controlling the hero character in the Barbarians demo scene
|
||||
/// It simulates a very simple character controller in an ARPG game
|
||||
/// </summary>
|
||||
[AddComponentMenu("")]
|
||||
public class Barbarian : MonoBehaviour
|
||||
{
|
||||
[Header("Cooldown")]
|
||||
/// a duration, in seconds, between two attacks, during which attacks are prevented
|
||||
[Tooltip("a duration, in seconds, between two attacks, during which attacks are prevented")]
|
||||
public float CooldownDuration = 0.1f;
|
||||
|
||||
[Header("Feedbacks")]
|
||||
/// a feedback to call when the attack starts
|
||||
[Tooltip("a feedback to call when the attack starts")]
|
||||
public MMFeedbacks AttackFeedback;
|
||||
/// a feedback to call when each individual attack phase starts
|
||||
[Tooltip("a feedback to call when each individual attack phase starts")]
|
||||
public MMFeedbacks IndividualAttackFeedback;
|
||||
/// a feedback to call when trying to attack while in cooldown
|
||||
[Tooltip("a feedback to call when trying to attack while in cooldown")]
|
||||
public MMFeedbacks DeniedFeedback;
|
||||
|
||||
[Header("Attack settings")]
|
||||
/// a curve on which to move the character when it attacks
|
||||
public MMTween.MMTweenCurve AttackCurve = MMTween.MMTweenCurve.EaseInOutOverhead;
|
||||
/// the duration of the attack in seconds
|
||||
public float AttackDuration = 2.5f;
|
||||
/// an offset at which to attack enemies
|
||||
public float AttackPositionOffset = 0.3f;
|
||||
/// a duration by which to reduce movement duration after every attack (making each attack faster than the previous one)
|
||||
public float IntervalDecrement = 0.1f;
|
||||
|
||||
protected List<Vector3> _targets;
|
||||
protected float _lastAttackStartedAt = -100f;
|
||||
protected Vector3 _initialPosition;
|
||||
protected Vector3 _initialLookAtTarget;
|
||||
protected Vector3 _lookAtTarget;
|
||||
protected BarbarianEnemy _enemy;
|
||||
|
||||
/// <summary>
|
||||
/// On Awake we store our initial position
|
||||
/// </summary>
|
||||
protected virtual void Awake()
|
||||
{
|
||||
_initialPosition = this.transform.position;
|
||||
_initialLookAtTarget = this.transform.position + this.transform.forward * 10f;
|
||||
_lookAtTarget = _initialLookAtTarget;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// On Update we look for input
|
||||
/// </summary>
|
||||
protected virtual void Update()
|
||||
{
|
||||
HandleInput();
|
||||
LookAtTarget();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Makes the character look at the target it's attacking
|
||||
/// </summary>
|
||||
protected virtual void LookAtTarget()
|
||||
{
|
||||
Vector3 direction = _lookAtTarget - _initialPosition;
|
||||
this.transform.LookAt(_lookAtTarget + direction * 5f);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Detects input
|
||||
/// </summary>
|
||||
protected virtual void HandleInput()
|
||||
{
|
||||
if (FeelDemosInputHelper.CheckMainActionInputPressedThisFrame())
|
||||
{
|
||||
Attack();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs an attack if possible, otherwise plays a denied feedback
|
||||
/// </summary>
|
||||
protected virtual void Attack()
|
||||
{
|
||||
if (Time.time - _lastAttackStartedAt < CooldownDuration + AttackDuration)
|
||||
{
|
||||
DeniedFeedback?.PlayFeedbacks();
|
||||
}
|
||||
else
|
||||
{
|
||||
AcquireTargets();
|
||||
StartCoroutine(AttackCoroutine());
|
||||
_lastAttackStartedAt = Time.time;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds targets around the Barbarian and stores them
|
||||
/// </summary>
|
||||
protected virtual void AcquireTargets()
|
||||
{
|
||||
_targets = new List<Vector3>();
|
||||
|
||||
Collider[] hitColliders = Physics.OverlapSphere(this.transform.position, 5f);
|
||||
foreach (var hitCollider in hitColliders)
|
||||
{
|
||||
Vector3 enemyPosition = hitCollider.transform.position;
|
||||
Vector3 direction = this.transform.position - enemyPosition;
|
||||
|
||||
if (hitCollider.GetComponent<BarbarianEnemy>() != null)
|
||||
{
|
||||
_targets.Add(enemyPosition + direction * AttackPositionOffset);
|
||||
}
|
||||
}
|
||||
_targets.MMShuffle();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A coroutine used to move to each stored target to attack them
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected virtual IEnumerator AttackCoroutine()
|
||||
{
|
||||
float intervalDuration = AttackDuration / _targets.Count;
|
||||
|
||||
// we play our initial attack feedback
|
||||
AttackFeedback?.PlayFeedbacks();
|
||||
|
||||
int enemyCounter = 0;
|
||||
|
||||
foreach (Vector3 destination in _targets)
|
||||
{
|
||||
// for each new enemy, we play an attack feedback
|
||||
IndividualAttackFeedback?.PlayFeedbacks();
|
||||
MMTween.MoveTransform(this, this.transform, this.transform.position, destination, null, 0f, intervalDuration, AttackCurve);
|
||||
_lookAtTarget = destination;
|
||||
yield return MMCoroutine.WaitFor(intervalDuration - enemyCounter * IntervalDecrement);
|
||||
enemyCounter++;
|
||||
}
|
||||
|
||||
MMTween.MoveTransform(this, this.transform, this.transform.position, _initialPosition, null, 0f, intervalDuration, AttackCurve);
|
||||
_lookAtTarget = _initialLookAtTarget;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// When we collide with an enemy, we apply damage to it
|
||||
/// </summary>
|
||||
/// <param name="other"></param>
|
||||
protected virtual void OnTriggerEnter(Collider other)
|
||||
{
|
||||
_enemy = other.GetComponent<BarbarianEnemy>();
|
||||
if (_enemy != null)
|
||||
{
|
||||
// we randomize the damage done and apply it to the enemy
|
||||
int damage = Random.Range(50, 250);
|
||||
_enemy.TakeDamage(damage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/Feel/FeelDemos/Barbarians/Scripts/Barbarian.cs.meta
Normal file
11
Assets/Feel/FeelDemos/Barbarians/Scripts/Barbarian.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e95bc0f9cf6fa3b45ba4a4e45e2c460b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
37
Assets/Feel/FeelDemos/Barbarians/Scripts/BarbarianEnemy.cs
Normal file
37
Assets/Feel/FeelDemos/Barbarians/Scripts/BarbarianEnemy.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using MoreMountains.Feedbacks;
|
||||
using MoreMountains.Tools;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MoreMountains.Feel
|
||||
{
|
||||
/// <summary>
|
||||
/// A simple class used to handle enemies in Feel's Barbarian demo scene
|
||||
/// </summary>
|
||||
[AddComponentMenu("")]
|
||||
public class BarbarianEnemy : MonoBehaviour
|
||||
{
|
||||
/// a feedback to play when getting damage
|
||||
public MMFeedbacks DamageFeedback;
|
||||
/// a cooldown, in seconds, during which the character can't be damaged
|
||||
public float DamageCooldown = 1f;
|
||||
|
||||
protected float _lastDamageTakenAt = -10f;
|
||||
|
||||
/// <summary>
|
||||
/// Applies damage to this character, if not in cooldown
|
||||
/// </summary>
|
||||
/// <param name="damage"></param>
|
||||
public virtual void TakeDamage(int damage)
|
||||
{
|
||||
// we make sure enough time has passed since the last time this enemy took damage
|
||||
if (Time.time - _lastDamageTakenAt < DamageCooldown)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_lastDamageTakenAt = Time.time;
|
||||
DamageFeedback?.PlayFeedbacks(this.transform.position, damage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 696054e9657662b4aadf57bec27bf7e2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user