You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Character.cs 16 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. using Preparation.Interface;
  2. using Preparation.Utility;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Numerics;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. namespace GameClass.GameObj
  9. {
  10. public partial class Character : GameObj, ICharacter // 负责人LHR摆烂终了
  11. {
  12. private readonly object beAttackedLock = new();
  13. #region 角色的基本属性及方法,包括与道具的交互方法
  14. /// <summary>
  15. /// 装弹冷却
  16. /// </summary>
  17. protected int cd;
  18. public int CD
  19. {
  20. get => cd;
  21. private set
  22. {
  23. lock (gameObjLock)
  24. {
  25. cd = value;
  26. Debugger.Output(this, string.Format("'s CD has been set to: {0}.", value));
  27. }
  28. }
  29. }
  30. public int OrgCD { get; protected set; }
  31. protected int maxBulletNum;
  32. public int MaxBulletNum => maxBulletNum; // 人物最大子弹数
  33. protected int bulletNum;
  34. public int BulletNum => bulletNum; // 目前持有的子弹数
  35. public int MaxHp { get; protected set; } // 最大血量
  36. protected int hp;
  37. public int HP
  38. {
  39. get => hp;
  40. set
  41. {
  42. if (value > 0)
  43. {
  44. lock (gameObjLock)
  45. hp = value <= MaxHp ? value : MaxHp;
  46. }
  47. else
  48. lock (gameObjLock)
  49. hp = 0;
  50. }
  51. }
  52. private PlayerStateType playerState = PlayerStateType.Null;
  53. public PlayerStateType PlayerState
  54. {
  55. get
  56. {
  57. if (IsResetting) return PlayerStateType.IsDeceased;
  58. if (IsMoving) return PlayerStateType.IsMoving;
  59. return playerState;
  60. }
  61. set
  62. {
  63. if (!(value == PlayerStateType.IsMoving))
  64. lock (gameObjLock)
  65. IsMoving = false;
  66. lock (gameObjLock) playerState = (value == PlayerStateType.IsMoving) ? PlayerStateType.Null : value;
  67. }
  68. }
  69. // private int deathCount = 0;
  70. // public int DeathCount => deathCount; // 玩家的死亡次数
  71. private int score = 0;
  72. public int Score
  73. {
  74. get => score;
  75. }
  76. // public double AttackRange => BulletFactory.BulletAttackRange(this.BulletOfPlayer);
  77. private double vampire = 0; // 回血率:0-1之间
  78. public double Vampire
  79. {
  80. get => vampire;
  81. set
  82. {
  83. if (value > 1)
  84. lock (gameObjLock)
  85. vampire = 1;
  86. else if (value < 0)
  87. lock (gameObjLock)
  88. vampire = 0;
  89. else
  90. lock (gameObjLock)
  91. vampire = value;
  92. }
  93. }
  94. private double oriVampire = 0;
  95. public double OriVampire
  96. {
  97. get => oriVampire;
  98. set
  99. {
  100. if (value > 1)
  101. lock (gameObjLock)
  102. vampire = 1;
  103. else if (value < 0)
  104. lock (gameObjLock)
  105. vampire = 0;
  106. else
  107. lock (gameObjLock)
  108. vampire = value;
  109. }
  110. }
  111. public readonly BulletType OriBulletOfPlayer;
  112. private BulletType bulletOfPlayer;
  113. public BulletType BulletOfPlayer
  114. {
  115. get => bulletOfPlayer;
  116. set
  117. {
  118. lock (gameObjLock)
  119. bulletOfPlayer = value;
  120. }
  121. }
  122. private Prop? propInventory;
  123. public Prop? PropInventory // 持有的道具
  124. {
  125. get => propInventory;
  126. set
  127. {
  128. lock (gameObjLock)
  129. {
  130. propInventory = value;
  131. Debugger.Output(this, " prop becomes " + (PropInventory == null ? "null" : PropInventory.ToString()));
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// 使用物品栏中的道具
  137. /// </summary>
  138. /// <returns>被使用的道具</returns>
  139. public Prop? UseProp()
  140. {
  141. lock (gameObjLock)
  142. {
  143. var oldProp = PropInventory;
  144. PropInventory = null;
  145. return oldProp;
  146. }
  147. }
  148. /// <summary>
  149. /// 是否在隐身
  150. /// </summary>
  151. private bool isInvisible = false;
  152. public bool IsInvisible
  153. {
  154. get => isInvisible;
  155. set
  156. {
  157. lock (gameObjLock)
  158. {
  159. isInvisible = value;
  160. }
  161. }
  162. }
  163. private Dictionary<BgmType, double> bgmDictionary = new();
  164. public Dictionary<BgmType, double> BgmDictionary
  165. {
  166. get => bgmDictionary;
  167. set
  168. {
  169. lock (gameObjLock)
  170. {
  171. bgmDictionary = value;
  172. }
  173. }
  174. }
  175. private int alertnessRadius;
  176. public int AlertnessRadius
  177. {
  178. get => alertnessRadius;
  179. set
  180. {
  181. lock (gameObjLock)
  182. {
  183. alertnessRadius = value;
  184. }
  185. }
  186. }
  187. private double concealment;
  188. public double Concealment
  189. {
  190. get => concealment;
  191. set
  192. {
  193. lock (gameObjLock)
  194. {
  195. concealment = value;
  196. }
  197. }
  198. }
  199. /// <summary>
  200. /// 进行一次远程攻击
  201. /// </summary>
  202. /// <param name="posOffset">子弹初始位置偏差值</param>
  203. /// <returns>攻击操作发出的子弹</returns>
  204. public Bullet? RemoteAttack(XY posOffset)
  205. {
  206. if (TrySubBulletNum())
  207. return ProduceOneBullet(this.Position + posOffset);
  208. else
  209. return null;
  210. }
  211. protected Bullet? ProduceOneBullet(XY initPos)
  212. {
  213. var newBullet = BulletFactory.GetBullet(this);
  214. newBullet?.SetPosition(initPos);
  215. return newBullet;
  216. }
  217. /// <summary>
  218. /// 尝试将子弹数量减1
  219. /// </summary>
  220. /// <returns>减操作是否成功</returns>
  221. private bool TrySubBulletNum()
  222. {
  223. lock (gameObjLock)
  224. {
  225. if (bulletNum > 0)
  226. {
  227. --bulletNum;
  228. return true;
  229. }
  230. return false;
  231. }
  232. }
  233. /// <summary>
  234. /// 尝试将子弹数量加1
  235. /// </summary>
  236. /// <returns>加操作是否成功</returns>
  237. public bool TryAddBulletNum()
  238. {
  239. lock (gameObjLock)
  240. {
  241. if (bulletNum < maxBulletNum)
  242. {
  243. ++bulletNum;
  244. return true;
  245. }
  246. return false;
  247. }
  248. }
  249. /// <summary>
  250. /// 尝试加血
  251. /// </summary>
  252. /// <param name="add">欲加量</param>
  253. /// <returns>加操作是否成功</returns>
  254. public bool TryAddHp(int add)
  255. {
  256. if (hp < MaxHp)
  257. {
  258. lock (gameObjLock)
  259. hp = MaxHp > hp + add ? hp + add : MaxHp;
  260. Debugger.Output(this, " hp has added to: " + hp.ToString());
  261. return true;
  262. }
  263. return false;
  264. }
  265. /// <summary>
  266. /// 尝试减血
  267. /// </summary>
  268. /// <param name="sub">减血量</param>
  269. /// <returns>减操作是否成功</returns>
  270. public int TrySubHp(int sub)
  271. {
  272. int previousHp = hp;
  273. lock (gameObjLock)
  274. hp = hp >= sub ? 0 : hp - sub;
  275. Debugger.Output(this, " hp has subed to: " + hp.ToString());
  276. return previousHp - hp;
  277. }
  278. /* /// <summary>
  279. /// 增加死亡次数
  280. /// </summary>
  281. /// <returns>当前死亡次数</returns>
  282. private int AddDeathCount()
  283. {
  284. lock (gameObjLock)
  285. {
  286. ++deathCount;
  287. return deathCount;
  288. }
  289. }*/
  290. /// <summary>
  291. /// 加分
  292. /// </summary>
  293. /// <param name="add">增加量</param>
  294. public void AddScore(int add)
  295. {
  296. lock (gameObjLock)
  297. {
  298. score += add;
  299. Debugger.Output(this, " 's score has been added to: " + score.ToString());
  300. }
  301. }
  302. /// <summary>
  303. /// 减分
  304. /// </summary>
  305. /// <param name="sub">减少量</param>
  306. public void SubScore(int sub)
  307. {
  308. lock (gameObjLock)
  309. {
  310. score -= sub;
  311. Debugger.Output(this, " 's score has been subed to: " + score.ToString());
  312. }
  313. }
  314. /// <summary>
  315. /// 遭受攻击
  316. /// </summary>
  317. /// <param name="subHP"></param>
  318. /// <param name="hasSpear"></param>
  319. /// <param name="attacker">伤害来源</param>
  320. /// <returns>人物在受到攻击后死了吗</returns>
  321. public bool BeAttacked(Bullet bullet)
  322. {
  323. lock (beAttackedLock)
  324. {
  325. if (hp <= 0)
  326. return false; // 原来已经死了
  327. if (bullet.Parent.TeamID != this.TeamID)
  328. {
  329. if (HasShield)
  330. {
  331. if (bullet.HasSpear)
  332. _ = TrySubHp(bullet.AP);
  333. else
  334. return false;
  335. }
  336. else
  337. {
  338. bullet.Parent.HP = (int)(bullet.Parent.HP + (bullet.Parent.Vampire * TrySubHp(bullet.AP)));
  339. }
  340. #if DEBUG
  341. Console.WriteLine($"PlayerID:{ID} is being shot! Now his hp is {hp}.");
  342. #endif
  343. if (hp <= 0)
  344. TryActivatingLIFE(); // 如果有复活甲
  345. }
  346. return hp <= 0;
  347. }
  348. }
  349. /// <summary>
  350. /// 攻击被反弹,反弹伤害不会再被反弹
  351. /// </summary>
  352. /// <param name="subHP"></param>
  353. /// <param name="hasSpear"></param>
  354. /// <param name="bouncer">反弹伤害者</param>
  355. /// <returns>是否因反弹伤害而死</returns>
  356. private bool BeBounced(int subHP, bool hasSpear, Character? bouncer)
  357. {
  358. lock (beAttackedLock)
  359. {
  360. if (hp <= 0)
  361. return false;
  362. if (!(bouncer?.TeamID == this.TeamID))
  363. {
  364. if (hasSpear || !HasShield)
  365. _ = TrySubHp(subHP);
  366. if (hp <= 0)
  367. TryActivatingLIFE();
  368. }
  369. return hp <= 0;
  370. }
  371. }
  372. /// <summary>
  373. /// 角色所属队伍ID
  374. /// </summary>
  375. private long teamID = long.MaxValue;
  376. public long TeamID
  377. {
  378. get => teamID;
  379. set
  380. {
  381. lock (gameObjLock)
  382. {
  383. teamID = value;
  384. Debugger.Output(this, " joins in the team: " + value.ToString());
  385. }
  386. }
  387. }
  388. private long playerID = long.MaxValue;
  389. public long PlayerID
  390. {
  391. get => playerID;
  392. set
  393. {
  394. lock (gameObjLock)
  395. {
  396. playerID = value;
  397. }
  398. }
  399. }
  400. /// <summary>
  401. /// 角色携带的信息
  402. /// </summary>
  403. private string message = "THUAI6";
  404. public string Message
  405. {
  406. get => message;
  407. set
  408. {
  409. lock (gameObjLock)
  410. {
  411. message = value;
  412. }
  413. }
  414. }
  415. #endregion
  416. #region 角色拥有的buff相关属性、方法
  417. public void AddMoveSpeed(int buffTime, double add = 2.0) => buffManager.AddMoveSpeed(add, buffTime, newVal =>
  418. { MoveSpeed = newVal < GameData.characterMaxSpeed ? newVal : GameData.characterMaxSpeed; },
  419. OrgMoveSpeed);
  420. public bool HasFasterSpeed => buffManager.HasFasterSpeed;
  421. public void AddShield(int shieldTime) => buffManager.AddShield(shieldTime);
  422. public bool HasShield => buffManager.HasShield;
  423. public void AddLIFE(int LIFETime) => buffManager.AddLIFE(LIFETime);
  424. public bool HasLIFE => buffManager.HasLIFE;
  425. public void AddSpear(int spearTime) => buffManager.AddSpear(spearTime);
  426. public bool HasSpear => buffManager.HasSpear;
  427. private Array buffTypeArray = Enum.GetValues(typeof(BuffType));
  428. public Dictionary<BuffType, bool> Buff
  429. {
  430. get
  431. {
  432. Dictionary<BuffType, bool> buff = new Dictionary<BuffType, bool>();
  433. foreach (BuffType type in buffTypeArray)
  434. {
  435. if (type != BuffType.Null)
  436. buff.Add(type, GetBuffStatus(type));
  437. }
  438. return buff;
  439. }
  440. }
  441. private bool GetBuffStatus(BuffType type)
  442. {
  443. switch (type)
  444. {
  445. case BuffType.Spear:
  446. return this.HasSpear;
  447. case BuffType.AddSpeed:
  448. return this.HasFasterSpeed;
  449. case BuffType.Shield:
  450. return this.HasShield;
  451. case BuffType.AddLIFE:
  452. return this.HasLIFE;
  453. default:
  454. return false;
  455. }
  456. }
  457. private void TryActivatingLIFE()
  458. {
  459. if (buffManager.TryActivatingLIFE())
  460. {
  461. hp = MaxHp;
  462. }
  463. }
  464. #endregion
  465. public override void Reset() // 要加锁吗?
  466. {
  467. lock (gameObjLock)
  468. {
  469. // _ = AddDeathCount();
  470. base.Reset();
  471. this.MoveSpeed = OrgMoveSpeed;
  472. HP = MaxHp;
  473. PropInventory = null;
  474. BulletOfPlayer = OriBulletOfPlayer;
  475. lock (gameObjLock)
  476. bulletNum = maxBulletNum;
  477. buffManager.ClearAll();
  478. IsInvisible = false;
  479. this.Vampire = this.OriVampire;
  480. }
  481. }
  482. public void Die(PlayerStateType playerStateType)
  483. {
  484. lock (gameObjLock)
  485. {
  486. playerState = playerStateType;
  487. CanMove = false;
  488. IsResetting = true;
  489. Position = GameData.PosWhoDie;
  490. }
  491. }
  492. public override bool IsRigid => true;
  493. public override ShapeType Shape => ShapeType.Circle;
  494. protected override bool IgnoreCollideExecutor(IGameObj targetObj)
  495. {
  496. if (IsResetting)
  497. return true;
  498. if (targetObj.Type == GameObjType.Prop) // 自己队的地雷忽略碰撞
  499. {
  500. return true;
  501. }
  502. return false;
  503. }
  504. }
  505. }