-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathSecondUnitBrain.cs
More file actions
109 lines (100 loc) · 4.01 KB
/
SecondUnitBrain.cs
File metadata and controls
109 lines (100 loc) · 4.01 KB
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 Model;
using Model.Runtime.Projectiles;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Utilities;
namespace UnitBrains.Player
{
public class SecondUnitBrain : DefaultPlayerUnitBrain
{
private static int ENEMY_ATTACK_COUNT = 3;
private static int idCounter = -1;
private int id = idCounter++;
public override string TargetUnitName => "Cobra Commando";
private const float OverheatTemperature = 3f;
private const float OverheatCooldown = 2f;
private float _temperature = 0f;
private float _cooldownTime = 0f;
private bool _overheated;
private List<Vector2Int> outOfReachTargets = new();
protected override void GenerateProjectiles(Vector2Int forTarget, List<BaseProjectile> intoList)
{
float overheatTemperature = OverheatTemperature;
///////////////////////////////////////
// Homework 1.3 (1st block, 3rd module)
///////////////////////////////////////
int currentTemperature = GetTemperature();
if (currentTemperature >= overheatTemperature)
{
return;
}
for (int i = -1; i < currentTemperature; i++)
{
var projectile = CreateProjectile(forTarget);
AddProjectileToList(projectile, intoList);
}
IncreaseTemperature();
///////////////////////////////////////
}
public override Vector2Int GetNextStep()
{
Vector2Int unitPos = unit.Pos;
if (!outOfReachTargets.Any() || GetReachableTargets().Contains(outOfReachTargets[0]))
{
return unitPos;
}
return unitPos.CalcNextStepTowards(outOfReachTargets[0]);
}
protected override List<Vector2Int> SelectTargets()
{
///////////////////////////////////////
// Homework 1.4 (1st block, 4rd module)
///////////////////////////////////////
List<Vector2Int> result = new();
List<Vector2Int> allTargetPositions = GetAllTargets().ToList();
if (!allTargetPositions.Any())
{
result.Add(runtimeModel.RoMap.Bases[IsPlayerUnitBrain ? RuntimeModel.BotPlayerId : RuntimeModel.PlayerId]);
return result;
}
SortByDistanceToOwnBase(allTargetPositions);
int targetIndex = id < ENEMY_ATTACK_COUNT ? id : id % ENEMY_ATTACK_COUNT;
var targetPosition = allTargetPositions.Count <= targetIndex ? allTargetPositions[allTargetPositions.Count - 1] : allTargetPositions[targetIndex];
List<Vector2Int> reachableTargetPositions = GetReachableTargets();
outOfReachTargets.Clear();
outOfReachTargets.Add(targetPosition);
if (reachableTargetPositions.Contains(targetPosition))
{
result.Clear();
result.Add(targetPosition);
}
return result;
///////////////////////////////////////
}
public override void Update(float deltaTime, float time)
{
if (_overheated)
{
_cooldownTime += Time.deltaTime;
float t = _cooldownTime / (OverheatCooldown/10);
_temperature = Mathf.Lerp(OverheatTemperature, 0, t);
if (t >= 1)
{
_cooldownTime = 0;
_overheated = false;
}
}
}
private int GetTemperature()
{
if(_overheated) return (int) OverheatTemperature;
else return (int)_temperature;
}
private void IncreaseTemperature()
{
_temperature += 1f;
if (_temperature >= OverheatTemperature) _overheated = true;
}
}
}