-
Notifications
You must be signed in to change notification settings - Fork 309
Expand file tree
/
Copy pathBotController.cs
More file actions
75 lines (67 loc) · 2.3 KB
/
BotController.cs
File metadata and controls
75 lines (67 loc) · 2.3 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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Model;
using Model.Config;
using UnityEngine;
using Utilities;
//hello
//How are you???
namespace Controller
{
public class BotController
{
private readonly TimeUtil _timeUtil;
private readonly IReadOnlyRuntimeModel _runtimeModel;
private readonly List<UnitConfig> _sortedUnits;
private readonly Action<UnitConfig> _onBotUnitChosen;
private Coroutine _updateCoroutine;
public BotController(Action<UnitConfig> onBotUnitChosen)
{
_onBotUnitChosen = onBotUnitChosen;
_timeUtil = ServiceLocator.Get<TimeUtil>();
_runtimeModel = ServiceLocator.Get<IReadOnlyRuntimeModel>();
_sortedUnits = ServiceLocator.Get<Settings>().EnemyUnits.Keys
.OrderBy(x => x.Cost).ToList();
_updateCoroutine = _timeUtil.StartCoroutine(UpdateCoroutine());
}
public void Stop()
{
_timeUtil.StopCoroutine(_updateCoroutine);
_updateCoroutine = null;
}
private IEnumerator UpdateCoroutine()
{
var delay = new WaitForSeconds(1f);
while (true)
{
var stage = _runtimeModel.Stage;
switch (stage)
{
case RuntimeModel.GameStage.ChooseUnit:
ChooseUnit();
break;
}
yield return delay;
}
}
private void ChooseUnit()
{
var moneyLeft = _runtimeModel.RoMoney[RuntimeModel.BotPlayerId];
if (_sortedUnits[0].Cost > moneyLeft)
return;
for (int i = _sortedUnits.Count - 1; i >= 0; i--)
{
moneyLeft = _runtimeModel.RoMoney[RuntimeModel.BotPlayerId];
var unitBudget = moneyLeft / (i + 1);
var unit = _sortedUnits[i];
while (unit.Cost <= unitBudget && unit.Cost < moneyLeft)
{
_onBotUnitChosen?.Invoke(unit);
unitBudget -= unit.Cost;
}
}
}
}
}