|
1 | 1 | using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Text; |
5 | | -using System.Threading.Tasks; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.Threading; |
| 4 | +using GameClass.GameObj; |
| 5 | +using GameClass.GameObj.Areas; |
| 6 | +using Preparation.Utility; |
6 | 7 |
|
7 | | -namespace logic.Gaming |
| 8 | +namespace Game |
8 | 9 | { |
9 | | - internal class ActionManager |
| 10 | + public partial class Game |
10 | 11 | { |
| 12 | + private readonly ActionManager actionManager; |
| 13 | + |
| 14 | + private sealed class ActionManager |
| 15 | + { |
| 16 | + private readonly Game game; |
| 17 | + private readonly ConcurrentDictionary<long, CancellationTokenSource> occupying = new(); // key: ComputeCenter.ID |
| 18 | + private readonly ConcurrentDictionary<(long playerId, long resourceId), CancellationTokenSource> harvesting = new(); |
| 19 | + |
| 20 | + public ActionManager(Game game) |
| 21 | + { |
| 22 | + this.game = game; |
| 23 | + } |
| 24 | + |
| 25 | + public bool Harvest(long playerId, Resource resource, long durationMs) |
| 26 | + { |
| 27 | + if (resource == null) return false; |
| 28 | + if (!game.characterManager.TryGetCharacter(playerId, out var ch)) return false; |
| 29 | + // 仅当在资源点周围 3x3 格内可采 |
| 30 | + if (!GameData.ApproachToInteract(ch.Position, resource.Position)) return false; |
| 31 | + |
| 32 | + // 仅汽车可采集(每秒10单位),其他单位暂不支持 |
| 33 | + int ratePerSec = ch.CharacterType == CharacterType.AUTONOMOUS_CAR ? 10 : 0; |
| 34 | + if (ratePerSec <= 0) return false; |
| 35 | + |
| 36 | + var key = (playerId, resource.ID); |
| 37 | + if (harvesting.ContainsKey(key)) return false; // 已有进行中的采集 |
| 38 | + |
| 39 | + var cts = new CancellationTokenSource(); |
| 40 | + if (!harvesting.TryAdd(key, cts)) { cts.Dispose(); return false; } |
| 41 | + |
| 42 | + new Thread(() => |
| 43 | + { |
| 44 | + try |
| 45 | + { |
| 46 | + int elapsed = 0; |
| 47 | + int step = 200; // 200ms tick |
| 48 | + while (!cts.IsCancellationRequested && elapsed < durationMs) |
| 49 | + { |
| 50 | + if (!game.Map.Timer.IsGaming) { Thread.Sleep(step); continue; } |
| 51 | + // 位置保持在 3x3 范围内,否则中断 |
| 52 | + if (!GameData.ApproachToInteract(ch.Position, resource.Position)) break; |
| 53 | + |
| 54 | + // 扣减资源:ratePerSec * step/1000 |
| 55 | + long delta = (long)Math.Max(1, ratePerSec * step / 1000.0); |
| 56 | + resource.HP.SubPositiveV(delta); |
| 57 | + |
| 58 | + // 资源耗尽:转为障碍(保留方块、Rigid=true,状态设为 HARVESTED),即形成阻挡 |
| 59 | + if (resource.HP.GetValue() == 0) |
| 60 | + { |
| 61 | + resource.SetERState(ResourceState.HARVESTED); |
| 62 | + break; |
| 63 | + } |
| 64 | + |
| 65 | + Thread.Sleep(step); |
| 66 | + elapsed += step; |
| 67 | + } |
| 68 | + } |
| 69 | + finally |
| 70 | + { |
| 71 | + harvesting.TryRemove(key, out var oldCts); |
| 72 | + oldCts?.Dispose(); |
| 73 | + } |
| 74 | + }) |
| 75 | + { IsBackground = true }.Start(); |
| 76 | + |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + public bool Occupy(long playerId, ComputeCenter center) |
| 81 | + { |
| 82 | + if (center == null) return false; |
| 83 | + if (!game.characterManager.TryGetCharacter(playerId, out var ch)) return false; |
| 84 | + // 仅无人机/机器人可占领 |
| 85 | + if (ch.CharacterType != CharacterType.DRONE && ch.CharacterType != CharacterType.ROBOT) return false; |
| 86 | + |
| 87 | + // 已在占领 |
| 88 | + if (occupying.ContainsKey(center.ID)) return false; |
| 89 | + // 需在中心范围内(使用 1 格邻近判定) |
| 90 | + if (!GameData.ApproachToInteract(ch.Position, center.Position)) return false; |
| 91 | + |
| 92 | + var cts = new CancellationTokenSource(); |
| 93 | + if (!occupying.TryAdd(center.ID, cts)) { cts.Dispose(); return false; } |
| 94 | + |
| 95 | + new Thread(() => |
| 96 | + { |
| 97 | + try |
| 98 | + { |
| 99 | + int occupyTimeMs = 10_000; |
| 100 | + int elapsed = 0; |
| 101 | + int tick = 200; // 200ms 检查 |
| 102 | + while (!cts.IsCancellationRequested && elapsed < occupyTimeMs) |
| 103 | + { |
| 104 | + if (!game.Map.Timer.IsGaming) { Thread.Sleep(tick); continue; } |
| 105 | + // 离开范围则打断 |
| 106 | + if (!GameData.ApproachToInteract(ch.Position, center.Position)) return; |
| 107 | + Thread.Sleep(tick); |
| 108 | + elapsed += tick; |
| 109 | + } |
| 110 | + if (elapsed >= occupyTimeMs && !cts.IsCancellationRequested) |
| 111 | + { |
| 112 | + center.SetOccupied(ch.PlayerID.Get()); |
| 113 | + } |
| 114 | + } |
| 115 | + finally |
| 116 | + { |
| 117 | + occupying.TryRemove(center.ID, out var oldCts); |
| 118 | + oldCts?.Dispose(); |
| 119 | + } |
| 120 | + }) |
| 121 | + { IsBackground = true }.Start(); |
| 122 | + |
| 123 | + return true; |
| 124 | + } |
| 125 | + |
| 126 | + public bool AddGoods(long playerId, GoodsType type, int delta) |
| 127 | + { |
| 128 | + if (!game.characterManager.TryGetCharacter(playerId, out var ch)) return false; |
| 129 | + return ch.GoodsLoad.Add(type, delta); |
| 130 | + } |
| 131 | + |
| 132 | + public bool SetGoods(long playerId, GoodsType type, int value) |
| 133 | + { |
| 134 | + if (!game.characterManager.TryGetCharacter(playerId, out var ch)) return false; |
| 135 | + return ch.GoodsLoad.Set(type, value); |
| 136 | + } |
| 137 | + } |
11 | 138 | } |
12 | 139 | } |
0 commit comments