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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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 (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. /// <returns>攻击操作发出的子弹</returns>
  203. public Bullet? Attack(XY pos, PlaceType place)
  204. {
  205. if (TrySubBulletNum())
  206. return BulletFactory.GetBullet(this, place, pos);
  207. else
  208. return null;
  209. }
  210. /// <summary>
  211. /// 尝试将子弹数量减1
  212. /// </summary>
  213. /// <returns>减操作是否成功</returns>
  214. private bool TrySubBulletNum()
  215. {
  216. lock (gameObjLock)
  217. {
  218. if (bulletNum > 0)
  219. {
  220. --bulletNum;
  221. return true;
  222. }
  223. return false;
  224. }
  225. }
  226. /// <summary>
  227. /// 尝试将子弹数量加1
  228. /// </summary>
  229. /// <returns>加操作是否成功</returns>
  230. public bool TryAddBulletNum()
  231. {
  232. lock (gameObjLock)
  233. {
  234. if (bulletNum < maxBulletNum)
  235. {
  236. ++bulletNum;
  237. return true;
  238. }
  239. return false;
  240. }
  241. }
  242. /// <summary>
  243. /// 尝试加血
  244. /// </summary>
  245. /// <param name="add">欲加量</param>
  246. /// <returns>加操作是否成功</returns>
  247. public bool TryAddHp(int add)
  248. {
  249. if (hp < MaxHp)
  250. {
  251. lock (gameObjLock)
  252. hp = MaxHp > hp + add ? hp + add : MaxHp;
  253. Debugger.Output(this, " hp has added to: " + hp.ToString());
  254. return true;
  255. }
  256. return false;
  257. }
  258. /// <summary>
  259. /// 尝试减血
  260. /// </summary>
  261. /// <param name="sub">减血量</param>
  262. /// <returns>减操作是否成功</returns>
  263. public int TrySubHp(int sub)
  264. {
  265. int previousHp = hp;
  266. lock (gameObjLock)
  267. hp = hp >= sub ? 0 : hp - sub;
  268. Debugger.Output(this, " hp has subed to: " + hp.ToString());
  269. return previousHp - hp;
  270. }
  271. /* /// <summary>
  272. /// 增加死亡次数
  273. /// </summary>
  274. /// <returns>当前死亡次数</returns>
  275. private int AddDeathCount()
  276. {
  277. lock (gameObjLock)
  278. {
  279. ++deathCount;
  280. return deathCount;
  281. }
  282. }*/
  283. /// <summary>
  284. /// 加分
  285. /// </summary>
  286. /// <param name="add">增加量</param>
  287. public void AddScore(int add)
  288. {
  289. lock (gameObjLock)
  290. {
  291. score += add;
  292. Debugger.Output(this, " 's score has been added to: " + score.ToString());
  293. }
  294. }
  295. /// <summary>
  296. /// 减分
  297. /// </summary>
  298. /// <param name="sub">减少量</param>
  299. public void SubScore(int sub)
  300. {
  301. lock (gameObjLock)
  302. {
  303. score -= sub;
  304. Debugger.Output(this, " 's score has been subed to: " + score.ToString());
  305. }
  306. }
  307. /// <summary>
  308. /// 遭受攻击
  309. /// </summary>
  310. /// <param name="subHP"></param>
  311. /// <param name="hasSpear"></param>
  312. /// <param name="attacker">伤害来源</param>
  313. /// <returns>人物在受到攻击后死了吗</returns>
  314. public bool BeAttacked(Bullet bullet)
  315. {
  316. lock (beAttackedLock)
  317. {
  318. if (hp <= 0)
  319. return false; // 原来已经死了
  320. if (bullet.Parent.TeamID != this.TeamID)
  321. {
  322. if (HasShield)
  323. {
  324. if (bullet.HasSpear)
  325. _ = TrySubHp(bullet.AP);
  326. else
  327. return false;
  328. }
  329. else
  330. {
  331. bullet.Parent.HP = (int)(bullet.Parent.HP + (bullet.Parent.Vampire * TrySubHp(bullet.AP)));
  332. }
  333. #if DEBUG
  334. Console.WriteLine($"PlayerID:{ID} is being shot! Now his hp is {hp}.");
  335. #endif
  336. if (hp <= 0)
  337. TryActivatingLIFE(); // 如果有复活甲
  338. }
  339. return hp <= 0;
  340. }
  341. }
  342. /// <summary>
  343. /// 攻击被反弹,反弹伤害不会再被反弹
  344. /// </summary>
  345. /// <param name="subHP"></param>
  346. /// <param name="hasSpear"></param>
  347. /// <param name="bouncer">反弹伤害者</param>
  348. /// <returns>是否因反弹伤害而死</returns>
  349. private bool BeBounced(int subHP, bool hasSpear, Character? bouncer)
  350. {
  351. lock (beAttackedLock)
  352. {
  353. if (hp <= 0)
  354. return false;
  355. if (!(bouncer?.TeamID == this.TeamID))
  356. {
  357. if (hasSpear || !HasShield)
  358. _ = TrySubHp(subHP);
  359. if (hp <= 0)
  360. TryActivatingLIFE();
  361. }
  362. return hp <= 0;
  363. }
  364. }
  365. /// <summary>
  366. /// 角色所属队伍ID
  367. /// </summary>
  368. private long teamID = long.MaxValue;
  369. public long TeamID
  370. {
  371. get => teamID;
  372. set
  373. {
  374. lock (gameObjLock)
  375. {
  376. teamID = value;
  377. Debugger.Output(this, " joins in the team: " + value.ToString());
  378. }
  379. }
  380. }
  381. private long playerID = long.MaxValue;
  382. public long PlayerID
  383. {
  384. get => playerID;
  385. set
  386. {
  387. lock (gameObjLock)
  388. {
  389. playerID = value;
  390. }
  391. }
  392. }
  393. /// <summary>
  394. /// 角色携带的信息
  395. /// </summary>
  396. private string message = "THUAI6";
  397. public string Message
  398. {
  399. get => message;
  400. set
  401. {
  402. lock (gameObjLock)
  403. {
  404. message = value;
  405. }
  406. }
  407. }
  408. #endregion
  409. #region 角色拥有的buff相关属性、方法
  410. public void AddMoveSpeed(int buffTime, double add = 2.0) => buffManager.AddMoveSpeed(add, buffTime, newVal =>
  411. { MoveSpeed = newVal < GameData.characterMaxSpeed ? newVal : GameData.characterMaxSpeed; },
  412. OrgMoveSpeed);
  413. public bool HasFasterSpeed => buffManager.HasFasterSpeed;
  414. public void AddShield(int shieldTime) => buffManager.AddShield(shieldTime);
  415. public bool HasShield => buffManager.HasShield;
  416. public void AddLIFE(int LIFETime) => buffManager.AddLIFE(LIFETime);
  417. public bool HasLIFE => buffManager.HasLIFE;
  418. public void AddSpear(int spearTime) => buffManager.AddSpear(spearTime);
  419. public bool HasSpear => buffManager.HasSpear;
  420. private Array buffTypeArray = Enum.GetValues(typeof(BuffType));
  421. public Dictionary<BuffType, bool> Buff
  422. {
  423. get
  424. {
  425. Dictionary<BuffType, bool> buff = new Dictionary<BuffType, bool>();
  426. foreach (BuffType type in buffTypeArray)
  427. {
  428. if (type != BuffType.Null)
  429. buff.Add(type, GetBuffStatus(type));
  430. }
  431. return buff;
  432. }
  433. }
  434. private bool GetBuffStatus(BuffType type)
  435. {
  436. switch (type)
  437. {
  438. case BuffType.Spear:
  439. return this.HasSpear;
  440. case BuffType.AddSpeed:
  441. return this.HasFasterSpeed;
  442. case BuffType.Shield:
  443. return this.HasShield;
  444. case BuffType.AddLIFE:
  445. return this.HasLIFE;
  446. default:
  447. return false;
  448. }
  449. }
  450. private void TryActivatingLIFE()
  451. {
  452. if (buffManager.TryActivatingLIFE())
  453. {
  454. hp = MaxHp;
  455. }
  456. }
  457. #endregion
  458. /* public override void Reset() // 要加锁吗?
  459. {
  460. lock (gameObjLock)
  461. {
  462. // _ = AddDeathCount();
  463. base.Reset();
  464. this.MoveSpeed = OrgMoveSpeed;
  465. HP = MaxHp;
  466. PropInventory = null;
  467. BulletOfPlayer = OriBulletOfPlayer;
  468. lock (gameObjLock)
  469. bulletNum = maxBulletNum;
  470. buffManager.ClearAll();
  471. IsInvisible = false;
  472. this.Vampire = this.OriVampire;
  473. }
  474. }*/
  475. public void Die(PlayerStateType playerStateType)
  476. {
  477. lock (gameObjLock)
  478. {
  479. playerState = playerStateType;
  480. CanMove = false;
  481. IsResetting = true;
  482. Position = GameData.PosWhoDie;
  483. place = PlaceType.Grass;
  484. }
  485. }
  486. public override bool IsRigid => true;
  487. public override ShapeType Shape => ShapeType.Circle;
  488. protected override bool IgnoreCollideExecutor(IGameObj targetObj)
  489. {
  490. if (IsResetting)
  491. return true;
  492. if (targetObj.Type == GameObjType.Prop) // 自己队的地雷忽略碰撞
  493. {
  494. return true;
  495. }
  496. return false;
  497. }
  498. }
  499. }