174 lines
4.6 KiB
Plaintext
174 lines
4.6 KiB
Plaintext
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using PuzzleDefine;
|
||
|
using UnityEditor;
|
||
|
|
||
|
public class PuzzleMonster : MonoBehaviour
|
||
|
{
|
||
|
public string Name;
|
||
|
|
||
|
public Attribute Health;
|
||
|
public PuzzleGameMode.VoidDelegate OnAfterHealthChanged;
|
||
|
public void SetHealth(Attribute value)
|
||
|
{
|
||
|
Health = value;
|
||
|
OnAfterHealthChanged?.Invoke();
|
||
|
}
|
||
|
|
||
|
public Attribute MaxHealth;
|
||
|
|
||
|
public Attribute Attack;
|
||
|
public List<ElementTypes> WeaknessTypes;
|
||
|
|
||
|
public int ActionPointer;
|
||
|
public List<MonsterAction> Actions;
|
||
|
|
||
|
public PuzzleDefine.Sprite Icon;
|
||
|
|
||
|
#region Event
|
||
|
|
||
|
public PuzzleGameMode.OnDamageEvent OnAfterDamagedEvent;
|
||
|
|
||
|
public PuzzleGameMode.OnMonsterPreReleaseSkillEvent OnAfterPreReleaseSkillEvent;
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
|
||
|
private void Awake()
|
||
|
{
|
||
|
ActionPointer = 0;
|
||
|
}
|
||
|
|
||
|
#region Interface
|
||
|
|
||
|
public void TookAction()
|
||
|
{
|
||
|
if (Actions == null || Actions.Count == 0)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var action = Actions[ActionPointer];
|
||
|
switch (action.ActionType)
|
||
|
{
|
||
|
case MonsterActionTypes.Wait:
|
||
|
break;
|
||
|
case MonsterActionTypes.CastSkill:
|
||
|
{
|
||
|
foreach (var item in action.CastSkillIDs)
|
||
|
{
|
||
|
if (action.TargetType == MonsterCastSkillTargetTypes.All)
|
||
|
{
|
||
|
foreach (var hero in PuzzleGameMode.main.GamePlayer.Heroes)
|
||
|
{
|
||
|
if (!hero.IsDead)
|
||
|
{
|
||
|
PuzzleGameMode.main.MonsterCastSkill(this, hero, item);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
int selectNum = 0;
|
||
|
if (action.TargetType == MonsterCastSkillTargetTypes.Single)
|
||
|
{
|
||
|
selectNum = 1;
|
||
|
}
|
||
|
else if (action.TargetType == MonsterCastSkillTargetTypes.RandomNum)
|
||
|
{
|
||
|
selectNum = action.TargetNum;
|
||
|
}
|
||
|
|
||
|
foreach (var hero in SelectHeroes(selectNum, action.PriorityTypes))
|
||
|
{
|
||
|
PuzzleGameMode.main.MonsterCastSkill(this, hero, item);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
ActionPointer++;
|
||
|
if (ActionPointer >= Actions.Count)
|
||
|
{
|
||
|
ActionPointer = 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private PuzzleHero GetHeroWithType(ElementTypes type)
|
||
|
{
|
||
|
if (type == ElementTypes.Max)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
foreach (var item in PuzzleGameMode.main.GamePlayer.Heroes)
|
||
|
{
|
||
|
if (item.ElementType == type)
|
||
|
{
|
||
|
return item;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
public IEnumerable<PuzzleHero> SelectHeroes(int num, List<ElementTypes> priorities)
|
||
|
{
|
||
|
if (num <= 0)
|
||
|
{
|
||
|
yield break;
|
||
|
}
|
||
|
|
||
|
num = Mathf.Min(num, (int)ElementTypes.Max);
|
||
|
|
||
|
List<ElementTypes> allTypes = new List<ElementTypes>();
|
||
|
for (int i = 0; i < (int)ElementTypes.Max; i++)
|
||
|
{
|
||
|
allTypes.Add((ElementTypes)i);
|
||
|
}
|
||
|
|
||
|
int priorityCount = 0;
|
||
|
if (priorities != null && priorities.Count != 0)
|
||
|
{
|
||
|
priorityCount = Mathf.Min(num, priorities.Count);
|
||
|
for (int i = 0; i < priorityCount; i++)
|
||
|
{
|
||
|
allTypes.Remove(priorities[i]);
|
||
|
yield return GetHeroWithType(priorities[i]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int rest = num - priorityCount;
|
||
|
for (int i = 0; i < rest; i++)
|
||
|
{
|
||
|
int index = Random.Range(0, allTypes.Count);
|
||
|
yield return GetHeroWithType(allTypes[index]);
|
||
|
allTypes.RemoveAt(index);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// A function that generates n random numbers with no repeats, in range from a to b
|
||
|
public static List<int> RandomNumbers(int n, int a, int b)
|
||
|
{
|
||
|
List<int> result = new List<int>();
|
||
|
for (int i = 0; i < n; i++)
|
||
|
{
|
||
|
int r = Random.Range(a, b);
|
||
|
while (result.Contains(r))
|
||
|
{
|
||
|
r = Random.Range(a, b);
|
||
|
}
|
||
|
result.Add(r);
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
}
|