using System Collections using System Collections Generic using UnityE

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AIPlayer))]
public class BotDefaultArmyManager : MonoBehaviour
{
public List<AIPlayer.GroupConfig> groupConfigs;
public float pauseBetweenAttacksSec = 60.0f;
AIPlayer botPlayer;
LevelLoadModel levelLoadModel;
int managedGroupId = -1;
int orderedGroupId = -1;
float pauseBetweenAttacksCounter;
[System.Serializable]
public struct DATA
{
public float pauseBetweenAttacksCounter;
};
public DATA DATA_Serialize()
{
DATA aiPlayerData;
aiPlayerData.pauseBetweenAttacksCounter = pauseBetweenAttacksCounter;
return aiPlayerData;
}
public void DATA_Deserialize(DATA aiPlayerData, SavingModel savingModel)
{
pauseBetweenAttacksCounter = aiPlayerData.pauseBetweenAttacksCounter;
}
private void Awake()
{
botPlayer = GetComponent<AIPlayer>();
levelLoadModel = FindObjectOfType<LevelLoadModel>();
}
private void OnEnable()
{
botPlayer.groupReadyEvent += onGroupReady;
botPlayer.tickEvent += onTick;
}
private void OnDisable()
{
botPlayer.groupReadyEvent -= onGroupReady;
botPlayer.tickEvent -= onTick;
}
void onGroupReady(int groupId)
{
orderedGroupId = -1;
managedGroupId = groupId;
}
void attackEnemyObject(List<AttackControl> units, IDamageReceiver enemy)
{
var destinations = ModelUtils.calcAttackPositions(units, enemy);
foreach (var destination in destinations)
{
var positionOnTerrain = ModelUtils.pointOnTerrain(destination.destination2d, levelLoadModel.getLevelModel().getTerrainCollider());
destination.unit.goAttack(destination.target, positionOnTerrain);
}
}
void controlGroup(List<GameObject> units)
{
var enemyObject = ModelUtils.findClosestEnemyObject(botPlayer.getMainEnemy());
attackEnemyObject(units.ConvertAll(item => item.GetComponent<AttackControl>()), enemyObject);
}
void onTick()
{
if (managedGroupId == -1 && orderedGroupId == -1)
{
if (pauseBetweenAttacksCounter > pauseBetweenAttacksSec)
{
orderedGroupId = botPlayer.orderUnitsGroup(groupConfigs[0]);
pauseBetweenAttacksCounter = 0.0f;
}
}
if (managedGroupId != -1)
{
var units = botPlayer.getUnitsGroup(managedGroupId);
if (units.Count == 0)
{
orderedGroupId = -1;
}
else
{
controlGroup(units);
}
}
}
private void Update()
{
pauseBetweenAttacksCounter += Time.deltaTime;
}
}