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

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